|
| 1 | +package admin |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/golang-jwt/jwt/v5" |
| 10 | +) |
| 11 | + |
| 12 | +const authTestSecret = "test-secret-key" |
| 13 | + |
| 14 | +func signedToken(t *testing.T, secret string, expiry time.Time) string { |
| 15 | + t.Helper() |
| 16 | + claims := jwt.RegisteredClaims{ExpiresAt: jwt.NewNumericDate(expiry)} |
| 17 | + tok, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(secret)) |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("sign token: %v", err) |
| 20 | + } |
| 21 | + return tok |
| 22 | +} |
| 23 | + |
| 24 | +func TestAuthMiddleware(t *testing.T) { |
| 25 | + validToken := signedToken(t, authTestSecret, time.Now().Add(time.Hour)) |
| 26 | + expiredToken := signedToken(t, authTestSecret, time.Now().Add(-time.Hour)) |
| 27 | + wrongSecretToken := signedToken(t, "wrong-secret", time.Now().Add(time.Hour)) |
| 28 | + |
| 29 | + cases := []struct { |
| 30 | + name string |
| 31 | + authHeader string |
| 32 | + wantStatus int |
| 33 | + }{ |
| 34 | + {"valid token", "Bearer " + validToken, http.StatusOK}, |
| 35 | + {"no token", "", http.StatusUnauthorized}, |
| 36 | + {"wrong secret", "Bearer " + wrongSecretToken, http.StatusUnauthorized}, |
| 37 | + {"expired token", "Bearer " + expiredToken, http.StatusUnauthorized}, |
| 38 | + {"malformed token", "Bearer notajwt", http.StatusUnauthorized}, |
| 39 | + {"missing bearer prefix", validToken, http.StatusUnauthorized}, |
| 40 | + } |
| 41 | + |
| 42 | + next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 43 | + w.WriteHeader(http.StatusOK) |
| 44 | + }) |
| 45 | + middleware := AuthMiddleware(authTestSecret)(next) |
| 46 | + |
| 47 | + for _, tc := range cases { |
| 48 | + t.Run(tc.name, func(t *testing.T) { |
| 49 | + req := httptest.NewRequest(http.MethodPost, "/graphql", nil) |
| 50 | + if tc.authHeader != "" { |
| 51 | + req.Header.Set("Authorization", tc.authHeader) |
| 52 | + } |
| 53 | + rr := httptest.NewRecorder() |
| 54 | + middleware.ServeHTTP(rr, req) |
| 55 | + if rr.Code != tc.wantStatus { |
| 56 | + t.Errorf("want %d, got %d (body: %s)", tc.wantStatus, rr.Code, rr.Body.String()) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestAuthMiddleware_ResponseBody(t *testing.T) { |
| 63 | + next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 64 | + w.WriteHeader(http.StatusOK) |
| 65 | + }) |
| 66 | + middleware := AuthMiddleware(authTestSecret)(next) |
| 67 | + |
| 68 | + req := httptest.NewRequest(http.MethodPost, "/graphql", nil) |
| 69 | + rr := httptest.NewRecorder() |
| 70 | + middleware.ServeHTTP(rr, req) |
| 71 | + |
| 72 | + if ct := rr.Header().Get("Content-Type"); ct != "application/json" { |
| 73 | + t.Errorf("Content-Type: want application/json, got %s", ct) |
| 74 | + } |
| 75 | + body := rr.Body.String() |
| 76 | + if body == "" { |
| 77 | + t.Error("expected non-empty response body on 401") |
| 78 | + } |
| 79 | +} |
0 commit comments