|
| 1 | +import { AssumeRoleWithWebIdentityParams } from "@aws-sdk/credential-provider-web-identity"; |
| 2 | +import { CredentialProvider, Credentials } from "@aws-sdk/types"; |
| 3 | +import { getMasterProfileName, parseKnownFiles } from "@aws-sdk/util-credentials"; |
| 4 | + |
| 5 | +import { fromIni } from "./fromIni"; |
| 6 | +import { AssumeRoleParams } from "./resolveAssumeRoleCredentials"; |
| 7 | +import { resolveProfileData } from "./resolveProfileData"; |
| 8 | + |
| 9 | +jest.mock("@aws-sdk/util-credentials"); |
| 10 | +jest.mock("./resolveProfileData"); |
| 11 | + |
| 12 | +describe(fromIni.name, () => { |
| 13 | + const mockMasterProfileName = "mockMasterProfileName"; |
| 14 | + const mockProfileName = "mockProfileName"; |
| 15 | + const mockInit = { profile: mockProfileName }; |
| 16 | + const mockProfiles = { [mockProfileName]: { key: "value" } }; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + (parseKnownFiles as jest.Mock).mockResolvedValue(mockProfiles); |
| 20 | + (getMasterProfileName as jest.Mock).mockReturnValue(mockMasterProfileName); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + jest.clearAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("rethrows error if parsing known files fails", async () => { |
| 28 | + const expectedError = new Error("from parseKnownFiles"); |
| 29 | + (parseKnownFiles as jest.Mock).mockRejectedValue(expectedError); |
| 30 | + try { |
| 31 | + await fromIni(mockInit)(); |
| 32 | + fail(`expected ${expectedError}`); |
| 33 | + } catch (error) { |
| 34 | + expect(error).toStrictEqual(expectedError); |
| 35 | + } |
| 36 | + expect(parseKnownFiles).toHaveBeenCalledWith(mockInit); |
| 37 | + expect(getMasterProfileName).not.toHaveBeenCalled(); |
| 38 | + expect(resolveProfileData).not.toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("rethrows error if resolving process creds fails", async () => { |
| 42 | + const expectedError = new Error("from resolveProcessCredentials"); |
| 43 | + (resolveProfileData as jest.Mock).mockRejectedValue(expectedError); |
| 44 | + try { |
| 45 | + await fromIni(mockInit)(); |
| 46 | + fail(`expected ${expectedError}`); |
| 47 | + } catch (error) { |
| 48 | + expect(error).toStrictEqual(expectedError); |
| 49 | + } |
| 50 | + expect(parseKnownFiles).toHaveBeenCalledWith(mockInit); |
| 51 | + expect(getMasterProfileName).toHaveBeenCalledWith(mockInit); |
| 52 | + expect(resolveProfileData).toHaveBeenCalledWith(mockMasterProfileName, mockProfiles, mockInit); |
| 53 | + }); |
| 54 | + |
| 55 | + it("returns resolved process creds", async () => { |
| 56 | + const expectedCreds: Credentials = { |
| 57 | + accessKeyId: "mockAccessKeyId", |
| 58 | + secretAccessKey: "mockSecretAccessKey", |
| 59 | + }; |
| 60 | + (resolveProfileData as jest.Mock).mockResolvedValue(expectedCreds); |
| 61 | + const receivedCreds = await fromIni(mockInit)(); |
| 62 | + expect(receivedCreds).toStrictEqual(expectedCreds); |
| 63 | + expect(parseKnownFiles).toHaveBeenCalledWith(mockInit); |
| 64 | + expect(getMasterProfileName).toHaveBeenCalledWith(mockInit); |
| 65 | + expect(resolveProfileData).toHaveBeenCalledWith(mockMasterProfileName, mockProfiles, mockInit); |
| 66 | + }); |
| 67 | +}); |
0 commit comments