Skip to content

Instantly share code, notes, and snippets.

@TylerLeonhardt
Created October 16, 2021 01:35
Show Gist options
  • Save TylerLeonhardt/e164c158b4aaf25f0081e8534d5e0268 to your computer and use it in GitHub Desktop.
Save TylerLeonhardt/e164c158b4aaf25f0081e8534d5e0268 to your computer and use it in GitHub Desktop.
tests I'm trying to get working
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { TestDialogService } from 'vs/platform/dialogs/test/common/testDialogService';
import { ExtensionIdentifier, IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { TestNotificationService } from 'vs/platform/notification/test/common/testNotificationService';
import { IQuickInputHideEvent, IQuickInputService, IQuickPickDidAcceptEvent } from 'vs/platform/quickinput/common/quickInput';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
import { MainThreadAuthentication } from 'vs/workbench/api/browser/mainThreadAuthentication';
import { MainContext } from 'vs/workbench/api/common/extHost.protocol';
import { ExtHostAuthentication } from 'vs/workbench/api/common/extHostAuthentication';
import { IActivityService } from 'vs/workbench/services/activity/common/activity';
import { AuthenticationService, IAuthenticationService } from 'vs/workbench/services/authentication/browser/authenticationService';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
import { TestRemoteAgentService } from 'vs/workbench/services/remote/test/common/testServices';
import { TestRPCProtocol } from 'vs/workbench/test/browser/api/testRPCProtocol';
import { TestQuickInputService } from 'vs/workbench/test/browser/workbenchTestServices';
import { TestActivityService, TestExtensionService, TestStorageService } from 'vs/workbench/test/common/workbenchTestServices';
import type { AuthenticationProvider, AuthenticationProviderAuthenticationSessionsChangeEvent, AuthenticationSession, Event } from 'vscode';
class AuthQuickPick {
private listener: ((e: IQuickPickDidAcceptEvent) => any) | undefined;
public items = [];
public get selectedItems(): string[] {
return this.items;
}
onDidAccept(listener: (e: IQuickPickDidAcceptEvent) => any) {
this.listener = listener;
}
onDidHide(listener: (e: IQuickInputHideEvent) => any) {
}
dispose() {
}
show() {
this.listener!({
inBackground: false
});
}
}
class AuthTestQuickInputService extends TestQuickInputService {
override createQuickPick() {
return <any>new AuthQuickPick();
}
}
class TestAuthProvider implements AuthenticationProvider {
private sessions = new Map<string, AuthenticationSession>();
// private _onDidChangeSessions: EventEmitter<AuthenticationProviderAuthenticationSessionsChangeEvent> = new EventEmitter<AuthenticationProviderAuthenticationSessionsChangeEvent>();
onDidChangeSessions: Event<AuthenticationProviderAuthenticationSessionsChangeEvent> = () => { return { dispose() { } }; }; //= this._onDidChangeSessions.event;
async getSessions(scopes?: readonly string[]): Promise<AuthenticationSession[]> {
if (!scopes) {
return [...this.sessions.values()];
}
const sessions = this.sessions.get(scopes.join(' '));
return sessions ? [sessions] : [];
}
async createSession(scopes: readonly string[]): Promise<AuthenticationSession> {
const scopesStr = scopes.join(' ');
const session = {
scopes,
id: scopesStr,
account: {
label: scopesStr,
id: scopesStr,
},
accessToken: scopesStr,
};
this.sessions.set(scopesStr, session);
return session;
}
async removeSession(sessionId: string): Promise<void> {
this.sessions.delete(sessionId);
}
}
suite('ExtHostAuthentication', () => {
const disposables: DisposableStore = new DisposableStore();
let nullExtensionDescription: IExtensionDescription = {
identifier: new ExtensionIdentifier('nullExtensionDescription'),
name: 'Null Extension Description',
publisher: 'vscode',
enableProposedApi: false,
engines: undefined!,
extensionLocation: undefined!,
isBuiltin: false,
isUserBuiltin: false,
isUnderDevelopment: false,
version: undefined!
};
let extHostAuthentication: ExtHostAuthentication;
let instantiationService: TestInstantiationService;
suiteSetup(async () => {
instantiationService = new TestInstantiationService();
// extHostContext: IExtHostContext,
instantiationService.stub(IDialogService, new TestDialogService());
instantiationService.stub(IStorageService, new TestStorageService());
instantiationService.stub(IQuickInputService, new AuthTestQuickInputService());
instantiationService.stub(IExtensionService, new TestExtensionService());
instantiationService.stub(IActivityService, new TestActivityService());
instantiationService.stub(IRemoteAgentService, new TestRemoteAgentService());
instantiationService.stub(INotificationService, new TestNotificationService());
instantiationService.stub(ITelemetryService, NullTelemetryService);
const rpcProtocol = new TestRPCProtocol();
instantiationService.stub(IAuthenticationService, instantiationService.createInstance(AuthenticationService));
rpcProtocol.set(MainContext.MainThreadAuthentication, instantiationService.createInstance(MainThreadAuthentication, rpcProtocol));
extHostAuthentication = new ExtHostAuthentication(rpcProtocol);
});
setup(async () => {
disposables.add(extHostAuthentication.registerAuthenticationProvider('test', 'test provider', new TestAuthProvider()));
});
teardown(() => {
disposables.dispose();
});
test('createIfNone - true', async () => {
const session = await extHostAuthentication.getSession(
nullExtensionDescription,
'test',
['foo'],
{
createIfNone: true
});
assert.strictEqual(session?.id, 'test');
assert.strictEqual(session?.scopes[0], 'foo');
});
test('createIfNone - false', async () => {
const nosession = await extHostAuthentication.getSession(
nullExtensionDescription,
'test',
['foo'],
{});
assert.strictEqual(nosession, undefined);
// Now create the session
const session = await extHostAuthentication.getSession(
nullExtensionDescription,
'test',
['foo'],
{
createIfNone: true
});
assert.strictEqual(session?.id, 'test');
assert.strictEqual(session?.scopes[0], 'foo');
const session2 = await extHostAuthentication.getSession(
nullExtensionDescription,
'test',
['foo'],
{});
assert.strictEqual(session.id, session2?.id);
assert.strictEqual(session.scopes[0], session2?.scopes[0]);
assert.notStrictEqual(session.accessToken, session2?.accessToken);
});
// should behave the same as createIfNone: false
test('silent - true', async () => {
const nosession = await extHostAuthentication.getSession(
nullExtensionDescription,
'test',
['foo'],
{
silent: true
});
assert.strictEqual(nosession, undefined);
// Now create the session
const session = await extHostAuthentication.getSession(
nullExtensionDescription,
'test',
['foo'],
{
createIfNone: true
});
assert.strictEqual(session?.id, 'test');
assert.strictEqual(session?.scopes[0], 'foo');
const session2 = await extHostAuthentication.getSession(
nullExtensionDescription,
'test',
['foo'],
{
silent: true
});
assert.strictEqual(session.id, session2?.id);
assert.strictEqual(session.scopes[0], session2?.scopes[0]);
assert.notStrictEqual(session.accessToken, session2?.accessToken);
});
//#region error cases
test('forceNewSession with no sessions', async () => {
try {
await extHostAuthentication.getSession(
nullExtensionDescription,
'test',
['foo'],
{
forceNewSession: true
});
assert.fail('should have thrown an Error.');
} catch (e) {
assert.strictEqual(e.message, 'No existing sessions found.');
}
});
test('createIfNone and forceNewSession', async () => {
try {
await extHostAuthentication.getSession(
nullExtensionDescription,
'test',
['foo'],
{
createIfNone: true,
forceNewSession: true
});
assert.fail('should have thrown an Error.');
} catch (e) {
assert.ok(e);
}
});
test('forceNewSession and silent', async () => {
try {
await extHostAuthentication.getSession(
nullExtensionDescription,
'test',
['foo'],
{
forceNewSession: true,
silent: true
});
assert.fail('should have thrown an Error.');
} catch (e) {
assert.ok(e);
}
});
test('createIfNone and silent', async () => {
try {
await extHostAuthentication.getSession(
nullExtensionDescription,
'test',
['foo'],
{
createIfNone: true,
silent: true
});
assert.fail('should have thrown an Error.');
} catch (e) {
assert.ok(e);
}
});
//#endregion
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment