-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathauth-utils.test.js
More file actions
102 lines (95 loc) · 3.8 KB
/
auth-utils.test.js
File metadata and controls
102 lines (95 loc) · 3.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
/*
* Copyright (c) 2026, salesforce.com, 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 {
API_ERROR_MESSAGE,
FEATURE_UNAVAILABLE_ERROR_MESSAGE
} from '@salesforce/retail-react-app/app/constants'
import {
getPasswordlessCallbackUrl,
getPasswordlessErrorMessage,
getPasswordResetErrorMessage,
TOO_MANY_LOGIN_ATTEMPTS_ERROR_MESSAGE,
TOO_MANY_PASSWORD_RESET_ATTEMPTS_ERROR_MESSAGE
} from '@salesforce/retail-react-app/app/utils/auth-utils'
afterEach(() => {
jest.clearAllMocks()
})
jest.mock('@salesforce/pwa-kit-react-sdk/utils/url', () => {
const original = jest.requireActual('@salesforce/pwa-kit-react-sdk/utils/url')
return {
...original,
getAppOrigin: jest.fn(() => 'https://www.example.com')
}
})
jest.mock('@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths', () => {
const original = jest.requireActual('@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths')
return {
...original,
getEnvBasePath: jest.fn(() => '/test')
}
})
describe('getPasswordlessCallbackUrl', function () {
test.each([undefined, null, ''])('return undefined when callbackURI is %s', (callbackURI) => {
expect(getPasswordlessCallbackUrl(callbackURI)).toBeUndefined()
})
test('return callbackURI as-is when it is an absolute URL', () => {
const absoluteUrl = 'https://callback.example.com/passwordless'
expect(getPasswordlessCallbackUrl(absoluteUrl)).toBe(absoluteUrl)
})
test('return full URL with origin and base path when callbackURI is relative', () => {
expect(getPasswordlessCallbackUrl('/passwordless-login-callback')).toBe(
'https://www.example.com/test/passwordless-login-callback'
)
})
})
describe('getPasswordlessErrorMessage', () => {
test.each([
['no callback_uri is registered for client', FEATURE_UNAVAILABLE_ERROR_MESSAGE],
["callback_uri doesn't match the registered callbacks", FEATURE_UNAVAILABLE_ERROR_MESSAGE],
[
'Monthly quota for passwordless login mode email has been exceeded',
FEATURE_UNAVAILABLE_ERROR_MESSAGE
],
[
'PasswordLess Permissions Error for clientId:aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
FEATURE_UNAVAILABLE_ERROR_MESSAGE
],
['client secret is not provided', FEATURE_UNAVAILABLE_ERROR_MESSAGE],
[
'Too many login requests were made. Please try again later.',
TOO_MANY_LOGIN_ATTEMPTS_ERROR_MESSAGE
],
['unexpected error message', API_ERROR_MESSAGE],
[null, API_ERROR_MESSAGE]
])(
'maps passwordless error "%s" to the correct message descriptor',
(errorMessage, expectedMessage) => {
expect(getPasswordlessErrorMessage(errorMessage)).toBe(expectedMessage)
}
)
})
describe('getPasswordResetErrorMessage', () => {
test.each([
['no callback_uri is registered for client', FEATURE_UNAVAILABLE_ERROR_MESSAGE],
["callback_uri doesn't match the registered callbacks", FEATURE_UNAVAILABLE_ERROR_MESSAGE],
[
'Monthly quota for passwordless login mode email has been exceeded',
FEATURE_UNAVAILABLE_ERROR_MESSAGE
],
[
'Too many password reset requests were made. Please try again later.',
TOO_MANY_PASSWORD_RESET_ATTEMPTS_ERROR_MESSAGE
],
['unexpected error message', API_ERROR_MESSAGE],
[null, API_ERROR_MESSAGE]
])(
'maps password reset error "%s" to the correct message descriptor',
(errorMessage, expectedMessage) => {
expect(getPasswordResetErrorMessage(errorMessage)).toBe(expectedMessage)
}
)
})