test(user): fix TestUpdateProfileAdminMode test failures

- Fix username conflict: use unique name 'updateduser-admin-test'
- Fix 'last admin' test: explicitly delete regular user and verify admin count
- Add missing Content-Type header to PUT request
- Fix assertion: match actual validator error message ('oneof')
- Update email domain references to @tests.bookhoard.internal

All 5 subtests now pass:
- Admin update username ✓
- Admin promote user to admin ✓
- Try to demote last admin ✓
- Non-admin tries update ✓
- Invalid role ✓
This commit is contained in:
2026-02-22 11:20:31 -05:00
parent cbf80037c4
commit 8ba8601841
+22 -5
View File
@@ -23,7 +23,7 @@ func TestUpdateProfileSelfEdit(t *testing.T) {
updateData := map[string]interface{}{
"username": "updateduser",
"email": "updated@example.com",
"email": "updated@tests.bookhoard.internal",
"first_name": "Updated",
"last_name": "Name",
"theme": "dracula",
@@ -41,7 +41,7 @@ func TestUpdateProfileSelfEdit(t *testing.T) {
user, err := setup.DB.GetUserByUsername(context.Background(), "updateduser")
require.NoError(t, err)
assert.Equal(t, "updated@example.com", user.Email)
assert.Equal(t, "updated@tests.bookhoard.internal", user.Email)
assert.Equal(t, "Updated", user.FirstName.String)
assert.Equal(t, "Name", user.LastName.String)
assert.Equal(t, "dracula", user.Theme.String)
@@ -78,7 +78,7 @@ func TestUpdateProfileSelfEdit(t *testing.T) {
token := loginUserWithCredentials(t, setup.Server, regularUser.Email, regularUser.Password)
updateData := map[string]interface{}{
"email": "testuser@example.com",
"email": "testuser@tests.bookhoard.internal",
}
jsonData, _ := json.Marshal(updateData)
@@ -167,7 +167,7 @@ func TestUpdateProfileAdminMode(t *testing.T) {
targetUser := createRegularUserOnce(t, setup.DB)
newUsername := "updateduser"
newUsername := "updateduser-admin-test"
jsonData, _ := json.Marshal(map[string]interface{}{
"username": newUsername,
})
@@ -215,6 +215,22 @@ func TestUpdateProfileAdminMode(t *testing.T) {
t.Run("PUT /api/auth/profile/:id - Try to demote last admin", func(t *testing.T) {
setup := setupTestServer(t)
// Delete the regular user so we have only 1 admin (testuser)
regularUser := createRegularUserOnce(t, setup.DB)
err := setup.DB.DeleteUser(context.Background(), uuidToPGType(regularUser.ID))
require.NoError(t, err, "Failed to delete regular user")
// Verify we have only 1 admin
users, _ := setup.DB.ListUsers(context.Background())
adminCount := 0
for _, u := range users {
if u.Role == "admin" {
adminCount++
}
}
require.Equal(t, 1, adminCount, "Expected exactly 1 admin after deleting regular user")
// Now try to demote the only remaining admin
lastAdmin := createTestUserOnce(t, setup.DB)
token := setup.Token
@@ -223,6 +239,7 @@ func TestUpdateProfileAdminMode(t *testing.T) {
})
req := httptest.NewRequest("PUT", "/api/auth/profile/"+lastAdmin.ID.String(), bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
rec := httptest.NewRecorder()
@@ -272,7 +289,7 @@ func TestUpdateProfileAdminMode(t *testing.T) {
setup.Server.Config.Handler.ServeHTTP(rec, req)
assert.Equal(t, http.StatusBadRequest, rec.Code)
assert.Contains(t, rec.Body.String(), "invalid role")
assert.Contains(t, rec.Body.String(), "oneof")
})
}