forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-key-auth.test.ts
More file actions
98 lines (73 loc) · 2.78 KB
/
Copy pathapi-key-auth.test.ts
File metadata and controls
98 lines (73 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import request from "supertest";
const TEST_KEY = "test-secret-key-12345";
describe("API key authentication middleware (issue #81)", () => {
let originalKey: string | undefined;
beforeEach(() => {
originalKey = process.env.AUTH_API_KEY;
process.env.AUTH_API_KEY = TEST_KEY;
vi.resetModules();
});
afterEach(() => {
if (originalKey !== undefined) {
process.env.AUTH_API_KEY = originalKey;
} else {
delete process.env.AUTH_API_KEY;
}
});
it("returns 401 when no API key is provided", async () => {
const { createApp: create } = await import("../src/app");
const app = create();
const res = await request(app).get("/api/v1/agents");
expect(res.status).toBe(401);
expect(res.body.success).toBe(false);
expect(res.body.message).toContain("API key");
});
it("returns 403 when an invalid API key is provided", async () => {
const { createApp: create } = await import("../src/app");
const app = create();
const res = await request(app)
.get("/api/v1/agents")
.set("x-api-key", "wrong-key");
expect(res.status).toBe(403);
expect(res.body.success).toBe(false);
expect(res.body.message).toContain("Invalid");
});
it("returns 200 when the correct API key is provided", async () => {
const { createApp: create } = await import("../src/app");
const app = create();
const res = await request(app)
.get("/api/v1/agents")
.set("x-api-key", TEST_KEY);
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
});
it("keeps /health public without requiring an API key", async () => {
const { createApp: create } = await import("../src/app");
const app = create();
const res = await request(app).get("/api/v1/health");
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
});
it("disables auth with a warning when AUTH_API_KEY is not set", async () => {
delete process.env.AUTH_API_KEY;
const { createApp: create } = await import("../src/app");
const app = create();
const res = await request(app).get("/api/v1/agents");
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
});
it("supports a custom header name via AUTH_API_KEY_HEADER", async () => {
process.env.AUTH_API_KEY_HEADER = "x-custom-auth";
const { createApp: create } = await import("../src/app");
const app = create();
const resWrongHeader = await request(app)
.get("/api/v1/agents")
.set("x-api-key", TEST_KEY);
expect(resWrongHeader.status).toBe(401);
const resCorrectHeader = await request(app)
.get("/api/v1/agents")
.set("x-custom-auth", TEST_KEY);
expect(resCorrectHeader.status).toBe(200);
});
});