77import Auth , { AuthData } from './'
88import { waitFor } from '@testing-library/react'
99import jwt from 'jsonwebtoken'
10- import { helpers , ShopperCustomersTypes , ShopperLogin } from 'commerce-sdk-isomorphic'
10+ import {
11+ helpers ,
12+ ShopperCustomersTypes ,
13+ ShopperCustomers ,
14+ ShopperLogin
15+ } from 'commerce-sdk-isomorphic'
1116import * as utils from '../utils'
1217import { SLAS_SECRET_PLACEHOLDER } from '../constant'
1318import { ShopperLoginTypes } from 'commerce-sdk-isomorphic'
@@ -49,23 +54,23 @@ jest.mock('commerce-sdk-isomorphic', () => {
4954 authorizeIDP : jest . fn ( ) . mockResolvedValue ( '' ) ,
5055 authorizePasswordless : jest . fn ( ) . mockResolvedValue ( '' ) ,
5156 getPasswordLessAccessToken : jest . fn ( ) . mockResolvedValue ( '' )
52- } ,
53- ShopperCustomers : jest . fn ( ) . mockImplementation ( ( ) => {
54- return {
55- updateCustomerPassword : ( ) => { }
56- }
57- } )
57+ }
5858 }
5959} )
6060
61- jest . mock ( '../utils' , ( ) => ( {
62- __esModule : true ,
63- onClient : ( ) => true ,
64- getParentOrigin : jest . fn ( ) . mockResolvedValue ( '' ) ,
65- isOriginTrusted : ( ) => false ,
66- getDefaultCookieAttributes : ( ) => { } ,
67- isAbsoluteUrl : ( ) => true
68- } ) )
61+ jest . mock ( '../utils' , ( ) => {
62+ const originalModule = jest . requireActual ( '../utils' )
63+
64+ return {
65+ ...originalModule ,
66+ __esModule : true ,
67+ onClient : ( ) => true ,
68+ getParentOrigin : jest . fn ( ) . mockResolvedValue ( '' ) ,
69+ isOriginTrusted : ( ) => false ,
70+ getDefaultCookieAttributes : ( ) => { } ,
71+ isAbsoluteUrl : ( ) => true
72+ }
73+ } )
6974
7075/** The auth data we store has a slightly different shape than what we use. */
7176type StoredAuthData = Omit < AuthData , 'refresh_token' > & { refresh_token_guest ?: string }
@@ -491,6 +496,48 @@ describe('Auth', () => {
491496 expect ( helpers . loginGuestUser ) . toHaveBeenCalled ( )
492497 } )
493498
499+ test ( 'loginGuestUser can pass along custom parameters' , async ( ) => {
500+ const parameters = { c_test : 'custom parameter' }
501+ const auth = new Auth ( config )
502+ await auth . loginGuestUser ( parameters )
503+ // The first argument is the SLAS config, which we don't need to verify in this case
504+ // We only want to see that the custom parameters were included in the second argument
505+ expect ( helpers . loginGuestUser ) . toHaveBeenCalledWith (
506+ expect . anything ( ) ,
507+ expect . objectContaining ( { c_test : 'custom parameter' } )
508+ )
509+ } )
510+
511+ test ( 'register only sends custom parameters to registered login' , async ( ) => {
512+ const registerCustomerSpy = jest
513+ . spyOn ( ShopperCustomers . prototype , 'registerCustomer' )
514+ . mockImplementation ( )
515+ const auth = new Auth ( config )
516+ const inputToRegister = {
517+ customer : baseCustomer ,
518+ password : 'test' ,
519+ someOtherParameter : 'this should not be passed to login' ,
520+ c_test : 'custom parameter'
521+ }
522+
523+ await auth . register ( inputToRegister )
524+
525+ // Body should only include credentials. No other parameters
526+ expect ( registerCustomerSpy ) . toHaveBeenCalledWith (
527+ expect . objectContaining ( { body : { customer : baseCustomer , password : 'test' } } )
528+ )
529+
530+ // We don't need to verify the first and third parameters as they correspond to the SLAS client and mandatory parameters
531+ // The second argument is credentials
532+ // We want to see that only the custom parameters were included in the fourth argument and not any other parameters
533+ expect ( helpers . loginRegisteredUserB2C ) . toHaveBeenCalledWith (
534+ expect . anything ( ) ,
535+ expect . anything ( ) ,
536+ expect . anything ( ) ,
537+ { body : { c_test : 'custom parameter' } }
538+ )
539+ } )
540+
494541 test . each ( [
495542 // When user has not selected DNT pref
496543 [ true , undefined , { dnt : true } ] ,
@@ -614,6 +661,28 @@ describe('Auth', () => {
614661 } )
615662 } )
616663
664+ test ( 'loginRegisteredUserB2C can pass along custom parameters' , async ( ) => {
665+ const options = {
666+ body : { c_test : 'custom parameter' }
667+ }
668+ const credentials = {
669+ username : 'test' ,
670+ password : 'test'
671+ }
672+ const auth = new Auth ( config )
673+ await auth . loginRegisteredUserB2C ( { ...credentials , options} )
674+ // We don't need to verify the first and third parameters as they correspond to the SLAS client and mandatory parameters
675+ // The second argument is credentials, including the client secret
676+ // The fourth argument is custom parameters
677+ // We only want to see that the custom parameters were included in the fourth argument
678+ expect ( helpers . loginRegisteredUserB2C ) . toHaveBeenCalledWith (
679+ expect . anything ( ) ,
680+ expect . objectContaining ( credentials ) ,
681+ expect . anything ( ) ,
682+ options
683+ )
684+ } )
685+
617686 test ( 'loginIDPUser calls isomorphic loginIDPUser' , async ( ) => {
618687 const auth = new Auth ( config )
619688 await auth . loginIDPUser ( { redirectURI : 'redirectURI' , code : 'test' } )
@@ -634,10 +703,18 @@ describe('Auth', () => {
634703
635704 test ( 'authorizeIDP calls isomorphic authorizeIDP' , async ( ) => {
636705 const auth = new Auth ( config )
637- await auth . authorizeIDP ( { redirectURI : 'redirectURI' , hint : 'test' } )
706+ await auth . authorizeIDP ( {
707+ redirectURI : 'redirectURI' ,
708+ hint : 'test' ,
709+ c_customParam : 'customParam'
710+ } )
638711 expect ( helpers . authorizeIDP ) . toHaveBeenCalled ( )
639712 const functionArg = ( helpers . authorizeIDP as jest . Mock ) . mock . calls [ 0 ] [ 1 ]
640- expect ( functionArg ) . toMatchObject ( { redirectURI : 'redirectURI' , hint : 'test' } )
713+ expect ( functionArg ) . toMatchObject ( {
714+ redirectURI : 'redirectURI' ,
715+ hint : 'test' ,
716+ c_customParam : 'customParam'
717+ } )
641718 } )
642719
643720 test ( 'authorizeIDP adds clientSecret to parameters when using private client' , async ( ) => {
@@ -698,6 +775,7 @@ describe('Auth', () => {
698775 expect ( helpers . loginGuestUser ) . toHaveBeenCalled ( )
699776 } )
700777 test ( 'updateCustomerPassword calls registered login' , async ( ) => {
778+ jest . spyOn ( ShopperCustomers . prototype , 'updateCustomerPassword' ) . mockImplementation ( )
701779 const auth = new Auth ( config )
702780 await auth . updateCustomerPassword ( {
703781 customer : baseCustomer ,
0 commit comments