package httpapi import ( "context" "errors" "net/http" "net/http/httptest" "testing" "github.com/danielgtaylor/huma/v2" ) // headerAuth admits the caller its X-Test-Caller header names. type headerAuth struct{} func (headerAuth) Authenticate(ctx context.Context, r *http.Request) (context.Context, bool, error) { switch r.Header.Get("X-Test-Caller") { case "admin": return ctx, true, nil case "person": return ctx, false, nil case "broken": return nil, false, errors.New("database down") default: return nil, false, ErrUnauthenticated } } // TestOperationAccess guards the API's authentication boundary: public // operations need no credential, every other operation needs one, only an // instance administrator reaches an operation not marked otherwise, and a // cross-origin browser request cannot use the session cookie to change // anything. func TestOperationAccess(t *testing.T) { tests := []struct { name, method, path, caller, fetchSite string want int }{ {name: "public without a credential", method: http.MethodGet, path: "/public", want: http.StatusOK}, {name: "public sign-in from this origin", method: http.MethodPost, path: "/public", fetchSite: "same-origin", want: http.StatusOK}, {name: "public sign-in from another origin", method: http.MethodPost, path: "/public", fetchSite: "cross-site", want: http.StatusForbidden}, {name: "signed-in without a credential", method: http.MethodGet, path: "/signed-in", want: http.StatusUnauthorized}, {name: "signed-in as a person", method: http.MethodGet, path: "/signed-in", caller: "person", want: http.StatusOK}, {name: "a stream without a credential", method: http.MethodGet, path: "/stream", want: http.StatusUnauthorized}, {name: "a stream as a person", method: http.MethodGet, path: "/stream", caller: "person", want: http.StatusOK}, {name: "admin without a credential", method: http.MethodGet, path: "/admin", want: http.StatusUnauthorized}, {name: "admin as a person", method: http.MethodGet, path: "/admin", caller: "person", want: http.StatusForbidden}, {name: "admin as the admin", method: http.MethodGet, path: "/admin", caller: "admin", want: http.StatusOK}, {name: "mutation from this origin", method: http.MethodPost, path: "/admin", caller: "admin", fetchSite: "same-origin", want: http.StatusOK}, {name: "mutation from another origin", method: http.MethodPost, path: "/admin", caller: "admin", fetchSite: "cross-site", want: http.StatusForbidden}, {name: "a failed lookup is not a refusal", method: http.MethodGet, path: "/admin", caller: "broken", want: http.StatusServiceUnavailable}, } mux := http.NewServeMux() api := New(mux, "test", headerAuth{}) reached := func(context.Context, *struct{}) (*Reply[string], error) { return OK("reached"), nil } for _, method := range []string{http.MethodGet, http.MethodPost} { huma.Register(api, Public(huma.Operation{OperationID: "public-" + method, Method: method, Path: "/public"}), reached) huma.Register(api, SignedIn(huma.Operation{OperationID: "signed-in-" + method, Method: method, Path: "/signed-in"}), reached) huma.Register(api, huma.Operation{OperationID: "admin-" + method, Method: method, Path: "/admin"}, reached) } // A streamed response writes its own status, so it must still pass the // guard before its body runs. huma.Register(api, SignedIn(huma.Operation{OperationID: "stream", Method: http.MethodGet, Path: "/stream"}), func(context.Context, *struct{}) (*huma.StreamResponse, error) { return &huma.StreamResponse{Body: func(hc huma.Context) { hc.SetStatus(http.StatusOK) }}, nil }) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { req := httptest.NewRequestWithContext(t.Context(), tt.method, tt.path, nil) if tt.caller != "" { req.Header.Set("X-Test-Caller", tt.caller) } if tt.fetchSite != "" { req.Header.Set("Sec-Fetch-Site", tt.fetchSite) } rec := httptest.NewRecorder() mux.ServeHTTP(rec, req) if rec.Code != tt.want { t.Fatalf("status = %d, want %d: %s", rec.Code, tt.want, rec.Body) } }) } }