Skip to content

Commit 33adce1

Browse files
committed
tests for disabled output-env-credentials
1 parent 3d90187 commit 33adce1

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function exportCredentials(
5454

5555
if (creds?.SessionToken) {
5656
core.setSecret(creds.SessionToken);
57-
}
57+
}
5858

5959
if (outputEnvCredentials) {
6060
if (creds?.AccessKeyId) {

test/cleanup.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ describe('Configure AWS Credentials cleanup', {}, () => {
4545
cleanup();
4646
expect(core.setFailed).toHaveBeenCalled();
4747
});
48+
it(`doesn't export credentials as empty env variables if asked not to`, {}, () => {
49+
vi.spyOn(core, 'getInput').mockImplementation(mocks.getInput(mocks.NO_ENV_CREDS_INPUTS));
50+
cleanup();
51+
expect(core.exportVariable).toHaveBeenCalledTimes(0);
52+
})
4853
});

test/helpers.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Configure AWS Credentials helpers', {}, () => {
2727
vi.spyOn(core, 'setOutput').mockImplementation(() => {});
2828
vi.spyOn(core, 'setSecret').mockImplementation(() => {});
2929
vi.spyOn(core, 'exportVariable').mockImplementation(() => {});
30-
helpers.exportCredentials({ AccessKeyId: 'test', SecretAccessKey: 'test', SessionToken: 'test', Expiration: new Date(8640000000000000) }, true);
30+
helpers.exportCredentials({ AccessKeyId: 'test', SecretAccessKey: 'test', SessionToken: 'test', Expiration: new Date(8640000000000000) }, true, true);
3131
expect(core.setOutput).toHaveBeenCalledTimes(4);
3232
expect(core.setSecret).toHaveBeenCalledTimes(3);
3333
expect(core.exportVariable).toHaveBeenCalledTimes(3);
@@ -42,4 +42,15 @@ describe('Configure AWS Credentials helpers', {}, () => {
4242
expect(process.env.AWS_DEFAULT_REGION).toBeUndefined;
4343
process.env = env;
4444
});
45+
it(`won't output credentials to env if told not to`, {}, () => {
46+
vi.spyOn(core, 'setOutput').mockImplementation(() => {});
47+
vi.spyOn(core, 'setSecret').mockImplementation(() => {});
48+
vi.spyOn(core, 'exportVariable').mockImplementation(() => {});
49+
helpers.exportCredentials({ AccessKeyId: 'test', SecretAccessKey: 'test', SessionToken: 'test', Expiration: new Date(8640000000000000) }, true, false);
50+
helpers.unsetCredentials(false);
51+
helpers.exportRegion('fake-test-region', false);
52+
expect(core.setOutput).toHaveBeenCalledTimes(4);
53+
expect(core.setSecret).toHaveBeenCalledTimes(3);
54+
expect(core.exportVariable).toHaveBeenCalledTimes(0);
55+
});
4556
});

test/index.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,5 +312,26 @@ describe('Configure AWS Credentials', {}, () => {
312312
await run();
313313
expect(core.setFailed).not.toHaveBeenCalled();
314314
})
315+
it('doesn\'t export credentials as environment variables if told not to', {}, async () => {
316+
mockedSTSClient.on(AssumeRoleWithWebIdentityCommand).resolvesOnce(mocks.outputs.STS_CREDENTIALS);
317+
vi.spyOn(core, 'getInput').mockImplementation(mocks.getInput(mocks.NO_ENV_CREDS_INPUTS));
318+
vi.spyOn(core, 'getIDToken').mockResolvedValue('testoidctoken');
319+
process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN = 'fake-token';
320+
await run();
321+
expect(core.setSecret).toHaveBeenCalledTimes(3);
322+
expect(core.exportVariable).toHaveBeenCalledTimes(0);
323+
expect(core.setFailed).not.toHaveBeenCalled();
324+
})
325+
it('can export creds as step outputs without exporting as env variables', {}, async () => {
326+
mockedSTSClient.on(AssumeRoleWithWebIdentityCommand).resolvesOnce(mocks.outputs.STS_CREDENTIALS);
327+
vi.spyOn(core, 'getInput').mockImplementation(mocks.getInput(mocks.STEP_BUT_NO_ENV_INPUTS));
328+
vi.spyOn(core, 'getIDToken').mockResolvedValue('testoidctoken');
329+
process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN = 'fake-token';
330+
await run();
331+
expect(core.setSecret).toHaveBeenCalledTimes(3);
332+
expect(core.exportVariable).toHaveBeenCalledTimes(0);
333+
expect(core.setOutput).toHaveBeenCalledTimes(4);
334+
expect(core.setFailed).not.toHaveBeenCalled();
335+
})
315336
});
316337
});

test/mockinputs.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ const inputs = {
3131
'aws-region': 'fake-region-1',
3232
'use-existing-credentials': 'true',
3333
'role-to-assume': 'arn:aws:iam::111111111111:role/MY-ROLE',
34+
},
35+
NO_ENV_CREDS_INPUTS: {
36+
'role-to-assume': 'arn:aws:iam::111111111111:role/MY-ROLE',
37+
'aws-region': 'fake-region-1',
38+
'output-env-credentials': 'false'
39+
},
40+
STEP_BUT_NO_ENV_INPUTS: {
41+
'role-to-assume': 'arn:aws:iam::111111111111:role/MY-ROLE',
42+
'aws-region': 'fake-region-1',
43+
'output-env-credentials': 'false',
44+
'output-credentials': 'true',
3445
}
3546
};
3647

0 commit comments

Comments
 (0)