Skip to content

Commit f1d8e28

Browse files
jonradoffclaude
andcommitted
Fix remaining CI failures: testutil Drop cleanup, system_key_test signature
- testutil.CleanupCollections now uses Drop (not DeleteMany) — consistent with handlers package cleanup; prevents unique-index duplicate key errors when packages run concurrently against the same lightcms-test database - Update system_key_test.go calls to EnsureSystemAPIKey with new nil adminUserID arg Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 5214525 commit f1d8e28

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

internal/services/system_key_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestEnsureSystemAPIKey_CreatesNewKey(t *testing.T) {
1414
apiKeySvc := NewAPIKeyService(db)
1515
ctx := context.Background()
1616

17-
rawKey, err := EnsureSystemAPIKey(ctx, db, apiKeySvc)
17+
rawKey, err := EnsureSystemAPIKey(ctx, db, apiKeySvc, nil)
1818
if err != nil {
1919
t.Fatalf("EnsureSystemAPIKey failed: %v", err)
2020
}
@@ -38,13 +38,13 @@ func TestEnsureSystemAPIKey_ReturnsCachedKey(t *testing.T) {
3838
ctx := context.Background()
3939

4040
// First call creates
41-
key1, err := EnsureSystemAPIKey(ctx, db, apiKeySvc)
41+
key1, err := EnsureSystemAPIKey(ctx, db, apiKeySvc, nil)
4242
if err != nil {
4343
t.Fatalf("first EnsureSystemAPIKey failed: %v", err)
4444
}
4545

4646
// Second call returns same key
47-
key2, err := EnsureSystemAPIKey(ctx, db, apiKeySvc)
47+
key2, err := EnsureSystemAPIKey(ctx, db, apiKeySvc, nil)
4848
if err != nil {
4949
t.Fatalf("second EnsureSystemAPIKey failed: %v", err)
5050
}
@@ -62,7 +62,7 @@ func TestEnsureSystemAPIKey_RecreatesDeletedKey(t *testing.T) {
6262
ctx := context.Background()
6363

6464
// Create system key
65-
key1, _ := EnsureSystemAPIKey(ctx, db, apiKeySvc)
65+
key1, _ := EnsureSystemAPIKey(ctx, db, apiKeySvc, nil)
6666

6767
// Delete all API keys (simulating key deletion)
6868
keys, _ := apiKeySvc.ListAPIKeys(ctx)
@@ -71,7 +71,7 @@ func TestEnsureSystemAPIKey_RecreatesDeletedKey(t *testing.T) {
7171
}
7272

7373
// Should create a new key since the old one is invalid
74-
key2, err := EnsureSystemAPIKey(ctx, db, apiKeySvc)
74+
key2, err := EnsureSystemAPIKey(ctx, db, apiKeySvc, nil)
7575
if err != nil {
7676
t.Fatalf("EnsureSystemAPIKey after deletion failed: %v", err)
7777
}

internal/testutil/testutil.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"lightcms/internal/database"
1717

18-
"go.mongodb.org/mongo-driver/bson"
1918
"go.mongodb.org/mongo-driver/mongo/options"
2019
"go.mongodb.org/mongo-driver/mongo/writeconcern"
2120
)
@@ -121,9 +120,9 @@ func MustConnectTestDB(t *testing.T) (*database.DB, func()) {
121120
return sharedDB, cleanup
122121
}
123122

124-
// CleanupCollections removes all documents from test collections for isolation
125-
// between tests. Uses DeleteMany instead of Drop so that indexes are preserved —
126-
// Drop + recreate requires createIndexes to do real round-trips on every test.
123+
// CleanupCollections drops test collections for isolation between tests.
124+
// Dropping ensures unique indexes (e.g. users.email) are also cleared, preventing
125+
// duplicate key errors when MigrateToMultiUser runs in subsequent tests.
127126
func CleanupCollections(t *testing.T, db *database.DB) {
128127
t.Helper()
129128
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
@@ -136,9 +135,8 @@ func CleanupCollections(t *testing.T, db *database.DB) {
136135
"users", "audit_logs", "user_activity",
137136
"oauth_clients", "oauth_auth_codes", "oauth_access_tokens", "oauth_refresh_tokens",
138137
}
139-
empty := bson.M{}
140138
for _, name := range collections {
141-
db.Collection(name).DeleteMany(ctx, empty)
139+
db.Collection(name).Drop(ctx) //nolint:errcheck
142140
}
143141
}
144142

0 commit comments

Comments
 (0)