Skip to content

Commit

Permalink
test: add tests cases for connection info and secrets VSCODE-317 (#381)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alenakhineika authored Jan 17, 2022
1 parent ff15215 commit 0f21452
Show file tree
Hide file tree
Showing 10 changed files with 456 additions and 326 deletions.
44 changes: 28 additions & 16 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface StoreConnectionInfo {
id: string; // Connection model id or a new uuid.
name: string; // Possibly user given name, not unique.
storageLocation: StorageLocation;
connectionOptions: ConnectionOptions;
connectionOptions?: ConnectionOptions;
connectionModel?: ConnectionModel;
}

Expand All @@ -68,12 +68,15 @@ interface ConnectionSecretsInfo {
secrets: ConnectionSecrets
}

type StoreConnectionInfoWithConnectionOptions = StoreConnectionInfo & Required<Pick<StoreConnectionInfo, 'connectionOptions'>>;

export default class ConnectionController {
// This is a map of connection ids to their configurations.
// These connections can be saved on the session (runtime),
// on the workspace, or globally in vscode.
_connections: { [connectionId: string]: StoreConnectionInfo } = {};
_connections: { [connectionId: string]: StoreConnectionInfoWithConnectionOptions } = {};
_activeDataService: DataService| null = null;
_storageController: StorageController;

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

private _statusView: StatusView;
private _storageController: StorageController;
private _telemetryService: TelemetryService;

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

private async _migratePreviouslySavedConnection(
async _migratePreviouslySavedConnection(
savedConnectionInfo: StoreConnectionInfo
): Promise<StoreConnectionInfo> {
): Promise<StoreConnectionInfoWithConnectionOptions> {
// Transform a raw connection model from storage to an ampersand model.
const newConnectionInfoWithSecrets = convertConnectionModelToInfo(savedConnectionInfo.connectionModel);

Expand All @@ -124,9 +126,9 @@ export default class ConnectionController {
return newSavedConnectionInfoWithSecrets;
}

private async _getConnectionInfoWithSecrets(
async _getConnectionInfoWithSecrets(
savedConnectionInfo: StoreConnectionInfo
): Promise<StoreConnectionInfo|undefined> {
): Promise<StoreConnectionInfoWithConnectionOptions|undefined> {
// Migrate previously saved connections to a new format.
// Save only secrets to keychain.
// Remove connectionModel and use connectionOptions instead.
Expand All @@ -147,8 +149,8 @@ export default class ConnectionController {
// If connection has a new format already and keytar module is undefined.
// Return saved connection as it is.
if (!ext.keytarModule) {
log.error('VSCode extension keytar module is undefined.');
return savedConnectionInfo;
log.error('Load saved connections failed: VSCode extension keytar module is undefined.');
return savedConnectionInfo as StoreConnectionInfoWithConnectionOptions;
}

try {
Expand All @@ -159,7 +161,7 @@ export default class ConnectionController {

// Ignore empty secrets.
if (!unparsedSecrets) {
return savedConnectionInfo;
return savedConnectionInfo as StoreConnectionInfoWithConnectionOptions;
}

const secrets = JSON.parse(unparsedSecrets);
Expand All @@ -168,7 +170,7 @@ export default class ConnectionController {
{
id: savedConnectionInfo.id,
connectionOptions
},
} as ConnectionInfo,
secrets
);

Expand Down Expand Up @@ -339,7 +341,7 @@ export default class ConnectionController {
): Promise<StoreConnectionInfo> {
// We don't want to store secrets to disc.
const { connectionInfo: safeConnectionInfo, secrets } = extractSecrets(
newStoreConnectionInfoWithSecrets
newStoreConnectionInfoWithSecrets as ConnectionInfo
);
const savedConnectionInfo = await this._storageController.saveConnection({
...newStoreConnectionInfoWithSecrets,
Expand Down Expand Up @@ -379,7 +381,7 @@ export default class ConnectionController {
return this._connect(savedConnectionInfo.id, connectionType);
}

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

const connectionOptions = this._connections[connectionId].connectionOptions;

if (!connectionOptions) {
throw new Error('Connect failed: connectionOptions are missing.');
}

const newDataService = new DataService(connectionOptions);
let connectError;

Expand Down Expand Up @@ -711,9 +718,14 @@ export default class ConnectionController {

// Copy connection string from the sidebar does not need appname in it.
copyConnectionStringByConnectionId(connectionId: string): string {
const url = new ConnectionString(
this._connections[connectionId].connectionOptions.connectionString
);
const connectionOptions = this._connections[connectionId].connectionOptions;

if (!connectionOptions) {
throw new Error('Copy connection string failed: connectionOptions are missing.');
}

const url = new ConnectionString(connectionOptions.connectionString);

url.searchParams.delete('appname');
return url.toString();
}
Expand Down
4 changes: 2 additions & 2 deletions src/explorer/explorerTreeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ implements vscode.TreeDataProvider<vscode.TreeItem> {
this._onTreeItemUpdate();
}

if (selectedItem._contextValue === DOCUMENT_ITEM) {
if (selectedItem.contextValue === DOCUMENT_ITEM) {
await vscode.commands.executeCommand(
EXTENSION_COMMANDS.MDB_OPEN_MONGODB_DOCUMENT_FROM_TREE,
event.selection[0]
);
}

if (
selectedItem._contextValue === DOCUMENT_LIST_ITEM &&
selectedItem.contextValue === DOCUMENT_LIST_ITEM &&
selectedItem.type === CollectionTypes.view
) {
await vscode.commands.executeCommand(
Expand Down
Loading

0 comments on commit 0f21452

Please sign in to comment.