piral-oidc 1.3.3-beta.6190 → 1.3.3-beta.6201

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "piral-oidc",
3
- "version": "1.3.3-beta.6190",
3
+ "version": "1.3.3-beta.6201",
4
4
  "description": "Plugin to integrate authentication using OpenID connect in Piral.",
5
5
  "keywords": [
6
6
  "piral",
@@ -66,7 +66,7 @@
66
66
  "oidc-client": "^1.10.1"
67
67
  },
68
68
  "devDependencies": {
69
- "piral-core": "1.3.3-beta.6190"
69
+ "piral-core": "1.3.3-beta.6201"
70
70
  },
71
- "gitHead": "dc3358f0e08b06acea2056b612f268334a8c49c8"
71
+ "gitHead": "6e9f4e6f83514b5c38960ce015e073efc668f3d6"
72
72
  }
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
4
+ import { describe, it, expect, vitest, beforeEach } from 'vitest';
1
5
  import { createOidcApi } from './create';
2
6
  import { PiletOidcApi } from './types';
3
7
 
@@ -19,12 +23,12 @@ describe('Piral-Oidc create module', () => {
19
23
 
20
24
  beforeEach(() => {
21
25
  context = {
22
- on: jest.fn(),
26
+ on: vitest.fn(),
23
27
  };
24
28
 
25
29
  mock = {
26
- token: jest.fn(() => mockToken),
27
- account: jest.fn(() => mockProfile),
30
+ token: vitest.fn(() => mockToken),
31
+ account: vitest.fn(() => mockProfile),
28
32
  };
29
33
  });
30
34
 
package/src/setup.test.ts CHANGED
@@ -1,8 +1,10 @@
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
4
+ import { describe, it, expect, vitest, beforeAll, beforeEach, afterAll } from 'vitest';
1
5
  import { UserManager } from 'oidc-client';
2
6
  import { setupOidcClient } from './setup';
3
- import { OidcClient, OidcConfig, OidcErrorType } from './types';
4
-
5
- jest.mock('oidc-client');
7
+ import { OidcConfig, OidcErrorType } from './types';
6
8
 
7
9
  describe('Piral-Oidc setup module', () => {
8
10
  const setWindowInIFrame = () =>
@@ -11,57 +13,45 @@ describe('Piral-Oidc setup module', () => {
11
13
  Object.defineProperty(window, 'top', { value: window, configurable: true, writable: true });
12
14
  const originalWindowLocation = window.location;
13
15
  let oidcConfig: OidcConfig;
14
- const MockUserManager = UserManager as jest.MockedClass<typeof UserManager>;
15
16
  const user = {
16
17
  access_token: '123',
17
18
  expires_in: 100,
18
19
  };
19
- const mockGetUser = jest.fn().mockResolvedValue(user).mockName('getUser');
20
- const mockSigninCallback = jest.fn().mockResolvedValue(undefined).mockName('signinCallback');
21
- const mockSigninRedirectCallback = jest.fn().mockResolvedValue(undefined).mockName('signinRedirectCallback');
22
- const mockSigninRedirect = jest.fn().mockResolvedValue(undefined).mockName('signinRedirect');
23
- const mockSigninSilent = jest.fn().mockResolvedValue(undefined).mockName('signinSilent');
24
- const mockSigninSilentCallback = jest.fn().mockResolvedValue(undefined).mockName('signinSilentCallback');
25
- const mockSignoutRedirect = jest.fn().mockResolvedValue(undefined).mockName('signoutRedirect');
26
- const mockSignoutRedirectCallback = jest.fn().mockResolvedValue(undefined).mockName('signoutRedirectCallback');
27
- const mockSignoutPopupCallback = jest.fn().mockResolvedValue(undefined).mockName('signoutPopupCallback');
20
+ const mockGetUser = vitest.fn().mockResolvedValue(user).mockName('getUser');
21
+ const mockSigninCallback = vitest.fn().mockResolvedValue(undefined).mockName('signinCallback');
22
+ const mockSigninRedirectCallback = vitest.fn().mockResolvedValue(undefined).mockName('signinRedirectCallback');
23
+ const mockSigninRedirect = vitest.fn().mockResolvedValue(undefined).mockName('signinRedirect');
24
+ const mockSigninSilent = vitest.fn().mockResolvedValue(undefined).mockName('signinSilent');
25
+ const mockSigninSilentCallback = vitest.fn().mockResolvedValue(undefined).mockName('signinSilentCallback');
26
+ const mockSignoutRedirect = vitest.fn().mockResolvedValue(undefined).mockName('signoutRedirect');
27
+ const mockSignoutRedirectCallback = vitest.fn().mockResolvedValue(undefined).mockName('signoutRedirectCallback');
28
+ const mockSignoutPopupCallback = vitest.fn().mockResolvedValue(undefined).mockName('signoutPopupCallback');
28
29
 
29
30
  const postLogoutRedirectUri = 'http://localhost:8000/post-logout';
30
31
  const redirectUri = 'http://localhost:8000/callback';
31
32
  const appUri = 'http://localhost:8000/app';
32
33
 
33
34
  beforeAll(() => {
34
- //@ts-ignore
35
- MockUserManager.mockImplementation((settings) => {
36
- return {
37
- getUser: mockGetUser,
38
- settings: {
39
- popup_redirect_uri: settings.popup_redirect_uri || settings.redirect_uri,
40
- post_logout_redirect_uri: settings.post_logout_redirect_uri,
41
- redirect_uri: settings.redirect_uri,
42
- silent_redirect_uri: settings.silent_redirect_uri || settings.redirect_uri,
43
- },
44
- signinCallback: mockSigninCallback,
45
- signinRedirectCallback: mockSigninRedirectCallback,
46
- signinRedirect: mockSigninRedirect,
47
- signinSilent: mockSigninSilent,
48
- signinSilentCallback: mockSigninSilentCallback,
49
- signoutRedirect: mockSignoutRedirect,
50
- signoutRedirectCallback: mockSignoutRedirectCallback,
51
- signoutPopupCallback: mockSignoutPopupCallback,
52
- };
53
- });
35
+ vitest.spyOn(UserManager.prototype, 'getUser').mockImplementation(mockGetUser);
36
+ vitest.spyOn(UserManager.prototype, 'signinCallback').mockImplementation(mockSigninCallback);
37
+ vitest.spyOn(UserManager.prototype, 'signinRedirectCallback').mockImplementation(mockSigninRedirectCallback);
38
+ vitest.spyOn(UserManager.prototype, 'signinRedirect').mockImplementation(mockSigninRedirect);
39
+ vitest.spyOn(UserManager.prototype, 'signinSilent').mockImplementation(mockSigninSilent);
40
+ vitest.spyOn(UserManager.prototype, 'signinSilentCallback').mockImplementation(mockSigninSilentCallback);
41
+ vitest.spyOn(UserManager.prototype, 'signoutRedirect').mockImplementation(mockSignoutRedirect);
42
+ vitest.spyOn(UserManager.prototype, 'signoutRedirectCallback').mockImplementation(mockSignoutRedirectCallback);
43
+ vitest.spyOn(UserManager.prototype, 'signoutPopupCallback').mockImplementation(mockSignoutPopupCallback);
54
44
  });
55
45
 
56
46
  afterAll(() => {
57
- jest.unmock('oidc-client');
58
47
  window.location = originalWindowLocation;
59
48
  setWindowInTop();
60
49
  });
61
50
 
62
51
  beforeEach(() => {
63
- jest.clearAllMocks();
52
+ vitest.clearAllMocks();
64
53
  mockGetUser.mockResolvedValue(user);
54
+ //@ts-ignore
65
55
  delete window.location;
66
56
  //@ts-ignore
67
57
  window.location = new URL('http://localhost:8000/');
@@ -77,240 +67,267 @@ describe('Piral-Oidc setup module', () => {
77
67
  };
78
68
  });
79
69
 
80
- describe('setupOidcClient()', () => {
81
- it('setupOidcClient should return the following signature', () => {
82
- const client = setupOidcClient(oidcConfig);
83
- expect(client).toMatchObject<OidcClient>({
84
- login: expect.any(Function),
85
- logout: expect.any(Function),
86
- extendHeaders: expect.any(Function),
87
- token: expect.any(Function),
88
- account: expect.any(Function),
89
- handleAuthentication: expect.any(Function),
90
- });
91
- });
92
-
93
- it('should call signoutRedirectCallback when on the post_logout_redirect_uri in the top frame', () => {
94
- setWindowInTop();
95
- expect(UserManager).not.toHaveBeenCalled();
96
- expect(mockSignoutRedirectCallback).not.toHaveBeenCalled();
97
- //@ts-ignore
98
- window.location = new URL(postLogoutRedirectUri);
99
- setupOidcClient(oidcConfig);
100
- expect(UserManager).toHaveBeenCalledTimes(1);
101
- expect(mockSignoutRedirectCallback).toHaveBeenCalledTimes(1);
102
- expect(mockSignoutPopupCallback).not.toHaveBeenCalled();
70
+ it('setupOidcClient should return the following signature', () => {
71
+ const client = setupOidcClient(oidcConfig);
72
+ expect(client).toMatchObject({
73
+ login: expect.any(Function),
74
+ logout: expect.any(Function),
75
+ extendHeaders: expect.any(Function),
76
+ token: expect.any(Function),
77
+ account: expect.any(Function),
78
+ handleAuthentication: expect.any(Function),
103
79
  });
80
+ });
104
81
 
105
- it('should call signoutPopupCallback and signoutSilentCallback when on the post_logout_redirect_uri in an IFrame', () => {
106
- setWindowInIFrame();
107
- expect(UserManager).not.toHaveBeenCalled();
108
- expect(mockSignoutPopupCallback).not.toHaveBeenCalled();
109
- //@ts-ignore
110
- window.location = new URL(postLogoutRedirectUri);
111
- const client = setupOidcClient(oidcConfig);
112
- expect(UserManager).toHaveBeenCalledTimes(1);
113
- expect(mockSignoutPopupCallback).toHaveBeenCalledTimes(1);
114
- expect(mockSignoutRedirectCallback).not.toHaveBeenCalled();
115
- });
82
+ it('should call signoutRedirectCallback when on the post_logout_redirect_uri in the top frame', () => {
83
+ setWindowInTop();
84
+ expect(mockSignoutRedirectCallback).not.toHaveBeenCalled();
85
+ //@ts-ignore
86
+ window.location = new URL(postLogoutRedirectUri);
87
+ setupOidcClient(oidcConfig);
88
+ expect(mockSignoutRedirectCallback).toHaveBeenCalledTimes(1);
89
+ expect(mockSignoutPopupCallback).not.toHaveBeenCalled();
116
90
  });
117
91
 
118
- describe('setupOidcClient returned object', () => {
119
- let client: OidcClient;
92
+ it('should call signoutPopupCallback and signoutSilentCallback when on the post_logout_redirect_uri in an IFrame', () => {
93
+ setWindowInIFrame();
94
+ expect(mockSignoutPopupCallback).not.toHaveBeenCalled();
95
+ //@ts-ignore
96
+ window.location = new URL(postLogoutRedirectUri);
97
+ const client = setupOidcClient(oidcConfig);
98
+ expect(mockSignoutPopupCallback).toHaveBeenCalledTimes(1);
99
+ expect(mockSignoutRedirectCallback).not.toHaveBeenCalled();
100
+ });
120
101
 
121
- beforeEach(() => {
122
- client = setupOidcClient(oidcConfig);
102
+ it('login() should signInRedirect on the UserManager', async () => {
103
+ const client = setupOidcClient(oidcConfig);
104
+ const settings = client._.settings;
105
+ Object.defineProperty(settings, "popup_redirect_uri", {
106
+ get() {
107
+ return settings.popup_redirect_uri || settings.redirect_uri;
108
+ },
123
109
  });
124
-
125
- it('login() should signInRedirect on the UserManager', () => {
126
- expect(mockSigninRedirect).not.toHaveBeenCalled();
127
- client.login();
128
- expect(mockSigninRedirect).toHaveBeenCalledTimes(1);
110
+ Object.defineProperty(settings, "post_logout_redirect_uri", {
111
+ get() {
112
+ return settings.post_logout_redirect_uri;
113
+ },
129
114
  });
130
-
131
- it('logout() should call signoutRedirect on the UserManager', () => {
132
- expect(mockSignoutRedirect).not.toHaveBeenCalled();
133
- client.logout();
134
- expect(mockSignoutRedirect).toHaveBeenCalledTimes(1);
115
+ Object.defineProperty(settings, "redirect_uri", {
116
+ get() {
117
+ return settings.redirect_uri;
118
+ },
135
119
  });
136
-
137
- it('token() should get the token from the user manager', async () => {
138
- const token = await client.token();
139
- expect(token).toBe(user.access_token);
120
+ Object.defineProperty(settings, "silent_redirect_uri", {
121
+ get() {
122
+ return settings.silent_redirect_uri || settings.redirect_uri;
123
+ },
140
124
  });
125
+ expect(mockSigninRedirect).not.toHaveBeenCalled();
126
+ await client.login();
127
+ expect(mockSigninRedirect).toHaveBeenCalledTimes(1);
128
+ });
141
129
 
142
- it('token() should get a new token via signinSilent() with an expired user', async () => {
143
- const user: any = {
144
- access_token: '123',
145
- expires_in: 0,
146
- };
147
- const userTwo: any = {
148
- access_token: '456',
149
- expires_in: 10000,
150
- };
151
- mockSigninSilent.mockResolvedValueOnce(userTwo);
152
- mockGetUser.mockResolvedValueOnce(user);
153
- const token = await client.token();
154
- expect(token).toBe(userTwo.access_token);
155
- });
130
+ it('logout() should call signoutRedirect on the UserManager', () => {
131
+ const client = setupOidcClient(oidcConfig);
132
+ expect(mockSignoutRedirect).not.toHaveBeenCalled();
133
+ client.logout();
134
+ expect(mockSignoutRedirect).toHaveBeenCalledTimes(1);
135
+ });
156
136
 
157
- it('token() should reject without a user', () => {
158
- mockGetUser.mockResolvedValue(undefined);
159
- return expect(client.token()).rejects.toHaveProperty('type', OidcErrorType.notAuthorized);
160
- });
137
+ it('token() should get the token from the user manager', async () => {
138
+ const client = setupOidcClient(oidcConfig);
139
+ const token = await client.token();
140
+ expect(token).toBe(user.access_token);
141
+ });
161
142
 
162
- it('token() rejects when UserManager rejects', async () => {
163
- const e = new Error('test error');
164
- mockGetUser.mockRejectedValue(e);
165
- await expect(client.token()).rejects.toHaveProperty('type', OidcErrorType.unknown);
166
- });
143
+ it('token() should get a new token via signinSilent() with an expired user', async () => {
144
+ const client = setupOidcClient(oidcConfig);
145
+ const user: any = {
146
+ access_token: '123',
147
+ expires_in: 0,
148
+ };
149
+ const userTwo: any = {
150
+ access_token: '456',
151
+ expires_in: 10000,
152
+ };
153
+ mockSigninSilent.mockResolvedValueOnce(userTwo);
154
+ mockGetUser.mockResolvedValueOnce(user);
155
+ const token = await client.token();
156
+ expect(token).toBe(userTwo.access_token);
157
+ });
167
158
 
168
- it('token() should reject when a user does not have an access_token', () => {
169
- mockGetUser.mockResolvedValue({});
170
- mockSigninSilent.mockResolvedValue({});
171
- return expect(client.token()).rejects.toHaveProperty('type', OidcErrorType.invalidToken);
172
- });
159
+ it('token() should reject without a user', () => {
160
+ const client = setupOidcClient(oidcConfig);
161
+ mockGetUser.mockResolvedValue(undefined);
162
+ return expect(client.token()).rejects.toHaveProperty('type', OidcErrorType.notAuthorized);
163
+ });
173
164
 
174
- it('account() should return the User profile', () => {
175
- const user: any = {
176
- access_token: '123',
177
- expires_in: 100,
178
- profile: {
179
- foo: 'bar',
180
- },
181
- };
182
- mockGetUser.mockResolvedValue(user);
183
- return expect(client.account()).resolves.toBe(user.profile);
184
- });
165
+ it('token() rejects when UserManager rejects', async () => {
166
+ const client = setupOidcClient(oidcConfig);
167
+ const e = new Error('test error');
168
+ mockGetUser.mockRejectedValue(e);
169
+ await expect(client.token()).rejects.toHaveProperty('type', OidcErrorType.unknown);
170
+ });
185
171
 
186
- it('account() should reject when user is expired', () => {
187
- const user: any = {
188
- access_token: '123',
189
- expires_in: 0,
190
- profile: {
191
- foo: 'bar',
192
- },
193
- };
194
- mockGetUser.mockResolvedValue(user);
195
- return expect(client.account()).rejects.toHaveProperty('type', OidcErrorType.notAuthorized);
196
- });
172
+ it('token() should reject when a user does not have an access_token', () => {
173
+ const client = setupOidcClient(oidcConfig);
174
+ mockGetUser.mockResolvedValue({});
175
+ mockSigninSilent.mockResolvedValue({});
176
+ return expect(client.token()).rejects.toHaveProperty('type', OidcErrorType.invalidToken);
177
+ });
197
178
 
198
- it('account() should reject when user is not authenticated', () => {
199
- mockGetUser.mockResolvedValue(undefined);
200
- return expect(client.account()).rejects.toHaveProperty('type', OidcErrorType.notAuthorized);
201
- });
179
+ it('account() should return the User profile', () => {
180
+ const client = setupOidcClient(oidcConfig);
181
+ const user: any = {
182
+ access_token: '123',
183
+ expires_in: 100,
184
+ profile: {
185
+ foo: 'bar',
186
+ },
187
+ };
188
+ mockGetUser.mockResolvedValue(user);
189
+ return expect(client.account()).resolves.toBe(user.profile);
190
+ });
202
191
 
203
- it('handleAuthentication() calls signinSilentCallback when on the silent_redirect_uri path in an IFrame', async () => {
204
- setWindowInIFrame();
205
- //@ts-ignore
206
- window.location = new URL(redirectUri);
207
- expect(mockSigninSilentCallback).toBeCalledTimes(0);
208
- const { shouldRender } = await client.handleAuthentication();
209
- expect(mockSigninSilentCallback).toBeCalledTimes(1);
210
- expect(mockSigninCallback).toBeCalledTimes(0);
211
- expect(shouldRender).toBe(false);
212
- });
192
+ it('account() should reject when user is expired', () => {
193
+ const client = setupOidcClient(oidcConfig);
194
+ const user: any = {
195
+ access_token: '123',
196
+ expires_in: 0,
197
+ profile: {
198
+ foo: 'bar',
199
+ },
200
+ };
201
+ mockGetUser.mockResolvedValue(user);
202
+ return expect(client.account()).rejects.toHaveProperty('type', OidcErrorType.notAuthorized);
203
+ });
213
204
 
214
- it('handleAuthentication() calls signinCallback when on the redirect_uri path in the main viewport', async () => {
215
- setWindowInTop();
216
- //@ts-ignore
217
- window.location = new URL(redirectUri);
218
- expect(mockSigninCallback).toBeCalledTimes(0);
219
- const { shouldRender } = await client.handleAuthentication();
220
- expect(mockSigninSilentCallback).toBeCalledTimes(0);
221
- expect(mockSigninCallback).toBeCalledTimes(1);
222
- expect(shouldRender).toBe(false);
223
- });
205
+ it('account() should reject when user is not authenticated', () => {
206
+ const client = setupOidcClient(oidcConfig);
207
+ mockGetUser.mockResolvedValue(undefined);
208
+ return expect(client.account()).rejects.toHaveProperty('type', OidcErrorType.notAuthorized);
209
+ });
224
210
 
225
- it('handleAuthentication() redirects to appUri when on the redirect_uri path in the main viewport and appUri is configured', async () => {
226
- setWindowInTop();
227
- const url = new URL(redirectUri);
228
- //@ts-ignore
229
- window.location = url;
230
- expect(window.location.href).not.toBe(appUri);
231
- await client.handleAuthentication();
232
- expect(window.location.href).toBe(appUri);
233
- });
211
+ it('handleAuthentication() calls signinSilentCallback when on the silent_redirect_uri path in an IFrame', async () => {
212
+ const client = setupOidcClient(oidcConfig);
213
+ setWindowInIFrame();
214
+ //@ts-ignore
215
+ window.location = new URL(redirectUri);
216
+ expect(mockSigninSilentCallback).toBeCalledTimes(0);
217
+ const { shouldRender } = await client.handleAuthentication();
218
+ expect(mockSigninSilentCallback).toBeCalledTimes(1);
219
+ expect(mockSigninCallback).toBeCalledTimes(0);
220
+ expect(shouldRender).toBe(false);
221
+ });
234
222
 
235
- it('handleAuthentication() does not redirect to appUri when appUri is not configured', async () => {
236
- const secondClient = setupOidcClient({
237
- ...oidcConfig,
238
- appUri: undefined,
239
- });
223
+ it('handleAuthentication() calls signinCallback when on the redirect_uri path in the main viewport', async () => {
224
+ const client = setupOidcClient(oidcConfig);
225
+ setWindowInTop();
226
+ //@ts-ignore
227
+ window.location = new URL(redirectUri);
228
+ expect(mockSigninCallback).toBeCalledTimes(0);
229
+ const { shouldRender } = await client.handleAuthentication();
230
+ expect(mockSigninSilentCallback).toBeCalledTimes(0);
231
+ expect(mockSigninCallback).toBeCalledTimes(1);
232
+ expect(shouldRender).toBe(false);
233
+ });
240
234
 
241
- setWindowInTop();
242
- const url = new URL(redirectUri);
243
- //@ts-ignore
244
- window.location = url;
245
- expect(window.location.href).not.toBe(appUri);
246
- const { shouldRender } = await secondClient.handleAuthentication();
247
- expect(window.location.href).not.toBe(appUri);
248
- expect(shouldRender).toBe(true);
249
- });
235
+ it('handleAuthentication() redirects to appUri when on the redirect_uri path in the main viewport and appUri is configured', async () => {
236
+ const client = setupOidcClient(oidcConfig);
237
+ setWindowInTop();
238
+ const url = new URL(redirectUri);
239
+ //@ts-ignore
240
+ window.location = url;
241
+ expect(window.location.href).not.toBe(appUri);
242
+ await client.handleAuthentication();
243
+ expect(window.location.href).toBe(appUri);
244
+ });
250
245
 
251
- it('handleAuthentication() returns true when the user has an acess token on normal routes', async () => {
252
- const url = new URL(appUri);
253
- //@ts-ignore
254
- window.location = url;
255
- const { shouldRender } = await client.handleAuthentication();
256
- expect(shouldRender).toBe(true);
246
+ it('handleAuthentication() does not redirect to appUri when appUri is not configured', async () => {
247
+ const client = setupOidcClient(oidcConfig);
248
+ const secondClient = setupOidcClient({
249
+ ...oidcConfig,
250
+ appUri: undefined,
257
251
  });
258
252
 
259
- it('handleAuthentication() redirects to login when the user does not have an access token', async () => {
260
- const url = new URL(appUri);
261
- mockGetUser.mockResolvedValue(undefined);
262
- //@ts-ignore
263
- window.location = url;
264
- expect(mockSigninRedirect).toBeCalledTimes(0);
265
- const { shouldRender } = await client.handleAuthentication();
266
- expect(mockSigninRedirect).toBeCalledTimes(1);
267
- expect(shouldRender).toBe(false);
268
- });
253
+ setWindowInTop();
254
+ const url = new URL(redirectUri);
255
+ //@ts-ignore
256
+ window.location = url;
257
+ expect(window.location.href).not.toBe(appUri);
258
+ const { shouldRender } = await secondClient.handleAuthentication();
259
+ expect(window.location.href).not.toBe(appUri);
260
+ expect(shouldRender).toBe(true);
261
+ });
269
262
 
270
- it('handleAuthentication() rejects when token() rejects', async () => {
271
- const e = new Error('test error');
272
- mockGetUser.mockRejectedValue(e);
273
- await expect(client.handleAuthentication()).rejects.toHaveProperty('type', OidcErrorType.unknown);
274
- });
263
+ it('handleAuthentication() returns true when the user has an acess token on normal routes', async () => {
264
+ const client = setupOidcClient(oidcConfig);
265
+ const url = new URL(appUri);
266
+ //@ts-ignore
267
+ window.location = url;
268
+ const { shouldRender } = await client.handleAuthentication();
269
+ expect(shouldRender).toBe(true);
270
+ });
275
271
 
276
- it('extendHeaders() calls request.setHeaders with the authorization header when the user has a token', async () => {
277
- mockGetUser.mockResolvedValue(user);
278
- const expected = { Authorization: `Bearer ${user.access_token}` };
279
- const mockSetHeaders = jest.fn().mockResolvedValue(undefined);
280
- const req = {
281
- setHeaders: mockSetHeaders,
282
- };
283
- expect(mockSetHeaders).not.toHaveBeenCalled();
284
- await client.extendHeaders(req);
285
- expect(mockSetHeaders).toBeCalledTimes(1);
286
- const retrieveTokenPromise = mockSetHeaders.mock.calls[0][0];
287
- expect(await retrieveTokenPromise).toEqual(expected);
288
- });
272
+ it('handleAuthentication() redirects to login when the user does not have an access token', async () => {
273
+ const client = setupOidcClient(oidcConfig);
274
+ const url = new URL(appUri);
275
+ mockGetUser.mockResolvedValue(undefined);
276
+ //@ts-ignore
277
+ window.location = url;
278
+ expect(mockSigninRedirect).toBeCalledTimes(0);
279
+ const { shouldRender } = await client.handleAuthentication();
280
+ expect(mockSigninRedirect).toBeCalledTimes(1);
281
+ expect(shouldRender).toBe(false);
282
+ });
289
283
 
290
- it('extendHeaders() is a noop when `restrict` is true in configuration', async () => {
291
- const mockSetHeaders = jest.fn().mockResolvedValue(undefined);
292
- const req = {
293
- setHeaders: mockSetHeaders,
294
- };
295
- const client2 = setupOidcClient({
296
- ...oidcConfig,
297
- restrict: true,
298
- });
299
- await client2.extendHeaders(req);
300
- expect(mockSetHeaders).not.toBeCalled();
301
- });
284
+ it('handleAuthentication() rejects when token() rejects', async () => {
285
+ const client = setupOidcClient(oidcConfig);
286
+ const e = new Error('test error');
287
+ mockGetUser.mockRejectedValue(e);
288
+ await expect(client.handleAuthentication()).rejects.toHaveProperty('type', OidcErrorType.unknown);
289
+ });
302
290
 
303
- it('extendHeaders() does nothing when the user has no token', async () => {
304
- mockGetUser.mockResolvedValue(undefined);
305
- const mockSetHeaders = jest.fn().mockResolvedValue(undefined);
306
- const req = {
307
- setHeaders: mockSetHeaders,
308
- };
309
- expect(mockSetHeaders).not.toHaveBeenCalled();
310
- await client.extendHeaders(req);
311
- expect(mockSetHeaders).toBeCalledTimes(1);
312
- const retrieveTokenPromise = mockSetHeaders.mock.calls[0][0];
313
- expect(await retrieveTokenPromise).toEqual(undefined);
291
+ it('extendHeaders() calls request.setHeaders with the authorization header when the user has a token', async () => {
292
+ const client = setupOidcClient(oidcConfig);
293
+ mockGetUser.mockResolvedValue(user);
294
+ const expected = { Authorization: `Bearer ${user.access_token}` };
295
+ const mockSetHeaders = vitest.fn().mockResolvedValue(undefined);
296
+ const req = {
297
+ setHeaders: mockSetHeaders,
298
+ };
299
+ expect(mockSetHeaders).not.toHaveBeenCalled();
300
+ await client.extendHeaders(req);
301
+ expect(mockSetHeaders).toBeCalledTimes(1);
302
+ const retrieveTokenPromise = mockSetHeaders.mock.calls[0][0];
303
+ expect(await retrieveTokenPromise).toEqual(expected);
304
+ });
305
+
306
+ it('extendHeaders() is a noop when `restrict` is true in configuration', async () => {
307
+ const client = setupOidcClient(oidcConfig);
308
+ const mockSetHeaders = vitest.fn().mockResolvedValue(undefined);
309
+ const req = {
310
+ setHeaders: mockSetHeaders,
311
+ };
312
+ const client2 = setupOidcClient({
313
+ ...oidcConfig,
314
+ restrict: true,
314
315
  });
316
+ await client2.extendHeaders(req);
317
+ expect(mockSetHeaders).not.toBeCalled();
318
+ });
319
+
320
+ it('extendHeaders() does nothing when the user has no token', async () => {
321
+ const client = setupOidcClient(oidcConfig);
322
+ mockGetUser.mockResolvedValue(undefined);
323
+ const mockSetHeaders = vitest.fn().mockResolvedValue(undefined);
324
+ const req = {
325
+ setHeaders: mockSetHeaders,
326
+ };
327
+ expect(mockSetHeaders).not.toHaveBeenCalled();
328
+ await client.extendHeaders(req);
329
+ expect(mockSetHeaders).toBeCalledTimes(1);
330
+ const retrieveTokenPromise = mockSetHeaders.mock.calls[0][0];
331
+ expect(await retrieveTokenPromise).toEqual(undefined);
315
332
  });
316
333
  });