feat(backend): bcrypt+jwt auth package and admin seeding
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package seed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"booklib/internal/db"
|
||||
"booklib/internal/store"
|
||||
)
|
||||
|
||||
func TestSeedOnlyWhenEmpty(t *testing.T) {
|
||||
url := os.Getenv("DATABASE_URL")
|
||||
if url == "" {
|
||||
t.Skip("DATABASE_URL not set")
|
||||
}
|
||||
ctx := context.Background()
|
||||
p, _ := db.Connect(ctx, url)
|
||||
if err := db.Migrate(ctx, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := store.New(p)
|
||||
p.Exec(ctx, "DELETE FROM reading_progress; DELETE FROM books; DELETE FROM libraries; DELETE FROM users")
|
||||
if err := Admin(ctx, s, "admin", "pw12345"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Admin(ctx, s, "admin2", "pw12345"); err != nil { // 已有用户 → no-op
|
||||
t.Fatal(err)
|
||||
}
|
||||
n, _ := s.CountUsers(ctx)
|
||||
if n != 1 {
|
||||
t.Fatalf("want 1 user got %d", n)
|
||||
}
|
||||
u, err := s.GetUserByName(ctx, "admin")
|
||||
if err != nil || u.Role != "admin" {
|
||||
t.Fatalf("bad admin: %+v %v", u, err)
|
||||
}
|
||||
if err := Admin(ctx, s, "", ""); err != nil {
|
||||
t.Fatal("empty creds must no-op, got", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user