|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/goccy/go-json" |
| 10 | + fiber "github.com/gofiber/fiber/v3" |
| 11 | + |
| 12 | + "github.com/hyp3rd/hypercache/pkg/httpauth" |
| 13 | +) |
| 14 | + |
| 15 | +// TestHandleMe_BodyShape pins the wire shape of GET /v1/me. We mount |
| 16 | +// the handler behind a tiny inline middleware that stuffs a known |
| 17 | +// Identity into Locals — the same shape httpauth.Policy.Middleware |
| 18 | +// installs after a successful auth match — so the assertion is purely |
| 19 | +// about the response body, not the auth resolver. auth_test.go covers |
| 20 | +// the authentication contract; this test covers the rendering one. |
| 21 | +func TestHandleMe_BodyShape(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + |
| 24 | + cases := []struct { |
| 25 | + name string |
| 26 | + identity httpauth.Identity |
| 27 | + want meResponse |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "read-only operator", |
| 31 | + identity: httpauth.Identity{ |
| 32 | + ID: "ops-readonly", |
| 33 | + Scopes: []httpauth.Scope{httpauth.ScopeRead}, |
| 34 | + }, |
| 35 | + want: meResponse{ID: "ops-readonly", Scopes: []string{"read"}}, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "rw operator", |
| 39 | + identity: httpauth.Identity{ |
| 40 | + ID: "ops-rw", |
| 41 | + Scopes: []httpauth.Scope{httpauth.ScopeRead, httpauth.ScopeWrite}, |
| 42 | + }, |
| 43 | + want: meResponse{ID: "ops-rw", Scopes: []string{"read", "write"}}, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "anonymous (AllowAnonymous=true on the policy)", |
| 47 | + identity: httpauth.Identity{ |
| 48 | + ID: "anonymous", |
| 49 | + Scopes: []httpauth.Scope{httpauth.ScopeRead, httpauth.ScopeWrite, httpauth.ScopeAdmin}, |
| 50 | + }, |
| 51 | + want: meResponse{ID: "anonymous", Scopes: []string{"read", "write", "admin"}}, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tc := range cases { |
| 56 | + t.Run(tc.name, func(t *testing.T) { |
| 57 | + t.Parallel() |
| 58 | + |
| 59 | + got := callMeWithIdentity(t, tc.identity) |
| 60 | + assertMeBody(t, got, tc.want) |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// callMeWithIdentity drives /v1/me through a fiber app with a single |
| 66 | +// middleware that pre-populates IdentityKey. Returns the decoded |
| 67 | +// response body. Failed status / decode trips the test fatally. |
| 68 | +func callMeWithIdentity(t *testing.T, identity httpauth.Identity) meResponse { |
| 69 | + t.Helper() |
| 70 | + |
| 71 | + app := fiber.New() |
| 72 | + // Stand-in for httpauth.Policy.Middleware — installs the |
| 73 | + // Locals entry handleMe reads. Test owns the identity it |
| 74 | + // asserts against. |
| 75 | + app.Use(func(c fiber.Ctx) error { |
| 76 | + c.Locals(httpauth.IdentityKey, identity) |
| 77 | + |
| 78 | + return c.Next() |
| 79 | + }) |
| 80 | + app.Get("/v1/me", handleMe) |
| 81 | + |
| 82 | + req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/v1/me", strings.NewReader("")) |
| 83 | + |
| 84 | + resp, err := app.Test(req) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("app.Test: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + defer func() { _ = resp.Body.Close() }() |
| 90 | + |
| 91 | + if resp.StatusCode != http.StatusOK { |
| 92 | + t.Fatalf("status: got %d, want 200", resp.StatusCode) |
| 93 | + } |
| 94 | + |
| 95 | + var got meResponse |
| 96 | + |
| 97 | + err = json.NewDecoder(resp.Body).Decode(&got) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("decode body: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + return got |
| 103 | +} |
| 104 | + |
| 105 | +// assertMeBody compares a decoded meResponse against the expected |
| 106 | +// shape. ID is a single string compare; scopes are ordered slices |
| 107 | +// (handleMe preserves the Identity.Scopes order, so order is part |
| 108 | +// of the contract). |
| 109 | +func assertMeBody(t *testing.T, got, want meResponse) { |
| 110 | + t.Helper() |
| 111 | + |
| 112 | + if got.ID != want.ID { |
| 113 | + t.Errorf("id: got %q, want %q", got.ID, want.ID) |
| 114 | + } |
| 115 | + |
| 116 | + if len(got.Scopes) != len(want.Scopes) { |
| 117 | + t.Fatalf("scopes length: got %d, want %d (got=%v)", len(got.Scopes), len(want.Scopes), got.Scopes) |
| 118 | + } |
| 119 | + |
| 120 | + for i, s := range want.Scopes { |
| 121 | + if got.Scopes[i] != s { |
| 122 | + t.Errorf("scopes[%d]: got %q, want %q", i, got.Scopes[i], s) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// TestHandleMe_MissingLocals covers the wiring-bug path. If a future |
| 128 | +// refactor mounts handleMe without the auth middleware, the type |
| 129 | +// assertion in handleMe falls through to a 500 with codeInternal — |
| 130 | +// not a silent default identity. This test pins that fail-loud |
| 131 | +// behavior so a misconfiguration cannot silently degrade to |
| 132 | +// "everyone is anonymous, with all scopes.". |
| 133 | +func TestHandleMe_MissingLocals(t *testing.T) { |
| 134 | + t.Parallel() |
| 135 | + |
| 136 | + app := fiber.New() |
| 137 | + // No middleware installs IdentityKey — handleMe should 500. |
| 138 | + app.Get("/v1/me", handleMe) |
| 139 | + |
| 140 | + req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/v1/me", strings.NewReader("")) |
| 141 | + |
| 142 | + resp, err := app.Test(req) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("app.Test: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + defer func() { _ = resp.Body.Close() }() |
| 148 | + |
| 149 | + if resp.StatusCode != http.StatusInternalServerError { |
| 150 | + t.Fatalf("status: got %d, want 500", resp.StatusCode) |
| 151 | + } |
| 152 | +} |
0 commit comments