Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anemy committed Jan 14, 2024
1 parent 2eb96e0 commit a862484
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ function isOIDCAuth(connectionString: string): boolean {
return authMechanismString === 'MONGODB-OIDC';
}

function getNotifyDeviceFlowForConnectionAttempt(
// Exported for testing.
export function getNotifyDeviceFlowForConnectionAttempt(
connectionOptions: ConnectionOptions
) {
const isOIDCConnectionAttempt = isOIDCAuth(
Expand Down
47 changes: 46 additions & 1 deletion src/test/suite/connectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import * as vscode from 'vscode';
import { afterEach, beforeEach } from 'mocha';
import assert from 'assert';
import * as mongodbDataService from 'mongodb-data-service';
import ConnectionString from 'mongodb-connection-string-url';

import ConnectionController, {
DataServiceEventTypes,
getNotifyDeviceFlowForConnectionAttempt,
} from '../../connectionController';
import formatError from '../../utils/formatError';
import { StorageController, StorageVariables } from '../../storage';
Expand Down Expand Up @@ -53,10 +55,14 @@ suite('Connection Controller Test Suite', function () {
telemetryService: testTelemetryService,
});
let showErrorMessageStub: SinonStub;
let showInformationMessageStub: SinonStub;
const sandbox = sinon.createSandbox();

beforeEach(() => {
sandbox.stub(vscode.window, 'showInformationMessage');
showInformationMessageStub = sandbox.stub(
vscode.window,
'showInformationMessage'
);
sandbox.stub(testTelemetryService, 'trackNewConnection');
showErrorMessageStub = sandbox.stub(vscode.window, 'showErrorMessage');
});
Expand Down Expand Up @@ -462,6 +468,45 @@ suite('Connection Controller Test Suite', function () {
assert.strictEqual(JSON.stringify(workspaceStoreConnections), objectString);
});

test('getNotifyDeviceFlowForConnectionAttempt returns a function that shows a message with the url when oidc is set', function () {
const expectedUndefinedDeviceFlow = getNotifyDeviceFlowForConnectionAttempt(
{
connectionString: TEST_DATABASE_URI,
}
);

assert.strictEqual(expectedUndefinedDeviceFlow, undefined);

const oidcConnectionString = new ConnectionString(TEST_DATABASE_URI);
oidcConnectionString.searchParams.set('authMechanism', 'MONGODB-OIDC');

const expectedFunction = getNotifyDeviceFlowForConnectionAttempt({
connectionString: oidcConnectionString.toString(),
});
assert.notStrictEqual(expectedFunction, undefined);
assert.strictEqual(showInformationMessageStub.called, false);

(
expectedFunction as (deviceFlowInformation: {
verificationUrl: string;
userCode: string;
}) => void
)({
verificationUrl: 'test123',
userCode: 'testabc',
});

assert.strictEqual(showInformationMessageStub.called, true);
assert.strictEqual(
showInformationMessageStub.firstCall.args[0].includes('test123'),
true
);
assert.strictEqual(
showInformationMessageStub.firstCall.args[0].includes('testabc'),
true
);
});

test('when a connection is removed it is also removed from workspace store', async () => {
await testConnectionController.loadSavedConnections();
await vscode.workspace
Expand Down
62 changes: 61 additions & 1 deletion src/test/suite/views/webviewController.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sinon from 'sinon';
import * as vscode from 'vscode';
import assert from 'assert';
import { beforeEach, afterEach } from 'mocha';
import { before, after, beforeEach, afterEach } from 'mocha';
import fs from 'fs';
import path from 'path';

Expand Down Expand Up @@ -127,6 +127,66 @@ suite('Webview Test Suite', () => {
);
});

test('web view content sets the oidc device auth id globally', () => {
const fakeWebview: any = {
asWebviewUri: (jsUri) => {
return jsUri;
},
};

const extensionPath = mdbTestExtension.extensionContextStub.extensionPath;
const htmlString = getWebviewContent({
extensionPath,
telemetryUserId: 'test',
webview: fakeWebview,
});

assert(
htmlString.includes(
">window['VSCODE_EXTENSION_OIDC_DEVICE_AUTH_ID'] = false;"
)
);
});

suite('when oidc device auth flow setting is enabled', function () {
let originalDeviceAuthFlow;
before(async function () {
originalDeviceAuthFlow = vscode.workspace.getConfiguration(
'mdb.showOIDCDeviceAuthFlow'
);

await vscode.workspace
.getConfiguration('mdb')
.update('showOIDCDeviceAuthFlow', true);
});
after(async function () {
await vscode.workspace
.getConfiguration('mdb')
.update('showOIDCDeviceAuthFlow', originalDeviceAuthFlow);
});

test('web view content sets the oidc device auth id globally', () => {
const fakeWebview: any = {
asWebviewUri: (jsUri) => {
return jsUri;
},
};

const extensionPath = mdbTestExtension.extensionContextStub.extensionPath;
const htmlString = getWebviewContent({
extensionPath,
telemetryUserId: 'test',
webview: fakeWebview,
});

assert(
htmlString.includes(
">window['VSCODE_EXTENSION_OIDC_DEVICE_AUTH_ID'] = true;"
)
);
});
});

test('web view listens for a connect message and adds the connection', (done) => {
const extensionContextStub = new ExtensionContextStub();
const testStorageController = new StorageController(extensionContextStub);
Expand Down

0 comments on commit a862484

Please sign in to comment.