|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import assert from "assert"; |
| 5 | +import { ChainedTokenCredential, TokenCredential, AccessToken } from "../../../src"; |
| 6 | +import Sinon from "sinon"; |
| 7 | +import { logger as chainedTokenCredentialLogger } from "../../../src/credentials/chainedTokenCredential"; |
| 8 | + |
| 9 | +class TestMockCredential implements TokenCredential { |
| 10 | + constructor(public returnPromise: Promise<AccessToken | null>) {} |
| 11 | + |
| 12 | + getToken(): Promise<AccessToken | null> { |
| 13 | + return this.returnPromise; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +describe("ChainedTokenCredential", function() { |
| 18 | + it("Logs the expected successful message", async () => { |
| 19 | + const chainedTokenCredential = new ChainedTokenCredential( |
| 20 | + new TestMockCredential(Promise.resolve({ token: "firstToken", expiresOnTimestamp: 0 })) |
| 21 | + ); |
| 22 | + |
| 23 | + const infoSpy = Sinon.spy(chainedTokenCredentialLogger.parent, "info"); |
| 24 | + const getTokenInfoSpy = Sinon.spy(chainedTokenCredentialLogger.getToken, "info"); |
| 25 | + |
| 26 | + const accessToken = await chainedTokenCredential.getToken("<scope>"); |
| 27 | + assert.notStrictEqual(accessToken, null); |
| 28 | + |
| 29 | + assert.equal( |
| 30 | + infoSpy.getCalls()[0].args.join(" "), |
| 31 | + "ChainedTokenCredential => getToken() => Result for TestMockCredential: SUCCESS. Scopes: <scope>." |
| 32 | + ); |
| 33 | + assert.equal( |
| 34 | + getTokenInfoSpy.getCalls()[0].args[0], |
| 35 | + "Result for TestMockCredential: SUCCESS. Scopes: <scope>." |
| 36 | + ); |
| 37 | + |
| 38 | + infoSpy.restore(); |
| 39 | + getTokenInfoSpy.restore(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("Doesn't throw with a clossure credential", async () => { |
| 43 | + function mockCredential(returnPromise: Promise<AccessToken | null>): TokenCredential { |
| 44 | + return { |
| 45 | + getToken: () => returnPromise |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + const chainedTokenCredential = new ChainedTokenCredential( |
| 50 | + mockCredential(Promise.resolve({ token: "firstToken", expiresOnTimestamp: 0 })) |
| 51 | + ); |
| 52 | + |
| 53 | + const infoSpy = Sinon.spy(chainedTokenCredentialLogger.parent, "info"); |
| 54 | + const getTokenInfoSpy = Sinon.spy(chainedTokenCredentialLogger.getToken, "info"); |
| 55 | + |
| 56 | + const accessToken = await chainedTokenCredential.getToken("<scope>"); |
| 57 | + assert.notStrictEqual(accessToken, null); |
| 58 | + |
| 59 | + assert.equal( |
| 60 | + infoSpy.getCalls()[0].args.join(" "), |
| 61 | + "ChainedTokenCredential => getToken() => Result for Object: SUCCESS. Scopes: <scope>." |
| 62 | + ); |
| 63 | + assert.equal( |
| 64 | + getTokenInfoSpy.getCalls()[0].args[0], |
| 65 | + "Result for Object: SUCCESS. Scopes: <scope>." |
| 66 | + ); |
| 67 | + |
| 68 | + infoSpy.restore(); |
| 69 | + getTokenInfoSpy.restore(); |
| 70 | + }); |
| 71 | +}); |
0 commit comments