|
1 | 1 | const core = require('@actions/core');
|
2 | 2 | const assert = require('assert');
|
3 |
| - |
| 3 | +const aws = require('aws-sdk'); |
4 | 4 | const run = require('.');
|
5 | 5 |
|
6 | 6 | jest.mock('@actions/core');
|
@@ -49,6 +49,9 @@ const mockStsAssumeRole = jest.fn();
|
49 | 49 |
|
50 | 50 | jest.mock('aws-sdk', () => {
|
51 | 51 | return {
|
| 52 | + config: { |
| 53 | + getCredentials: jest.fn() |
| 54 | + }, |
52 | 55 | STS: jest.fn(() => ({
|
53 | 56 | getCallerIdentity: mockStsCallerIdentity,
|
54 | 57 | assumeRole: mockStsAssumeRole,
|
@@ -82,6 +85,21 @@ describe('Configure AWS Credentials', () => {
|
82 | 85 | }
|
83 | 86 | });
|
84 | 87 |
|
| 88 | + aws.config.getCredentials.mockReset(); |
| 89 | + aws.config.getCredentials |
| 90 | + .mockImplementationOnce(callback => { |
| 91 | + aws.config.credentials = { |
| 92 | + accessKeyId: FAKE_ACCESS_KEY_ID |
| 93 | + } |
| 94 | + callback(null); |
| 95 | + }) |
| 96 | + .mockImplementationOnce(callback => { |
| 97 | + aws.config.credentials = { |
| 98 | + accessKeyId: FAKE_STS_ACCESS_KEY_ID |
| 99 | + } |
| 100 | + callback(null); |
| 101 | + }); |
| 102 | + |
85 | 103 | mockStsAssumeRole.mockImplementation(() => {
|
86 | 104 | return {
|
87 | 105 | promise() {
|
@@ -134,6 +152,59 @@ describe('Configure AWS Credentials', () => {
|
134 | 152 | expect(core.setSecret).toHaveBeenCalledWith(FAKE_ACCOUNT_ID);
|
135 | 153 | });
|
136 | 154 |
|
| 155 | + test('action with no accessible credentials fails', async () => { |
| 156 | + process.env.SHOW_STACK_TRACE = 'false'; |
| 157 | + const mockInputs = {'aws-region': FAKE_REGION}; |
| 158 | + core.getInput = jest |
| 159 | + .fn() |
| 160 | + .mockImplementation(mockGetInput(mockInputs)); |
| 161 | + aws.config.getCredentials.mockReset(); |
| 162 | + aws.config.getCredentials.mockImplementation(callback => { |
| 163 | + callback(new Error('No credentials to load')); |
| 164 | + }); |
| 165 | + |
| 166 | + await run(); |
| 167 | + |
| 168 | + expect(core.setFailed).toHaveBeenCalledWith("Credentials could not be loaded, please check your action inputs: No credentials to load"); |
| 169 | + }); |
| 170 | + |
| 171 | + test('action with empty credentials fails', async () => { |
| 172 | + process.env.SHOW_STACK_TRACE = 'false'; |
| 173 | + const mockInputs = {'aws-region': FAKE_REGION}; |
| 174 | + core.getInput = jest |
| 175 | + .fn() |
| 176 | + .mockImplementation(mockGetInput(mockInputs)); |
| 177 | + aws.config.getCredentials.mockReset(); |
| 178 | + aws.config.getCredentials.mockImplementation(callback => { |
| 179 | + aws.config.credentials = { |
| 180 | + accessKeyId: '' |
| 181 | + } |
| 182 | + callback(null); |
| 183 | + }); |
| 184 | + |
| 185 | + await run(); |
| 186 | + |
| 187 | + expect(core.setFailed).toHaveBeenCalledWith("Credentials could not be loaded, please check your action inputs: Access key ID empty after loading credentials"); |
| 188 | + }); |
| 189 | + |
| 190 | + test('action fails when credentials are not set in the SDK correctly', async () => { |
| 191 | + process.env.SHOW_STACK_TRACE = 'false'; |
| 192 | + core.getInput = jest |
| 193 | + .fn() |
| 194 | + .mockImplementation(mockGetInput(ASSUME_ROLE_INPUTS)); |
| 195 | + aws.config.getCredentials.mockReset(); |
| 196 | + aws.config.getCredentials.mockImplementation(callback => { |
| 197 | + aws.config.credentials = { |
| 198 | + accessKeyId: FAKE_ACCESS_KEY_ID |
| 199 | + } |
| 200 | + callback(null); |
| 201 | + }); |
| 202 | + |
| 203 | + await run(); |
| 204 | + |
| 205 | + expect(core.setFailed).toHaveBeenCalledWith("Unexpected failure: Credentials loaded by the SDK do not match the access key ID configured by the action"); |
| 206 | + }); |
| 207 | + |
137 | 208 | test('session token is optional', async () => {
|
138 | 209 | const mockInputs = {...CREDS_INPUTS, 'aws-region': 'eu-west-1'};
|
139 | 210 | core.getInput = jest
|
|
0 commit comments