test(backup, scrobble, playevents, recsettings, apierror, auth, providers): merge repeated cases (#8299)
release / web (push) Successful in 1m41s
release / go (push) Successful in 2m4s
release / govulncheck (push) Successful in 23s
release / integration (push) Successful in 3m5s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Publish the release image to GHCR (dispatch only) (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / android (push) Canceled after 4m35s
release / Build signed APK (push) Canceled after 3m53s
release / web (push) Successful in 1m41s
release / go (push) Successful in 2m4s
release / govulncheck (push) Successful in 23s
release / integration (push) Successful in 3m5s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Publish the release image to GHCR (dispatch only) (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / android (push) Canceled after 4m35s
release / Build signed APK (push) Canceled after 3m53s
- backup: schema coverage's three tests and the three AWS signing examples, one table each. - scrobble: Qualifies' six boundary tests; the ListenBrainz client's refusal mapping, twelve tests of the same rule across three calls, one table over call x status; the two similarity lookups' success and URL tests, one table. - playevents: the skip rule's three plays share one fixture. Dropped TestRecordPlayEnded_SubThreshold_NoEnqueue: the writer only calls scrobble.MaybeEnqueue, whose own test and Qualifies' table hold that case; the qualifying-play test still proves the writer calls it. - recsettings: the three tag-scale tests share one database reset. - apierror: Error() and From, one test each; auth: RequireAdmin's three. - providers: GetJSON's not-found and transient answers, one table. Its two 401 tests expected the same answer with and without TreatAuthAsTransient, which changes only the error's text; the table says so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+36
-67
@@ -32,72 +32,41 @@ func assertErrorCode(t *testing.T, w *httptest.ResponseRecorder, code string) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireAdmin_AdminPasses(t *testing.T) {
|
||||
called := false
|
||||
stub := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
called = true
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
h := RequireAdmin()(stub)
|
||||
|
||||
req := injectUser(
|
||||
httptest.NewRequest(http.MethodGet, "/api/admin/test", nil),
|
||||
dbq.User{IsAdmin: true},
|
||||
)
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
if !called {
|
||||
t.Error("stub handler was not called for admin user")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireAdmin_NonAdminReturns403(t *testing.T) {
|
||||
stub := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
|
||||
t.Fatal("stub handler must not be called for non-admin user")
|
||||
})
|
||||
|
||||
h := RequireAdmin()(stub)
|
||||
|
||||
req := injectUser(
|
||||
httptest.NewRequest(http.MethodGet, "/api/admin/test", nil),
|
||||
dbq.User{IsAdmin: false},
|
||||
)
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Errorf("status = %d, want 403", w.Code)
|
||||
}
|
||||
assertErrorCode(t, w, "not_authorized")
|
||||
ct := w.Header().Get("Content-Type")
|
||||
if !strings.HasPrefix(ct, "application/json") {
|
||||
t.Errorf("Content-Type = %q, want application/json", ct)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireAdmin_NoUserContextReturns500(t *testing.T) {
|
||||
stub := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
|
||||
t.Fatal("stub handler must not be called when no user is in context")
|
||||
})
|
||||
|
||||
h := RequireAdmin()(stub)
|
||||
|
||||
// Do NOT inject a user — simulate RequireUser being bypassed.
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/admin/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("status = %d, want 500", w.Code)
|
||||
}
|
||||
assertErrorCode(t, w, "server_error")
|
||||
ct := w.Header().Get("Content-Type")
|
||||
if !strings.HasPrefix(ct, "application/json") {
|
||||
t.Errorf("Content-Type = %q, want application/json", ct)
|
||||
// RequireAdmin passes an admin through, answers anyone else 403, and answers
|
||||
// 500 when no user is in the context at all (RequireUser was not mounted in
|
||||
// front of it), always as the /api error envelope.
|
||||
func TestRequireAdmin(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
user *dbq.User
|
||||
wantCode int
|
||||
wantErr string // "" when the handler is reached
|
||||
}{
|
||||
{"an admin", &dbq.User{IsAdmin: true}, http.StatusOK, ""},
|
||||
{"a listener", &dbq.User{}, http.StatusForbidden, "not_authorized"},
|
||||
{"no user in the context", nil, http.StatusInternalServerError, "server_error"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
reached := false
|
||||
h := RequireAdmin()(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
reached = true
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/admin/test", nil)
|
||||
if tc.user != nil {
|
||||
req = injectUser(req, *tc.user)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, req)
|
||||
if w.Code != tc.wantCode || reached != (tc.wantErr == "") {
|
||||
t.Fatalf("status %d, handler reached %v; want %d, %v", w.Code, reached, tc.wantCode, tc.wantErr == "")
|
||||
}
|
||||
if tc.wantErr != "" {
|
||||
assertErrorCode(t, w, tc.wantErr)
|
||||
if ct := w.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") {
|
||||
t.Errorf("Content-Type = %q, want application/json", ct)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user