Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion packages/commerce-sdk-react/src/auth/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ jest.mock('commerce-sdk-isomorphic', () => {
fetchOptions: {
credentials: config?.fetchOptions?.credentials || 'same-origin'
}
}
},
getPasswordResetToken: jest.fn().mockResolvedValue({}),
resetPassword: jest.fn().mockResolvedValue({})
}))
}
})
Expand Down Expand Up @@ -959,6 +961,72 @@ describe('Auth', () => {
})
})

test.each([
[
'with all parameters specified',
{
pwd_action_token: '12345678',
new_password: 'newPassword123',
channel_id: 'customChannelId',
client_id: 'customClientId',
hint: 'custom_hint',
code_verifier: 'test-code-verifier',
user_id: 'userId'
},
{
pwd_action_token: '12345678',
new_password: 'newPassword123',
channel_id: 'customChannelId',
client_id: 'customClientId',
hint: 'custom_hint',
code_verifier: 'test-code-verifier',
user_id: 'userId'
}
],
[
'defaults all parameters when only required parameters are specified',
{
pwd_action_token: '12345678',
new_password: 'newPassword123'
},
{
pwd_action_token: '12345678',
new_password: 'newPassword123',
channel_id: config.siteId,
client_id: config.clientId,
hint: 'cross_device'
}
]
])('resetPassword %s', async (_, input: any, expectedBody: any) => {
const auth = new Auth(config)
const mockResponse = {status: 200, json: jest.fn().mockResolvedValue({})}
const resetPasswordSpy = jest.spyOn((auth as any).client, 'resetPassword')
resetPasswordSpy.mockResolvedValueOnce(mockResponse)

const result = await auth.resetPassword(input)
expect(result).toBe(mockResponse)
expect(resetPasswordSpy).toHaveBeenCalled()
const callArgs = resetPasswordSpy.mock.calls[0][0] as any
expect(callArgs.body).toMatchObject(expectedBody)
})

test('resetPassword with private client sets Authorization header', async () => {
const auth = new Auth(configSLASPrivate)
const mockResponse = {status: 200, json: jest.fn().mockResolvedValue({})}
const resetPasswordSpy = jest.spyOn((auth as any).client, 'resetPassword')
resetPasswordSpy.mockResolvedValueOnce(mockResponse)

await auth.resetPassword({
pwd_action_token: '12345678',
new_password: 'newPassword123'
} as any)

expect(resetPasswordSpy).toHaveBeenCalled()
const callArgs = resetPasswordSpy.mock.calls[0][0] as any
expect(callArgs.headers.Authorization).toBeTruthy()
expect(callArgs.headers.Authorization).toContain('Basic ')
})

test('logout as registered user calls isomorphic logout', async () => {
const auth = new Auth(config)
// simulate logging in as login function is mocked
Expand Down
5 changes: 3 additions & 2 deletions packages/commerce-sdk-react/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1365,8 +1365,9 @@ class Auth {
channel_id: parameters.channel_id || slasClient.clientConfig.parameters.siteId,
client_id: parameters.client_id || slasClient.clientConfig.parameters.clientId,
new_password: parameters.new_password,
hint: parameters.hint,
code_verifier: parameters.code_verifier
hint: parameters.hint || 'cross_device',
...(parameters.user_id && {user_id: parameters.user_id}),
...(parameters.code_verifier && {code_verifier: parameters.code_verifier})
}
}

Expand Down