|
1 | 1 | import * as assert from 'assert';
|
2 | 2 | import * as vscode from 'vscode';
|
3 |
| -import { before, after } from 'mocha'; |
| 3 | +import { beforeEach, afterEach } from 'mocha'; |
4 | 4 | import * as sinon from 'sinon';
|
5 |
| - |
| 5 | +import Connection = require('mongodb-connection-model/lib/model'); |
6 | 6 | import MDBExtensionController from '../../mdbExtensionController';
|
7 |
| - |
8 | 7 | import { TestExtensionContext } from './stubs';
|
9 |
| -import { TEST_DATABASE_URI } from './dbTestHelper'; |
10 |
| -import ConnectionController from '../../connectionController'; |
11 |
| -import { StorageController } from '../../storage'; |
12 |
| -import { StatusView } from '../../views'; |
13 |
| -import TelemetryController from '../../telemetry/telemetryController'; |
14 | 8 |
|
15 | 9 | suite('Extension Test Suite', () => {
|
16 | 10 | vscode.window.showInformationMessage('Starting tests...');
|
17 | 11 |
|
18 |
| - const disposables: vscode.Disposable[] = []; |
19 | 12 | const mockExtensionContext = new TestExtensionContext();
|
20 | 13 | const mockMDBExtension = new MDBExtensionController(mockExtensionContext);
|
21 |
| - const mockStorageController = new StorageController(mockExtensionContext); |
22 |
| - const testTelemetryController = new TelemetryController( |
23 |
| - mockStorageController, |
24 |
| - mockExtensionContext |
25 |
| - ); |
26 |
| - const testConnectionController = new ConnectionController( |
27 |
| - new StatusView(mockExtensionContext), |
28 |
| - mockStorageController, |
29 |
| - testTelemetryController |
30 |
| - ); |
31 | 14 | const sandbox = sinon.createSandbox();
|
| 15 | + |
32 | 16 | let fakeShowErrorMessage: any;
|
| 17 | + let fakeGetActiveConnectionModel: any; |
| 18 | + let createTerminalSpy: any; |
33 | 19 |
|
34 |
| - before(() => { |
| 20 | + beforeEach(() => { |
35 | 21 | sandbox.stub(vscode.window, 'showInformationMessage');
|
| 22 | + |
36 | 23 | fakeShowErrorMessage = sandbox.stub(vscode.window, 'showErrorMessage');
|
| 24 | + fakeGetActiveConnectionModel = sandbox.stub( |
| 25 | + mockMDBExtension._connectionController, |
| 26 | + 'getActiveConnectionModel' |
| 27 | + ); |
| 28 | + |
| 29 | + createTerminalSpy = sinon.spy(vscode.window, 'createTerminal'); |
37 | 30 | });
|
38 | 31 |
|
39 |
| - after(() => { |
40 |
| - disposables.forEach((d) => d.dispose()); |
41 |
| - disposables.length = 0; |
| 32 | + afterEach(() => { |
42 | 33 | sandbox.restore();
|
| 34 | + sinon.restore(); |
43 | 35 | });
|
44 | 36 |
|
45 | 37 | test('commands are registered in vscode', async () => {
|
@@ -102,54 +94,69 @@ suite('Extension Test Suite', () => {
|
102 | 94 | });
|
103 | 95 |
|
104 | 96 | test('openMongoDBShell should open a terminal with the active connection driver url', async () => {
|
105 |
| - try { |
106 |
| - const succesfullyConnected = await testConnectionController.addNewConnectionStringAndConnect( |
107 |
| - TEST_DATABASE_URI |
108 |
| - ); |
| 97 | + const driverUri = |
| 98 | + 'mongodb://localhost:27018/?readPreference=primary&ssl=false'; |
109 | 99 |
|
110 |
| - assert( |
111 |
| - succesfullyConnected === true, |
112 |
| - 'Expected a successful connection response.' |
113 |
| - ); |
| 100 | + fakeGetActiveConnectionModel.returns( |
| 101 | + new Connection({ |
| 102 | + hostname: 'localhost', |
| 103 | + port: 27018 |
| 104 | + }) |
| 105 | + ); |
114 | 106 |
|
| 107 | + try { |
115 | 108 | await mockMDBExtension.openMongoDBShell();
|
116 | 109 |
|
117 |
| - const spyActiveConnectionDriverUrl = sinon.spy( |
118 |
| - testConnectionController, |
119 |
| - 'getActiveConnectionDriverUrl' |
| 110 | + assert(createTerminalSpy.called); |
| 111 | + |
| 112 | + const terminalOptions: vscode.TerminalOptions = |
| 113 | + createTerminalSpy.firstCall.args[0]; |
| 114 | + |
| 115 | + assert( |
| 116 | + terminalOptions.env?.MDB_CONNECTION_STRING === driverUri, |
| 117 | + `Expected open terminal to set env var 'MDB_CONNECTION_STRING' to ${driverUri} found ${terminalOptions.env?.MDB_CONNECTION_STRING}` |
120 | 118 | );
|
121 |
| - const createTerminalSpy = sinon.spy(vscode.window, 'createTerminal'); |
122 | 119 |
|
123 |
| - const checkResult = async () => { |
124 |
| - await testConnectionController.disconnect(); |
| 120 | + await mockMDBExtension._connectionController.disconnect(); |
| 121 | + mockMDBExtension._connectionController.clearAllConnections(); |
| 122 | + } catch (e) { |
| 123 | + assert(false); |
| 124 | + return; |
| 125 | + } |
| 126 | + }); |
125 | 127 |
|
126 |
| - try { |
127 |
| - assert(spyActiveConnectionDriverUrl.called); |
128 |
| - assert(createTerminalSpy.called); |
| 128 | + test('openMongoDBShell should open a terminal with ssh tunnel port injected', async () => { |
| 129 | + const driverUri = |
| 130 | + 'mongodb://127.0.0.1:27017/?readPreference=primary&ssl=false'; |
129 | 131 |
|
130 |
| - const expectedUri = |
131 |
| - 'mongodb://localhost:27018/?readPreference=primary&ssl=false'; |
| 132 | + fakeGetActiveConnectionModel.returns( |
| 133 | + new Connection({ |
| 134 | + hostname: '127.0.0.1', |
| 135 | + sshTunnel: 'USER_PASSWORD', |
| 136 | + sshTunnelHostname: 'my.ssh-server.com', |
| 137 | + sshTunnelUsername: 'my-user', |
| 138 | + sshTunnelPassword: 'password' |
| 139 | + }) |
| 140 | + ); |
132 | 141 |
|
133 |
| - const terminalOptions: vscode.TerminalOptions = createTerminalSpy.firstCall.args[0]; |
| 142 | + try { |
| 143 | + await mockMDBExtension.openMongoDBShell(); |
134 | 144 |
|
135 |
| - assert( |
136 |
| - terminalOptions.env?.MDB_CONNECTION_STRING === |
137 |
| - expectedUri, |
138 |
| - `Expected open terminal to set env var 'MDB_CONNECTION_STRING' to ${expectedUri} found ${ |
139 |
| - terminalOptions.env?.MDB_CONNECTION_STRING |
140 |
| - }` |
141 |
| - ); |
| 145 | + assert(createTerminalSpy.called); |
142 | 146 |
|
143 |
| - testConnectionController.clearAllConnections(); |
144 |
| - } catch (e) { |
145 |
| - assert(false); |
146 |
| - return; |
147 |
| - } |
148 |
| - }; |
| 147 | + const terminalOptions: vscode.TerminalOptions = |
| 148 | + createTerminalSpy.firstCall.args[0]; |
149 | 149 |
|
150 |
| - disposables.push(vscode.window.onDidOpenTerminal(checkResult)); |
151 |
| - } catch (error) { |
| 150 | + assert( |
| 151 | + terminalOptions.env?.MDB_CONNECTION_STRING !== driverUri, |
| 152 | + `Expected open terminal to set env var 'MDB_CONNECTION_STRING' to driver url with ssh tunnel port injected found ${terminalOptions.env?.MDB_CONNECTION_STRING}` |
| 153 | + ); |
| 154 | + |
| 155 | + await mockMDBExtension._connectionController.disconnect(); |
| 156 | + mockMDBExtension._connectionController.clearAllConnections(); |
| 157 | + } catch (e) { |
152 | 158 | assert(false);
|
| 159 | + return; |
153 | 160 | }
|
154 | 161 | });
|
155 | 162 | });
|
0 commit comments