(t *testing.T)
| 204 | gin.SetMode(gin.TestMode) |
| 205 | sso := newTestSSO(t) |
| 206 | resolve := roleByEmail("a@posthog.com") |
| 207 | r := gin.New() |
| 208 | r.GET("/audit", AuthMiddleware(NewTokenSet("secret", nil), resolve, sso.verifier), RequireAdmin(), func(c *gin.Context) { |
| 209 | c.Status(http.StatusOK) |
| 210 | }) |
| 211 | viewer := sso.oidc(t, map[string]any{"email": "v@posthog.com"}) |
| 212 | admin := sso.oidc(t, map[string]any{"email": "a@posthog.com"}) |
| 213 | for _, tc := range []struct { |
| 214 | name, oidc string |
| 215 | want int |
| 216 | }{ |
| 217 | {"viewer blocked", viewer, http.StatusForbidden}, |
| 218 | {"admin allowed", admin, http.StatusOK}, |
| 219 | } { |
| 220 | t.Run(tc.name, func(t *testing.T) { |
| 221 | req := httptest.NewRequest(http.MethodGet, "/audit", nil) |
| 222 | req.Header.Set(albOIDCDataHeader, tc.oidc) |
| 223 | rec := httptest.NewRecorder() |
| 224 | r.ServeHTTP(rec, req) |
| 225 | if rec.Code != tc.want { |
| 226 | t.Fatalf("status = %d, want %d", rec.Code, tc.want) |
| 227 | } |
| 228 | }) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func TestAuthMiddlewareRejectsUnauthenticated(t *testing.T) { |
| 233 | gin.SetMode(gin.TestMode) |
| 234 | r := gin.New() |
| 235 | r.GET("/x", AuthMiddleware(NewTokenSet("secret", nil), roleByEmail(), nil), func(c *gin.Context) { |
| 236 | c.JSON(http.StatusOK, gin.H{"ok": true}) |
| 237 | }) |
| 238 | req := httptest.NewRequest(http.MethodGet, "/x", nil) |
| 239 | rec := httptest.NewRecorder() |
| 240 | r.ServeHTTP(rec, req) |
| 241 | if rec.Code != http.StatusUnauthorized { |
nothing calls this directly
no test coverage detected