-
Notifications
You must be signed in to change notification settings - Fork 268
/
authentication.spec.ts
344 lines (307 loc) · 13.4 KB
/
authentication.spec.ts
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**********************************************************************
* Copyright (C) 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import type {
AuthenticationProvider,
AuthenticationProviderAuthenticationSessionsChangeEvent,
AuthenticationSession,
AuthenticationSessionAccountInformation,
Event,
} from '@podman-desktop/api';
import { beforeEach, afterEach, expect, test, vi, suite } from 'vitest';
import type { ApiSenderType } from './api.js';
import { AuthenticationImpl } from './authentication.js';
import type { CommandRegistry } from './command-registry.js';
import type { ConfigurationRegistry } from './configuration-registry.js';
import type { ContainerProviderRegistry } from './container-registry.js';
import { Emitter as EventEmitter } from './events/emitter.js';
import { ExtensionLoader } from './extension-loader.js';
import type { FilesystemMonitoring } from './filesystem-monitoring.js';
import type { ImageRegistry } from './image-registry.js';
import type { InputQuickPickRegistry } from './input-quickpick/input-quickpick-registry.js';
import type { KubernetesClient } from './kubernetes-client.js';
import type { MenuRegistry } from './menu-registry.js';
import type { MessageBox } from './message-box.js';
import type { NotificationImpl } from './notification-impl.js';
import type { ProgressImpl } from './progress-impl.js';
import type { ProviderRegistry } from './provider-registry.js';
import type { StatusBarRegistry } from './statusbar/statusbar-registry.js';
import type { Telemetry } from './telemetry/telemetry.js';
import type { TrayMenuRegistry } from './tray-menu-registry.js';
import type { Proxy } from './proxy.js';
import type { IconRegistry } from './icon-registry.js';
import type { Directories } from './directories.js';
import type { CustomPickRegistry } from './custompick/custompick-registry.js';
import type { ViewRegistry } from './view-registry.js';
import type { ContextRegistry } from './context-registry.js';
function randomNumber(n = 5) {
return Math.round(Math.random() * 10 * n);
}
class RandomAuthenticationSession implements AuthenticationSession {
id: string;
accessToken: string;
account: AuthenticationSessionAccountInformation;
constructor(public readonly scopes: readonly string[]) {
this.id = `id${randomNumber()}`;
this.accessToken = `accessToken${randomNumber()}`;
this.account = {
id: `${randomNumber()}`,
label: `label${randomNumber()}`,
};
}
}
class AuthenticationProviderSingleAccount implements AuthenticationProvider {
private _onDidChangeSession = new EventEmitter<AuthenticationProviderAuthenticationSessionsChangeEvent>();
private session: AuthenticationSession | undefined;
onDidChangeSessions: Event<AuthenticationProviderAuthenticationSessionsChangeEvent> = this._onDidChangeSession.event;
async getSessions(scopes?: string[]): Promise<readonly AuthenticationSession[]> {
if (scopes) {
return this.session ? [this.session] : [];
}
return this.session ? [this.session] : [];
}
async createSession(scopes: string[]): Promise<AuthenticationSession> {
this.session = new RandomAuthenticationSession(scopes);
return this.session;
}
async removeSession(): Promise<void> {
this.session = undefined;
}
}
const apiSender: ApiSenderType = {
send: vi.fn(),
receive: vi.fn(),
};
let authModule: AuthenticationImpl;
const directories = {
getPluginsDirectory: () => '/fake-plugins-directory',
getPluginsScanDirectory: () => '/fake-plugins-scanning-directory',
getExtensionsStorageDirectory: () => '/fake-extensions-storage-directory',
} as unknown as Directories;
beforeEach(function () {
authModule = new AuthenticationImpl(apiSender);
});
test('Registered authentication provider stored in authentication module', async () => {
const authProvidrer1 = new AuthenticationProviderSingleAccount();
authModule.registerAuthenticationProvider('company.auth-provider', 'Provider 1', authProvidrer1);
const providersInfo = await authModule.getAuthenticationProvidersInfo();
expect(providersInfo).length(1, 'Provider was not registered');
});
test('Session request with option silent===false does not fail if there is no provider with requested ID', async () => {
const err = await authModule
.getSession({ id: 'ext1', label: 'Ext 1' }, 'company.auth-provider', ['scope1', 'scope2'], { silent: false })
.catch((err: unknown) => Promise.resolve(err));
expect(err).toBeUndefined();
});
test('Authentication provider does not creates session when silent options is false', async () => {
const authProvidrer1 = new AuthenticationProviderSingleAccount();
authModule.registerAuthenticationProvider('company.auth-provider', 'Provider 1', authProvidrer1);
const sessions = await authModule.getSession(
{ id: 'ext1', label: 'Ext 1' },
'company.auth-provider',
['scope1', 'scope2'],
{ silent: false },
);
expect(sessions).toBeUndefined();
});
test('Authentication creates new auth request when silent is true and session for requested provider does not exist', async () => {
const authProvidrer1 = new AuthenticationProviderSingleAccount();
authModule.registerAuthenticationProvider('company.auth-provider', 'Provider 1', authProvidrer1);
const session2 = await authModule.getSession(
{ id: 'ext2', label: 'Ext 2' },
'company.auth-provider',
['scope1', 'scope2'],
{ silent: false },
);
expect(session2).toBeUndefined();
expect(authModule.getSessionRequests()).length(1);
});
test('Authentication does not create new auth request when silent is true and session exists', async () => {
const authProvidrer1 = new AuthenticationProviderSingleAccount();
authModule.registerAuthenticationProvider('company.auth-provider', 'Provider 1', authProvidrer1);
const session1 = await authModule.getSession(
{ id: 'ext1', label: 'Ext 2' },
'company.auth-provider',
['scope1', 'scope2'],
{ createIfNone: true },
);
expect(session1).toBeDefined();
const session2 = await authModule.getSession(
{ id: 'ext2', label: 'Ext 2' },
'company.auth-provider',
['scope1', 'scope2'],
{ silent: false },
);
expect(session2).toBeDefined();
expect(authModule.getSessionRequests()).length(0);
});
test('Authentication creates one auth request per extension', async () => {
const authProvidrer1 = new AuthenticationProviderSingleAccount();
authModule.registerAuthenticationProvider('company.auth-provider', 'Provider 1', authProvidrer1);
const session1 = await authModule.getSession(
{ id: 'ext1', label: 'Ext 2' },
'company.auth-provider',
['scope1', 'scope2'],
{ silent: false },
);
expect(session1).toBeUndefined();
const session2 = await authModule.getSession(
{ id: 'ext2', label: 'Ext 2' },
'company.auth-provider',
['scope1', 'scope2'],
{ silent: false },
);
expect(session2).toBeUndefined();
expect(authModule.getSessionRequests()).length(2);
});
test('Authentication does not creates auth request when request for the same extension and scopes exists', async () => {
const authProvidrer1 = new AuthenticationProviderSingleAccount();
authModule.registerAuthenticationProvider('company.auth-provider', 'Provider 1', authProvidrer1);
const session1 = await authModule.getSession(
{ id: 'ext1', label: 'Ext 2' },
'company.auth-provider',
['scope1', 'scope2'],
{ silent: false },
);
expect(session1).toBeUndefined();
const session2 = await authModule.getSession(
{ id: 'ext1', label: 'Ext 1' },
'company.auth-provider',
['scope1', 'scope2'],
{ silent: false },
);
expect(session2).toBeUndefined();
expect(authModule.getSessionRequests()).length(1);
});
test('Authentication provider creates session when session request is executed', async () => {
const authProvidrer1 = new AuthenticationProviderSingleAccount();
const createSessionSpy = vi.spyOn(authProvidrer1, 'createSession');
authModule.registerAuthenticationProvider('company.auth-provider', 'Provider 1', authProvidrer1);
await authModule.getSession({ id: 'ext1', label: 'Ext 1' }, 'company.auth-provider', ['scope1', 'scope2'], {
silent: false,
});
expect(authModule.getSessionRequests()).length(1);
const [signInRequest] = authModule.getSessionRequests();
await authModule.executeSessionRequest(signInRequest.id);
expect(createSessionSpy).toBeCalledTimes(1);
});
suite('Authentication', () => {
let extLoader: ExtensionLoader;
let authentication: AuthenticationImpl;
let providerMock: AuthenticationProvider;
beforeEach(() => {
authentication = new AuthenticationImpl(apiSender);
extLoader = new ExtensionLoader(
vi.fn() as unknown as CommandRegistry,
vi.fn() as unknown as MenuRegistry,
vi.fn() as unknown as ProviderRegistry,
vi.fn() as unknown as ConfigurationRegistry,
vi.fn() as unknown as ImageRegistry,
vi.fn() as unknown as ApiSenderType,
vi.fn() as unknown as TrayMenuRegistry,
vi.fn() as unknown as MessageBox,
vi.fn() as unknown as ProgressImpl,
vi.fn() as unknown as NotificationImpl,
vi.fn() as unknown as StatusBarRegistry,
vi.fn() as unknown as KubernetesClient,
vi.fn() as unknown as FilesystemMonitoring,
vi.fn() as unknown as Proxy,
vi.fn() as unknown as ContainerProviderRegistry,
vi.fn() as unknown as InputQuickPickRegistry,
vi.fn() as unknown as CustomPickRegistry,
authentication,
vi.fn() as unknown as IconRegistry,
vi.fn() as unknown as Telemetry,
vi.fn() as unknown as ContextRegistry,
vi.fn() as unknown as ViewRegistry,
directories,
);
providerMock = {
onDidChangeSessions: vi.fn(),
getSessions: vi.fn().mockResolvedValue([]),
createSession: vi.fn(),
removeSession: vi.fn(),
};
});
afterEach(() => {
vi.resetAllMocks();
});
const BASE64ENCODEDIMAGE = 'BASE64ENCODEDIMAGE';
test('allows images option to be undefined or empty', async () => {
vi.spyOn(extLoader, 'getBase64Image').mockImplementation(() => BASE64ENCODEDIMAGE);
const api = extLoader.createApi('/path', {});
expect(api).toBeDefined();
api.authentication.registerAuthenticationProvider('provider1.id', 'Provider1 Label', providerMock);
let providers = await authentication.getAuthenticationProvidersInfo();
const provider1 = providers.find(item => item.id === 'provider1.id');
expect(provider1).toBeDefined();
expect(provider1?.images).toBeUndefined();
api.authentication.registerAuthenticationProvider('provider2.id', 'Provider2 Label', providerMock, {
images: {},
});
providers = await authentication.getAuthenticationProvidersInfo();
const provider2 = providers.find(item => item.id === 'provider2.id');
expect(provider2).toBeDefined();
expect(provider2?.images).toBeDefined();
expect(provider2?.images?.logo).toBeUndefined();
expect(provider2?.images?.icon).toBeUndefined();
});
test('converts images.icon path to base 64 image when registering provider', async () => {
vi.spyOn(extLoader, 'getBase64Image').mockImplementation(() => BASE64ENCODEDIMAGE);
const api = extLoader.createApi('/path', {});
expect(api).toBeDefined();
api.authentication.registerAuthenticationProvider('provider1.id', 'Provider1 Label', providerMock, {
images: {
icon: './image.png',
logo: './image.png',
},
});
const providers = await authentication.getAuthenticationProvidersInfo();
const provider = providers.find(item => item.id === 'provider1.id');
expect(provider).toBeDefined();
expect(provider?.images?.icon).equals(BASE64ENCODEDIMAGE);
expect(provider?.images?.icon).equals(BASE64ENCODEDIMAGE);
});
test('converts images.icon with themes path to base 64 image when registering provider', async () => {
vi.spyOn(extLoader, 'getBase64Image').mockImplementation(() => BASE64ENCODEDIMAGE);
const api = extLoader.createApi('/path', {});
expect(api).toBeDefined();
api.authentication.registerAuthenticationProvider('provider2.id', 'Provider2 Label', providerMock, {
images: {
icon: {
light: './image.png',
dark: './image.png',
},
logo: {
light: './image.png',
dark: './image.png',
},
},
});
const providers = await authentication.getAuthenticationProvidersInfo();
const provider = providers.find(item => item.id === 'provider2.id');
expect(provider).toBeDefined();
const themeIcon = typeof provider?.images?.icon === 'string' ? undefined : provider?.images?.icon;
expect(themeIcon).toBeDefined();
expect(themeIcon?.light).equals(BASE64ENCODEDIMAGE);
expect(themeIcon?.dark).equals(BASE64ENCODEDIMAGE);
const themeLogo = typeof provider?.images?.logo === 'string' ? undefined : provider?.images?.logo;
expect(themeLogo).toBeDefined();
expect(themeLogo?.light).equals(BASE64ENCODEDIMAGE);
expect(themeLogo?.dark).equals(BASE64ENCODEDIMAGE);
});
});