Path: blob/main/extensions/microsoft-authentication/src/common/test/loopbackClientAndOpener.test.ts
5237 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as assert from 'assert';6import { env, Uri, window } from 'vscode';7import * as sinon from 'sinon';8import { UriHandlerLoopbackClient } from '../loopbackClientAndOpener';9import { UriEventHandler } from '../../UriEventHandler';1011suite('UriHandlerLoopbackClient', () => {12const redirectUri = 'http://localhost';13let uriHandler: UriEventHandler;14let client: UriHandlerLoopbackClient;15let envStub: sinon.SinonStubbedInstance<typeof env>;16let callbackUri: Uri;1718setup(async () => {19callbackUri = await env.asExternalUri(Uri.parse(`${env.uriScheme}://vscode.microsoft-authentication`));20envStub = sinon.stub(env);21envStub.openExternal.resolves(true);22envStub.asExternalUri.callThrough();23uriHandler = new UriEventHandler();24client = new UriHandlerLoopbackClient(uriHandler, redirectUri, callbackUri, window.createOutputChannel('test', { log: true }));25});2627teardown(() => {28sinon.restore();29uriHandler.dispose();30});3132suite('openBrowser', () => {33test('should open browser with correct URL', async () => {34const testUrl = 'http://example.com?foo=5';3536await client.openBrowser(testUrl);37assert.ok(envStub.openExternal.calledOnce);3839const expectedUri = Uri.parse(testUrl + `&state=${encodeURI(callbackUri.toString(true))}`);40const value = envStub.openExternal.getCalls()[0].args[0];41assert.strictEqual(value.toString(true), expectedUri.toString(true));42});43});4445suite('getRedirectUri', () => {46test('should return the redirect URI', () => {47const result = client.getRedirectUri();48assert.strictEqual(result, redirectUri);49});50});5152// Skipped for now until `listenForAuthCode` is refactored to not show quick pick53suite('listenForAuthCode', () => {54test('should return auth code from URL', async () => {55const code = '1234';56const state = '5678';57const testUrl = Uri.parse(`http://example.com?code=${code}&state=${state}`);58const promise = client.listenForAuthCode();59uriHandler.handleUri(testUrl);60const result = await promise;6162assert.strictEqual(result.code, code);63assert.strictEqual(result.state, state);64});6566test('should return auth error from URL', async () => {67const error = 'access_denied';68const errorDescription = 'reason';69const errorUri = 'uri';70const testUrl = Uri.parse(`http://example.com?error=${error}&error_description=${errorDescription}&error_uri=${errorUri}`);7172const promise = client.listenForAuthCode();73uriHandler.handleUri(testUrl);74const result = await promise;7576assert.strictEqual(result.error, 'access_denied');77assert.strictEqual(result.error_description, 'reason');78assert.strictEqual(result.error_uri, 'uri');79});80});81});828384