aboutsummaryrefslogtreecommitdiff
path: root/packages/sdk/src/api-key.test.ts
blob: 1ff815da9645b5e20f3af9f3ccadd36f7f302eab (plain) (blame)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { describe, expect, it, vi } from "vitest";
import {
	API_KEY_PREFIX,
	extractKeyPrefix,
	generateApiKey,
	hashApiKey,
	isValidApiKeyFormat,
} from "./api-key.js";
import { EdgeFunctionApiKeyValidator } from "./api-key-validator.js";

describe("API Key Generation", () => {
	describe("generateApiKey", () => {
		it("generates a key with the correct prefix", () => {
			const apiKey = generateApiKey();

			expect(apiKey.startsWith(API_KEY_PREFIX)).toBe(true);
		});
		it("generates a key with 32 hex characters after prefix", () => {
			const apiKey = generateApiKey();
			const keyPart = apiKey.slice(API_KEY_PREFIX.length);

			expect(keyPart.length).toBe(32);
			expect(/^[0-9a-f]+$/i.test(keyPart)).toBe(true);
		});
		it("generates unique keys on each call", () => {
			const apiKey1 = generateApiKey();
			const apiKey2 = generateApiKey();

			expect(apiKey1).not.toBe(apiKey2);
		});
		it("generates keys with total length of prefix + 32 characters", () => {
			const apiKey = generateApiKey();

			expect(apiKey.length).toBe(API_KEY_PREFIX.length + 32);
		});
	});
	describe("extractKeyPrefix", () => {
		it("extracts first 8 characters after prefix", () => {
			const apiKey = "imemio_0cee07c39ef7ba7d8e23e760929ce7bc";
			const prefix = extractKeyPrefix(apiKey);

			expect(prefix).toBe("0cee07c3");
		});
		it("throws error for invalid key format", () => {
			expect(() => extractKeyPrefix("invalid_key")).toThrow(
				"Invalid API key format",
			);
		});
		it("throws error for key without imemio prefix", () => {
			expect(() => extractKeyPrefix("sk_0cee07c39ef7ba7d")).toThrow(
				"Invalid API key format",
			);
		});
	});
	describe("hashApiKey", () => {
		it("returns a 64 character hex string", async () => {
			const apiKey = "imemio_0cee07c39ef7ba7d8e23e760929ce7bc";
			const hash = await hashApiKey(apiKey);

			expect(hash.length).toBe(64);
			expect(/^[0-9a-f]+$/i.test(hash)).toBe(true);
		});
		it("returns consistent hash for same input", async () => {
			const apiKey = "imemio_0cee07c39ef7ba7d8e23e760929ce7bc";
			const hash1 = await hashApiKey(apiKey);
			const hash2 = await hashApiKey(apiKey);

			expect(hash1).toBe(hash2);
		});
		it("returns different hashes for different inputs", async () => {
			const apiKey1 = "imemio_0cee07c39ef7ba7d8e23e760929ce7bc";
			const apiKey2 = "imemio_1111111111111111111111111111111a";
			const hash1 = await hashApiKey(apiKey1);
			const hash2 = await hashApiKey(apiKey2);

			expect(hash1).not.toBe(hash2);
		});
	});
	describe("isValidApiKeyFormat", () => {
		it("returns true for valid API key", () => {
			const apiKey = "imemio_0cee07c39ef7ba7d8e23e760929ce7bc";

			expect(isValidApiKeyFormat(apiKey)).toBe(true);
		});
		it("returns false for key without prefix", () => {
			const apiKey = "0cee07c39ef7ba7d8e23e760929ce7bc";

			expect(isValidApiKeyFormat(apiKey)).toBe(false);
		});
		it("returns false for key with wrong prefix", () => {
			const apiKey = "sk_0cee07c39ef7ba7d8e23e760929ce7bc";

			expect(isValidApiKeyFormat(apiKey)).toBe(false);
		});
		it("returns false for key with too few hex characters", () => {
			const apiKey = "imemio_0cee07c3";

			expect(isValidApiKeyFormat(apiKey)).toBe(false);
		});
		it("returns false for key with too many hex characters", () => {
			const apiKey = "imemio_0cee07c39ef7ba7d8e23e760929ce7bc00";

			expect(isValidApiKeyFormat(apiKey)).toBe(false);
		});
		it("returns false for key with non-hex characters", () => {
			const apiKey = "imemio_0cee07c39ef7ba7d8e23e760929cexyz";

			expect(isValidApiKeyFormat(apiKey)).toBe(false);
		});
		it("accepts uppercase hex characters", () => {
			const apiKey = "imemio_0CEE07C39EF7BA7D8E23E760929CE7BC";

			expect(isValidApiKeyFormat(apiKey)).toBe(true);
		});
	});
});
describe("EdgeFunctionApiKeyValidator", () => {
	const mockSupabaseUrl = "https://test.supabase.co";
	const mockSupabaseAnonKey = "test-anon-key";

	describe("validate", () => {
		it("returns valid result with userId and apiKeyId on success", async () => {
			const mockResponse = {
				ok: true,
				json: vi.fn().mockResolvedValue({
					userId: "user-123",
					apiKeyId: "key-456",
				}),
			};

			global.fetch = vi.fn().mockResolvedValue(mockResponse);

			const validator = new EdgeFunctionApiKeyValidator(
				mockSupabaseUrl,
				mockSupabaseAnonKey,
			);
			const result = await validator.validate(
				"imemio_0cee07c39ef7ba7d8e23e760929ce7bc",
			);

			expect(result.valid).toBe(true);

			if (result.valid) {
				expect(result.userId).toBe("user-123");
				expect(result.apiKeyId).toBe("key-456");
			}
		});
		it("sends correct request to Edge Function", async () => {
			const mockResponse = {
				ok: true,
				json: vi.fn().mockResolvedValue({
					userId: "user-123",
					apiKeyId: "key-456",
				}),
			};

			global.fetch = vi.fn().mockResolvedValue(mockResponse);

			const validator = new EdgeFunctionApiKeyValidator(
				mockSupabaseUrl,
				mockSupabaseAnonKey,
			);

			await validator.validate("imemio_0cee07c39ef7ba7d8e23e760929ce7bc");
			expect(fetch).toHaveBeenCalledWith(
				`${mockSupabaseUrl}/functions/v1/validate-api-key`,
				{
					method: "POST",
					headers: {
						"Content-Type": "application/json",
						Authorization: `Bearer ${mockSupabaseAnonKey}`,
					},
					body: JSON.stringify({
						apiKey: "imemio_0cee07c39ef7ba7d8e23e760929ce7bc",
					}),
				},
			);
		});
		it("returns invalid result with error on non-ok response", async () => {
			const mockResponse = {
				ok: false,
				json: vi.fn().mockResolvedValue({
					error: "Invalid or revoked API key",
				}),
			};

			global.fetch = vi.fn().mockResolvedValue(mockResponse);

			const validator = new EdgeFunctionApiKeyValidator(
				mockSupabaseUrl,
				mockSupabaseAnonKey,
			);
			const result = await validator.validate("imemio_invalid");

			expect(result.valid).toBe(false);

			if (!result.valid) {
				expect(result.error).toBe("Invalid or revoked API key");
			}
		});
		it("returns default error message when response has no error field", async () => {
			const mockResponse = {
				ok: false,
				json: vi.fn().mockResolvedValue({}),
			};

			global.fetch = vi.fn().mockResolvedValue(mockResponse);

			const validator = new EdgeFunctionApiKeyValidator(
				mockSupabaseUrl,
				mockSupabaseAnonKey,
			);
			const result = await validator.validate("imemio_invalid");

			expect(result.valid).toBe(false);

			if (!result.valid) {
				expect(result.error).toBe("Validation failed");
			}
		});
		it("returns invalid result on network error", async () => {
			global.fetch = vi.fn().mockRejectedValue(new Error("Network error"));

			const validator = new EdgeFunctionApiKeyValidator(
				mockSupabaseUrl,
				mockSupabaseAnonKey,
			);
			const result = await validator.validate(
				"imemio_0cee07c39ef7ba7d8e23e760929ce7bc",
			);

			expect(result.valid).toBe(false);

			if (!result.valid) {
				expect(result.error).toBe("Failed to validate API key");
			}
		});
	});
});