feat(auth): add MINDA_DEMO support with auto-seeded demo accounts and username login
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 59s
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 59s
This commit is contained in:
1 parent
41c64c4924
commit
7f312fef14
12 files changed
+297
-23
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/sandrodach/mindsift/internal/auth"
|
||||
"github.com/sandrodach/mindsift/internal/domain"
|
||||
)
|
||||
|
||||
@@ -162,3 +163,59 @@ func TestStoreLifecycle(t *testing.T) {
|
||||
t.Fatalf("expected GetDocumentByID to fail after share removed, but succeeded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureDemoAccountsAndLoginLookup(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
dbPath := filepath.Join(tmpDir, "demo_test.db")
|
||||
|
||||
store, err := NewStore(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("NewStore failed: %v", err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
if err := store.EnsureDemoAccounts(); err != nil {
|
||||
t.Fatalf("EnsureDemoAccounts failed: %v", err)
|
||||
}
|
||||
|
||||
// 1. Lookup by username "admin"
|
||||
adminUser, err := store.GetUserByLogin("admin")
|
||||
if err != nil || adminUser == nil {
|
||||
t.Fatalf("expected to find admin user by username 'admin', got %v", err)
|
||||
}
|
||||
if adminUser.Role != "admin" {
|
||||
t.Fatalf("expected role 'admin', got %s", adminUser.Role)
|
||||
}
|
||||
if !auth.CheckPassword("admin", adminUser.PasswordHash) {
|
||||
t.Fatalf("admin password check failed")
|
||||
}
|
||||
|
||||
// 2. Lookup by email "admin@mindsift.local"
|
||||
adminByEmail, err := store.GetUserByLogin("admin@mindsift.local")
|
||||
if err != nil || adminByEmail.ID != adminUser.ID {
|
||||
t.Fatalf("lookup by email failed: got %v", err)
|
||||
}
|
||||
|
||||
// 3. Lookup with case insensitivity "ADMIN"
|
||||
adminCaps, err := store.GetUserByLogin("ADMIN")
|
||||
if err != nil || adminCaps.ID != adminUser.ID {
|
||||
t.Fatalf("lookup by uppercase failed: got %v", err)
|
||||
}
|
||||
|
||||
// 4. Lookup by username "user"
|
||||
normalUser, err := store.GetUserByLogin("user")
|
||||
if err != nil || normalUser == nil {
|
||||
t.Fatalf("expected to find user by username 'user', got %v", err)
|
||||
}
|
||||
if normalUser.Role != "member" {
|
||||
t.Fatalf("expected role 'member', got %s", normalUser.Role)
|
||||
}
|
||||
if !auth.CheckPassword("user", normalUser.PasswordHash) {
|
||||
t.Fatalf("user password check failed")
|
||||
}
|
||||
|
||||
// 5. Ensure re-running is idempotent and preserves/refreshes credentials
|
||||
if err := store.EnsureDemoAccounts(); err != nil {
|
||||
t.Fatalf("second EnsureDemoAccounts call failed: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user