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
5237 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, callbackUri, 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
assert.ok(envStub.openExternal.calledOnce);
39
40
const expectedUri = Uri.parse(testUrl + `&state=${encodeURI(callbackUri.toString(true))}`);
41
const value = envStub.openExternal.getCalls()[0].args[0];
42
assert.strictEqual(value.toString(true), expectedUri.toString(true));
43
});
44
});
45
46
suite('getRedirectUri', () => {
47
test('should return the redirect URI', () => {
48
const result = client.getRedirectUri();
49
assert.strictEqual(result, redirectUri);
50
});
51
});
52
53
// Skipped for now until `listenForAuthCode` is refactored to not show quick pick
54
suite('listenForAuthCode', () => {
55
test('should return auth code from URL', async () => {
56
const code = '1234';
57
const state = '5678';
58
const testUrl = Uri.parse(`http://example.com?code=${code}&state=${state}`);
59
const promise = client.listenForAuthCode();
60
uriHandler.handleUri(testUrl);
61
const result = await promise;
62
63
assert.strictEqual(result.code, code);
64
assert.strictEqual(result.state, state);
65
});
66
67
test('should return auth error from URL', async () => {
68
const error = 'access_denied';
69
const errorDescription = 'reason';
70
const errorUri = 'uri';
71
const testUrl = Uri.parse(`http://example.com?error=${error}&error_description=${errorDescription}&error_uri=${errorUri}`);
72
73
const promise = client.listenForAuthCode();
74
uriHandler.handleUri(testUrl);
75
const result = await promise;
76
77
assert.strictEqual(result.error, 'access_denied');
78
assert.strictEqual(result.error_description, 'reason');
79
assert.strictEqual(result.error_uri, 'uri');
80
});
81
});
82
});
83
84