-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathuse-auth-modal.test.js
More file actions
1204 lines (1042 loc) · 44.1 KB
/
use-auth-modal.test.js
File metadata and controls
1204 lines (1042 loc) · 44.1 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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2022, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import PropTypes from 'prop-types'
import {screen, within, waitFor} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import {
renderWithProviders,
createPathWithDefaults,
guestToken,
registerUserToken,
clearAllCookies
} from '@salesforce/retail-react-app/app/utils/test-utils'
import {
AuthModal,
useAuthModal,
EMAIL_VIEW
} from '@salesforce/retail-react-app/app/hooks/use-auth-modal'
import {BrowserRouter as Router, Route} from 'react-router-dom'
import Account from '@salesforce/retail-react-app/app/pages/account'
import {rest} from 'msw'
import {mockedRegisteredCustomer} from '@salesforce/retail-react-app/app/mocks/mock-data'
import * as ReactHookForm from 'react-hook-form'
import mockConfig from '@salesforce/retail-react-app/config/mocks/default'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
jest.mock('@salesforce/pwa-kit-runtime/utils/ssr-config', () => ({
getConfig: jest.fn()
}))
jest.setTimeout(60000)
const mockMergedBasket = {
basketId: 'a10ff320829cb0eef93ca5310a',
currency: 'USD',
customerInfo: {
customerId: 'registeredCustomerId',
email: 'customer@test.com'
}
}
const mockPasswordToken = {
email: 'foo@test.com',
expiresInMinutes: 10,
login: 'foo@test.com',
resetToken: 'testresettoken'
}
const mockRegisteredCustomer = {
authType: 'registered',
customerId: 'registeredCustomerId',
customerNo: 'testno',
email: 'customer@test.com',
firstName: 'Tester',
lastName: 'Testing',
login: 'customer@test.com'
}
let authModal = undefined
const MockedComponent = (props) => {
const {initialView, isPasswordlessEnabled = false} = props
authModal = useAuthModal(initialView || undefined)
const match = {
params: {pageName: 'profile'}
}
return (
<Router>
<button onClick={authModal.onOpen}>Open Modal</button>
<AuthModal {...authModal} isPasswordlessEnabled={isPasswordlessEnabled} />
<Route path={createPathWithDefaults('/account')}>
<Account match={match} />
</Route>
</Router>
)
}
MockedComponent.propTypes = {
initialView: PropTypes.string,
isPasswordlessEnabled: PropTypes.bool
}
// Set up and clean up
beforeEach(() => {
authModal = undefined
// Set default config mock (passkey enabled by default in mockConfig)
getConfig.mockReturnValue(mockConfig)
global.server.use(
rest.post('*/customers', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockRegisteredCustomer))
}),
rest.get('*/customers/:customerId', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockRegisteredCustomer))
}),
rest.post('*/customers/password/actions/create-reset-token', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockPasswordToken))
}),
rest.post('*/oauth2/token', (req, res, ctx) =>
res(
ctx.delay(0),
ctx.json({
customer_id: 'customerid',
access_token: guestToken,
refresh_token: 'testrefeshtoken',
usid: 'testusid',
enc_user_id: 'testEncUserId',
id_token: 'testIdToken'
})
)
),
rest.post('*/baskets/actions/merge', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockMergedBasket))
})
)
})
afterEach(() => {
localStorage.clear()
clearAllCookies()
jest.resetModules()
jest.restoreAllMocks()
})
test('Renders login modal by default', async () => {
const user = userEvent.setup()
renderWithProviders(<MockedComponent />)
// open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
await waitFor(() => {
expect(screen.getByText(/welcome back/i)).toBeInTheDocument()
expect(screen.getByLabelText(/email/i)).toBeInTheDocument()
expect(screen.getByLabelText(/Password/)).toBeInTheDocument()
expect(screen.getByText(/forgot password/i)).toBeInTheDocument()
expect(screen.getByText(/sign in/i)).toBeInTheDocument()
})
})
// TODO: Skipping this test because our jest version seems to too old and is run into issues with react-hooks-form
// when trying to run jest.spyOn on useForm hook. Need to bump version for jest.
test.skip('Renders check email modal on email mode', async () => {
// Store the original useForm function
const originalUseForm = ReactHookForm.useForm
// Spy on useForm
const mockUseForm = jest.spyOn(ReactHookForm, 'useForm').mockImplementation((...args) => {
// Call the original useForm
const methods = originalUseForm(...args)
// Override only formState
return {
...methods,
formState: {
...methods.formState,
isSubmitSuccessful: true // Set to true to render the Check Your Email modal
}
}
})
const user = userEvent.setup()
renderWithProviders(<MockedComponent initialView="email" />)
// open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
await waitFor(() => {
expect(screen.getByText(/check your email/i)).toBeInTheDocument()
})
mockUseForm.mockRestore()
})
test('allows regular login via Enter key in password mode', async () => {
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
const validEmail = 'test@salesforce.com'
const validPassword = 'Password123!'
// open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
await waitFor(() => {
expect(screen.getByText(/Continue/i)).toBeInTheDocument()
})
// enter email and switch to password mode
await user.type(screen.getByLabelText('Email'), validEmail)
await user.click(screen.getByText(/password/i))
// enter password
await user.type(screen.getByLabelText('Password'), validPassword)
// simulate Enter key press in password field
await user.keyboard('{Enter}')
// login successfully and close the modal
await waitFor(() => {
expect(screen.queryByText(/Welcome back/i)).not.toBeInTheDocument()
})
})
describe('Passwordless enabled', () => {
beforeEach(() => {
global.server.use(
rest.post('*/oauth2/passwordless/login', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json({}))
}),
rest.post('*/oauth2/passwordless/token', (req, res, ctx) => {
return res(
ctx.delay(0),
ctx.status(200),
ctx.json({
customer_id: 'registeredCustomerId',
access_token: registerUserToken,
refresh_token: 'testrefeshtoken',
usid: 'testusid',
enc_user_id: 'testEncUserId',
id_token: 'testIdToken'
})
)
})
)
})
test('Renders passwordless login when enabled', async () => {
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
// open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
await waitFor(() => {
expect(screen.getByText(/Continue/i)).toBeInTheDocument()
})
})
test('Allows passwordless login', async () => {
// Disable passkey to test passwordless in isolation
getConfig.mockReturnValue({
...mockConfig,
app: {
...mockConfig.app,
login: {
...mockConfig.app.login,
passkey: {enabled: false}
}
}
})
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />, {
wrapperProps: {
bypassAuth: false
}
})
const validEmail = 'test@salesforce.com'
// open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
await waitFor(() => {
expect(screen.getByText(/Continue/i)).toBeInTheDocument()
})
// enter a valid email address
await user.type(screen.getByLabelText('Email'), validEmail)
// initiate passwordless login
const passwordlessLoginButton = screen.getByText(/Continue/i)
await user.click(passwordlessLoginButton)
// check that the auth modal is closed
expect(authModal.isOpen).toBe(false)
// check that OTP auth modal is open
await waitFor(() => {
expect(
screen.getByText(/To log in to your account, enter the code/i)
).toBeInTheDocument()
})
// resend the email
await user.click(screen.getByText(/Resend Code/i))
// enter the code manually
const code = '12345678'
const otpInputs = screen.getAllByRole('textbox')
for (let i = 0; i < 8; i++) {
await user.type(otpInputs[i], code[i])
}
await waitFor(() => {
expect(
screen.queryByText(/To log in to your account, enter the code/i)
).not.toBeInTheDocument()
})
await waitFor(() => {
expect(screen.getByText(/You're now signed in./i)).toBeInTheDocument()
})
})
test('allows passwordless login via Enter key', async () => {
jest.spyOn(window, 'location', 'get').mockReturnValue({
pathname: '/',
origin: 'https://example.com'
})
// Disable passkey to test passwordless in isolation
getConfig.mockReturnValue({
...mockConfig,
app: {
...mockConfig.app,
login: {
...mockConfig.app.login,
passkey: {enabled: false}
}
}
})
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
const validEmail = 'test@salesforce.com'
// open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
await waitFor(() => {
expect(screen.getByText(/Continue/i)).toBeInTheDocument()
})
// enter a valid email address
await user.type(screen.getByLabelText('Email'), validEmail)
// simulate Enter key press in email field
await user.keyboard('{Enter}')
// check that the auth modal is closed
expect(authModal.isOpen).toBe(false)
// check that the OtpAuthModal is open
await waitFor(() => {
expect(
screen.getByText(/To log in to your account, enter the code/i)
).toBeInTheDocument()
})
})
test('sends callbackURI when passwordless callback is configured', async () => {
getConfig.mockReturnValue({
...mockConfig,
app: {
...mockConfig.app,
login: {
passwordless: {
mode: 'callback',
callbackURI: 'https://callback.com/passwordless'
}
}
}
})
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
const validEmail = 'test@salesforce.com'
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
await waitFor(() => {
expect(screen.getByText(/Continue/i)).toBeInTheDocument()
})
await user.type(screen.getByLabelText('Email'), validEmail)
await user.click(screen.getByText(/Continue/i))
// check that the auth modal is closed
expect(authModal.isOpen).toBe(false)
// check that the OtpAuthModal is open
await waitFor(() => {
expect(
screen.getByText(/To log in to your account, enter the code/i)
).toBeInTheDocument()
})
})
test('shows check your email view when initial view is set to email', async () => {
const {user} = renderWithProviders(
<MockedComponent isPasswordlessEnabled={true} initialView={EMAIL_VIEW} />
)
// open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
await waitFor(() => {
expect(screen.getByText(/Check Your Email/i)).toBeInTheDocument()
})
await user.click(screen.getByText(/Resend Link/i))
// check that the Check Your Email view is still open
await waitFor(() => {
expect(screen.getByText(/Check Your Email/i)).toBeInTheDocument()
})
})
test.each([
['no callback_uri is registered for client', 'This feature is not currently available.'],
[
'Too many login requests were made. Please try again later.',
'You reached the limit for login attempts. For your security, wait 10 minutes and try again.'
],
['unexpected error message', 'Something went wrong. Try again!']
])(
'displays correct error message when passwordless login fails with "%s"',
async (apiErrorMessage, expectedMessage) => {
global.server.use(
rest.post('*/oauth2/passwordless/login', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(400), ctx.json({message: apiErrorMessage}))
})
)
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />)
const validEmail = 'test@salesforce.com'
// open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
await waitFor(() => {
expect(screen.getByText(/Continue/i)).toBeInTheDocument()
})
// enter email and submit
await user.type(screen.getByLabelText('Email'), validEmail)
await user.click(screen.getByText(/Continue/i))
// Verify error message is displayed
await waitFor(() => {
expect(screen.getByText(expectedMessage)).toBeInTheDocument()
})
}
)
})
// TODO: Fix flaky/broken test
// eslint-disable-next-line jest/no-disabled-tests
test.skip('Renders error when given incorrect log in credentials', async () => {
const user = userEvent.setup()
// render our test component
renderWithProviders(<MockedComponent />, {
wrapperProps: {
bypassAuth: false
}
})
// open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
// enter credentials and submit
await user.type(screen.getByLabelText('Email'), 'bad@test.com')
await user.type(screen.getByLabelText('Password'), 'SomeFakePassword1!')
// mock failed auth request
global.server.use(
rest.post('*/oauth2/login', (req, res, ctx) =>
res(ctx.delay(0), ctx.status(401), ctx.json({message: 'Unauthorized Credentials.'}))
),
rest.post('*/customers', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(404), ctx.json({message: 'Not Found.'}))
})
)
await user.click(screen.getByText(/sign in/i))
// give it some time to show the error in the form
await waitFor(
() => {
// wait for login error alert to appear
expect(
screen.getByText(/something's not right with your email or password\. try again\./i)
).toBeInTheDocument()
},
{
timeout: 10000
}
)
})
test('Allows customer to create an account', async () => {
// render our test component
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {
bypassAuth: true
}
})
// open the modal
const trigger = screen.getByText('Open Modal')
await user.click(trigger)
let form
await waitFor(() => {
form = screen.queryByTestId('sf-auth-modal-form')
expect(form).toBeInTheDocument()
})
const createAccount = screen.getByText(/create account/i)
await user.click(createAccount)
let registerForm
await waitFor(() => {
registerForm = screen.getByTestId('sf-auth-modal-form-register')
expect(registerForm).toBeInTheDocument()
})
const withinForm = within(registerForm)
// fill out form and submit
await waitFor(() => {
const firstName = withinForm.getByLabelText(/First Name/i)
expect(firstName).toBeInTheDocument()
})
await user.type(withinForm.getByLabelText('First Name'), 'Tester')
await user.type(withinForm.getByLabelText('Last Name'), 'Tester')
await user.type(withinForm.getByPlaceholderText(/you@email.com/i), 'customer@test.com')
await user.type(withinForm.getAllByLabelText(/password/i)[0], 'Password!1')
// login with credentials
global.server.use(
rest.post('*/oauth2/token', (req, res, ctx) => {
return res(
ctx.delay(0),
ctx.json({
customer_id: 'customerid_1',
access_token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXQiOiJHVUlEIiwic2NwIjoic2ZjYy5zaG9wcGVyLW15YWNjb3VudC5iYXNrZXRzIHNmY2Muc2hvcHBlci1teWFjY291bnQuYWRkcmVzc2VzIHNmY2Muc2hvcHBlci1wcm9kdWN0cyBzZmNjLnNob3BwZXItZGlzY292ZXJ5LXNlYXJjaCBzZmNjLnNob3BwZXItbXlhY2NvdW50LnJ3IHNmY2Muc2hvcHBlci1teWFjY291bnQucGF5bWVudGluc3RydW1lbnRzIHNmY2Muc2hvcHBlci1jdXN0b21lcnMubG9naW4gc2ZjYy5zaG9wcGVyLWV4cGVyaWVuY2Ugc2ZjYy5zaG9wcGVyLW15YWNjb3VudC5vcmRlcnMgc2ZjYy5zaG9wcGVyLWN1c3RvbWVycy5yZWdpc3RlciBzZmNjLnNob3BwZXItYmFza2V0cy1vcmRlcnMgc2ZjYy5zaG9wcGVyLW15YWNjb3VudC5hZGRyZXNzZXMucncgc2ZjYy5zaG9wcGVyLW15YWNjb3VudC5wcm9kdWN0bGlzdHMucncgc2ZjYy5zaG9wcGVyLXByb2R1Y3RsaXN0cyBzZmNjLnNob3BwZXItcHJvbW90aW9ucyBzZmNjLnNob3BwZXItYmFza2V0cy1vcmRlcnMucncgc2ZjYy5zaG9wcGVyLW15YWNjb3VudC5wYXltZW50aW5zdHJ1bWVudHMucncgc2ZjYy5zaG9wcGVyLWdpZnQtY2VydGlmaWNhdGVzIHNmY2Muc2hvcHBlci1wcm9kdWN0LXNlYXJjaCBzZmNjLnNob3BwZXItbXlhY2NvdW50LnByb2R1Y3RsaXN0cyBzZmNjLnNob3BwZXItY2F0ZWdvcmllcyBzZmNjLnNob3BwZXItbXlhY2NvdW50Iiwic3ViIjoiY2Mtc2xhczo6enpyZl8wMDE6OnNjaWQ6YzljNDViZmQtMGVkMy00YWEyLTk5NzEtNDBmODg5NjJiODM2Ojp1c2lkOjhlODgzOTczLTY4ZWItNDFmZS1hM2M1LTc1NjIzMjY1MmZmNSIsImN0eCI6InNsYXMiLCJpc3MiOiJzbGFzL3Byb2QvenpyZl8wMDEiLCJpc3QiOjEsImF1ZCI6ImNvbW1lcmNlY2xvdWQvcHJvZC96enJmXzAwMSIsIm5iZiI6MTY3ODgzNDI3MSwic3R5IjoiVXNlciIsImlzYiI6InVpZG86ZWNvbTo6dXBuOmtldjVAdGVzdC5jb206OnVpZG46a2V2aW4gaGU6OmdjaWQ6YWJtZXMybWJrM2xYa1JsSEZKd0dZWWt1eEo6OnJjaWQ6YWJVTXNhdnBEOVk2alcwMGRpMlNqeEdDTVU6OmNoaWQ6UmVmQXJjaEdsb2JhbCIsImV4cCI6MjY3ODgzNjEwMSwiaWF0IjoxNjc4ODM0MzAxLCJqdGkiOiJDMkM0ODU2MjAxODYwLTE4OTA2Nzg5MDM0ODA1ODMyNTcwNjY2NTQyIn0._tUrxeXdFYPj6ZoY-GILFRd3-aD1RGPkZX6TqHeS494',
refresh_token: 'testrefeshtoken_1',
usid: 'testusid_1',
enc_user_id: 'testEncUserId_1',
id_token: 'testIdToken_1'
})
)
}),
rest.post('*/oauth2/login', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockedRegisteredCustomer))
}),
rest.get('*/customers/:customerId', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockedRegisteredCustomer))
})
)
const submitButton = withinForm.getByText(/create account/i)
await user.click(submitButton)
await waitFor(() => {
expect(form).not.toBeInTheDocument()
})
// wait for success state to appear
await waitFor(
() => {
expect(window.location.pathname).toBe('/uk/en-GB/account')
const myAccount = screen.getAllByText(/My Account/)
expect(myAccount).toHaveLength(3) // h1 (sr-only), h2 (accordion), h2 (sidebar)
},
{
timeout: 5000
}
)
})
// TODO: investigate why this test is failing when running with other tests
// eslint-disable-next-line jest/no-disabled-tests
test.skip('Allows customer to sign in to their account', async () => {
// render our test component
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {
bypassAuth: false
}
})
// open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
// enter credentials and submit
await user.type(screen.getByLabelText('Email'), 'customer@test.com')
await user.type(screen.getByLabelText('Password'), 'Password!1')
// login with credentials
global.server.use(
rest.post('*/oauth2/token', (req, res, ctx) =>
res(
ctx.delay(0),
ctx.json({
customer_id: 'customerid_1',
access_token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXQiOiJHVUlEIiwic2NwIjoic2ZjYy5zaG9wcGVyLW15YWNjb3VudC5iYXNrZXRzIHNmY2Muc2hvcHBlci1teWFjY291bnQuYWRkcmVzc2VzIHNmY2Muc2hvcHBlci1wcm9kdWN0cyBzZmNjLnNob3BwZXItZGlzY292ZXJ5LXNlYXJjaCBzZmNjLnNob3BwZXItbXlhY2NvdW50LnJ3IHNmY2Muc2hvcHBlci1teWFjY291bnQucGF5bWVudGluc3RydW1lbnRzIHNmY2Muc2hvcHBlci1jdXN0b21lcnMubG9naW4gc2ZjYy5zaG9wcGVyLWV4cGVyaWVuY2Ugc2ZjYy5zaG9wcGVyLW15YWNjb3VudC5vcmRlcnMgc2ZjYy5zaG9wcGVyLWN1c3RvbWVycy5yZWdpc3RlciBzZmNjLnNob3BwZXItYmFza2V0cy1vcmRlcnMgc2ZjYy5zaG9wcGVyLW15YWNjb3VudC5hZGRyZXNzZXMucncgc2ZjYy5zaG9wcGVyLW15YWNjb3VudC5wcm9kdWN0bGlzdHMucncgc2ZjYy5zaG9wcGVyLXByb2R1Y3RsaXN0cyBzZmNjLnNob3BwZXItcHJvbW90aW9ucyBzZmNjLnNob3BwZXItYmFza2V0cy1vcmRlcnMucncgc2ZjYy5zaG9wcGVyLW15YWNjb3VudC5wYXltZW50aW5zdHJ1bWVudHMucncgc2ZjYy5zaG9wcGVyLWdpZnQtY2VydGlmaWNhdGVzIHNmY2Muc2hvcHBlci1wcm9kdWN0LXNlYXJjaCBzZmNjLnNob3BwZXItbXlhY2NvdW50LnByb2R1Y3RsaXN0cyBzZmNjLnNob3BwZXItY2F0ZWdvcmllcyBzZmNjLnNob3BwZXItbXlhY2NvdW50Iiwic3ViIjoiY2Mtc2xhczo6enpyZl8wMDE6OnNjaWQ6YzljNDViZmQtMGVkMy00YWEyLTk5NzEtNDBmODg5NjJiODM2Ojp1c2lkOjhlODgzOTczLTY4ZWItNDFmZS1hM2M1LTc1NjIzMjY1MmZmNSIsImN0eCI6InNsYXMiLCJpc3MiOiJzbGFzL3Byb2QvenpyZl8wMDEiLCJpc3QiOjEsImF1ZCI6ImNvbW1lcmNlY2xvdWQvcHJvZC96enJmXzAwMSIsIm5iZiI6MTY3ODgzNDI3MSwic3R5IjoiVXNlciIsImlzYiI6InVpZG86ZWNvbTo6dXBuOmtldjVAdGVzdC5jb206OnVpZG46a2V2aW4gaGU6OmdjaWQ6YWJtZXMybWJrM2xYa1JsSEZKd0dZWWt1eEo6OnJjaWQ6YWJVTXNhdnBEOVk2alcwMGRpMlNqeEdDTVU6OmNoaWQ6UmVmQXJjaEdsb2JhbCIsImV4cCI6MjY3ODgzNjEwMSwiaWF0IjoxNjc4ODM0MzAxLCJqdGkiOiJDMkM0ODU2MjAxODYwLTE4OTA2Nzg5MDM0ODA1ODMyNTcwNjY2NTQyIn0._tUrxeXdFYPj6ZoY-GILFRd3-aD1RGPkZX6TqHeS494',
refresh_token: 'testrefeshtoken_1',
usid: 'testusid_1',
enc_user_id: 'testEncUserId_1',
id_token: 'testIdToken_1'
})
)
)
)
await user.click(screen.getByText(/sign in/i))
// allow time to transition to account page
await waitFor(
() => {
expect(window.location.pathname).toBe('/uk/en-GB/account')
expect(screen.getByText(/My Profile/i)).toBeInTheDocument()
},
{timeout: 5000}
)
})
describe('Passkey login', () => {
let mockCredentialsGet
let mockPublicKeyCredential
beforeEach(() => {
// Clear all mocks
jest.clearAllMocks()
// Override getConfig to return config with passkey enabled
getConfig.mockReturnValue({
...mockConfig,
app: {
...mockConfig.app,
login: {
...mockConfig.app.login,
passkey: {enabled: true}
}
}
})
// Mock WebAuthn API - default to never resolving (simulating no user action)
mockCredentialsGet = jest.fn().mockImplementation(() => new Promise(() => {}))
mockPublicKeyCredential = {
parseRequestOptionsFromJSON: jest.fn(),
isConditionalMediationAvailable: jest.fn().mockResolvedValue(true),
isUserVerifyingPlatformAuthenticatorAvailable: jest.fn().mockResolvedValue(true)
}
global.PublicKeyCredential = mockPublicKeyCredential
global.window.PublicKeyCredential = mockPublicKeyCredential
global.navigator.credentials = {
get: mockCredentialsGet
}
// Mock parseRequestOptionsFromJSON to return mock options
mockPublicKeyCredential.parseRequestOptionsFromJSON.mockReturnValue({
challenge: 'mock-challenge',
allowCredentials: []
})
// Setup MSW handlers for WebAuthn API endpoints
global.server.use(
rest.post('*/oauth2/webauthn/authenticate/start', (req, res, ctx) => {
return res(
ctx.delay(0),
ctx.json({
publicKey: {
challenge: 'mock-challenge-data',
rpId: 'example.com',
allowCredentials: [],
timeout: 60000
}
})
)
}),
rest.post('*/oauth2/webauthn/authenticate/finish', (req, res, ctx) => {
return res(
ctx.delay(0),
ctx.json({
tokenResponse: {
customer_id: 'customerid_passkey',
access_token: registerUserToken,
refresh_token: 'testrefeshtoken_passkey',
usid: 'testusid_passkey',
enc_user_id: 'testEncUserId_passkey',
id_token: 'testIdToken_passkey'
}
})
)
})
)
})
afterEach(() => {
delete global.PublicKeyCredential
delete global.window.PublicKeyCredential
})
test('Triggers passkey login when modal opens with passkey enabled', async () => {
// Mock credential that will be returned from navigator.credentials.get
const mockCredential = {
id: 'mock-credential-id',
rawId: new ArrayBuffer(32),
type: 'public-key',
response: {
authenticatorData: new ArrayBuffer(37),
clientDataJSON: new ArrayBuffer(128),
signature: new ArrayBuffer(64),
userHandle: new ArrayBuffer(16)
},
getClientExtensionResults: jest.fn().mockReturnValue({}),
toJSON: jest.fn().mockReturnValue({
id: 'mock-credential-id',
rawId: 'mock-raw-id',
type: 'public-key',
response: {
authenticatorData: 'mock-auth-data',
clientDataJSON: 'mock-client-data',
signature: 'mock-signature',
userHandle: 'mock-user-handle'
}
})
}
mockCredentialsGet.mockResolvedValue(mockCredential)
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {
bypassAuth: false
}
})
// Open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
// Wait for passkey flow to be triggered
await waitFor(
() => {
expect(mockCredentialsGet).toHaveBeenCalledWith(
expect.objectContaining({
mediation: 'conditional'
})
)
},
{timeout: 2000}
)
})
test('User can login with other method when passkey login is cancelled', async () => {
// Simulate user cancelling passkey selection (NotAllowedError)
const notAllowedError = new Error('User cancelled')
notAllowedError.name = 'NotAllowedError'
mockCredentialsGet.mockRejectedValue(notAllowedError)
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />, {
wrapperProps: {
bypassAuth: false
}
})
// Open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
// Login form should be shown
await waitFor(() => {
expect(mockCredentialsGet).toHaveBeenCalled()
expect(screen.getByText(/welcome back/i)).toBeInTheDocument()
expect(screen.getByLabelText(/email/i)).toBeInTheDocument()
expect(screen.getByText(/continue/i)).toBeInTheDocument()
expect(screen.getByText(/password/i)).toBeInTheDocument()
})
})
test('Shows error when passkey authentication fails with error from the browser', async () => {
// Simulate error in loginWithPasskey hook
mockCredentialsGet.mockRejectedValue(new Error('Authentication failed'))
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />, {
wrapperProps: {
bypassAuth: false
}
})
// Open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
// Should show error - passkey error should be caught and handled
await waitFor(() => {
expect(mockCredentialsGet).toHaveBeenCalled()
expect(screen.getByText(/Something went wrong. Try again!/i)).toBeInTheDocument()
})
})
test('Shows error when passkey authentication fails with error from the WebAuthn API', async () => {
global.server.use(
rest.post('*/oauth2/webauthn/authenticate/start', (req, res, ctx) => {
return res(
ctx.delay(0),
ctx.status(401),
ctx.json({message: 'Authentication failed'})
)
})
)
const {user} = renderWithProviders(<MockedComponent isPasswordlessEnabled={true} />, {
wrapperProps: {
bypassAuth: false
}
})
// Open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
// Should show error - 401 error from WebAuthn API should be caught and converted to user-friendly message
await waitFor(() => {
expect(screen.getByText(/Something went wrong. Try again!/i)).toBeInTheDocument()
})
})
test('Passkey prompt is aborted when modal is closed', async () => {
// Capture the abort signal passed to credentials.get
let capturedSignal = null
// Mock credentials.get to capture the abort signal and stay pending
mockCredentialsGet.mockImplementation(({signal}) => {
capturedSignal = signal
return new Promise(() => {
// Never resolve - simulates passkey prompt staying open
})
})
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {
bypassAuth: false
}
})
// Open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
// Wait for passkey conditional mediation to start and capture the signal
await waitFor(() => {
expect(mockCredentialsGet).toHaveBeenCalledWith(
expect.objectContaining({
mediation: 'conditional',
signal: expect.any(AbortSignal)
})
)
expect(capturedSignal).not.toBeNull()
})
// Verify signal is not yet aborted
expect(capturedSignal.aborted).toBe(false)
// Close the modal by clicking the close button
const closeButton = screen.getByLabelText(/close login form/i)
await user.click(closeButton)
// Verify the signal was aborted when modal closed
expect(capturedSignal.aborted).toBe(true)
})
test('Passkey prompt is aborted when user logs in', async () => {
// This test verifies that when the user logs in while the passkey
// prompt is pending, the modal closes (login succeeds) and the passkey flow is
// aborted via the cleanup that runs when the modal closes.
let capturedSignal = null
mockCredentialsGet.mockImplementation(({signal}) => {
expect(signal).toBeInstanceOf(AbortSignal)
capturedSignal = signal
return new Promise(() => {
// Never resolve - simulates passkey prompt staying open
})
})
// Successful email/password login
global.server.use(
rest.post('*/oauth2/token', (req, res, ctx) =>
res(
ctx.delay(0),
ctx.json({
customer_id: 'customerid_1',
access_token: registerUserToken,
refresh_token: 'testrefeshtoken_1',
usid: 'testusid_1',
enc_user_id: 'testEncUserId_1',
id_token: 'testIdToken_1'
})
)
),
rest.post('*/baskets/actions/merge', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockMergedBasket))
})
)
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {
bypassAuth: false
}
})
// Open the modal - passkey flow starts and stays pending
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
await waitFor(() => {
expect(mockCredentialsGet).toHaveBeenCalledWith(
expect.objectContaining({
mediation: 'conditional',
signal: expect.any(AbortSignal)
})
)
expect(capturedSignal).not.toBeNull()
})
// User logs in with password while passkey prompt is still open
await user.type(screen.getByLabelText(/email/i), 'customer@test.com')
await user.type(screen.getByLabelText(/^password$/i), 'Password!1')
await user.click(screen.getByRole('button', {name: /sign in/i}))
await waitFor(
() => {
// Verify the passkey prompt was aborted
expect(capturedSignal.aborted).toBe(true)
// Verify the modal was closed
expect(screen.queryByRole('button', {name: /sign in/i})).not.toBeInTheDocument()
// Verify the user was signed in
expect(screen.getByText(/You're now signed in./i)).toBeInTheDocument()
},
{timeout: 3000}
)
})
test('Does not trigger passkey when not enabled', async () => {
const mockAppConfig = {
...mockConfig.app,
login: {
...mockConfig.app.login,
passkey: {enabled: false}
}
}
// Override getConfig to return config with passkey disabled
getConfig.mockReturnValue({
...mockConfig,
app: mockAppConfig
})
const {user} = renderWithProviders(<MockedComponent />, {
wrapperProps: {
appConfig: mockAppConfig,
bypassAuth: false
}
})
// Open the modal
const trigger = screen.getByText(/open modal/i)
await user.click(trigger)
// Wait for modal to open
await waitFor(() => {
expect(screen.getByText(/welcome back/i)).toBeInTheDocument()
})
// Should not have called WebAuthn APIs
expect(mockCredentialsGet).not.toHaveBeenCalled()
})
test('Successfully logs in with passkey', async () => {
const mockCredential = {
id: 'mock-credential-id',
rawId: new ArrayBuffer(32),
type: 'public-key',
response: {
authenticatorData: new ArrayBuffer(37),
clientDataJSON: new ArrayBuffer(128),
signature: new ArrayBuffer(64),
userHandle: new ArrayBuffer(16)
},
getClientExtensionResults: jest.fn().mockReturnValue({}),
toJSON: jest.fn().mockReturnValue({
id: 'mock-credential-id',
rawId: 'mock-raw-id',
type: 'public-key',
response: {
authenticatorData: 'mock-auth-data',
clientDataJSON: 'mock-client-data',
signature: 'mock-signature',
userHandle: 'mock-user-handle'
}
})
}
mockCredentialsGet.mockResolvedValue(mockCredential)
// Mock customer as registered after passkey login