forked from aws-actions/configure-aws-credentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
95 lines (80 loc) · 4.15 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const core = require('@actions/core');
const run = require('.');
jest.mock('@actions/core');
const mockStsCallerIdentity = jest.fn();
jest.mock('aws-sdk', () => {
return {
STS: jest.fn(() => ({
getCallerIdentity: mockStsCallerIdentity
}))
};
});
describe('Configure AWS Credentials', () => {
beforeEach(() => {
jest.clearAllMocks();
core.getInput = jest
.fn()
.mockReturnValueOnce('MY-AWS-ACCESS-KEY-ID') // aws-access-key-id
.mockReturnValueOnce('MY-AWS-SECRET-ACCESS-KEY') // aws-secret-access-key
.mockReturnValueOnce('us-east-2') // aws-default-region
.mockReturnValueOnce('MY-AWS-SESSION-TOKEN') // aws-session-token
.mockReturnValueOnce('TRUE'); // mask-aws-account-id
mockStsCallerIdentity.mockImplementation(() => {
return {
promise() {
return Promise.resolve({ Account: '123456789012' });
}
};
});
});
test('exports env vars', async () => {
await run();
expect(core.exportVariable).toHaveBeenCalledTimes(5);
expect(core.exportVariable).toHaveBeenCalledWith('AWS_ACCESS_KEY_ID', 'MY-AWS-ACCESS-KEY-ID');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SECRET_ACCESS_KEY', 'MY-AWS-SECRET-ACCESS-KEY');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SESSION_TOKEN', 'MY-AWS-SESSION-TOKEN');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_REGION', 'us-east-2');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_REGION', 'us-east-2');
expect(core.setOutput).toHaveBeenCalledWith('aws-account-id', '123456789012');
expect(core.setSecret).toHaveBeenCalledWith('123456789012');
});
test('session token is optional', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('MY-AWS-ACCESS-KEY-ID') // aws-access-key-id
.mockReturnValueOnce('MY-AWS-SECRET-ACCESS-KEY') // aws-secret-access-key
.mockReturnValueOnce('eu-west-1'); // aws-default-region
await run();
expect(core.exportVariable).toHaveBeenCalledTimes(4);
expect(core.exportVariable).toHaveBeenCalledWith('AWS_ACCESS_KEY_ID', 'MY-AWS-ACCESS-KEY-ID');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SECRET_ACCESS_KEY', 'MY-AWS-SECRET-ACCESS-KEY');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_REGION', 'eu-west-1');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_REGION', 'eu-west-1');
expect(core.setOutput).toHaveBeenCalledWith('aws-account-id', '123456789012');
expect(core.setSecret).toHaveBeenCalledWith('123456789012');
});
test('can opt out of masking account ID', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('MY-AWS-ACCESS-KEY-ID') // aws-access-key-id
.mockReturnValueOnce('MY-AWS-SECRET-ACCESS-KEY') // aws-secret-access-key
.mockReturnValueOnce('us-east-1') // aws-default-region
.mockReturnValueOnce('') // aws-session-token
.mockReturnValueOnce('false'); // mask-aws-account-id
await run();
expect(core.exportVariable).toHaveBeenCalledTimes(4);
expect(core.exportVariable).toHaveBeenCalledWith('AWS_ACCESS_KEY_ID', 'MY-AWS-ACCESS-KEY-ID');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SECRET_ACCESS_KEY', 'MY-AWS-SECRET-ACCESS-KEY');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_REGION', 'us-east-1');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_REGION', 'us-east-1');
expect(core.setOutput).toHaveBeenCalledWith('aws-account-id', '123456789012');
expect(core.setSecret).toHaveBeenCalledTimes(0);
});
test('error is caught by core.setFailed', async () => {
mockStsCallerIdentity.mockImplementation(() => {
throw new Error();
});
await run();
expect(core.setFailed).toBeCalled();
});
});