Skip to content
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0b31112
WIP
MicroFish91 Nov 10, 2025
71f4f14
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 Nov 11, 2025
f188eca
Add auth tests and supporting logic
MicroFish91 Nov 11, 2025
e71785c
Update with better comments
MicroFish91 Nov 12, 2025
3f4cb3a
Revert launch change
MicroFish91 Nov 12, 2025
34d2f1a
Use extensionId var
MicroFish91 Nov 12, 2025
c34d952
Add note
MicroFish91 Nov 12, 2025
5b0d57a
Merge with parent branch
MicroFish91 Nov 12, 2025
4c49c09
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 Nov 13, 2025
0b1d03e
Update mock credential manager
MicroFish91 Nov 13, 2025
570ecf4
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 Nov 13, 2025
b446718
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 Nov 13, 2025
0a7b447
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 Nov 13, 2025
3cf4fa3
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 Nov 13, 2025
69051c0
Remove part of a comment
MicroFish91 Nov 14, 2025
e433035
Merge with main + some new changes
MicroFish91 Nov 19, 2025
1718244
Fix formatting
MicroFish91 Nov 19, 2025
06e2b99
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 Nov 19, 2025
ef7ad23
Match to equal
MicroFish91 Nov 19, 2025
97b1239
Merge branch 'mwf/v4' of https://github.com/microsoft/vscode-azureres…
MicroFish91 Nov 19, 2025
eda6ce6
Escape the symbol
MicroFish91 Nov 19, 2025
b5eb5f9
Merge branch 'mwf/v4-client-tools' of https://github.com/microsoft/vs…
MicroFish91 Nov 20, 2025
81ce320
Merge with new eng package changes
MicroFish91 Nov 20, 2025
5c11b97
Inject more extension vars
MicroFish91 Nov 20, 2025
ff9d912
Merge branch 'mwf/v4-client-tools' of https://github.com/microsoft/vs…
MicroFish91 Nov 20, 2025
1e9269d
Merge with main + add getUI
MicroFish91 Nov 21, 2025
8edafe9
Add type
MicroFish91 Nov 21, 2025
0d63c1c
Add an extra test
MicroFish91 Nov 21, 2025
56385e1
Remove env var
MicroFish91 Dec 9, 2025
1039782
Upgrade utils package
MicroFish91 Dec 11, 2025
0aa6f1c
Remove ui extension vars; leverage testGlobalSetup
MicroFish91 Dec 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"preLaunchTask": "Watch: ESBuild",
"env": {
"DEBUGTELEMETRY": "v",
"AzCode_EnableLongRunningTestsLocal": ""
"AzCode_EnableLongRunningTestsLocal": "",
"VSCODE_RUNNING_TESTS": "true",
}
},
]
Expand Down
6 changes: 6 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ export async function activate(context: vscode.ExtensionContext, perfStats: { lo
getAppResourceTree: () => ext.appResourceTree,
},
extensionVariables: {
getUI: () => {
return {
context: ext.context,
outputChannel: ext.outputChannel,
};
},
getOutputChannel: () => ext.outputChannel,
},
testing: {
Expand Down
7 changes: 6 additions & 1 deletion src/testApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { AzureSubscriptionProvider } from "@microsoft/vscode-azext-azureauth";
import { AzExtTreeDataProvider, IAzExtLogOutputChannel } from "@microsoft/vscode-azext-utils";
import { AzExtTreeDataProvider, IAzExtLogOutputChannel, UIExtensionVariables } from "@microsoft/vscode-azext-utils";
import { AzureResourcesApiInternal } from "./hostapi.v2.internal";
import { AzureResourcesServiceFactory } from "./services/AzureResourcesService";

Expand Down Expand Up @@ -38,6 +38,11 @@ export interface TestApi {
* Get extension variables for tests
*/
extensionVariables: {
/**
* Get the common extension variables used throughout the UI package
*/
getUI(): UIExtensionVariables;

/**
* Get the output channel
*/
Expand Down
39 changes: 39 additions & 0 deletions test/api/auth/MockUUIDCredentialManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { AzExtCredentialManager } from "../../../api/src/auth/credentialManager/AzExtCredentialManager";
import { maskValue } from "../../../api/src/utils/maskValue";

/**
* A mock credential manager with the same implementation as `AzExtUUIDCredentialManager`,
* but with a public getter to inspect the UUIDs during test.
Copy link
Contributor Author

@MicroFish91 MicroFish91 Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A less copy and paste mock would be possible if I extended from the AzExtUUIDCredentialManager directly, but the map would need to be protected not private for me to be able to access it.

*/
export class MockUUIDCredentialManager implements AzExtCredentialManager {
#uuidMap: Map<string, string> = new Map();

get uuidMap() {
return this.#uuidMap;
}

createCredential(extensionId: string): string {
const uuid: string = crypto.randomUUID();
this.#uuidMap.set(extensionId, uuid);
return uuid;
}

verifyCredential(credential: string, extensionId: string): boolean {
if (!credential || !extensionId) {
return false;
}
return credential === this.#uuidMap.get(extensionId);
}

maskCredentials(data: string): string {
for (const uuid of this.#uuidMap.values()) {
data = maskValue(data, uuid);
}
return data;
}
}
37 changes: 37 additions & 0 deletions test/api/auth/mockAuthApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { AzureExtensionApiFactory, createApiProvider } from "@microsoft/vscode-azext-utils";
import { apiUtils, AzureExtensionApi, AzureResourcesExtensionAuthApi, GetApiOptions } from "../../../api/src";
import { AuthApiFactoryDependencies, createAuthApiFactory } from "../../../src/api/auth/createAuthApiFactory";

/**
* Creates a mock API provider with API factories matching the versions provided.
* Only the values required by the interface will be implemented.
*/
function createMockApiProvider(versions: string[]): apiUtils.AzureExtensionApiProvider {
const apiFactories: AzureExtensionApiFactory<AzureExtensionApi>[] = versions.map(version => {
return {
apiVersion: version,
createApi: (_options?: GetApiOptions) => {
return {
apiVersion: version,
};
},
};
});

return createApiProvider(apiFactories);
}

/**
* Creates a mock auth API protecting core API versions: ['0.0.1', '2.0.0', '3.0.0']
*/
export function createMockAuthApi(customDependencies?: AuthApiFactoryDependencies): AzureResourcesExtensionAuthApi {
const coreApiVersions: string[] = ['0.0.1', '2.0.0', '3.0.0'];
const coreApiProvider = createMockApiProvider(coreApiVersions);
const authApiProvider = createAuthApiFactory(coreApiProvider, customDependencies);
return authApiProvider.createApi({ extensionId: 'ms-azuretools.vscode-azureresourcegroups-tests' });
}
120 changes: 120 additions & 0 deletions test/api/auth/v4.host.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { nonNullValue, parseError } from "@microsoft/vscode-azext-utils";
import * as assert from "assert";
import { apiUtils, AzureExtensionApi, AzureResourcesExtensionAuthApi } from "../../../api/src";
import { assertThrowsAsync } from "../../wrapFunctionsInTelemetry.test";
import { MockUUIDCredentialManager } from "./MockUUIDCredentialManager";
import { createMockAuthApi } from "./mockAuthApi";

const clientExtensionId: string = 'ms-azuretools.vscode-azurecontainerapps';
const clientExtensionVersion: string = '1.0.0';

suite('v4 internal API auth tests', async () => {
test('v4 API should be defined', async () => {
const apiProvider = await apiUtils.getExtensionExports<apiUtils.AzureExtensionApiProvider>('ms-azuretools.vscode-azureresourcegroups');
assert.ok(apiProvider, 'API provider is undefined');

const v4Api = apiProvider.getApi('^4.0.0', { extensionId: 'ms-azuretools.vscode-azureresourcegroups-tests' });
assert.ok(v4Api);
});

test('createAzureResourcesApiSession should provide a valid credential but not return it directly', async () => {
let apiSession: unknown;
let receivedHostCredential: string = '';
let receivedClientCredential: string = '';

const credentialManager = new MockUUIDCredentialManager();
const generatedClientCredential: string = crypto.randomUUID();

await new Promise<void>((resolve) => {
const timeout = setTimeout(resolve, 5000);

const mockClientExtensionApi: AzureExtensionApi = {
apiVersion: clientExtensionVersion,
receiveAzureResourcesApiSession: (hostCredential: string, clientCredential: string) => {
clearTimeout(timeout);
receivedHostCredential = hostCredential;
receivedClientCredential = clientCredential;
resolve();
},
};

const authApi: AzureResourcesExtensionAuthApi = createMockAuthApi({ credentialManager, clientApiProvider: { getApi: () => mockClientExtensionApi } });
authApi.createAzureResourcesApiSession(clientExtensionId, clientExtensionVersion, generatedClientCredential)
.then(session => apiSession = session)
.catch(() => { clearTimeout(timeout); resolve(); });
});

assert.equal(apiSession, undefined);
assert.equal(receivedClientCredential, generatedClientCredential);

const generatedHostCredential: string = nonNullValue(credentialManager.uuidMap.get(clientExtensionId));
assert.equal(receivedHostCredential, generatedHostCredential);
});

test('createAzureResourcesApiSession should throw if an unallowed extension id is provided', async () => {
const authApi: AzureResourcesExtensionAuthApi = createMockAuthApi();
await assertThrowsAsync(async () => await authApi.createAzureResourcesApiSession('extension1', clientExtensionVersion, crypto.randomUUID()));
});

test('createAzureResourcesApiSession should not spill sensitive extension credentials in errors', async () => {
const credentialManager = new MockUUIDCredentialManager();
credentialManager.createCredential('extension1');
credentialManager.createCredential = () => {
throw new Error(credentialManager.uuidMap.get('extension1'));
};

const authApi: AzureResourcesExtensionAuthApi = createMockAuthApi({ credentialManager });

try {
await authApi.createAzureResourcesApiSession(clientExtensionId, clientExtensionVersion, crypto.randomUUID());
assert.fail('We expect the credential manager to throw in this test.');
} catch (err) {
const perr = parseError(err);
assert.doesNotMatch(perr.message, new RegExp(nonNullValue(credentialManager.uuidMap.get('extension1')), 'i'));
}
});

test('getAzureResourcesApis should return matching APIs if provided a valid credential', async () => {
const credentialManager = new MockUUIDCredentialManager();
const generatedHostCredential: string = credentialManager.createCredential(clientExtensionId);

const authApi: AzureResourcesExtensionAuthApi = createMockAuthApi({ credentialManager });
const resourcesApis = await authApi.getAzureResourcesApis(clientExtensionId, generatedHostCredential, ['0.0.1', '^2.0.0']);

assert.equal(resourcesApis[0]?.apiVersion, '0.0.1');
assert.match(resourcesApis[1]?.apiVersion ?? '', /^2\./);
});

test('getAzureResourcesApis should throw if provided an invalid credential', async () => {
const credentialManager = new MockUUIDCredentialManager();
const authApi: AzureResourcesExtensionAuthApi = createMockAuthApi({ credentialManager });
await assertThrowsAsync(async () => await authApi.getAzureResourcesApis(clientExtensionId, crypto.randomUUID(), ['^2.0.0']));

credentialManager.createCredential(clientExtensionId);
await assertThrowsAsync(async () => await authApi.getAzureResourcesApis(clientExtensionId, crypto.randomUUID(), ['^2.0.0']));
});

test('getAzureResourcesApis should not spill sensitive extension credentials in errors', async () => {
const credentialManager = new MockUUIDCredentialManager();
const authApi: AzureResourcesExtensionAuthApi = createMockAuthApi({ credentialManager });

credentialManager.createCredential('extension1');
credentialManager.createCredential('extension2');
credentialManager.createCredential('extension3');

try {
await authApi.getAzureResourcesApis(clientExtensionId, crypto.randomUUID(), ['^2.0.0']);
assert.fail('Should throw if requesting Azure Resources APIs without a valid credential.');
} catch (err) {
const perr = parseError(err);
for (const credential of credentialManager.uuidMap.values()) {
assert.doesNotMatch(perr.message, new RegExp(credential, 'i'));
}
}
});
});
6 changes: 4 additions & 2 deletions test/global.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { registerOnActionStartHandler, TestUserInput } from '@microsoft/vscode-azext-utils';
import { registerOnActionStartHandler, registerUIExtensionVariables, TestUserInput } from '@microsoft/vscode-azext-utils';
import * as vscode from 'vscode';
import { TestApi } from '../src/testApi';
import { settingUtils } from '../src/utils/settingUtils';
import { getTestApi } from './utils/testApiAccess';

Expand All @@ -19,7 +20,8 @@ suiteSetup(async function (this: Mocha.Context): Promise<void> {
this.timeout(1 * 60 * 1000);

// Initialize test API - this caches it for use throughout tests
await getTestApi();
const testApi: TestApi = await getTestApi();
registerUIExtensionVariables(testApi.extensionVariables.getUI());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way around needing to do registerUIExtensionVariables? Elsewhere for example, we just expose from the extension what is needed (only when running tests).

Copy link
Contributor Author

@MicroFish91 MicroFish91 Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TLDR:
I'm not personally sure how to do it otherwise, so I might need help coming up with a better way.

Long story:
I think what's happening is I'm calling createApiProvider during tests which is exported from utils. createApiProvider calls into getPackageInfo (also in utils) which calls ext.context which is uninitialized and throws an error.

At this point ext.context is uninitialized because we haven't registered the extension vars at any point during testing. I assume registrations in activation.ts won't count since they probably live in a separate process.

I was looking around at some other extensions because I do see that we get by with not always registering in global.test.ts. I'm currently looking at Functions, and though I don't see a registration call, I'm thinking maybe we're just never calling into the actual methods that leverage these variables?

Like the most obvious culprit for me to follow-up on was showQuickPick because we call that all the time. But then I thought about it some more, and likely showQuickPick is only getting called in the actual extension context (where it has already been successfully registered) and not the test context.

Just as a quick reference, these are the common spots where these variables are used from utils. It doesn't look like there are that many files where they're used, so maybe it's possible we're just accidentally getting away with not registering in some extensions since we just rarely use these outside of the normal extension context.

image image

Copy link
Contributor

@bwateratmsft bwateratmsft Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this looks very similar to what I had to do to make the tests in utils/azure/appservice work. Those are in a truly extensionless environment so it's sorta inevitable there. But, it may make sense to do the same here.

testGlobalSetup.ts: https://github.com/microsoft/vscode-azuretools/blob/main/utils/src/dev/testGlobalSetup.ts

Used here: https://github.com/microsoft/vscode-azuretools/blob/main/utils/test/global.test.ts

Somewhat more advanced example of usage here: https://github.com/microsoft/vscode-azuretools/blob/a6733fc8b101519db04dd98832359faa167a6fd3/appservice/test/global.test.ts

For them it was a similar story--getPackageInfo was getting called. It was sufficient to fill in just a small subset of what would normally be there for the extension variables.


await vscode.commands.executeCommand('azureResourceGroups.refresh'); // activate the extension before tests begin

Expand Down
2 changes: 1 addition & 1 deletion test/utils/testApiAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { TestApi } from '../../src/testApi';
import { apiUtils } from '../../api/src/utils/apiUtils';
import { TestApi } from '../../src/testApi';

let cachedTestApi: TestApi | undefined;

Expand Down