From af247a4fcd90f5d9ee7e93bf6b1387b2d6512d81 Mon Sep 17 00:00:00 2001 From: Karishma Ghiya Date: Mon, 7 Feb 2022 17:12:06 -0800 Subject: [PATCH] added a test for managed identity credetnial with and without client id in Cloudshell env --- .../node/managedIdentityCredential.spec.ts | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/sdk/identity/identity/test/internal/node/managedIdentityCredential.spec.ts b/sdk/identity/identity/test/internal/node/managedIdentityCredential.spec.ts index fbce216678b6..2ad000febf27 100644 --- a/sdk/identity/identity/test/internal/node/managedIdentityCredential.spec.ts +++ b/sdk/identity/identity/test/internal/node/managedIdentityCredential.spec.ts @@ -19,6 +19,7 @@ import { import { createResponse, IdentityTestContextInterface } from "../../httpRequestsCommon"; import { IdentityTestContext } from "../../httpRequests"; import { AzureAuthorityHosts, DefaultAuthorityHost, DefaultTenantId } from "../../../src/constants"; +import { setLogLevel } from "@azure/logger"; describe("ManagedIdentityCredential", function () { let testContext: IdentityTestContextInterface; @@ -360,18 +361,36 @@ describe("ManagedIdentityCredential", function () { it("sends an authorization request correctly in an Cloud Shell environment", async () => { // Trigger Cloud Shell behavior by setting environment variables process.env.MSI_ENDPOINT = "https://endpoint"; - const authDetails = await testContext.sendCredentialRequests({ scopes: ["https://service/.default"], - credential: new ManagedIdentityCredential("client"), + credential: new ManagedIdentityCredential(), secureResponses: [createResponse(200, { access_token: "token" })], }); - + console.dir(authDetails); const authRequest = authDetails.requests[0]; assert.equal(authRequest.method, "POST"); assert.equal(authDetails.result!.token, "token"); }); + it("authorization request fails with client id passed in an Cloud Shell environment", async () => { + // Trigger Cloud Shell behavior by setting environment variables + process.env.MSI_ENDPOINT = "https://endpoint"; + setLogLevel("warning"); + const authDetails = await testContext.sendCredentialRequests({ + scopes: ["https://service/.default"], + credential: new ManagedIdentityCredential("client"), + secureResponses: [createResponse(200, { access_token: "token" })], + }); + console.dir(authDetails); + assert.equal(authDetails.result, null); + assert.equal(authDetails.error?.name, "CredentialUnavailableError"); + assert.equal( + authDetails.error?.message, + "ManagedIdentityCredential: Authentication failed. Message No responses left." + ); + assert.equal(authDetails.requests.length, 0); + }); + it("sends an authorization request correctly in an Azure Arc environment", async function (this: Mocha.Context) { // Trigger Azure Arc behavior by setting environment variables