package daos_test import ( "testing" "github.com/pocketbase/pocketbase/models" "github.com/pocketbase/pocketbase/tests" ) func TestAdminQuery(t *testing.T) { app, _ := tests.NewTestApp() defer app.Cleanup() expected := "SELECT {{_admins}}.* FROM `_admins`" sql := app.Dao().AdminQuery().Build().SQL() if sql != expected { t.Errorf("Expected sql %s, got %s", expected, sql) } } func TestFindAdminById(t *testing.T) { app, _ := tests.NewTestApp() defer app.Cleanup() scenarios := []struct { id string expectError bool }{ {"00000000-2b4a-a26b-4d01-42d3c3d77bc8", true}, {"3f8397cc-2b4a-a26b-4d01-42d3c3d77bc8", false}, } for i, scenario := range scenarios { admin, err := app.Dao().FindAdminById(scenario.id) hasErr := err != nil if hasErr != scenario.expectError { t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err) } if admin != nil && admin.Id != scenario.id { t.Errorf("(%d) Expected admin with id %s, got %s", i, scenario.id, admin.Id) } } } func TestFindAdminByEmail(t *testing.T) { app, _ := tests.NewTestApp() defer app.Cleanup() scenarios := []struct { email string expectError bool }{ {"invalid", true}, {"missing@example.com", true}, {"test@example.com", false}, } for i, scenario := range scenarios { admin, err := app.Dao().FindAdminByEmail(scenario.email) hasErr := err != nil if hasErr != scenario.expectError { t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err) continue } if !scenario.expectError && admin.Email != scenario.email { t.Errorf("(%d) Expected admin with email %s, got %s", i, scenario.email, admin.Email) } } } func TestFindAdminByToken(t *testing.T) { app, _ := tests.NewTestApp() defer app.Cleanup() scenarios := []struct { token string baseKey string expectedEmail string expectError bool }{ // invalid base key (password reset key for auth token) { "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjJiNGE5N2NjLTNmODMtNGQwMS1hMjZiLTNkNzdiYzg0MmQzYyIsInR5cGUiOiJhZG1pbiIsImV4cCI6MTg3MzQ2Mjc5Mn0.AtRtXR6FHBrCUGkj5OffhmxLbSZaQ4L_Qgw4gfoHyfo", app.Settings().AdminPasswordResetToken.Secret, "", true, }, // expired token { "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjJiNGE5N2NjLTNmODMtNGQwMS1hMjZiLTNkNzdiYzg0MmQzYyIsInR5cGUiOiJhZG1pbiIsImV4cCI6MTY0MDk5MTY2MX0.uXZ_ywsZeRFSvDNQ9zBoYUXKXw7VEr48Fzx-E06OkS8", app.Settings().AdminAuthToken.Secret, "", true, }, // valid token { "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjJiNGE5N2NjLTNmODMtNGQwMS1hMjZiLTNkNzdiYzg0MmQzYyIsInR5cGUiOiJhZG1pbiIsImV4cCI6MTg3MzQ2Mjc5Mn0.AtRtXR6FHBrCUGkj5OffhmxLbSZaQ4L_Qgw4gfoHyfo", app.Settings().AdminAuthToken.Secret, "test@example.com", false, }, } for i, scenario := range scenarios { admin, err := app.Dao().FindAdminByToken(scenario.token, scenario.baseKey) hasErr := err != nil if hasErr != scenario.expectError { t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err) continue } if !scenario.expectError && admin.Email != scenario.expectedEmail { t.Errorf("(%d) Expected admin model %s, got %s", i, scenario.expectedEmail, admin.Email) } } } func TestTotalAdmins(t *testing.T) { app, _ := tests.NewTestApp() defer app.Cleanup() result1, err := app.Dao().TotalAdmins() if err != nil { t.Fatal(err) } if result1 != 2 { t.Fatalf("Expected 2 admins, got %d", result1) } // delete all app.Dao().DB().NewQuery("delete from {{_admins}}").Execute() result2, err := app.Dao().TotalAdmins() if err != nil { t.Fatal(err) } if result2 != 0 { t.Fatalf("Expected 0 admins, got %d", result2) } } func TestIsAdminEmailUnique(t *testing.T) { app, _ := tests.NewTestApp() defer app.Cleanup() scenarios := []struct { email string excludeId string expected bool }{ {"", "", false}, {"test@example.com", "", false}, {"new@example.com", "", true}, {"test@example.com", "2b4a97cc-3f83-4d01-a26b-3d77bc842d3c", true}, } for i, scenario := range scenarios { result := app.Dao().IsAdminEmailUnique(scenario.email, scenario.excludeId) if result != scenario.expected { t.Errorf("(%d) Expected %v, got %v", i, scenario.expected, result) } } } func TestDeleteAdmin(t *testing.T) { app, _ := tests.NewTestApp() defer app.Cleanup() // try to delete unsaved admin model deleteErr0 := app.Dao().DeleteAdmin(&models.Admin{}) if deleteErr0 == nil { t.Fatal("Expected error, got nil") } admin1, err := app.Dao().FindAdminByEmail("test@example.com") if err != nil { t.Fatal(err) } admin2, err := app.Dao().FindAdminByEmail("test2@example.com") if err != nil { t.Fatal(err) } deleteErr1 := app.Dao().DeleteAdmin(admin1) if deleteErr1 != nil { t.Fatal(deleteErr1) } // cannot delete the only remaining admin deleteErr2 := app.Dao().DeleteAdmin(admin2) if deleteErr2 == nil { t.Fatal("Expected delete error, got nil") } total, _ := app.Dao().TotalAdmins() if total != 1 { t.Fatalf("Expected only 1 admin, got %d", total) } } func TestSaveAdmin(t *testing.T) { app, _ := tests.NewTestApp() defer app.Cleanup() // create newAdmin := &models.Admin{} newAdmin.Email = "new@example.com" newAdmin.SetPassword("123456") saveErr1 := app.Dao().SaveAdmin(newAdmin) if saveErr1 != nil { t.Fatal(saveErr1) } if newAdmin.Id == "" { t.Fatal("Expected admin id to be set") } // update existingAdmin, err := app.Dao().FindAdminByEmail("test@example.com") if err != nil { t.Fatal(err) } updatedEmail := "test_update@example.com" existingAdmin.Email = updatedEmail saveErr2 := app.Dao().SaveAdmin(existingAdmin) if saveErr2 != nil { t.Fatal(saveErr2) } existingAdmin, _ = app.Dao().FindAdminById(existingAdmin.Id) if existingAdmin.Email != updatedEmail { t.Fatalf("Expected admin email to be %s, got %s", updatedEmail, existingAdmin.Email) } }