package httpapi import ( "context" "net/http" "net/http/httptest" "testing" "github.com/danielgtaylor/huma/v2" "github.com/samcharles93/archie-core/internal/platform/httpjson" ) // TestEveryOperationRequiresTheAdminToken guards the bootstrap admin boundary: // only a bearer token whose hash matches the configured one reaches an // operation, and an empty configured hash locks everyone out rather than // letting everyone in. func TestEveryOperationRequiresTheAdminToken(t *testing.T) { const token = "a-plainly-fake-admin-token" //nolint:gosec // test fixture, not a credential tests := []struct { name string hash string header string want int }{ {name: "matching token", hash: httpjson.TokenHash(token), header: "Bearer " + token, want: http.StatusOK}, {name: "wrong token", hash: httpjson.TokenHash(token), header: "Bearer other", want: http.StatusUnauthorized}, {name: "no token", hash: httpjson.TokenHash(token), header: "", want: http.StatusUnauthorized}, {name: "not a bearer token", hash: httpjson.TokenHash(token), header: "Basic " + token, want: http.StatusUnauthorized}, {name: "no configured hash locks everyone out", hash: "", header: "Bearer ", want: http.StatusUnauthorized}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mux := http.NewServeMux() api := New(mux, "test", tt.hash) huma.Get(api, "/api/thing", func(context.Context, *struct{}) (*Reply[string], error) { return OK("reached"), nil }) req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/thing", nil) if tt.header != "" { req.Header.Set("Authorization", tt.header) } rec := httptest.NewRecorder() mux.ServeHTTP(rec, req) if rec.Code != tt.want { t.Fatalf("status = %d, want %d", rec.Code, tt.want) } }) } }