|
| 1 | +/** |
| 2 | + * Admin Authentication Tests |
| 3 | + * |
| 4 | + * Tests for admin API key validation. |
| 5 | + */ |
| 6 | + |
| 7 | +import { Effect } from "effect"; |
| 8 | +import { describe, expect, it, beforeAll, afterAll } from "vitest"; |
| 9 | +import { NextRequest } from "next/server"; |
| 10 | +import { validateAdminKey, isAuthorizationError } from "../adminAuth"; |
| 11 | +import { AuthorizationError } from "../../errors"; |
| 12 | +import { MCPConfigService } from "../../services/config"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Create a mock NextRequest for testing |
| 16 | + */ |
| 17 | +function createMockRequest(options: { |
| 18 | + headers?: Record<string, string>; |
| 19 | + url?: string; |
| 20 | +} = {}): NextRequest { |
| 21 | + const url = options.url || "http://localhost:3000/api/test"; |
| 22 | + const headers = new Headers(); |
| 23 | + if (options.headers) { |
| 24 | + for (const [key, value] of Object.entries(options.headers)) { |
| 25 | + headers.set(key, value); |
| 26 | + } |
| 27 | + } |
| 28 | + return new NextRequest(url, { headers }); |
| 29 | +} |
| 30 | + |
| 31 | +describe("Admin Authentication", () => { |
| 32 | + const prevAdminKey = process.env.ADMIN_API_KEY; |
| 33 | + const prevNodeEnv = process.env.NODE_ENV; |
| 34 | + |
| 35 | + beforeAll(() => { |
| 36 | + process.env.ADMIN_API_KEY = "admin-secret-key"; |
| 37 | + process.env.NODE_ENV = "production"; |
| 38 | + // Ensure config service can load |
| 39 | + if (!process.env.PATTERN_API_KEY) { |
| 40 | + process.env.PATTERN_API_KEY = "test-key"; |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + afterAll(() => { |
| 45 | + if (prevAdminKey !== undefined) { |
| 46 | + process.env.ADMIN_API_KEY = prevAdminKey; |
| 47 | + } else { |
| 48 | + delete process.env.ADMIN_API_KEY; |
| 49 | + } |
| 50 | + if (prevNodeEnv !== undefined) { |
| 51 | + process.env.NODE_ENV = prevNodeEnv; |
| 52 | + } else { |
| 53 | + delete process.env.NODE_ENV; |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + describe("validateAdminKey", () => { |
| 58 | + it("should allow valid admin key from header", async () => { |
| 59 | + const request = createMockRequest({ |
| 60 | + headers: { "x-admin-key": "admin-secret-key" }, |
| 61 | + }); |
| 62 | + |
| 63 | + const result = await Effect.runPromise( |
| 64 | + validateAdminKey(request).pipe( |
| 65 | + Effect.provide(MCPConfigService.Default) |
| 66 | + ) |
| 67 | + ); |
| 68 | + |
| 69 | + expect(result).toBeUndefined(); // Success |
| 70 | + }); |
| 71 | + |
| 72 | + it("should allow valid admin key from query parameter", async () => { |
| 73 | + const request = createMockRequest({ |
| 74 | + url: "http://localhost:3000/api/test?admin-key=admin-secret-key", |
| 75 | + }); |
| 76 | + |
| 77 | + const result = await Effect.runPromise( |
| 78 | + validateAdminKey(request).pipe( |
| 79 | + Effect.provide(MCPConfigService.Default) |
| 80 | + ) |
| 81 | + ); |
| 82 | + |
| 83 | + expect(result).toBeUndefined(); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should prefer header over query parameter", async () => { |
| 87 | + const request = createMockRequest({ |
| 88 | + headers: { "x-admin-key": "admin-secret-key" }, |
| 89 | + url: "http://localhost:3000/api/test?admin-key=wrong-key", |
| 90 | + }); |
| 91 | + |
| 92 | + const result = await Effect.runPromise( |
| 93 | + validateAdminKey(request).pipe( |
| 94 | + Effect.provide(MCPConfigService.Default) |
| 95 | + ) |
| 96 | + ); |
| 97 | + |
| 98 | + expect(result).toBeUndefined(); // Header takes precedence |
| 99 | + }); |
| 100 | + |
| 101 | + it("should reject missing admin key in production", async () => { |
| 102 | + const request = createMockRequest({}); |
| 103 | + |
| 104 | + const result = await Effect.runPromise( |
| 105 | + validateAdminKey(request).pipe( |
| 106 | + Effect.provide(MCPConfigService.Default), |
| 107 | + Effect.either |
| 108 | + ) |
| 109 | + ); |
| 110 | + |
| 111 | + expect(result._tag).toBe("Left"); |
| 112 | + if (result._tag === "Left") { |
| 113 | + const error = result.left; |
| 114 | + expect(error).toBeDefined(); |
| 115 | + // Check if it's an AuthorizationError by _tag |
| 116 | + const errorObj = error as any; |
| 117 | + expect(errorObj._tag).toBe("AuthorizationError"); |
| 118 | + expect(errorObj.message).toContain("required"); |
| 119 | + expect(errorObj.requiredRole).toBe("admin"); |
| 120 | + // Verify isAuthorizationError works (it should, but if not, the direct checks above validate the error) |
| 121 | + // Note: Effect.either may wrap errors differently, so we check _tag directly |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + it("should reject invalid admin key", async () => { |
| 126 | + const request = createMockRequest({ |
| 127 | + headers: { "x-admin-key": "wrong-key" }, |
| 128 | + }); |
| 129 | + |
| 130 | + const result = await Effect.runPromise( |
| 131 | + validateAdminKey(request).pipe( |
| 132 | + Effect.provide(MCPConfigService.Default), |
| 133 | + Effect.either |
| 134 | + ) |
| 135 | + ); |
| 136 | + |
| 137 | + expect(result._tag).toBe("Left"); |
| 138 | + if (result._tag === "Left") { |
| 139 | + const error = result.left; |
| 140 | + expect(error).toBeDefined(); |
| 141 | + const errorObj = error as any; |
| 142 | + expect(errorObj._tag).toBe("AuthorizationError"); |
| 143 | + expect(errorObj.message).toContain("Invalid"); |
| 144 | + expect(errorObj.requiredRole).toBe("admin"); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + it("should allow requests when no admin key configured in dev mode", async () => { |
| 149 | + process.env.NODE_ENV = "development"; |
| 150 | + process.env.ADMIN_API_KEY = ""; |
| 151 | + |
| 152 | + const request = createMockRequest({}); |
| 153 | + |
| 154 | + const result = await Effect.runPromise( |
| 155 | + validateAdminKey(request).pipe( |
| 156 | + Effect.provide(MCPConfigService.Default) |
| 157 | + ) |
| 158 | + ); |
| 159 | + |
| 160 | + expect(result).toBeUndefined(); |
| 161 | + |
| 162 | + // Restore |
| 163 | + process.env.NODE_ENV = "production"; |
| 164 | + process.env.ADMIN_API_KEY = "admin-secret-key"; |
| 165 | + }); |
| 166 | + |
| 167 | + it("should reject requests when admin key not configured in production", async () => { |
| 168 | + process.env.NODE_ENV = "production"; |
| 169 | + process.env.ADMIN_API_KEY = ""; |
| 170 | + |
| 171 | + const request = createMockRequest({ |
| 172 | + headers: { "x-admin-key": "any-key" }, |
| 173 | + }); |
| 174 | + |
| 175 | + const result = await Effect.runPromise( |
| 176 | + validateAdminKey(request).pipe( |
| 177 | + Effect.provide(MCPConfigService.Default), |
| 178 | + Effect.either |
| 179 | + ) |
| 180 | + ); |
| 181 | + |
| 182 | + expect(result._tag).toBe("Left"); |
| 183 | + if (result._tag === "Left") { |
| 184 | + const error = result.left; |
| 185 | + expect(error).toBeDefined(); |
| 186 | + const errorObj = error as any; |
| 187 | + expect(errorObj._tag).toBe("AuthorizationError"); |
| 188 | + expect(errorObj.message).toContain("not configured"); |
| 189 | + } |
| 190 | + |
| 191 | + // Restore |
| 192 | + process.env.ADMIN_API_KEY = "admin-secret-key"; |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + describe("isAuthorizationError", () => { |
| 197 | + it("should return true for AuthorizationError instances", () => { |
| 198 | + const error = new AuthorizationError({ |
| 199 | + message: "Test error", |
| 200 | + requiredRole: "admin", |
| 201 | + }); |
| 202 | + |
| 203 | + expect(isAuthorizationError(error)).toBe(true); |
| 204 | + }); |
| 205 | + |
| 206 | + it("should return false for other error types", () => { |
| 207 | + const error = new Error("Regular error"); |
| 208 | + expect(isAuthorizationError(error)).toBe(false); |
| 209 | + |
| 210 | + const authError = { |
| 211 | + _tag: "AuthenticationError", |
| 212 | + message: "Auth failed", |
| 213 | + }; |
| 214 | + expect(isAuthorizationError(authError)).toBe(false); |
| 215 | + |
| 216 | + expect(isAuthorizationError(null)).toBe(false); |
| 217 | + expect(isAuthorizationError(undefined)).toBe(false); |
| 218 | + expect(isAuthorizationError("string")).toBe(false); |
| 219 | + }); |
| 220 | + }); |
| 221 | +}); |
0 commit comments