Skip to content

Commit 0f21452

Browse files
test: add tests cases for connection info and secrets VSCODE-317 (#381)
* test: add tests cases for connection info and secrets VSCODE-317 * test: update test descriptions * test: remove extra nested suite * refactor: address pr comments * test: use strictEqual without custom message
1 parent ff15215 commit 0f21452

File tree

10 files changed

+456
-326
lines changed

10 files changed

+456
-326
lines changed

src/connectionController.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface StoreConnectionInfo {
4444
id: string; // Connection model id or a new uuid.
4545
name: string; // Possibly user given name, not unique.
4646
storageLocation: StorageLocation;
47-
connectionOptions: ConnectionOptions;
47+
connectionOptions?: ConnectionOptions;
4848
connectionModel?: ConnectionModel;
4949
}
5050

@@ -68,12 +68,15 @@ interface ConnectionSecretsInfo {
6868
secrets: ConnectionSecrets
6969
}
7070

71+
type StoreConnectionInfoWithConnectionOptions = StoreConnectionInfo & Required<Pick<StoreConnectionInfo, 'connectionOptions'>>;
72+
7173
export default class ConnectionController {
7274
// This is a map of connection ids to their configurations.
7375
// These connections can be saved on the session (runtime),
7476
// on the workspace, or globally in vscode.
75-
_connections: { [connectionId: string]: StoreConnectionInfo } = {};
77+
_connections: { [connectionId: string]: StoreConnectionInfoWithConnectionOptions } = {};
7678
_activeDataService: DataService| null = null;
79+
_storageController: StorageController;
7780

7881
private readonly _serviceName = 'mdb.vscode.savedConnections';
7982
private _currentConnectionId: null | string = null;
@@ -89,7 +92,6 @@ export default class ConnectionController {
8992
private _disconnecting = false;
9093

9194
private _statusView: StatusView;
92-
private _storageController: StorageController;
9395
private _telemetryService: TelemetryService;
9496

9597
// Used by other parts of the extension that respond to changes in the connections.
@@ -105,9 +107,9 @@ export default class ConnectionController {
105107
this._telemetryService = telemetryService;
106108
}
107109

108-
private async _migratePreviouslySavedConnection(
110+
async _migratePreviouslySavedConnection(
109111
savedConnectionInfo: StoreConnectionInfo
110-
): Promise<StoreConnectionInfo> {
112+
): Promise<StoreConnectionInfoWithConnectionOptions> {
111113
// Transform a raw connection model from storage to an ampersand model.
112114
const newConnectionInfoWithSecrets = convertConnectionModelToInfo(savedConnectionInfo.connectionModel);
113115

@@ -124,9 +126,9 @@ export default class ConnectionController {
124126
return newSavedConnectionInfoWithSecrets;
125127
}
126128

127-
private async _getConnectionInfoWithSecrets(
129+
async _getConnectionInfoWithSecrets(
128130
savedConnectionInfo: StoreConnectionInfo
129-
): Promise<StoreConnectionInfo|undefined> {
131+
): Promise<StoreConnectionInfoWithConnectionOptions|undefined> {
130132
// Migrate previously saved connections to a new format.
131133
// Save only secrets to keychain.
132134
// Remove connectionModel and use connectionOptions instead.
@@ -147,8 +149,8 @@ export default class ConnectionController {
147149
// If connection has a new format already and keytar module is undefined.
148150
// Return saved connection as it is.
149151
if (!ext.keytarModule) {
150-
log.error('VSCode extension keytar module is undefined.');
151-
return savedConnectionInfo;
152+
log.error('Load saved connections failed: VSCode extension keytar module is undefined.');
153+
return savedConnectionInfo as StoreConnectionInfoWithConnectionOptions;
152154
}
153155

154156
try {
@@ -159,7 +161,7 @@ export default class ConnectionController {
159161

160162
// Ignore empty secrets.
161163
if (!unparsedSecrets) {
162-
return savedConnectionInfo;
164+
return savedConnectionInfo as StoreConnectionInfoWithConnectionOptions;
163165
}
164166

165167
const secrets = JSON.parse(unparsedSecrets);
@@ -168,7 +170,7 @@ export default class ConnectionController {
168170
{
169171
id: savedConnectionInfo.id,
170172
connectionOptions
171-
},
173+
} as ConnectionInfo,
172174
secrets
173175
);
174176

@@ -339,7 +341,7 @@ export default class ConnectionController {
339341
): Promise<StoreConnectionInfo> {
340342
// We don't want to store secrets to disc.
341343
const { connectionInfo: safeConnectionInfo, secrets } = extractSecrets(
342-
newStoreConnectionInfoWithSecrets
344+
newStoreConnectionInfoWithSecrets as ConnectionInfo
343345
);
344346
const savedConnectionInfo = await this._storageController.saveConnection({
345347
...newStoreConnectionInfoWithSecrets,
@@ -379,7 +381,7 @@ export default class ConnectionController {
379381
return this._connect(savedConnectionInfo.id, connectionType);
380382
}
381383

382-
private async _connect(
384+
async _connect(
383385
connectionId: string,
384386
connectionType: ConnectionTypes
385387
): Promise<ConnectionAttemptResult> {
@@ -398,6 +400,11 @@ export default class ConnectionController {
398400
this._statusView.showMessage('Connecting to MongoDB...');
399401

400402
const connectionOptions = this._connections[connectionId].connectionOptions;
403+
404+
if (!connectionOptions) {
405+
throw new Error('Connect failed: connectionOptions are missing.');
406+
}
407+
401408
const newDataService = new DataService(connectionOptions);
402409
let connectError;
403410

@@ -711,9 +718,14 @@ export default class ConnectionController {
711718

712719
// Copy connection string from the sidebar does not need appname in it.
713720
copyConnectionStringByConnectionId(connectionId: string): string {
714-
const url = new ConnectionString(
715-
this._connections[connectionId].connectionOptions.connectionString
716-
);
721+
const connectionOptions = this._connections[connectionId].connectionOptions;
722+
723+
if (!connectionOptions) {
724+
throw new Error('Copy connection string failed: connectionOptions are missing.');
725+
}
726+
727+
const url = new ConnectionString(connectionOptions.connectionString);
728+
717729
url.searchParams.delete('appname');
718730
return url.toString();
719731
}

src/explorer/explorerTreeController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ implements vscode.TreeDataProvider<vscode.TreeItem> {
101101
this._onTreeItemUpdate();
102102
}
103103

104-
if (selectedItem._contextValue === DOCUMENT_ITEM) {
104+
if (selectedItem.contextValue === DOCUMENT_ITEM) {
105105
await vscode.commands.executeCommand(
106106
EXTENSION_COMMANDS.MDB_OPEN_MONGODB_DOCUMENT_FROM_TREE,
107107
event.selection[0]
108108
);
109109
}
110110

111111
if (
112-
selectedItem._contextValue === DOCUMENT_LIST_ITEM &&
112+
selectedItem.contextValue === DOCUMENT_LIST_ITEM &&
113113
selectedItem.type === CollectionTypes.view
114114
) {
115115
await vscode.commands.executeCommand(

0 commit comments

Comments
 (0)