-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth-config.test.ts
More file actions
416 lines (349 loc) · 12.6 KB
/
auth-config.test.ts
File metadata and controls
416 lines (349 loc) · 12.6 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import { describe, it, expect } from 'vitest';
import {
AuthProviderConfigSchema,
AuthPluginConfigSchema,
AuthConfigSchema,
MutualTLSConfigSchema,
SocialProviderConfigSchema,
EmailAndPasswordConfigSchema,
EmailVerificationConfigSchema,
AdvancedAuthConfigSchema,
} from './auth-config.zod';
describe('AuthProviderConfigSchema', () => {
it('should accept valid provider config', () => {
const config = AuthProviderConfigSchema.parse({
id: 'github',
clientId: 'abc123',
clientSecret: 'secret456',
});
expect(config.id).toBe('github');
expect(config.clientId).toBe('abc123');
expect(config.clientSecret).toBe('secret456');
});
it('should accept optional scope', () => {
const config = AuthProviderConfigSchema.parse({
id: 'google',
clientId: 'abc123',
clientSecret: 'secret456',
scope: ['email', 'profile'],
});
expect(config.scope).toEqual(['email', 'profile']);
});
it('should reject missing required fields', () => {
expect(() => AuthProviderConfigSchema.parse({})).toThrow();
expect(() => AuthProviderConfigSchema.parse({ id: 'github' })).toThrow();
expect(() => AuthProviderConfigSchema.parse({ id: 'github', clientId: 'abc' })).toThrow();
});
});
describe('AuthPluginConfigSchema', () => {
it('should apply defaults for all fields', () => {
const config = AuthPluginConfigSchema.parse({});
expect(config.organization).toBe(false);
expect(config.twoFactor).toBe(false);
expect(config.passkeys).toBe(false);
expect(config.magicLink).toBe(false);
});
it('should accept custom values', () => {
const config = AuthPluginConfigSchema.parse({
organization: true,
twoFactor: true,
passkeys: true,
magicLink: true,
});
expect(config.organization).toBe(true);
expect(config.twoFactor).toBe(true);
expect(config.passkeys).toBe(true);
expect(config.magicLink).toBe(true);
});
});
describe('AuthConfigSchema', () => {
it('should accept minimal configuration', () => {
const config = AuthConfigSchema.parse({});
expect(config.secret).toBeUndefined();
expect(config.baseUrl).toBeUndefined();
expect(config.providers).toBeUndefined();
expect(config.plugins).toBeUndefined();
expect(config.session).toBeUndefined();
});
it('should accept full configuration', () => {
const config = AuthConfigSchema.parse({
secret: 'my-secret',
baseUrl: 'https://auth.example.com',
databaseUrl: 'postgres://localhost/auth',
providers: [
{ id: 'github', clientId: 'abc', clientSecret: 'def' },
],
plugins: { organization: true, twoFactor: true },
session: { expiresIn: 3600, updateAge: 600 },
});
expect(config.secret).toBe('my-secret');
expect(config.baseUrl).toBe('https://auth.example.com');
expect(config.providers).toHaveLength(1);
expect(config.plugins?.organization).toBe(true);
expect(config.session?.expiresIn).toBe(3600);
});
it('should apply session defaults', () => {
const config = AuthConfigSchema.parse({
session: {},
});
expect(config.session?.expiresIn).toBe(60 * 60 * 24 * 7);
expect(config.session?.updateAge).toBe(60 * 60 * 24);
});
it('should allow extra properties via catchall', () => {
const config = AuthConfigSchema.parse({
customSetting: 'value',
});
expect(config.customSetting).toBe('value');
});
it('should accept trustedOrigins as an array of strings', () => {
const config = AuthConfigSchema.parse({
trustedOrigins: ['https://*.example.com', 'http://localhost:3000'],
});
expect(config.trustedOrigins).toEqual(['https://*.example.com', 'http://localhost:3000']);
});
it('should accept empty trustedOrigins array', () => {
const config = AuthConfigSchema.parse({
trustedOrigins: [],
});
expect(config.trustedOrigins).toEqual([]);
});
it('should allow trustedOrigins to be omitted', () => {
const config = AuthConfigSchema.parse({});
expect(config.trustedOrigins).toBeUndefined();
});
});
// ==========================================
// Mutual TLS Configuration Tests
// ==========================================
describe('MutualTLSConfigSchema', () => {
it('should accept minimal mTLS config', () => {
const config = MutualTLSConfigSchema.parse({
trustedCAs: ['/path/to/ca.pem'],
certificateValidation: 'strict',
});
expect(config.enabled).toBe(false);
expect(config.clientCertRequired).toBe(false);
expect(config.trustedCAs).toHaveLength(1);
expect(config.certificateValidation).toBe('strict');
});
it('should accept full mTLS config', () => {
const config = MutualTLSConfigSchema.parse({
enabled: true,
clientCertRequired: true,
trustedCAs: ['/path/to/ca1.pem', '/path/to/ca2.pem'],
crlUrl: 'https://crl.example.com/crl.pem',
ocspUrl: 'https://ocsp.example.com',
certificateValidation: 'strict',
allowedCNs: ['client.example.com', 'service.example.com'],
allowedOUs: ['Engineering', 'DevOps'],
pinning: {
enabled: true,
pins: ['sha256/AAAA', 'sha256/BBBB'],
},
});
expect(config.enabled).toBe(true);
expect(config.clientCertRequired).toBe(true);
expect(config.trustedCAs).toHaveLength(2);
expect(config.allowedCNs).toHaveLength(2);
expect(config.allowedOUs).toHaveLength(2);
expect(config.pinning?.enabled).toBe(true);
expect(config.pinning?.pins).toHaveLength(2);
});
it('should accept all certificate validation levels', () => {
const levels = ['strict', 'relaxed', 'none'] as const;
levels.forEach(level => {
const config = MutualTLSConfigSchema.parse({
trustedCAs: [],
certificateValidation: level,
});
expect(config.certificateValidation).toBe(level);
});
});
it('should require trustedCAs and certificateValidation', () => {
expect(() => MutualTLSConfigSchema.parse({})).toThrow();
expect(() => MutualTLSConfigSchema.parse({
trustedCAs: [],
})).toThrow();
});
it('should accept mTLS in AuthConfigSchema', () => {
const config = AuthConfigSchema.parse({
mutualTls: {
enabled: true,
clientCertRequired: true,
trustedCAs: ['/certs/ca.pem'],
certificateValidation: 'strict',
},
});
expect(config.mutualTls?.enabled).toBe(true);
expect(config.mutualTls?.clientCertRequired).toBe(true);
});
});
// ==========================================
// Social Provider Configuration Tests
// ==========================================
describe('SocialProviderConfigSchema', () => {
it('should accept a map of providers', () => {
const config = SocialProviderConfigSchema.parse({
google: { clientId: 'gid', clientSecret: 'gsecret' },
github: { clientId: 'ghid', clientSecret: 'ghsecret', scope: ['repo'] },
});
expect(config?.google.clientId).toBe('gid');
expect(config?.github.scope).toEqual(['repo']);
});
it('should default enabled to true', () => {
const config = SocialProviderConfigSchema.parse({
google: { clientId: 'gid', clientSecret: 'gsecret' },
});
expect(config?.google.enabled).toBe(true);
});
it('should allow extra provider-specific properties via catchall', () => {
const config = SocialProviderConfigSchema.parse({
google: { clientId: 'gid', clientSecret: 'gsecret', accessType: 'offline' },
});
expect((config?.google as any).accessType).toBe('offline');
});
it('should accept undefined (omitted)', () => {
const config = SocialProviderConfigSchema.parse(undefined);
expect(config).toBeUndefined();
});
});
// ==========================================
// Email And Password Configuration Tests
// ==========================================
describe('EmailAndPasswordConfigSchema', () => {
it('should accept full config', () => {
const config = EmailAndPasswordConfigSchema.parse({
enabled: true,
disableSignUp: false,
requireEmailVerification: true,
minPasswordLength: 10,
maxPasswordLength: 64,
autoSignIn: false,
revokeSessionsOnPasswordReset: true,
resetPasswordTokenExpiresIn: 7200,
});
expect(config?.enabled).toBe(true);
expect(config?.minPasswordLength).toBe(10);
expect(config?.revokeSessionsOnPasswordReset).toBe(true);
});
it('should default enabled to true', () => {
const config = EmailAndPasswordConfigSchema.parse({});
expect(config?.enabled).toBe(true);
});
it('should accept undefined (omitted)', () => {
const config = EmailAndPasswordConfigSchema.parse(undefined);
expect(config).toBeUndefined();
});
});
// ==========================================
// Email Verification Configuration Tests
// ==========================================
describe('EmailVerificationConfigSchema', () => {
it('should accept full config', () => {
const config = EmailVerificationConfigSchema.parse({
sendOnSignUp: true,
sendOnSignIn: false,
autoSignInAfterVerification: true,
expiresIn: 1800,
});
expect(config?.sendOnSignUp).toBe(true);
expect(config?.expiresIn).toBe(1800);
});
it('should accept empty object', () => {
const config = EmailVerificationConfigSchema.parse({});
expect(config).toBeDefined();
expect(config?.sendOnSignUp).toBeUndefined();
});
it('should accept undefined (omitted)', () => {
const config = EmailVerificationConfigSchema.parse(undefined);
expect(config).toBeUndefined();
});
});
// ==========================================
// Advanced Auth Configuration Tests
// ==========================================
describe('AdvancedAuthConfigSchema', () => {
it('should accept crossSubDomainCookies', () => {
const config = AdvancedAuthConfigSchema.parse({
crossSubDomainCookies: {
enabled: true,
domain: '.objectos.app',
},
});
expect(config?.crossSubDomainCookies?.enabled).toBe(true);
expect(config?.crossSubDomainCookies?.domain).toBe('.objectos.app');
});
it('should accept all advanced options', () => {
const config = AdvancedAuthConfigSchema.parse({
crossSubDomainCookies: {
enabled: true,
additionalCookies: ['my_cookie'],
},
useSecureCookies: true,
disableCSRFCheck: false,
cookiePrefix: 'objectos',
});
expect(config?.useSecureCookies).toBe(true);
expect(config?.cookiePrefix).toBe('objectos');
});
it('should accept undefined (omitted)', () => {
const config = AdvancedAuthConfigSchema.parse(undefined);
expect(config).toBeUndefined();
});
});
// ==========================================
// AuthConfigSchema integration with new fields
// ==========================================
describe('AuthConfigSchema – new passthrough fields', () => {
it('should accept socialProviders in AuthConfig', () => {
const config = AuthConfigSchema.parse({
socialProviders: {
google: { clientId: 'gid', clientSecret: 'gsecret' },
},
});
expect(config.socialProviders?.google.clientId).toBe('gid');
});
it('should accept emailAndPassword in AuthConfig', () => {
const config = AuthConfigSchema.parse({
emailAndPassword: {
enabled: true,
minPasswordLength: 12,
},
});
expect(config.emailAndPassword?.minPasswordLength).toBe(12);
});
it('should accept emailVerification in AuthConfig', () => {
const config = AuthConfigSchema.parse({
emailVerification: { sendOnSignUp: true, expiresIn: 600 },
});
expect(config.emailVerification?.sendOnSignUp).toBe(true);
});
it('should accept advanced in AuthConfig', () => {
const config = AuthConfigSchema.parse({
advanced: {
crossSubDomainCookies: { enabled: true, domain: '.objectos.app' },
useSecureCookies: true,
},
});
expect(config.advanced?.crossSubDomainCookies?.enabled).toBe(true);
expect(config.advanced?.useSecureCookies).toBe(true);
});
it('should accept all new fields together', () => {
const config = AuthConfigSchema.parse({
secret: 'my-secret',
baseUrl: 'https://app.objectos.app',
trustedOrigins: ['https://*.objectos.app'],
socialProviders: {
google: { clientId: 'g', clientSecret: 's' },
github: { clientId: 'g', clientSecret: 's' },
},
emailAndPassword: { enabled: true, requireEmailVerification: true },
emailVerification: { sendOnSignUp: true },
advanced: { crossSubDomainCookies: { enabled: true } },
});
expect(config.socialProviders).toBeDefined();
expect(config.emailAndPassword?.requireEmailVerification).toBe(true);
expect(config.emailVerification?.sendOnSignUp).toBe(true);
expect(config.advanced?.crossSubDomainCookies?.enabled).toBe(true);
});
});