Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Identity] Test improvements #15999

Merged
merged 4 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ interface AuthRequestDetails {
describe("ManagedIdentityCredential", function() {
let envCopy: string = "";
let sandbox: Sinon.SinonSandbox;
let clock: Sinon.SinonFakeTimers;

beforeEach(() => {
envCopy = JSON.stringify(process.env);
Expand All @@ -39,10 +38,6 @@ describe("ManagedIdentityCredential", function() {
delete process.env.IDENTITY_SERVER_THUMBPRINT;
delete process.env.IMDS_ENDPOINT;
sandbox = Sinon.createSandbox();
clock = sandbox.useFakeTimers({
now: Date.now(),
shouldAdvanceTime: true
});
});
afterEach(() => {
const env = JSON.parse(envCopy);
Expand All @@ -53,7 +48,6 @@ describe("ManagedIdentityCredential", function() {
process.env.IDENTITY_SERVER_THUMBPRINT = env.IDENTITY_SERVER_THUMBPRINT;
process.env.IMDS_ENDPOINT = env.IMDS_ENDPOINT;
sandbox.restore();
clock.restore();
});

it("sends an authorization request with a modified resource name", async function() {
Expand Down Expand Up @@ -232,20 +226,22 @@ describe("ManagedIdentityCredential", function() {
...mockHttpClient.tokenCredentialOptions
});

const promise = credential.getToken("scopes");
const clock = sandbox.useFakeTimers();

imdsMsiRetryConfig.startDelayInMs = 80; // 800ms / 10

// 800ms -> 1600ms -> 3200ms, results in 6400ms, / 10 = 640ms
clock.tick(640);
let errorMessage: string = "";
credential.getToken("scopes").catch((error) => {
errorMessage = error.message;
});
// 800ms -> 1600ms -> 3200ms, results in 6400ms

await assertRejects(
promise,
(error: AuthenticationError) =>
error.message.indexOf(
`Failed to retrieve IMDS token after ${imdsMsiRetryConfig.maxRetries} retries.`
) > -1
await clock.tickAsync(6400);
assert.ok(
errorMessage.indexOf(
`Failed to retrieve IMDS token after ${imdsMsiRetryConfig.maxRetries} retries.`
) > -1
);

clock.restore();
});

// Unavailable exception throws while IMDS endpoint is unavailable. This test not valid.
Expand Down Expand Up @@ -397,15 +393,7 @@ describe("ManagedIdentityCredential", function() {
);

assert.equal(authRequest.headers.get("Authorization"), `Basic ${key}`);
if (authDetails.token) {
// We use Date.now underneath.
assert.equal(
Math.floor(authDetails.token.expiresOnTimestamp / 1000000),
Math.floor(Date.now() / 1000000)
);
} else {
assert.fail("No token was returned!");
}
assert.ok(authDetails.token?.expiresOnTimestamp);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There’s really no reason to test the contents of expiresOnTimestamp. We define this value in our test utilities. The processing of the request will take a variable time depending on the environment (Node, VM, etc)

} finally {
unlinkSync(tempFile);
rmdirSync(tempDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */

import assert from "assert";
import { env, delay, isLiveMode } from "@azure/test-utils-recorder";
import { env, delay, isRecordMode } from "@azure/test-utils-recorder";
import { AbortController } from "@azure/abort-controller";
import { MsalTestCleanup, msalNodeTestSetup, testTracing } from "../../msalTestUtils";
import { ClientSecretCredential, RegionalAuthority } from "../../../src";
Expand Down Expand Up @@ -83,7 +83,10 @@ describe("ClientSecretCredential", function() {
);

it("supports specifying the regional authority", async function(this: Context) {
if (isLiveMode()) {
// This test is extremely slow. Let's skip it for now.
// I've tried Sinon's clock and it doesn't affect it.
// We have internal tests that check that the parameters are properly sent to MSAL, which should be enough from the perspective of the SDK.
if (!isRecordMode()) {
this.skip();
}

Expand Down