2024-01-31 19:42:00 -05:00
|
|
|
package player_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitlab.com/zephyrtronium/sq"
|
|
|
|
|
|
|
|
"git.sunturtle.xyz/studio/shotgun/player"
|
|
|
|
|
|
|
|
_ "modernc.org/sqlite" // sqlite driver
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogin(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
db, err := sq.Open("sqlite", ":memory:")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
conn, err := db.Conn(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := player.InitUsers(ctx, conn); err != nil {
|
|
|
|
t.Fatalf("couldn't init users: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, pass := "bocchi", "the rock!"
|
|
|
|
id, err := player.Login(ctx, conn, user, pass)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("logging in nonexistent user didn't err")
|
|
|
|
}
|
2024-01-31 23:40:12 -05:00
|
|
|
if id.UUID != uuid.Nil {
|
2024-01-31 19:42:00 -05:00
|
|
|
t.Errorf("got nonzero user %v before registering", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := player.Register(ctx, conn, user, pass); err != nil {
|
|
|
|
t.Errorf("failed to register user: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err = player.Login(ctx, conn, user, pass)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("couldn't login after registering: %v", err)
|
|
|
|
}
|
2024-01-31 23:40:12 -05:00
|
|
|
if id.UUID == uuid.Nil {
|
2024-01-31 19:42:00 -05:00
|
|
|
t.Errorf("got zero player id after registering")
|
|
|
|
}
|
|
|
|
|
|
|
|
wrong, err := player.Login(ctx, conn, user, "not the rock")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("logged in with wrong password")
|
|
|
|
}
|
2024-01-31 23:40:12 -05:00
|
|
|
if wrong.UUID != uuid.Nil {
|
2024-01-31 19:42:00 -05:00
|
|
|
t.Errorf("got nonzero user %v with wrong password (real is %v)", wrong, id)
|
|
|
|
}
|
|
|
|
}
|