-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(credential-provider-web-identity): update unit tests
- Loading branch information
1 parent
52de007
commit b61a6cb
Showing
5 changed files
with
147 additions
and
132 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
84 changes: 84 additions & 0 deletions
84
packages/credential-provider-web-identity/src/fromWebToken.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,84 @@ | ||
import { ProviderError } from "@aws-sdk/property-provider"; | ||
|
||
import { fromWebToken } from "./fromWebToken"; | ||
|
||
const mockToken = "exampletoken"; | ||
const mockRoleArn = "mockRoleArn"; | ||
const mockRoleSessionName = "mockRoleSessionName"; | ||
const mockPolicy = "mockPolicy"; | ||
const mockPolicyArns = [{ arn: "policyArn" }]; | ||
const mockProviderId = "mockProviderId"; | ||
const mockDurationSeconds = 7200; | ||
const MOCK_CREDS = { | ||
accessKeyId: "accessKeyId", | ||
secretAccessKey: "secretAccessKey", | ||
sessionToken: "sessionToken", | ||
}; | ||
|
||
describe("fromWebToken", () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
it("throws if roleAssumerWithWebIdentity is not defined", async () => { | ||
try { | ||
await fromWebToken({ | ||
webIdentityToken: mockToken, | ||
roleArn: mockRoleArn, | ||
})(); | ||
fail(`Expected error to be thrown`); | ||
} catch (error) { | ||
expect(error).toEqual( | ||
new ProviderError( | ||
`Role Arn '${mockRoleArn}' needs to be assumed with web identity, but no role assumption callback was provided.`, | ||
false | ||
) | ||
); | ||
} | ||
}); | ||
|
||
it("passes values to roleAssumerWithWebIdentity", async () => { | ||
expect.assertions(2); | ||
const init = { | ||
webIdentityToken: mockToken, | ||
roleArn: mockRoleArn, | ||
roleSessionName: mockRoleSessionName, | ||
providerId: mockProviderId, | ||
policyArns: mockPolicyArns, | ||
policy: mockPolicy, | ||
durationSeconds: mockDurationSeconds, | ||
}; | ||
const creds = await fromWebToken({ | ||
...init, | ||
roleAssumerWithWebIdentity: async (params) => { | ||
expect(params).toMatchObject({ | ||
WebIdentityToken: mockToken, | ||
RoleArn: mockRoleArn, | ||
RoleSessionName: mockRoleSessionName, | ||
ProviderId: mockProviderId, | ||
PolicyArns: mockPolicyArns, | ||
Policy: mockPolicy, | ||
DurationSeconds: mockDurationSeconds, | ||
}); | ||
return MOCK_CREDS; | ||
}, | ||
})(); | ||
expect(creds).toEqual(MOCK_CREDS); | ||
}); | ||
|
||
it("generates a random value for RoleSessionName if not available", async () => { | ||
const mockDateNow = Date.now(); | ||
const spyDateNow = jest.spyOn(Date, "now").mockReturnValueOnce(mockDateNow); | ||
|
||
const creds = await fromWebToken({ | ||
webIdentityToken: mockToken, | ||
roleArn: mockRoleArn, | ||
roleAssumerWithWebIdentity: async (params) => { | ||
expect(params.RoleSessionName).toEqual(`aws-sdk-js-session-${mockDateNow}`); | ||
return MOCK_CREDS; | ||
}, | ||
})(); | ||
expect(creds).toEqual(MOCK_CREDS); | ||
expect(spyDateNow).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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