Skip to content

Commit a44f3b4

Browse files
committed
added test for undefined credentials
1 parent 449d677 commit a44f3b4

1 file changed

Lines changed: 43 additions & 13 deletions

File tree

test/integration/auth/aws4.test.ts

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ import { aws4Sign } from '../../../src/aws4';
99
// To run this test, simply run `./etc/aws-test.sh`.
1010

1111
describe('AwsSigV4', function () {
12-
beforeEach(function () {
13-
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
14-
this.skipReason = 'AWS credentials are not present in the environment';
15-
this.skip();
16-
}
17-
});
18-
1912
const testSigning = async credentials => {
2013
const host = 'sts.amazonaws.com';
2114
const body = 'Action=GetCallerIdentity&Version=2011-06-15';
@@ -50,7 +43,7 @@ describe('AwsSigV4', function () {
5043
for (const [key, value] of Object.entries(headers)) {
5144
fetchHeaders.append(key, value.toString());
5245
}
53-
if (credentials.sessionToken) {
46+
if (credentials && credentials.sessionToken) {
5447
fetchHeaders.append('X-Amz-Security-Token', credentials.sessionToken);
5548
}
5649
fetchHeaders.append('Authorization', authorization);
@@ -60,12 +53,20 @@ describe('AwsSigV4', function () {
6053
headers: fetchHeaders,
6154
body
6255
});
63-
expect(response.status).to.equal(200);
64-
expect(response.statusText).to.equal('OK');
6556
const text = await response.text();
66-
expect(text).to.match(
67-
/<GetCallerIdentityResponse xmlns="https:\/\/sts.amazonaws.com\/doc\/2011-06-15\/">/
68-
);
57+
58+
const expectSuccess = credentials !== undefined;
59+
if (expectSuccess) {
60+
expect(response.status).to.equal(200);
61+
expect(response.statusText).to.equal('OK');
62+
expect(text).to.match(
63+
/<GetCallerIdentityResponse xmlns="https:\/\/sts.amazonaws.com\/doc\/2011-06-15\/">/
64+
);
65+
} else {
66+
expect(response.status).to.equal(403);
67+
expect(response.statusText).to.equal('Forbidden');
68+
expect(text).to.match(/<Code>InvalidClientTokenId<\/Code>/);
69+
}
6970
};
7071

7172
describe('AWS4 signs requests with missing AWS env vars', function () {
@@ -75,6 +76,11 @@ describe('AwsSigV4', function () {
7576
process.env.AWS_SECRET_ACCESS_KEY ||
7677
process.env.AWS_SESSION_TOKEN
7778
) {
79+
console.log('Skipping missing credentials test because AWS credentials are set: ', {
80+
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID ? 'SET' : 'NOT SET',
81+
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY ? 'SET' : 'NOT SET',
82+
AWS_SESSION_TOKEN: process.env.AWS_SESSION_TOKEN ? 'SET' : 'NOT SET'
83+
});
7884
this.skipReason = 'Skipping missing credentials test because AWS credentials are set';
7985
this.skip();
8086
}
@@ -88,9 +94,21 @@ describe('AwsSigV4', function () {
8894
describe('AWS4 signs requests with AWS permanent env vars', function () {
8995
before(function () {
9096
if (process.env.AWS_SESSION_TOKEN) {
97+
console.log('Skipping permanent credentials test because AWS_SESSION_TOKEN is set', {
98+
AWS_SESSION_TOKEN: process.env.AWS_SESSION_TOKEN ? 'SET' : 'NOT SET'
99+
});
91100
this.skipReason = 'Skipping permanent credentials test because session token is set';
92101
this.skip();
93102
}
103+
104+
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
105+
console.log('Skipping permanent credentials test because AWS credentials are not set', {
106+
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID ? 'SET' : 'NOT SET',
107+
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY ? 'SET' : 'NOT SET'
108+
});
109+
this.skipReason = 'Skipping permanent credentials test because AWS credentials are not set';
110+
this.skip();
111+
}
94112
});
95113

96114
it('AWS4 signs requests with AWS permanent env vars', async () => {
@@ -105,9 +123,21 @@ describe('AwsSigV4', function () {
105123
describe('AWS4 signs requests with AWS session env vars', function () {
106124
before(function () {
107125
if (!process.env.AWS_SESSION_TOKEN) {
126+
console.log('Skipping session credentials test because AWS_SESSION_TOKEN is not set', {
127+
AWS_SESSION_TOKEN: process.env.AWS_SESSION_TOKEN ? 'SET' : 'NOT SET'
128+
});
108129
this.skipReason = 'Skipping session credentials test because session token is not set';
109130
this.skip();
110131
}
132+
133+
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
134+
console.log('Skipping session credentials test because AWS credentials are not set', {
135+
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID ? 'SET' : 'NOT SET',
136+
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY ? 'SET' : 'NOT SET'
137+
});
138+
this.skipReason = 'Skipping session credentials test because AWS credentials are not set';
139+
this.skip();
140+
}
111141
});
112142

113143
it('AWS4 signs requests with AWS session env vars', async () => {

0 commit comments

Comments
 (0)