Files
minstrel/internal/auth/admin_test.go
T
bvandeusenandClaude Opus 5.5 0811775af6 fix(api): every /api error answers in one envelope, so Android shows the real reason (#8002)
Three error shapes were on the wire: {"error":{"code","message"}} from
writeErr, a flat {"error":"code"} from the admin queues, playback errors
and RequireAdmin, and plain text from RequireUser's 401s. Android reads
only the first, so an admin's quarantine, duplicate or suspect-file
error, and "you're not an admin", all showed "Something went wrong."

apierror.Write is now the one writer, and api.writeErr, RequireAdmin,
RequireUser, the client-APK endpoints, the events stream, the unknown-
route 404 and the admin scan all go through it. The flat writers are
gone, and the web client drops its second branch. The admin codes keep
their names; each now carries a message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 12:43:27 -05:00

104 lines
2.9 KiB
Go

package auth
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// injectUser returns a copy of r with the given user stored under userCtxKey.
// This mirrors what RequireUser does at runtime.
func injectUser(r *http.Request, u dbq.User) *http.Request {
ctx := context.WithValue(r.Context(), userCtxKey, u)
return r.WithContext(ctx)
}
// assertErrorCode checks the response is the /api error envelope with code.
// Clients read only that shape, so a flat {"error":"code"} must fail here.
func assertErrorCode(t *testing.T, w *httptest.ResponseRecorder, code string) {
t.Helper()
var env apierror.Body
if err := json.Unmarshal(w.Body.Bytes(), &env); err != nil {
t.Fatalf("body %q is not the error envelope: %v", w.Body.String(), err)
}
if env.Error.Code != code {
t.Errorf("error.code = %q, want %q; body %q", env.Error.Code, code, w.Body.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)
}
}