Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/authentication/test/node/authentication.spec.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { afterEach, beforeEach, expect, suite, test } from 'vitest';
7
import { Event } from '../../../../util/vs/base/common/event';
8
import { DisposableStore } from '../../../../util/vs/base/common/lifecycle';
9
import { IConfigurationService } from '../../../configuration/common/configurationService';
10
import { ICAPIClientService } from '../../../endpoint/common/capiClient';
11
import { IDomainService } from '../../../endpoint/common/domainService';
12
import { IEnvService } from '../../../env/common/envService';
13
import { ILogService } from '../../../log/common/logService';
14
import { IFetcherService } from '../../../networking/common/fetcherService';
15
import { ITelemetryService } from '../../../telemetry/common/telemetry';
16
import { createPlatformServices } from '../../../test/node/services';
17
import { StaticGitHubAuthenticationService } from '../../common/staticGitHubAuthenticationService';
18
import { CopilotToken, createTestExtendedTokenInfo } from '../../common/copilotToken';
19
import { ICopilotTokenStore } from '../../common/copilotTokenStore';
20
import { FixedCopilotTokenManager } from '../../node/copilotTokenManager';
21
22
suite('AuthenticationService', function () {
23
let disposables: DisposableStore;
24
// These will be used to test the authentication service, but eventually these will
25
// be folded into the authentication service itself.
26
let copilotTokenManager: FixedCopilotTokenManager;
27
let authenticationService: StaticGitHubAuthenticationService;
28
29
const testToken = 'tid=test';
30
31
beforeEach(async () => {
32
disposables = new DisposableStore();
33
const accessor = disposables.add(createPlatformServices().createTestingAccessor());
34
copilotTokenManager = new FixedCopilotTokenManager(
35
testToken,
36
accessor.get(ILogService),
37
accessor.get(ITelemetryService),
38
accessor.get(ICAPIClientService),
39
accessor.get(IDomainService),
40
accessor.get(IFetcherService),
41
accessor.get(IEnvService)
42
);
43
authenticationService = new StaticGitHubAuthenticationService(
44
() => testToken,
45
accessor.get(ILogService),
46
accessor.get(ICopilotTokenStore),
47
copilotTokenManager,
48
accessor.get(IConfigurationService)
49
);
50
disposables.add(authenticationService);
51
});
52
53
afterEach(() => {
54
disposables.dispose();
55
});
56
57
test('Can get anyGitHubToken', async () => {
58
const token = await authenticationService.getGitHubSession('any', { silent: true });
59
expect(token?.accessToken).toBe(testToken);
60
expect(authenticationService.anyGitHubSession?.accessToken).toBe(testToken);
61
});
62
63
test('Can get permissiveGitHubToken', async () => {
64
const token = await authenticationService.getGitHubSession('permissive', { silent: true });
65
expect(token?.accessToken).toBe(testToken);
66
expect(authenticationService.permissiveGitHubSession?.accessToken).toBe(testToken);
67
});
68
69
test('Can get copilotToken', async () => {
70
const token = await authenticationService.getCopilotToken();
71
expect(token.token).toBe(testToken);
72
expect(authenticationService.copilotToken?.token).toBe(testToken);
73
});
74
75
test('Emits onDidAuthenticationChange when a Copilot Token change is notified', async () => {
76
const promise = Event.toPromise(authenticationService.onDidAuthenticationChange);
77
const newToken = 'tid=new';
78
authenticationService.setCopilotToken(new CopilotToken(createTestExtendedTokenInfo({
79
token: newToken,
80
username: 'fake',
81
copilot_plan: 'unknown',
82
})));
83
await promise;
84
expect(authenticationService.copilotToken?.token).toBe(newToken);
85
});
86
87
test.skip('Emits onDidAuthenticationChange when a Copilot Token change is notified from the manager', async () => {
88
const promise = Event.toPromise(authenticationService.onDidAuthenticationChange);
89
const newToken = 'tid=new';
90
copilotTokenManager.completionsToken = newToken;
91
await promise;
92
expect(authenticationService.copilotToken?.token).toBe(newToken);
93
});
94
});
95
96