forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Identity] ChainedTokenCredential logging fix (Azure#14847)
Fixes to the ChainedTokenCredential logging. It now documents what credential succeeds. Also added a test. Fixes Azure#6769
- Loading branch information
1 parent
b87332c
commit 2f3c3f9
Showing
4 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
sdk/identity/identity/test/internal/node/chainedTokenCredential.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import assert from "assert"; | ||
import { ChainedTokenCredential, TokenCredential, AccessToken } from "../../../src"; | ||
import Sinon from "sinon"; | ||
import { logger as chainedTokenCredentialLogger } from "../../../src/credentials/chainedTokenCredential"; | ||
|
||
class TestMockCredential implements TokenCredential { | ||
constructor(public returnPromise: Promise<AccessToken | null>) {} | ||
|
||
getToken(): Promise<AccessToken | null> { | ||
return this.returnPromise; | ||
} | ||
} | ||
|
||
describe("ChainedTokenCredential", function() { | ||
it("Logs the expected successful message", async () => { | ||
const chainedTokenCredential = new ChainedTokenCredential( | ||
new TestMockCredential(Promise.resolve({ token: "firstToken", expiresOnTimestamp: 0 })) | ||
); | ||
|
||
const infoSpy = Sinon.spy(chainedTokenCredentialLogger.parent, "info"); | ||
const getTokenInfoSpy = Sinon.spy(chainedTokenCredentialLogger.getToken, "info"); | ||
|
||
const accessToken = await chainedTokenCredential.getToken("<scope>"); | ||
assert.notStrictEqual(accessToken, null); | ||
|
||
assert.equal( | ||
infoSpy.getCalls()[0].args.join(" "), | ||
"ChainedTokenCredential => getToken() => Result for TestMockCredential: SUCCESS. Scopes: <scope>." | ||
); | ||
assert.equal( | ||
getTokenInfoSpy.getCalls()[0].args[0], | ||
"Result for TestMockCredential: SUCCESS. Scopes: <scope>." | ||
); | ||
|
||
infoSpy.restore(); | ||
getTokenInfoSpy.restore(); | ||
}); | ||
|
||
it("Doesn't throw with a clossure credential", async () => { | ||
function mockCredential(returnPromise: Promise<AccessToken | null>): TokenCredential { | ||
return { | ||
getToken: () => returnPromise | ||
}; | ||
} | ||
|
||
const chainedTokenCredential = new ChainedTokenCredential( | ||
mockCredential(Promise.resolve({ token: "firstToken", expiresOnTimestamp: 0 })) | ||
); | ||
|
||
const infoSpy = Sinon.spy(chainedTokenCredentialLogger.parent, "info"); | ||
const getTokenInfoSpy = Sinon.spy(chainedTokenCredentialLogger.getToken, "info"); | ||
|
||
const accessToken = await chainedTokenCredential.getToken("<scope>"); | ||
assert.notStrictEqual(accessToken, null); | ||
|
||
assert.equal( | ||
infoSpy.getCalls()[0].args.join(" "), | ||
"ChainedTokenCredential => getToken() => Result for Object: SUCCESS. Scopes: <scope>." | ||
); | ||
assert.equal( | ||
getTokenInfoSpy.getCalls()[0].args[0], | ||
"Result for Object: SUCCESS. Scopes: <scope>." | ||
); | ||
|
||
infoSpy.restore(); | ||
getTokenInfoSpy.restore(); | ||
}); | ||
}); |