-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathauth-provider.test.tsx
329 lines (308 loc) · 11.3 KB
/
auth-provider.test.tsx
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
import { useContext } from 'react';
import { mocked } from 'ts-jest/utils';
import Auth0Context from '../src/auth0-context';
import { renderHook, act } from '@testing-library/react-hooks';
import { Auth0Client } from '@auth0/auth0-spa-js';
import { createWrapper } from './helpers';
const clientMock = mocked(new Auth0Client({ client_id: '', domain: '' }));
describe('Auth0Provider', () => {
it('should provide the Auth0Provider result', async () => {
const wrapper = createWrapper();
const { result, waitForNextUpdate } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
expect(result.current).toBeDefined();
await waitForNextUpdate();
});
it('should configure an instance of the Auth0Client', async () => {
const opts = {
clientId: 'foo',
domain: 'bar',
redirectUri: 'baz',
maxAge: 'qux',
extra_param: '__test_extra_param__',
};
const wrapper = createWrapper(opts);
const { waitForNextUpdate } = renderHook(() => useContext(Auth0Context), {
wrapper,
});
expect(Auth0Client).toHaveBeenCalledWith({
client_id: 'foo',
domain: 'bar',
redirect_uri: 'baz',
max_age: 'qux',
extra_param: '__test_extra_param__',
});
await waitForNextUpdate();
});
it('should get token silently when logged out', async () => {
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
expect(result.current.isLoading).toBe(true);
await waitForNextUpdate();
expect(result.current.isLoading).toBe(false);
expect(clientMock.getTokenSilently).toHaveBeenCalled();
expect(result.current.isAuthenticated).toBe(false);
});
it('should get token silently when logged in', async () => {
clientMock.isAuthenticated.mockResolvedValue(true);
clientMock.getUser.mockResolvedValue('__test_user__');
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(clientMock.getTokenSilently).toHaveBeenCalled();
expect(result.current.isAuthenticated).toBe(true);
expect(result.current.user).toBe('__test_user__');
});
it('should handle login_required errors when getting token', async () => {
clientMock.getTokenSilently.mockRejectedValue({
error: 'login_required',
});
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(clientMock.getTokenSilently).toHaveBeenCalled();
expect(result.current.error).toBeUndefined();
expect(result.current.isAuthenticated).toBe(false);
});
it('should handle other errors when getting token', async () => {
clientMock.getTokenSilently.mockRejectedValue({
error: '__test_error__',
error_description: '__test_error_description__',
});
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(clientMock.getTokenSilently).toHaveBeenCalled();
expect(() => {
throw result.current.error;
}).toThrowError('__test_error_description__');
expect(result.current.isAuthenticated).toBe(false);
});
it('should handle redirect callback success and clear the url', async () => {
window.history.pushState(
{},
document.title,
'/?code=__test_code__&state=__test_state__'
);
expect(window.location.href).toBe(
'https://www.example.com/?code=__test_code__&state=__test_state__'
);
const wrapper = createWrapper();
const { waitForNextUpdate } = renderHook(() => useContext(Auth0Context), {
wrapper,
});
await waitForNextUpdate();
expect(clientMock.handleRedirectCallback).toHaveBeenCalled();
expect(window.location.href).toBe('https://www.example.com/');
});
it('should handle redirect callback errors', async () => {
window.history.pushState({}, document.title, '/?error=__test_error__');
clientMock.handleRedirectCallback.mockRejectedValue(
new Error('__test_error__')
);
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(clientMock.handleRedirectCallback).toHaveBeenCalled();
expect(() => {
throw result.current.error;
}).toThrowError('__test_error__');
});
it('should handle redirect and call a custom handler', async () => {
window.history.pushState(
{},
document.title,
'/?code=__test_code__&state=__test_state__'
);
clientMock.handleRedirectCallback.mockResolvedValue({
appState: { foo: 'bar' },
});
const onRedirectCallback = jest.fn();
const wrapper = createWrapper({
onRedirectCallback,
});
const { waitForNextUpdate } = renderHook(() => useContext(Auth0Context), {
wrapper,
});
await waitForNextUpdate();
expect(onRedirectCallback).toHaveBeenCalledWith({ foo: 'bar' });
});
it('should login with a popup', async () => {
clientMock.isAuthenticated.mockResolvedValue(false);
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.isAuthenticated).toBe(false);
clientMock.isAuthenticated.mockResolvedValue(true);
act(() => {
result.current.loginWithPopup();
});
expect(result.current.isLoading).toBe(true);
await waitForNextUpdate();
expect(result.current.isLoading).toBe(false);
expect(clientMock.loginWithPopup).toHaveBeenCalled();
expect(result.current.isAuthenticated).toBe(true);
});
it('should handle errors when logging in with a popup', async () => {
clientMock.isAuthenticated.mockResolvedValue(false);
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.isAuthenticated).toBe(false);
clientMock.isAuthenticated.mockResolvedValue(false);
clientMock.loginWithPopup.mockRejectedValue(new Error('__test_error__'));
act(() => {
result.current.loginWithPopup();
});
expect(result.current.isLoading).toBe(true);
await waitForNextUpdate();
expect(result.current.isLoading).toBe(false);
expect(clientMock.loginWithPopup).toHaveBeenCalled();
expect(result.current.isAuthenticated).toBe(false);
expect(() => {
throw result.current.error;
}).toThrowError('__test_error__');
});
it('should provide a login method', async () => {
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.loginWithRedirect).toBeInstanceOf(Function);
await result.current.loginWithRedirect({
redirectUri: '__redirect_uri__',
});
expect(clientMock.loginWithRedirect).toHaveBeenCalledWith({
redirect_uri: '__redirect_uri__',
});
});
it('should provide a logout method', async () => {
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.logout).toBeInstanceOf(Function);
await result.current.logout({ returnTo: '__return_to__' });
expect(clientMock.logout).toHaveBeenCalledWith({
returnTo: '__return_to__',
});
});
it('should provide a getAccessTokenSilently method', async () => {
clientMock.getTokenSilently.mockResolvedValue('token');
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.getAccessTokenSilently).toBeInstanceOf(Function);
const token = await result.current.getAccessTokenSilently();
expect(clientMock.getTokenSilently).toHaveBeenCalled();
expect(token).toBe('token');
});
it('should normalize errors from getAccessTokenSilently method', async () => {
clientMock.getTokenSilently.mockRejectedValue(new ProgressEvent('error'));
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.getAccessTokenSilently).rejects.toThrowError(
'Get access token failed'
);
});
it('should call getAccessTokenSilently in the scope of the Auth0 client', async () => {
clientMock.getTokenSilently.mockReturnThis();
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
const returnedThis = await result.current.getAccessTokenSilently();
expect(returnedThis).toStrictEqual(clientMock);
});
it('should provide a getAccessTokenWithPopup method', async () => {
clientMock.getTokenWithPopup.mockResolvedValue('token');
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.getAccessTokenWithPopup).toBeInstanceOf(Function);
const token = await result.current.getAccessTokenWithPopup();
expect(clientMock.getTokenWithPopup).toHaveBeenCalled();
expect(token).toBe('token');
});
it('should call getAccessTokenWithPopup in the scope of the Auth0 client', async () => {
clientMock.getTokenWithPopup.mockReturnThis();
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
const returnedThis = await result.current.getAccessTokenWithPopup();
expect(returnedThis).toStrictEqual(clientMock);
});
it('should normalize errors from getAccessTokenWithPopup method', async () => {
clientMock.getTokenWithPopup.mockRejectedValue(new ProgressEvent('error'));
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.getAccessTokenWithPopup).rejects.toThrowError(
'Get access token failed'
);
});
it('should provide a getIdTokenClaims method', async () => {
clientMock.getIdTokenClaims.mockResolvedValue({
claim: '__test_claim__',
__raw: '__test_raw_token__',
});
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.getIdTokenClaims).toBeInstanceOf(Function);
const claims = await result.current.getIdTokenClaims();
expect(clientMock.getIdTokenClaims).toHaveBeenCalled();
expect(claims).toStrictEqual({
claim: '__test_claim__',
__raw: '__test_raw_token__',
});
});
});