Path: blob/main/extensions/microsoft-authentication/src/common/test/loopbackClientAndOpener.test.ts
3323 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, 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);3738assert.ok(envStub.asExternalUri.calledOnce);39assert.ok(envStub.openExternal.calledOnce);4041const expectedUri = Uri.parse(testUrl + `&state=${encodeURI(callbackUri.toString(true))}`);42const value = envStub.openExternal.getCalls()[0].args[0];43assert.strictEqual(value.toString(true), expectedUri.toString(true));44});45});4647suite('getRedirectUri', () => {48test('should return the redirect URI', () => {49const result = client.getRedirectUri();50assert.strictEqual(result, redirectUri);51});52});5354suite('listenForAuthCode', () => {55test('should return auth code from URL', async () => {56const code = '1234';57const state = '5678';58const testUrl = Uri.parse(`http://example.com?code=${code}&state=${state}`);59const promise = client.listenForAuthCode();60uriHandler.handleUri(testUrl);61const result = await promise;6263assert.strictEqual(result.code, code);64assert.strictEqual(result.state, state);65});6667test('should return auth error from URL', async () => {68const error = 'access_denied';69const errorDescription = 'reason';70const errorUri = 'uri';71const testUrl = Uri.parse(`http://example.com?error=${error}&error_description=${errorDescription}&error_uri=${errorUri}`);7273const promise = client.listenForAuthCode();74uriHandler.handleUri(testUrl);75const result = await promise;7677assert.strictEqual(result.error, 'access_denied');78assert.strictEqual(result.error_description, 'reason');79assert.strictEqual(result.error_uri, 'uri');80});81});82});838485