-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathauthcode.test.ts
More file actions
728 lines (586 loc) · 23.8 KB
/
authcode.test.ts
File metadata and controls
728 lines (586 loc) · 23.8 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { Address, Hex, Bytes } from 'ox'
import { Network, Payload } from '@0xsequence/wallet-primitives'
import { IdentityInstrument, IdentityType, KeyType, AuthCodeChallenge } from '@0xsequence/identity-instrument'
import { AuthCodeHandler } from '../src/sequence/handlers/authcode.js'
import { Signatures } from '../src/sequence/signatures.js'
import * as Db from '../src/dbs/index.js'
import { IdentitySigner } from '../src/identity/signer.js'
import { BaseSignatureRequest } from '../src/sequence/types/signature-request.js'
// Mock the global crypto API
const mockCryptoSubtle = {
sign: vi.fn(),
generateKey: vi.fn(),
exportKey: vi.fn(),
}
Object.defineProperty(global, 'window', {
value: {
crypto: {
subtle: mockCryptoSubtle,
},
location: {
pathname: '/test-path',
href: '',
},
},
writable: true,
})
// Mock URLSearchParams
class MockURLSearchParams {
private params: Record<string, string> = {}
constructor(params?: Record<string, string>) {
if (params) {
this.params = { ...params }
}
}
toString() {
return Object.entries(this.params)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&')
}
}
// @ts-ignore - Override global URLSearchParams for testing
global.URLSearchParams = MockURLSearchParams as any
// Mock dependencies with proper vi.fn() types
const mockCommitVerifier = vi.fn()
const mockCompleteAuth = vi.fn()
const mockAddSignature = vi.fn()
const mockAuthCommitmentsSet = vi.fn()
const mockAuthCommitmentsGet = vi.fn()
const mockAuthCommitmentsDel = vi.fn()
const mockGetBySigner = vi.fn()
const mockDelBySigner = vi.fn()
const mockAuthKeysSet = vi.fn()
const mockAddListener = vi.fn()
const mockIdentityInstrument = {
commitVerifier: mockCommitVerifier,
completeAuth: mockCompleteAuth,
} as unknown as IdentityInstrument
const mockSignatures = {
addSignature: mockAddSignature,
} as unknown as Signatures
const mockAuthCommitments = {
set: mockAuthCommitmentsSet,
get: mockAuthCommitmentsGet,
del: mockAuthCommitmentsDel,
} as unknown as Db.AuthCommitments
const mockAuthKeys = {
getBySigner: mockGetBySigner,
delBySigner: mockDelBySigner,
set: mockAuthKeysSet,
addListener: mockAddListener,
} as unknown as Db.AuthKeys
describe('AuthCodeHandler', () => {
let authCodeHandler: AuthCodeHandler
let testWallet: Address.Address
let testCommitment: Db.AuthCommitment
let testRequest: BaseSignatureRequest
beforeEach(() => {
vi.clearAllMocks()
testWallet = '0x1234567890123456789012345678901234567890' as Address.Address
// Create mock CryptoKey
const mockCryptoKey = {
algorithm: { name: 'ECDSA', namedCurve: 'P-256' },
extractable: false,
type: 'private',
usages: ['sign'],
} as CryptoKey
mockCryptoSubtle.generateKey.mockResolvedValue({
publicKey: {} as CryptoKey,
privateKey: mockCryptoKey,
})
mockCryptoSubtle.exportKey.mockResolvedValue(new ArrayBuffer(64))
testCommitment = {
id: 'test-state-123',
kind: 'google-pkce',
metadata: {},
target: '/test-target',
isSignUp: false,
signer: testWallet,
}
testRequest = {
id: 'test-request-id',
envelope: {
wallet: testWallet,
chainId: Network.ChainId.ARBITRUM,
payload: Payload.fromMessage(Hex.fromString('Test message')),
},
} as BaseSignatureRequest
authCodeHandler = new AuthCodeHandler(
'google-pkce',
'https://accounts.google.com',
'https://accounts.google.com/o/oauth2/v2/auth',
'test-audience',
mockIdentityInstrument,
mockSignatures,
mockAuthCommitments,
mockAuthKeys,
)
})
afterEach(() => {
vi.resetAllMocks()
})
// === CONSTRUCTOR AND PROPERTIES ===
describe('Constructor', () => {
it('Should create AuthCodeHandler with Google PKCE configuration', () => {
const handler = new AuthCodeHandler(
'google-pkce',
'https://accounts.google.com',
'https://accounts.google.com/o/oauth2/v2/auth',
'google-client-id',
mockIdentityInstrument,
mockSignatures,
mockAuthCommitments,
mockAuthKeys,
)
expect(handler.signupKind).toBe('google-pkce')
expect(handler.issuer).toBe('https://accounts.google.com')
expect(handler.audience).toBe('google-client-id')
expect(handler.identityType).toBe(IdentityType.OIDC)
})
it('Should create AuthCodeHandler with Apple configuration', () => {
const handler = new AuthCodeHandler(
'apple',
'https://appleid.apple.com',
'https://appleid.apple.com/auth/authorize',
'apple-client-id',
mockIdentityInstrument,
mockSignatures,
mockAuthCommitments,
mockAuthKeys,
)
expect(handler.signupKind).toBe('apple')
expect(handler.issuer).toBe('https://appleid.apple.com')
expect(handler.audience).toBe('apple-client-id')
})
it('Should initialize with empty redirect URI', () => {
expect(authCodeHandler['redirectUri']).toBe('')
})
})
// === KIND GETTER ===
describe('kind getter', () => {
it('Should return login-google-pkce for Google PKCE handler', () => {
const googleHandler = new AuthCodeHandler(
'google-pkce',
'https://accounts.google.com',
'https://accounts.google.com/o/oauth2/v2/auth',
'test-audience',
mockIdentityInstrument,
mockSignatures,
mockAuthCommitments,
mockAuthKeys,
)
expect(googleHandler.kind).toBe('login-google-pkce')
})
it('Should return login-apple for Apple handler', () => {
const appleHandler = new AuthCodeHandler(
'apple',
'https://appleid.apple.com',
'https://appleid.apple.com/auth/authorize',
'test-audience',
mockIdentityInstrument,
mockSignatures,
mockAuthCommitments,
mockAuthKeys,
)
expect(appleHandler.kind).toBe('login-apple')
})
})
// === REDIRECT URI MANAGEMENT ===
describe('setRedirectUri()', () => {
it('Should set redirect URI', () => {
const testUri = 'https://example.com/callback'
authCodeHandler.setRedirectUri(testUri)
expect(authCodeHandler['redirectUri']).toBe(testUri)
})
it('Should update redirect URI when called multiple times', () => {
authCodeHandler.setRedirectUri('https://first.com/callback')
authCodeHandler.setRedirectUri('https://second.com/callback')
expect(authCodeHandler['redirectUri']).toBe('https://second.com/callback')
})
})
// === COMMIT AUTH FLOW ===
describe('commitAuth()', () => {
beforeEach(() => {
authCodeHandler.setRedirectUri('https://example.com/callback')
})
it('Should create auth commitment and return OAuth URL', async () => {
const target = '/test-target'
const isSignUp = true
const signer = testWallet
const result = await authCodeHandler.commitAuth(target, isSignUp, undefined, signer)
// Verify commitment was saved
expect(mockAuthCommitmentsSet).toHaveBeenCalledOnce()
const commitmentCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
expect(commitmentCall.kind).toBe('google-pkce')
expect(commitmentCall.signer).toBe(signer)
expect(commitmentCall.target).toBe(target)
expect(commitmentCall.metadata).toEqual({})
expect(commitmentCall.isSignUp).toBe(isSignUp)
expect(commitmentCall.id).toBeDefined()
expect(typeof commitmentCall.id).toBe('string')
// Verify OAuth URL structure
expect(result).toContain('https://accounts.google.com/o/oauth2/v2/auth?')
expect(result).toContain('client_id=test-audience')
expect(result).toContain('redirect_uri=https%3A%2F%2Fexample.com%2Fcallback') // Fix URL encoding
expect(result).toContain('response_type=code')
expect(result).toContain('scope=openid')
expect(result).toContain(`state=${commitmentCall.id}`)
})
it('Should use provided state parameter', async () => {
const customState = 'custom-state-123'
const result = await authCodeHandler.commitAuth('/target', false, customState)
// Verify commitment uses custom state
const commitmentCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
expect(commitmentCall.id).toBe(customState)
expect(result).toContain(`state=${customState}`)
})
it('Should generate random state when not provided', async () => {
const result = await authCodeHandler.commitAuth('/target', false)
const commitmentCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
expect(commitmentCall.id).toBeDefined()
expect(typeof commitmentCall.id).toBe('string')
expect(commitmentCall.id.startsWith('0x')).toBe(true)
expect(commitmentCall.id.length).toBe(66) // 0x + 64 hex chars
})
it('Should handle Apple OAuth URL', async () => {
const appleHandler = new AuthCodeHandler(
'apple',
'https://appleid.apple.com',
'https://appleid.apple.com/auth/authorize',
'apple-client-id',
mockIdentityInstrument,
mockSignatures,
mockAuthCommitments,
mockAuthKeys,
)
appleHandler.setRedirectUri('https://example.com/callback')
const result = await appleHandler.commitAuth('/target', false)
expect(result).toContain('https://appleid.apple.com/auth/authorize?')
expect(result).toContain('client_id=apple-client-id')
const resultUrl = new URL(result)
expect(resultUrl.searchParams.get('scope')).toBe('name email')
})
it('Should create commitment without signer', async () => {
const result = await authCodeHandler.commitAuth('/target', true)
const commitmentCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
expect(commitmentCall.signer).toBeUndefined()
expect(commitmentCall.isSignUp).toBe(true)
})
})
// === COMPLETE AUTH FLOW ===
describe('completeAuth()', () => {
it('Should complete auth flow with code and return signer', async () => {
const authCode = 'test-auth-code-123'
const mockSigner = {} as IdentitySigner
const mockEmail = 'test@example.com'
mockCommitVerifier.mockResolvedValueOnce(undefined)
mockCompleteAuth.mockResolvedValueOnce({
signer: { address: testWallet },
identity: { email: mockEmail },
})
// Mock getAuthKey to return a key for the commitVerifier and completeAuth calls
mockGetBySigner.mockResolvedValue({
address: '0x742d35cc6635c0532925a3b8d563a6b35b7f05f1',
privateKey: {} as CryptoKey,
identitySigner: '',
expiresAt: new Date(Date.now() + 3600000),
})
const [signer, metadata] = await authCodeHandler.completeAuth(testCommitment, authCode)
// Verify commitVerifier was called
expect(mockCommitVerifier).toHaveBeenCalledOnce()
const commitVerifierCall = mockCommitVerifier.mock.calls[0]!
expect(commitVerifierCall[1]).toBeInstanceOf(AuthCodeChallenge)
// Verify completeAuth was called
expect(mockCompleteAuth).toHaveBeenCalledOnce()
const completeAuthCall = mockCompleteAuth.mock.calls[0]!
expect(completeAuthCall[1]).toBeInstanceOf(AuthCodeChallenge)
// Verify results
expect(signer).toBeInstanceOf(IdentitySigner)
expect(metadata.email).toBe(mockEmail)
})
it('Should complete auth flow with existing signer', async () => {
const authCode = 'test-auth-code-123'
const commitmentWithSigner = { ...testCommitment, signer: testWallet }
mockCommitVerifier.mockResolvedValueOnce(undefined)
mockCompleteAuth.mockResolvedValueOnce({
signer: { address: testWallet },
identity: { email: 'test@example.com' },
})
mockGetBySigner.mockResolvedValue({
address: '0x742d35cc6635c0532925a3b8d563a6b35b7f05f1',
privateKey: {} as CryptoKey,
identitySigner: '',
expiresAt: new Date(Date.now() + 3600000),
})
const [signer, metadata] = await authCodeHandler.completeAuth(commitmentWithSigner, authCode)
expect(signer).toBeDefined()
expect(metadata.email).toBe('test@example.com')
})
it('Should handle commitVerifier failure', async () => {
const authCode = 'test-auth-code-123'
mockGetBySigner.mockResolvedValue(null)
// The actual error comes from trying to access commitment.signer
await expect(authCodeHandler.completeAuth(testCommitment, authCode)).rejects.toThrow(
'Cannot read properties of undefined',
)
})
it('Should handle completeAuth failure', async () => {
const authCode = 'test-auth-code-123'
mockCommitVerifier.mockResolvedValueOnce(undefined)
mockCompleteAuth.mockRejectedValueOnce(new Error('OAuth verification failed'))
mockGetBySigner.mockResolvedValue({
address: '0x742d35cc6635c0532925a3b8d563a6b35b7f05f1',
privateKey: {} as CryptoKey,
identitySigner: '',
expiresAt: new Date(Date.now() + 3600000),
})
await expect(authCodeHandler.completeAuth(testCommitment, authCode)).rejects.toThrow('OAuth verification failed')
})
})
// === STATUS METHOD ===
describe('status()', () => {
it('Should return ready status when auth key signer exists', async () => {
const mockAuthKey = {
address: '0x742d35cc6635c0532925a3b8d563a6b35b7f05f1',
privateKey: {} as CryptoKey,
identitySigner: testWallet,
expiresAt: new Date(Date.now() + 3600000),
}
mockGetBySigner.mockResolvedValueOnce(mockAuthKey)
const result = await authCodeHandler.status(testWallet, undefined, testRequest)
expect(result.status).toBe('ready')
expect(result.address).toBe(testWallet)
expect(result.handler).toBe(authCodeHandler)
expect(typeof (result as any).handle).toBe('function')
})
it('Should execute signing when handle is called on ready status', async () => {
const mockAuthKey = {
address: '0x742d35cc6635c0532925a3b8d563a6b35b7f05f1',
privateKey: {} as CryptoKey,
identitySigner: testWallet,
expiresAt: new Date(Date.now() + 3600000),
}
mockGetBySigner.mockResolvedValueOnce(mockAuthKey)
const result = await authCodeHandler.status(testWallet, undefined, testRequest)
// Mock the signer's sign method
const mockSignature = {
type: 'hash' as const,
r: 0x1234567890abcdefn,
s: 0xfedcba0987654321n,
yParity: 0,
}
// We need to mock the IdentitySigner's sign method
vi.spyOn(IdentitySigner.prototype, 'sign').mockResolvedValueOnce(mockSignature)
const handleResult = await (result as any).handle()
expect(handleResult).toBe(true)
expect(mockAddSignature).toHaveBeenCalledOnce()
expect(mockAddSignature).toHaveBeenCalledWith(testRequest.id, {
address: testWallet,
signature: mockSignature,
})
})
it('Should return actionable status when no auth key signer exists', async () => {
mockGetBySigner.mockResolvedValueOnce(null)
const result = await authCodeHandler.status(testWallet, undefined, testRequest)
expect(result.status).toBe('actionable')
expect(result.address).toBe(testWallet)
expect(result.handler).toBe(authCodeHandler)
expect((result as any).message).toBe('request-redirect')
expect(typeof (result as any).handle).toBe('function')
})
it('Should redirect to OAuth when handle is called on actionable status', async () => {
authCodeHandler.setRedirectUri('https://example.com/callback')
mockGetBySigner.mockResolvedValueOnce(null)
const result = await authCodeHandler.status(testWallet, undefined, testRequest)
const handleResult = await (result as any).handle()
expect(handleResult).toBe(true)
expect(window.location.href).toContain('https://accounts.google.com/o/oauth2/v2/auth')
expect(mockAuthCommitmentsSet).toHaveBeenCalledOnce()
const commitmentCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
expect(commitmentCall.target).toBe(window.location.pathname)
expect(commitmentCall.isSignUp).toBe(false)
expect(commitmentCall.signer).toBe(testWallet)
})
})
// === OAUTH URL PROPERTY ===
describe('oauthUrl', () => {
it('Should return Google OAuth URL for Google issuer', () => {
const googleHandler = new AuthCodeHandler(
'google-pkce',
'https://accounts.google.com',
'https://accounts.google.com/o/oauth2/v2/auth',
'test-audience',
mockIdentityInstrument,
mockSignatures,
mockAuthCommitments,
mockAuthKeys,
)
const url = googleHandler['oauthUrl']
expect(url).toBe('https://accounts.google.com/o/oauth2/v2/auth')
})
it('Should return Apple OAuth URL for Apple issuer', () => {
const appleHandler = new AuthCodeHandler(
'apple',
'https://appleid.apple.com',
'https://appleid.apple.com/auth/authorize',
'test-audience',
mockIdentityInstrument,
mockSignatures,
mockAuthCommitments,
mockAuthKeys,
)
const url = appleHandler['oauthUrl']
expect(url).toBe('https://appleid.apple.com/auth/authorize')
})
})
// === INHERITED METHODS FROM IDENTITYHANDLER ===
describe('Inherited IdentityHandler methods', () => {
it('Should provide onStatusChange listener', () => {
const mockUnsubscribe = vi.fn()
mockAddListener.mockReturnValueOnce(mockUnsubscribe)
const callback = vi.fn()
const unsubscribe = authCodeHandler.onStatusChange(callback)
expect(mockAddListener).toHaveBeenCalledWith(callback)
expect(unsubscribe).toBe(mockUnsubscribe)
})
it('Should handle nitroCommitVerifier with auth key cleanup', async () => {
const mockChallenge = {} as any
const mockAuthKey = {
address: '0x742d35cc6635c0532925a3b8d563a6b35b7f05f1',
privateKey: {} as CryptoKey,
identitySigner: '',
expiresAt: new Date(Date.now() + 3600000),
}
mockGetBySigner.mockResolvedValueOnce(mockAuthKey)
mockCommitVerifier.mockResolvedValueOnce('result')
const result = await authCodeHandler['nitroCommitVerifier'](mockChallenge)
expect(mockDelBySigner).toHaveBeenCalledWith('')
expect(mockCommitVerifier).toHaveBeenCalledWith(
expect.objectContaining({
address: mockAuthKey.address,
keyType: KeyType.WebCrypto_Secp256r1,
signer: mockAuthKey.identitySigner,
}),
mockChallenge,
)
expect(result).toBe('result')
})
it('Should handle nitroCompleteAuth with auth key management', async () => {
const mockChallenge = {} as any
const mockAuthKey = {
address: '0x742d35cc6635c0532925a3b8d563a6b35b7f05f1',
privateKey: {} as CryptoKey,
identitySigner: '',
expiresAt: new Date(Date.now() + 3600000),
}
const mockIdentityResult = {
signer: { address: testWallet },
identity: { email: 'test@example.com' },
}
mockGetBySigner.mockResolvedValueOnce(mockAuthKey)
mockCompleteAuth.mockResolvedValueOnce(mockIdentityResult)
const result = await authCodeHandler['nitroCompleteAuth'](mockChallenge)
expect(mockCompleteAuth).toHaveBeenCalledWith(
expect.objectContaining({
address: mockAuthKey.address,
}),
mockChallenge,
)
// Verify auth key cleanup and updates
expect(mockDelBySigner).toHaveBeenCalledWith('')
expect(mockDelBySigner).toHaveBeenCalledWith(testWallet)
expect(mockAuthKeysSet).toHaveBeenCalledWith(
expect.objectContaining({
identitySigner: testWallet,
}),
)
expect(result.signer).toBeInstanceOf(IdentitySigner)
expect(result.email).toBe('test@example.com')
})
})
// === ERROR HANDLING ===
describe('Error Handling', () => {
it('Should handle missing auth key in commitVerifier', async () => {
const mockChallenge = {} as any
mockGetBySigner.mockResolvedValueOnce(null)
// Make crypto operations fail to prevent auto-generation of auth key
mockCryptoSubtle.generateKey.mockRejectedValueOnce(new Error('Crypto not available'))
await expect(authCodeHandler['nitroCommitVerifier'](mockChallenge)).rejects.toThrow('Crypto not available')
})
it('Should handle missing auth key in completeAuth', async () => {
const mockChallenge = {} as any
mockGetBySigner.mockResolvedValueOnce(null)
// Make crypto operations fail to prevent auto-generation of auth key
mockCryptoSubtle.generateKey.mockRejectedValueOnce(new Error('Crypto not available'))
await expect(authCodeHandler['nitroCompleteAuth'](mockChallenge)).rejects.toThrow('Crypto not available')
})
it('Should handle identity instrument failures in commitVerifier', async () => {
const mockChallenge = {} as any
const mockAuthKey = {
address: '0x742d35cc6635c0532925a3b8d563a6b35b7f05f1',
privateKey: {} as CryptoKey,
identitySigner: '',
expiresAt: new Date(Date.now() + 3600000),
}
mockGetBySigner.mockResolvedValueOnce(mockAuthKey)
mockCommitVerifier.mockRejectedValueOnce(new Error('Identity service error'))
await expect(authCodeHandler['nitroCommitVerifier'](mockChallenge)).rejects.toThrow('Identity service error')
})
it('Should handle auth commitments database errors', async () => {
mockAuthCommitmentsSet.mockRejectedValueOnce(new Error('Database error'))
await expect(authCodeHandler.commitAuth('/target', false)).rejects.toThrow('Database error')
})
it('Should handle auth keys database errors', async () => {
mockGetBySigner.mockRejectedValueOnce(new Error('Database error'))
await expect(authCodeHandler.status(testWallet, undefined, testRequest)).rejects.toThrow('Database error')
})
})
// === INTEGRATION TESTS ===
describe('Integration Tests', () => {
it('Should handle complete OAuth flow from commitment to completion', async () => {
authCodeHandler.setRedirectUri('https://example.com/callback')
// Step 1: Commit auth
const commitUrl = await authCodeHandler.commitAuth('/test-target', false, 'test-state', testWallet)
expect(commitUrl).toContain('state=test-state')
expect(mockAuthCommitmentsSet).toHaveBeenCalledWith(
expect.objectContaining({
id: 'test-state',
kind: 'google-pkce',
target: '/test-target',
isSignUp: false,
signer: testWallet,
}),
)
// Step 2: Complete auth
const mockAuthKey = {
address: '0x742d35cc6635c0532925a3b8d563a6b35b7f05f1',
privateKey: {} as CryptoKey,
identitySigner: '',
expiresAt: new Date(Date.now() + 3600000),
}
mockGetBySigner.mockResolvedValue(mockAuthKey)
mockCommitVerifier.mockResolvedValueOnce(undefined)
mockCompleteAuth.mockResolvedValueOnce({
signer: { address: testWallet },
identity: { email: 'test@example.com' },
})
const [signer, metadata] = await authCodeHandler.completeAuth(testCommitment, 'auth-code-123')
expect(signer).toBeInstanceOf(IdentitySigner)
expect(metadata.email).toBe('test@example.com')
})
it('Should handle signup vs login flows correctly', async () => {
authCodeHandler.setRedirectUri('https://example.com/callback')
// Test signup flow
await authCodeHandler.commitAuth('/signup-target', true, 'signup-state')
const signupCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
expect(signupCall.isSignUp).toBe(true)
expect(signupCall.target).toBe('/signup-target')
// Test login flow
await authCodeHandler.commitAuth('/login-target', false, 'login-state')
const loginCall = mockAuthCommitmentsSet.mock.calls[1]![0]!
expect(loginCall.isSignUp).toBe(false)
expect(loginCall.target).toBe('/login-target')
})
})
})