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") } if id.UUID != uuid.Nil { 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) } if id.UUID == uuid.Nil { 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") } if wrong.UUID != uuid.Nil { t.Errorf("got nonzero user %v with wrong password (real is %v)", wrong, id) } }