Skip to content

Commit a209ab3

Browse files
jonradoffclaude
andcommitted
Fix CI test failures from RBAC/multiuser changes
- Fix %%v typo in main.go log.Printf (was printing literal %%v) - Add users and audit_logs to testutil CleanupCollections so auth tests get a clean slate between runs - Update apiauth_test.go callbacks to match new (interface{}, error) return type for APIKeyValidateFunc and OAuthValidateFunc - Add email field to oauth handler tests and fix expected error message ("Invalid email or password" not "Invalid password") Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1ce8222 commit a209ab3

4 files changed

Lines changed: 35 additions & 32 deletions

File tree

cmd/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func main() {
8686

8787
// Migrate to multi-user system (creates first admin user from legacy password if needed)
8888
if err := authManager.MigrateToMultiUser(context.Background()); err != nil {
89-
log.Printf("Warning: Failed to migrate to multi-user: %%v", err)
89+
log.Printf("Warning: Failed to migrate to multi-user: %v", err)
9090
}
9191

9292
// Initialize snippet service (needed by both handlers and API handler)

internal/middleware/apiauth_test.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
)
1111

1212
func TestAPIAuth_MissingAuthorizationHeader(t *testing.T) {
13-
m := NewAPIAuth(func(ctx context.Context, rawKey string) error {
14-
return nil
13+
m := NewAPIAuth(func(ctx context.Context, rawKey string) (interface{}, error) {
14+
return nil, nil
1515
})
1616
handler := m.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1717
w.WriteHeader(http.StatusOK)
@@ -33,8 +33,8 @@ func TestAPIAuth_MissingAuthorizationHeader(t *testing.T) {
3333
}
3434

3535
func TestAPIAuth_MalformedAuthorizationHeader(t *testing.T) {
36-
m := NewAPIAuth(func(ctx context.Context, rawKey string) error {
37-
return nil
36+
m := NewAPIAuth(func(ctx context.Context, rawKey string) (interface{}, error) {
37+
return nil, nil
3838
})
3939
handler := m.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
4040
w.WriteHeader(http.StatusOK)
@@ -65,12 +65,12 @@ func TestAPIAuth_MalformedAuthorizationHeader(t *testing.T) {
6565

6666
func TestAPIAuth_ValidAPIKey(t *testing.T) {
6767
called := false
68-
m := NewAPIAuth(func(ctx context.Context, rawKey string) error {
68+
m := NewAPIAuth(func(ctx context.Context, rawKey string) (interface{}, error) {
6969
if rawKey != "lc_testkey123" {
70-
return fmt.Errorf("invalid key")
70+
return nil, fmt.Errorf("invalid key")
7171
}
7272
called = true
73-
return nil
73+
return nil, nil
7474
})
7575
handler := m.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7676
w.WriteHeader(http.StatusOK)
@@ -91,8 +91,8 @@ func TestAPIAuth_ValidAPIKey(t *testing.T) {
9191
}
9292

9393
func TestAPIAuth_InvalidAPIKey(t *testing.T) {
94-
m := NewAPIAuth(func(ctx context.Context, rawKey string) error {
95-
return fmt.Errorf("invalid key")
94+
m := NewAPIAuth(func(ctx context.Context, rawKey string) (interface{}, error) {
95+
return nil, fmt.Errorf("invalid key")
9696
})
9797
handler := m.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9898
w.WriteHeader(http.StatusOK)
@@ -110,14 +110,14 @@ func TestAPIAuth_InvalidAPIKey(t *testing.T) {
110110

111111
func TestAPIAuth_ValidOAuthToken(t *testing.T) {
112112
systemKey := "lc_system_key_12345678901234567890"
113-
m := NewAPIAuth(func(ctx context.Context, rawKey string) error {
114-
return fmt.Errorf("not an API key")
113+
m := NewAPIAuth(func(ctx context.Context, rawKey string) (interface{}, error) {
114+
return nil, fmt.Errorf("not an API key")
115115
})
116-
m.SetOAuth(func(ctx context.Context, rawToken string) error {
116+
m.SetOAuth(func(ctx context.Context, rawToken string) (interface{}, error) {
117117
if rawToken != "oauth_access_token_123" {
118-
return fmt.Errorf("invalid token")
118+
return nil, fmt.Errorf("invalid token")
119119
}
120-
return nil
120+
return nil, nil
121121
}, systemKey, "https://example.com/.well-known/oauth-protected-resource")
122122

123123
var capturedAuth string
@@ -143,11 +143,11 @@ func TestAPIAuth_ValidOAuthToken(t *testing.T) {
143143
}
144144

145145
func TestAPIAuth_InvalidOAuthToken(t *testing.T) {
146-
m := NewAPIAuth(func(ctx context.Context, rawKey string) error {
147-
return fmt.Errorf("not an API key")
146+
m := NewAPIAuth(func(ctx context.Context, rawKey string) (interface{}, error) {
147+
return nil, fmt.Errorf("not an API key")
148148
})
149-
m.SetOAuth(func(ctx context.Context, rawToken string) error {
150-
return fmt.Errorf("invalid token")
149+
m.SetOAuth(func(ctx context.Context, rawToken string) (interface{}, error) {
150+
return nil, fmt.Errorf("invalid token")
151151
}, "lc_system", "https://example.com/.well-known/oauth-protected-resource")
152152

153153
handler := m.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -165,8 +165,8 @@ func TestAPIAuth_InvalidOAuthToken(t *testing.T) {
165165
}
166166

167167
func TestAPIAuth_OAuthNotConfigured_NonLCToken(t *testing.T) {
168-
m := NewAPIAuth(func(ctx context.Context, rawKey string) error {
169-
return fmt.Errorf("invalid key")
168+
m := NewAPIAuth(func(ctx context.Context, rawKey string) (interface{}, error) {
169+
return nil, fmt.Errorf("invalid key")
170170
})
171171
// OAuth NOT configured
172172

@@ -186,11 +186,11 @@ func TestAPIAuth_OAuthNotConfigured_NonLCToken(t *testing.T) {
186186

187187
func TestAPIAuth_WWWAuthenticateHeader(t *testing.T) {
188188
resourceURL := "https://example.com/.well-known/oauth-protected-resource"
189-
m := NewAPIAuth(func(ctx context.Context, rawKey string) error {
190-
return fmt.Errorf("invalid")
189+
m := NewAPIAuth(func(ctx context.Context, rawKey string) (interface{}, error) {
190+
return nil, fmt.Errorf("invalid")
191191
})
192-
m.SetOAuth(func(ctx context.Context, rawToken string) error {
193-
return fmt.Errorf("invalid")
192+
m.SetOAuth(func(ctx context.Context, rawToken string) (interface{}, error) {
193+
return nil, fmt.Errorf("invalid")
194194
}, "lc_system", resourceURL)
195195

196196
handler := m.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -212,8 +212,8 @@ func TestAPIAuth_WWWAuthenticateHeader(t *testing.T) {
212212
}
213213

214214
func TestAPIAuth_NoWWWAuthenticateWithoutResourceMetadata(t *testing.T) {
215-
m := NewAPIAuth(func(ctx context.Context, rawKey string) error {
216-
return fmt.Errorf("invalid")
215+
m := NewAPIAuth(func(ctx context.Context, rawKey string) (interface{}, error) {
216+
return nil, fmt.Errorf("invalid")
217217
})
218218
// No OAuth configured, no resource metadata URL
219219

@@ -232,8 +232,8 @@ func TestAPIAuth_NoWWWAuthenticateWithoutResourceMetadata(t *testing.T) {
232232
}
233233

234234
func TestAPIAuth_CaseInsensitiveBearer(t *testing.T) {
235-
m := NewAPIAuth(func(ctx context.Context, rawKey string) error {
236-
return nil
235+
m := NewAPIAuth(func(ctx context.Context, rawKey string) (interface{}, error) {
236+
return nil, nil
237237
})
238238
handler := m.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
239239
w.WriteHeader(http.StatusOK)

internal/oauth/handler_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ func TestAuthorizeSubmit_LoginSuccess(t *testing.T) {
461461
"code_challenge": {"challenge123"},
462462
"code_challenge_method": {"S256"},
463463
"action": {"login"},
464+
"email": {"admin@localhost"},
464465
"password": {"admin123"},
465466
}
466467

@@ -497,6 +498,7 @@ func TestAuthorizeSubmit_LoginWrongPassword(t *testing.T) {
497498
"code_challenge": {"challenge"},
498499
"code_challenge_method": {"S256"},
499500
"action": {"login"},
501+
"email": {"admin@localhost"},
500502
"password": {"wrongpassword"},
501503
}
502504

@@ -511,8 +513,8 @@ func TestAuthorizeSubmit_LoginWrongPassword(t *testing.T) {
511513
}
512514

513515
body := rr.Body.String()
514-
if !strings.Contains(body, "Invalid password") {
515-
t.Error("expected 'Invalid password' error in response")
516+
if !strings.Contains(body, "Invalid email or password") {
517+
t.Error("expected 'Invalid email or password' error in response")
516518
}
517519
}
518520

internal/testutil/testutil.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ func CleanupCollections(t *testing.T, db *database.DB) {
9898
collections := []string{
9999
"content", "content_versions", "templates", "folders", "collections",
100100
"custom_pages", "settings", "theme_versions", "contact_messages",
101-
"login_attempts", "assets", "api_keys", "redirects",
101+
"login_attempts", "assets", "api_keys", "redirects", "snippets",
102+
"users", "audit_logs",
102103
"oauth_clients", "oauth_auth_codes", "oauth_access_tokens", "oauth_refresh_tokens",
103104
}
104105
for _, name := range collections {

0 commit comments

Comments
 (0)