Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maorleger committed May 2, 2024
1 parent a127b3a commit 3fba79b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
5 changes: 5 additions & 0 deletions sdk/identity/identity/src/msal/nodeFlows/msalClient.ts
Expand Up @@ -187,10 +187,15 @@ export function createMsalClient(

let publicClientApp = publicApps.get(appKey);
if (publicClientApp) {
msalLogger.getToken.info("Existing PublicClientApplication found in cache, returning it.");
return publicClientApp;
}

// Initialize a new app and cache it
msalLogger.getToken.info(
`Creating new PublicClientApplication with CAE ${options.enableCae ? "enabled" : "disabled"}.`,
);

const cachePlugin = options.enableCae
? state.pluginConfiguration.cache.cachePluginCae
: state.pluginConfiguration.cache.cachePlugin;
Expand Down
Expand Up @@ -7,8 +7,6 @@ import { MsalTestCleanup, msalNodeTestSetup } from "../../node/msalNodeTestSetup
import { Recorder, env, isLiveMode } from "@azure-tools/test-recorder";
import { Context } from "mocha";
import { DeviceCodeCredential } from "../../../src";
import { GetTokenOptions } from "@azure/core-auth";
import { MsalNode } from "../../../src/msal/nodeFlows/msalNodeCommon";
import { PublicClientApplication } from "@azure/msal-node";
import Sinon from "sinon";
import { assert } from "chai";
Expand All @@ -24,9 +22,10 @@ describe("DeviceCodeCredential (internal)", function () {
cleanup = setup.cleanup;
recorder = setup.recorder;

getTokenSilentSpy = setup.sandbox.spy(MsalNode.prototype, "getTokenSilent");
// MsalClient calls to this method underneath when silent authentication can be attempted.
getTokenSilentSpy = setup.sandbox.spy(PublicClientApplication.prototype, "acquireTokenSilent");

// MsalClientSecret calls to this method underneath.
// MsalClient calls to this method underneath for interactive auth.
doGetTokenSpy = setup.sandbox.spy(
PublicClientApplication.prototype,
"acquireTokenByDeviceCode",
Expand All @@ -51,19 +50,18 @@ describe("DeviceCodeCredential (internal)", function () {
);

await credential.getToken(scope);
assert.equal(getTokenSilentSpy.callCount, 1, "getTokenSilentSpy.callCount should have been 1");
assert.equal(doGetTokenSpy.callCount, 1, "doGetTokenSpy.callCount should have been 1");
assert.equal(doGetTokenSpy.callCount, 1, "doGetTokenSpy.callCount should have been 1.");

await credential.getToken(scope);
assert.equal(
getTokenSilentSpy.callCount,
2,
"getTokenSilentSpy.callCount should have been 2 (2nd time)",
1,
"getTokenSilentSpy.callCount should have been 1 (Silent authentication after the initial request).",
);
assert.equal(
doGetTokenSpy.callCount,
1,
"doGetTokenSpy.callCount should have been 1 (2nd time)",
"Expected no additional calls to doGetTokenSpy after the initial request.",
);
});

Expand All @@ -79,8 +77,7 @@ describe("DeviceCodeCredential (internal)", function () {
}),
);

await credential.getToken(scope, { tenantId: env.AZURE_TENANT_ID } as GetTokenOptions);
assert.equal(getTokenSilentSpy.callCount, 1, "getTokenSilentSpy.callCount should have been 1");
await credential.getToken(scope, { tenantId: env.AZURE_TENANT_ID });
assert.equal(doGetTokenSpy.callCount, 1, "doGetTokenSpy.callCount should have been 1");
});
});

0 comments on commit 3fba79b

Please sign in to comment.