Skip to content

Commit e552d77

Browse files
committed
wdk: update tests
1 parent fab000a commit e552d77

3 files changed

Lines changed: 59 additions & 44 deletions

File tree

packages/wallet/wdk/test/authcode-pkce.test.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ describe('AuthCodePkceHandler', () => {
9090
describe('commitAuth', () => {
9191
it('Should create Google PKCE auth commitment and return OAuth URL', async () => {
9292
const target = 'https://example.com/success'
93-
const isSignUp = true
9493

95-
const result = await handler.commitAuth(target, isSignUp)
94+
const result = await handler.commitAuth(target, { type: 'auth' })
9695

9796
// Verify nitroCommitVerifier was called with correct challenge
9897
expect(handler['nitroCommitVerifier']).toHaveBeenCalledWith(
@@ -110,7 +109,7 @@ describe('AuthCodePkceHandler', () => {
110109
challenge: 'mock-challenge-hash',
111110
target,
112111
metadata: {},
113-
isSignUp,
112+
type: 'auth',
114113
})
115114

116115
// Verify OAuth URL is constructed correctly
@@ -127,10 +126,13 @@ describe('AuthCodePkceHandler', () => {
127126

128127
it('Should use provided state instead of generating random one', async () => {
129128
const target = 'https://example.com/success'
130-
const isSignUp = false
131129
const customState = 'custom-state-123'
132130

133-
const result = await handler.commitAuth(target, isSignUp, customState)
131+
const result = await handler.commitAuth(target, {
132+
type: 'reauth',
133+
state: customState,
134+
signer: '0x1234567890123456789012345678901234567890',
135+
})
134136

135137
// Verify commitment was saved with custom state
136138
expect(mockCommitments.set).toHaveBeenCalledWith({
@@ -140,7 +142,8 @@ describe('AuthCodePkceHandler', () => {
140142
challenge: 'mock-challenge-hash',
141143
target,
142144
metadata: {},
143-
isSignUp,
145+
type: 'reauth',
146+
signer: '0x1234567890123456789012345678901234567890',
144147
})
145148

146149
// Verify URL contains custom state
@@ -149,10 +152,9 @@ describe('AuthCodePkceHandler', () => {
149152

150153
it('Should include signer in challenge when provided', async () => {
151154
const target = 'https://example.com/success'
152-
const isSignUp = true
153155
const signer = '0x9876543210987654321098765432109876543210'
154156

155-
await handler.commitAuth(target, isSignUp, undefined, signer)
157+
await handler.commitAuth(target, { type: 'reauth', state: 'test-state', signer })
156158

157159
// Verify nitroCommitVerifier was called with signer in challenge
158160
expect(handler['nitroCommitVerifier']).toHaveBeenCalledWith(
@@ -164,9 +166,8 @@ describe('AuthCodePkceHandler', () => {
164166

165167
it('Should generate random state when not provided', async () => {
166168
const target = 'https://example.com/success'
167-
const isSignUp = true
168169

169-
const result = await handler.commitAuth(target, isSignUp)
170+
const result = await handler.commitAuth(target, { type: 'auth' })
170171

171172
// Verify that a state parameter is present and looks like a hex string
172173
expect(result).toMatch(/state=0x[a-f0-9]+/)
@@ -181,32 +182,40 @@ describe('AuthCodePkceHandler', () => {
181182
const target = 'https://example.com/success'
182183

183184
// Test signup
184-
await handler.commitAuth(target, true)
185+
await handler.commitAuth(target, { type: 'auth' })
185186
expect(mockCommitments.set).toHaveBeenLastCalledWith(
186187
expect.objectContaining({
187-
isSignUp: true,
188+
type: 'auth',
188189
}),
189190
)
190191

191192
// Test login
192-
await handler.commitAuth(target, false)
193+
await handler.commitAuth(target, {
194+
type: 'reauth',
195+
state: 'test-state',
196+
signer: '0x1234567890123456789012345678901234567890',
197+
})
193198
expect(mockCommitments.set).toHaveBeenLastCalledWith(
194199
expect.objectContaining({
195-
isSignUp: false,
200+
type: 'reauth',
196201
}),
197202
)
198203
})
199204

200205
it('Should handle errors from nitroCommitVerifier', async () => {
201206
vi.spyOn(handler as any, 'nitroCommitVerifier').mockRejectedValue(new Error('Nitro service unavailable'))
202207

203-
await expect(handler.commitAuth('https://example.com/success', true)).rejects.toThrow('Nitro service unavailable')
208+
await expect(handler.commitAuth('https://example.com/success', { type: 'auth' })).rejects.toThrow(
209+
'Nitro service unavailable',
210+
)
204211
})
205212

206213
it('Should handle database errors during commitment storage', async () => {
207214
vi.mocked(mockCommitments.set).mockRejectedValue(new Error('Database write failed'))
208215

209-
await expect(handler.commitAuth('https://example.com/success', true)).rejects.toThrow('Database write failed')
216+
await expect(handler.commitAuth('https://example.com/success', { type: 'auth' })).rejects.toThrow(
217+
'Database write failed',
218+
)
210219
})
211220
})
212221

@@ -221,7 +230,7 @@ describe('AuthCodePkceHandler', () => {
221230
challenge: 'test-challenge-hash',
222231
target: 'https://example.com/success',
223232
metadata: { scope: 'openid profile email' },
224-
isSignUp: true,
233+
type: 'auth',
225234
}
226235
})
227236

@@ -333,7 +342,7 @@ describe('AuthCodePkceHandler', () => {
333342
const newRedirectUri = 'https://newdomain.com/callback'
334343
handler.setRedirectUri(newRedirectUri)
335344

336-
return handler.commitAuth('https://example.com/success', true).then((result) => {
345+
return handler.commitAuth('https://example.com/success', { type: 'auth' }).then((result) => {
337346
expect(result).toContain(`redirect_uri=${encodeURIComponent(newRedirectUri)}`)
338347
})
339348
})

packages/wallet/wdk/test/authcode.test.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('AuthCodeHandler', () => {
113113
kind: 'google-pkce',
114114
metadata: {},
115115
target: '/test-target',
116-
isSignUp: false,
116+
type: 'reauth',
117117
signer: testWallet,
118118
}
119119

@@ -247,20 +247,17 @@ describe('AuthCodeHandler', () => {
247247

248248
it('Should create auth commitment and return OAuth URL', async () => {
249249
const target = '/test-target'
250-
const isSignUp = true
251-
const signer = testWallet
252250

253-
const result = await authCodeHandler.commitAuth(target, isSignUp, undefined, signer)
251+
const result = await authCodeHandler.commitAuth(target, { type: 'auth' })
254252

255253
// Verify commitment was saved
256254
expect(mockAuthCommitmentsSet).toHaveBeenCalledOnce()
257255
const commitmentCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
258256

259257
expect(commitmentCall.kind).toBe('google-pkce')
260-
expect(commitmentCall.signer).toBe(signer)
261258
expect(commitmentCall.target).toBe(target)
262259
expect(commitmentCall.metadata).toEqual({})
263-
expect(commitmentCall.isSignUp).toBe(isSignUp)
260+
expect(commitmentCall.type).toBe('auth')
264261
expect(commitmentCall.id).toBeDefined()
265262
expect(typeof commitmentCall.id).toBe('string')
266263

@@ -276,7 +273,11 @@ describe('AuthCodeHandler', () => {
276273
it('Should use provided state parameter', async () => {
277274
const customState = 'custom-state-123'
278275

279-
const result = await authCodeHandler.commitAuth('/target', false, customState)
276+
const result = await authCodeHandler.commitAuth('/target', {
277+
type: 'reauth',
278+
state: customState,
279+
signer: testWallet,
280+
})
280281

281282
// Verify commitment uses custom state
282283
const commitmentCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
@@ -285,7 +286,7 @@ describe('AuthCodeHandler', () => {
285286
})
286287

287288
it('Should generate random state when not provided', async () => {
288-
await authCodeHandler.commitAuth('/target', false)
289+
await authCodeHandler.commitAuth('/target', { type: 'auth' })
289290
const commitmentCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
290291
expect(commitmentCall.id).toBeDefined()
291292
expect(typeof commitmentCall.id).toBe('string')
@@ -306,7 +307,7 @@ describe('AuthCodeHandler', () => {
306307
)
307308
appleHandler.setRedirectUri('https://example.com/callback')
308309

309-
const result = await appleHandler.commitAuth('/target', false)
310+
const result = await appleHandler.commitAuth('/target', { type: 'auth' })
310311

311312
expect(result).toContain('https://appleid.apple.com/auth/authorize?')
312313
expect(result).toContain('client_id=apple-client-id')
@@ -315,10 +316,10 @@ describe('AuthCodeHandler', () => {
315316
})
316317

317318
it('Should create commitment without signer', async () => {
318-
await authCodeHandler.commitAuth('/target', true)
319+
await authCodeHandler.commitAuth('/target', { type: 'auth' })
319320
const commitmentCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
320321
expect(commitmentCall.signer).toBeUndefined()
321-
expect(commitmentCall.isSignUp).toBe(true)
322+
expect(commitmentCall.type).toBe('auth')
322323
})
323324
})
324325

@@ -492,7 +493,7 @@ describe('AuthCodeHandler', () => {
492493

493494
const commitmentCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
494495
expect(commitmentCall.target).toBe(window.location.pathname)
495-
expect(commitmentCall.isSignUp).toBe(false)
496+
expect(commitmentCall.type).toBe('reauth')
496497
expect(commitmentCall.signer).toBe(testWallet)
497498
})
498499
})
@@ -654,7 +655,7 @@ describe('AuthCodeHandler', () => {
654655
it('Should handle auth commitments database errors', async () => {
655656
mockAuthCommitmentsSet.mockRejectedValueOnce(new Error('Database error'))
656657

657-
await expect(authCodeHandler.commitAuth('/target', false)).rejects.toThrow('Database error')
658+
await expect(authCodeHandler.commitAuth('/target', { type: 'auth' })).rejects.toThrow('Database error')
658659
})
659660

660661
it('Should handle auth keys database errors', async () => {
@@ -671,15 +672,19 @@ describe('AuthCodeHandler', () => {
671672
authCodeHandler.setRedirectUri('https://example.com/callback')
672673

673674
// Step 1: Commit auth
674-
const commitUrl = await authCodeHandler.commitAuth('/test-target', false, 'test-state', testWallet)
675+
const commitUrl = await authCodeHandler.commitAuth('/test-target', {
676+
type: 'reauth',
677+
state: 'test-state',
678+
signer: testWallet,
679+
})
675680

676681
expect(commitUrl).toContain('state=test-state')
677682
expect(mockAuthCommitmentsSet).toHaveBeenCalledWith(
678683
expect.objectContaining({
679684
id: 'test-state',
680685
kind: 'google-pkce',
681686
target: '/test-target',
682-
isSignUp: false,
687+
type: 'reauth',
683688
signer: testWallet,
684689
}),
685690
)
@@ -709,17 +714,17 @@ describe('AuthCodeHandler', () => {
709714
authCodeHandler.setRedirectUri('https://example.com/callback')
710715

711716
// Test signup flow
712-
await authCodeHandler.commitAuth('/signup-target', true, 'signup-state')
717+
await authCodeHandler.commitAuth('/signup-target', { type: 'auth', state: 'signup-state' })
713718

714719
const signupCall = mockAuthCommitmentsSet.mock.calls[0]![0]!
715-
expect(signupCall.isSignUp).toBe(true)
720+
expect(signupCall.type).toBe('auth')
716721
expect(signupCall.target).toBe('/signup-target')
717722

718723
// Test login flow
719-
await authCodeHandler.commitAuth('/login-target', false, 'login-state')
724+
await authCodeHandler.commitAuth('/login-target', { type: 'reauth', state: 'login-state', signer: testWallet })
720725

721726
const loginCall = mockAuthCommitmentsSet.mock.calls[1]![0]!
722-
expect(loginCall.isSignUp).toBe(false)
727+
expect(loginCall.type).toBe('reauth')
723728
expect(loginCall.target).toBe('/login-target')
724729
})
725730
})

packages/wallet/wdk/test/identity-auth-dbs.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('Identity Authentication Databases', () => {
3434
verifier: 'test-verifier-code',
3535
challenge: 'test-challenge-hash',
3636
target: 'test-target-url',
37-
isSignUp: true,
37+
type: 'reauth',
3838
signer: '0x1234567890123456789012345678901234567890',
3939
}
4040

@@ -66,15 +66,15 @@ describe('Identity Authentication Databases', () => {
6666
response_mode: 'form_post',
6767
},
6868
target: 'apple-redirect-url',
69-
isSignUp: false,
69+
type: 'auth',
7070
}
7171

7272
await authCommitmentsDb.set(appleCommitment)
7373
const retrieved = await authCommitmentsDb.get(appleCommitment.id)
7474

7575
expect(retrieved).toBeDefined()
7676
expect(retrieved!.kind).toBe('apple')
77-
expect(retrieved!.isSignUp).toBe(false)
77+
expect(retrieved!.type).toBe('auth')
7878
expect(retrieved!.metadata.response_type).toBe('code id_token')
7979
})
8080

@@ -85,21 +85,22 @@ describe('Identity Authentication Databases', () => {
8585
kind: 'google-pkce',
8686
metadata: {},
8787
target: 'target-1',
88-
isSignUp: true,
88+
type: 'auth',
8989
},
9090
{
9191
id: 'commit-2',
9292
kind: 'apple',
9393
metadata: {},
9494
target: 'target-2',
95-
isSignUp: false,
95+
type: 'reauth',
96+
signer: '0x1234567890123456789012345678901234567890',
9697
},
9798
{
9899
id: 'commit-3',
99100
kind: 'google-pkce',
100101
metadata: {},
101102
target: 'target-3',
102-
isSignUp: true,
103+
type: 'auth',
103104
},
104105
]
105106

@@ -129,7 +130,7 @@ describe('Identity Authentication Databases', () => {
129130
kind: 'google-pkce',
130131
metadata: {},
131132
target: 'init-target',
132-
isSignUp: true,
133+
type: 'auth',
133134
}
134135

135136
await freshDb.set(testCommitment)

0 commit comments

Comments
 (0)