diff --git a/extensions/azurecore/src/account-provider/auths/azureAuth.ts b/extensions/azurecore/src/account-provider/auths/azureAuth.ts index 0e1ff65614..690807926f 100644 --- a/extensions/azurecore/src/account-provider/auths/azureAuth.ts +++ b/extensions/azurecore/src/account-provider/auths/azureAuth.ts @@ -435,7 +435,7 @@ export abstract class AzureAuth implements vscode.Disposable { } } - private async openConsentDialog(tenant: Tenant, resourceId: string): Promise { + public async openConsentDialog(tenant: Tenant, resourceId: string): Promise { if (!tenant.displayName && !tenant.id) { throw new Error('Tenant did not have display name or id'); } @@ -451,18 +451,18 @@ export abstract class AzureAuth implements vscode.Disposable { }; // The user wants to ignore this tenant. - if (getTenantConfigurationSet().has(tenant?.displayName ?? tenant?.id)) { + if (getTenantConfigurationSet().has(tenant.id)) { return false; } - const updateTenantConfigurationSet = (set: Set): void => { + const updateTenantConfigurationSet = async (set: Set): Promise => { const configuration = vscode.workspace.getConfiguration('azure.tenant.config'); - configuration.update('filter', Array.from(set), vscode.ConfigurationTarget.Global); + await configuration.update('filter', Array.from(set), vscode.ConfigurationTarget.Global); }; interface ConsentMessageItem extends vscode.MessageItem { booleanResult: boolean; - action?: (tenantId: string) => void; + action?: (tenantId: string) => Promise; } const openItem: ConsentMessageItem = { @@ -479,10 +479,10 @@ export abstract class AzureAuth implements vscode.Disposable { const dontAskAgainItem: ConsentMessageItem = { title: localize('azurecore.consentDialog.ignore', "Ignore Tenant"), booleanResult: false, - action: (tenantId: string) => { + action: async (tenantId: string) => { let set = getTenantConfigurationSet(); set.add(tenantId); - updateTenantConfigurationSet(set); + await updateTenantConfigurationSet(set); } }; @@ -490,7 +490,7 @@ export abstract class AzureAuth implements vscode.Disposable { const result = await vscode.window.showInformationMessage(messageBody, { modal: true }, openItem, closeItem, dontAskAgainItem); if (result.action) { - result.action(tenant.id); + await result.action(tenant.id); } return result.booleanResult; diff --git a/extensions/azurecore/src/test/account-provider/auths/azureAuth.test.ts b/extensions/azurecore/src/test/account-provider/auths/azureAuth.test.ts index d270976796..9b9ec4673c 100644 --- a/extensions/azurecore/src/test/account-provider/auths/azureAuth.test.ts +++ b/extensions/azurecore/src/test/account-provider/auths/azureAuth.test.ts @@ -6,10 +6,11 @@ import * as should from 'should'; import * as os from 'os'; import 'mocha'; +import * as vscode from 'vscode'; import { PromptFailedResult, AccountKey } from 'azdata'; import { AzureAuth, AccessToken, RefreshToken, TokenClaims, TokenRefreshResponse } from '../../../account-provider/auths/azureAuth'; -import { AzureAccount, AzureAuthType, Deferred } from '../../../account-provider/interfaces'; +import { AzureAccount, AzureAuthType, Deferred, Tenant } from '../../../account-provider/interfaces'; import providerSettings from '../../../account-provider/providerSettings'; import { SimpleTokenCache } from '../../../account-provider/simpleTokenCache'; import { CredentialsTestProvider } from '../../stubs/credentialsTestProvider'; @@ -48,6 +49,11 @@ const refreshToken: RefreshToken = { const resourceId = 'resource'; const tenantId = 'tenant'; +const tenant: Tenant = { + id: tenantId, + displayName: 'common' +}; + // These tests don't work on Linux systems because gnome-keyring doesn't like running on headless machines. describe('AccountProvider.AzureAuth', function (): void { beforeEach(async function (): Promise { @@ -96,4 +102,16 @@ describe('AccountProvider.AzureAuth', function (): void { should(account.key.accountId).be.equal('someKey'); should(account.properties.isMsAccount).be.equal(true); }); + it('Should handle ignored tenants', async function (): Promise { + // Don't sit on the await openConsentDialog if test is failing + this.timeout(3000); + + const configuration = vscode.workspace.getConfiguration('azure.tenant.config'); + const values = [tenantId]; + + await configuration.update('filter', values, vscode.ConfigurationTarget.Global); + const x = await baseAuth.openConsentDialog(tenant, resourceId); + + should(x).be.false(); + }); });