Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/microsoft-authentication/src/common/test/loopbackClientAndOpener.test.ts
3323 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 * as assert from 'assert';
7
import { env, Uri, window } from 'vscode';
8
import * as sinon from 'sinon';
9
import { UriHandlerLoopbackClient } from '../loopbackClientAndOpener';
10
import { UriEventHandler } from '../../UriEventHandler';
11
12
suite('UriHandlerLoopbackClient', () => {
13
const redirectUri = 'http://localhost';
14
let uriHandler: UriEventHandler;
15
let client: UriHandlerLoopbackClient;
16
let envStub: sinon.SinonStubbedInstance<typeof env>;
17
let callbackUri: Uri;
18
19
setup(async () => {
20
callbackUri = await env.asExternalUri(Uri.parse(`${env.uriScheme}://vscode.microsoft-authentication`));
21
envStub = sinon.stub(env);
22
envStub.openExternal.resolves(true);
23
envStub.asExternalUri.callThrough();
24
uriHandler = new UriEventHandler();
25
client = new UriHandlerLoopbackClient(uriHandler, redirectUri, window.createOutputChannel('test', { log: true }));
26
});
27
28
teardown(() => {
29
sinon.restore();
30
uriHandler.dispose();
31
});
32
33
suite('openBrowser', () => {
34
test('should open browser with correct URL', async () => {
35
const testUrl = 'http://example.com?foo=5';
36
37
await client.openBrowser(testUrl);
38
39
assert.ok(envStub.asExternalUri.calledOnce);
40
assert.ok(envStub.openExternal.calledOnce);
41
42
const expectedUri = Uri.parse(testUrl + `&state=${encodeURI(callbackUri.toString(true))}`);
43
const value = envStub.openExternal.getCalls()[0].args[0];
44
assert.strictEqual(value.toString(true), expectedUri.toString(true));
45
});
46
});
47
48
suite('getRedirectUri', () => {
49
test('should return the redirect URI', () => {
50
const result = client.getRedirectUri();
51
assert.strictEqual(result, redirectUri);
52
});
53
});
54
55
suite('listenForAuthCode', () => {
56
test('should return auth code from URL', async () => {
57
const code = '1234';
58
const state = '5678';
59
const testUrl = Uri.parse(`http://example.com?code=${code}&state=${state}`);
60
const promise = client.listenForAuthCode();
61
uriHandler.handleUri(testUrl);
62
const result = await promise;
63
64
assert.strictEqual(result.code, code);
65
assert.strictEqual(result.state, state);
66
});
67
68
test('should return auth error from URL', async () => {
69
const error = 'access_denied';
70
const errorDescription = 'reason';
71
const errorUri = 'uri';
72
const testUrl = Uri.parse(`http://example.com?error=${error}&error_description=${errorDescription}&error_uri=${errorUri}`);
73
74
const promise = client.listenForAuthCode();
75
uriHandler.handleUri(testUrl);
76
const result = await promise;
77
78
assert.strictEqual(result.error, 'access_denied');
79
assert.strictEqual(result.error_description, 'reason');
80
assert.strictEqual(result.error_uri, 'uri');
81
});
82
});
83
});
84
85