Skip to content

Commit

Permalink
test: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Dec 16, 2024
1 parent 8bee88d commit 36a0037
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
19 changes: 14 additions & 5 deletions packages/credential-provider-ini/src/fromIni.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe(fromIni.name, () => {
const mockMasterProfileName = "mockMasterProfileName";
const mockProfileName = "mockProfileName";
const mockInit = { profile: mockProfileName };
const mockInitWithParentClientConfig = { profile: mockProfileName, parentClientConfig: {} };
const mockProfiles = { [mockProfileName]: { key: "value" } };

beforeEach(() => {
Expand All @@ -32,7 +33,7 @@ describe(fromIni.name, () => {
} catch (error) {
expect(error).toStrictEqual(expectedError);
}
expect(parseKnownFiles).toHaveBeenCalledWith(mockInit);
expect(parseKnownFiles).toHaveBeenCalledWith(mockInitWithParentClientConfig);
expect(getProfileName).not.toHaveBeenCalled();
expect(resolveProfileData).not.toHaveBeenCalled();
});
Expand All @@ -46,9 +47,13 @@ describe(fromIni.name, () => {
} catch (error) {
expect(error).toStrictEqual(expectedError);
}
expect(parseKnownFiles).toHaveBeenCalledWith(mockInit);
expect(parseKnownFiles).toHaveBeenCalledWith(mockInitWithParentClientConfig);
expect(getProfileName).toHaveBeenCalledWith(mockInit);
expect(resolveProfileData).toHaveBeenCalledWith(mockMasterProfileName, mockProfiles, mockInit);
expect(resolveProfileData).toHaveBeenCalledWith(
mockMasterProfileName,
mockProfiles,
mockInitWithParentClientConfig
);
});

it("returns resolved process creds", async () => {
Expand All @@ -59,8 +64,12 @@ describe(fromIni.name, () => {
vi.mocked(resolveProfileData).mockResolvedValue(expectedCreds);
const receivedCreds = await fromIni(mockInit)();
expect(receivedCreds).toStrictEqual(expectedCreds);
expect(parseKnownFiles).toHaveBeenCalledWith(mockInit);
expect(parseKnownFiles).toHaveBeenCalledWith(mockInitWithParentClientConfig);
expect(getProfileName).toHaveBeenCalledWith(mockInit);
expect(resolveProfileData).toHaveBeenCalledWith(mockMasterProfileName, mockProfiles, mockInit);
expect(resolveProfileData).toHaveBeenCalledWith(
mockMasterProfileName,
mockProfiles,
mockInitWithParentClientConfig
);
});
});
29 changes: 23 additions & 6 deletions packages/token-providers/src/fromSso.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe(fromSso.name, () => {

const mockProfileName = "mockProfileName";
const mockInit = { profile: mockProfileName };
const mockInitWithParentClientConfig = { profile: mockProfileName, parentClientConfig: {} };
const mockProfiles = { [mockProfileName]: mockSsoProfile };

const mockSsoToken = {
Expand Down Expand Up @@ -68,7 +69,7 @@ describe(fromSso.name, () => {
});

afterEach(() => {
expect(parseKnownFiles).toHaveBeenCalledWith(mockInit);
expect(parseKnownFiles).toHaveBeenCalledWith(mockInitWithParentClientConfig);
expect(getProfileName).toHaveBeenCalledWith(mockInit);
vi.clearAllMocks();
});
Expand Down Expand Up @@ -167,7 +168,11 @@ describe(fromSso.name, () => {
const { fromSso } = await import("./fromSso");
await expect(fromSso(mockInit)()).resolves.toStrictEqual(mockNewToken);
expect(getNewSsoOidcToken).toHaveBeenCalledTimes(1);
expect(getNewSsoOidcToken).toHaveBeenCalledWith(mockSsoToken, mockSsoSession.sso_region, mockInit);
expect(getNewSsoOidcToken).toHaveBeenCalledWith(
mockSsoToken,
mockSsoSession.sso_region,
mockInitWithParentClientConfig
);

// Simulate token expiration.
const ssoTokenExpiryError = new TokenProviderError(`SSO Token is expired. ${REFRESH_MESSAGE}`, false);
Expand All @@ -183,7 +188,11 @@ describe(fromSso.name, () => {
const { fromSso } = await import("./fromSso");
await expect(fromSso(mockInit)()).resolves.toStrictEqual(mockNewToken);
expect(getNewSsoOidcToken).toHaveBeenCalledTimes(1);
expect(getNewSsoOidcToken).toHaveBeenCalledWith(mockSsoToken, mockSsoSession.sso_region, mockInit);
expect(getNewSsoOidcToken).toHaveBeenCalledWith(
mockSsoToken,
mockSsoSession.sso_region,
mockInitWithParentClientConfig
);

// Return a valid token for second call.
const mockValidSsoToken = {
Expand Down Expand Up @@ -234,7 +243,7 @@ describe(fromSso.name, () => {
expect(getNewSsoOidcToken).toHaveBeenCalledWith(
mockValidSsoTokenInExpiryWindow,
mockSsoSession.sso_region,
mockInit
mockInitWithParentClientConfig
);
};

Expand All @@ -244,7 +253,11 @@ describe(fromSso.name, () => {
throw ssoTokenExpiryError;
});
await expect(fromSsoImpl(mockInit)()).rejects.toStrictEqual(ssoTokenExpiryError);
expect(getNewSsoOidcToken).toHaveBeenCalledWith(mockSsoToken, mockSsoSession.sso_region, mockInit);
expect(getNewSsoOidcToken).toHaveBeenCalledWith(
mockSsoToken,
mockSsoSession.sso_region,
mockInitWithParentClientConfig
);
};

afterEach(() => {
Expand Down Expand Up @@ -290,7 +303,11 @@ describe(fromSso.name, () => {
const { fromSso } = await import("./fromSso");
await expect(fromSso(mockInit)()).resolves.toStrictEqual(mockNewToken);
expect(getNewSsoOidcToken).toHaveBeenCalledTimes(1);
expect(getNewSsoOidcToken).toHaveBeenCalledWith(mockSsoToken, mockSsoSession.sso_region, mockInit);
expect(getNewSsoOidcToken).toHaveBeenCalledWith(
mockSsoToken,
mockSsoSession.sso_region,
mockInitWithParentClientConfig
);

expect(writeSSOTokenToFile).toHaveBeenCalledWith(mockSsoSessionName, {
...mockSsoToken,
Expand Down

0 comments on commit 36a0037

Please sign in to comment.