Files
minstrel/internal/auth/admin_test.go
T
bvandeusenandClaude Opus 5.5 e1f270362d
release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(release): everything that ships points at GitHub and GHCR (#7897)
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every
  import and the Dockerfile's version ldflags.
- buildinfo.ProjectURL, the contact outside services see in Minstrel's
  User-Agent: https://github.com/Fabled-Sword/minstrel.
- README's compose example pulls ghcr.io/fabled-sword/minstrel:latest.
- docs/security.md sends problem reports to the GitHub issues.
- The demo-library stack builds Minstrel from the checkout, since :dev
  is not published to GHCR.
- hygiene.go's same-site example uses example.com names.

The workbench CI (.gitea/, ci/, export-ignored) still builds and
publishes :dev to the private registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:28:16 -05:00

104 lines
2.9 KiB
Go

package auth
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/Fabled-Sword/minstrel/internal/apierror"
"github.com/Fabled-Sword/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)
}
}