Coverage gate (70%) fell to 67.6% with the new M-MIG-5 code paths.
Adds tests for GET /v1/tenants pagination + filters + validation,
GET /v1/tenants/{id}, POST /v1/tenants/{id}/status validation and the
Keycloak-failure audit path, /readyz, the requireAuthz middleware and
LogAuthz, and HTTPAdapter.SyncTenantStatus / findUserByEmail against a
stub Keycloak admin API. CI package set now reports 73.5%.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
64 lines
2.1 KiB
Go
64 lines
2.1 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/authn"
|
|
)
|
|
|
|
func TestRequireAuthz_middleware(t *testing.T) {
|
|
ok := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) })
|
|
s := &Server{Log: slog.New(slog.NewTextHandler(io.Discard, nil))}
|
|
|
|
// nil rules → passthrough
|
|
rr := httptest.NewRecorder()
|
|
s.requireAuthz(ok).ServeHTTP(rr, httptest.NewRequest("POST", "/v1/tenants", nil))
|
|
if rr.Code != http.StatusNoContent {
|
|
t.Fatalf("nil rules: %d", rr.Code)
|
|
}
|
|
|
|
s.Authz = ParseAuthzRules("pruefwerk-svc:entitlements,tenants.read")
|
|
// no principal (auth disabled) → passthrough
|
|
rr = httptest.NewRecorder()
|
|
s.requireAuthz(ok).ServeHTTP(rr, httptest.NewRequest("POST", "/v1/tenants", nil))
|
|
if rr.Code != http.StatusNoContent {
|
|
t.Fatalf("no principal: %d", rr.Code)
|
|
}
|
|
// allowed group
|
|
req := httptest.NewRequest("GET", "/v1/tenants", nil)
|
|
req = req.WithContext(authn.WithPrincipal(req.Context(), &authn.Principal{Subject: "svc", ClientID: "pruefwerk-svc"}))
|
|
rr = httptest.NewRecorder()
|
|
s.requireAuthz(ok).ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusNoContent {
|
|
t.Fatalf("allowed: %d", rr.Code)
|
|
}
|
|
// forbidden group
|
|
req = httptest.NewRequest("POST", "/v1/tenants", nil)
|
|
req = req.WithContext(authn.WithPrincipal(req.Context(), &authn.Principal{Subject: "svc", ClientID: "pruefwerk-svc"}))
|
|
rr = httptest.NewRecorder()
|
|
s.requireAuthz(ok).ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusForbidden || !strings.Contains(rr.Body.String(), "FORBIDDEN_CLIENT") {
|
|
t.Fatalf("forbidden: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestLogAuthz(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
log := slog.New(slog.NewTextHandler(&buf, nil))
|
|
LogAuthz(log, nil)
|
|
if !strings.Contains(buf.String(), "AUTHZ_RULES not set") {
|
|
t.Fatalf("nil rules must warn: %s", buf.String())
|
|
}
|
|
buf.Reset()
|
|
LogAuthz(log, ParseAuthzRules("portal-svc:tenants;root:*"))
|
|
if !strings.Contains(buf.String(), "portal-svc") || !strings.Contains(buf.String(), "root") {
|
|
t.Fatalf("rules must be logged: %s", buf.String())
|
|
}
|
|
}
|