forked from claudiajs/claudia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-authorizer-integration-spec.js
136 lines (135 loc) · 4.63 KB
/
custom-authorizer-integration-spec.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const create = require('../src/commands/create'),
update = require('../src/commands/update'),
setVersion = require('../src/commands/set-version'),
destroy = require('../src/commands/destroy'),
path = require('path'),
tmppath = require('../src/util/tmppath'),
callApi = require('../src/util/call-api'),
fsUtil = require('../src/util/fs-util'),
fs = require('fs'),
fsPromise = require('../src/util/fs-promise'),
awsRegion = require('./util/test-aws-region');
describe('customAuthorizers', () => {
'use strict';
let workingdir, testRunName, apiId;
const invoke = function (url, options) {
if (!options) {
options = {};
}
options.retry = 403;
return callApi(apiId, awsRegion, url, options);
},
waitUntilDeployed = function (version) {
return invoke(version + '/', {
method: 'GET',
resolveErrors: false,
retryTimeout: process.env.AWS_DEPLOY_TIMEOUT || 10000,
retries: process.env.AWS_DEPLOY_RETRIES || 5
});
},
createTestFixture = function () {
workingdir = tmppath();
testRunName = 'test' + Date.now();
fs.mkdirSync(workingdir);
fsUtil.copy('spec/test-projects/custom-authorizers', workingdir, true);
return fsPromise.readFileAsync(path.join(workingdir, 'api.js'), 'utf-8')
.then(content => content.replace('TEST-AUTH-LAMBDA-NAME', `${testRunName}Auth`))
.then(content => fsPromise.writeFileAsync(path.join(workingdir, 'api.js'), content))
.then(() => create({
name: `${testRunName}Auth`,
version: 'original',
region: awsRegion,
config: path.join(workingdir, 'claudia-auth.json'),
handler: 'authorizer.auth',
source: workingdir
}))
.then(() => create({
name: testRunName,
version: 'original',
region: awsRegion,
config: path.join(workingdir, 'claudia-api.json'),
'api-module': 'api',
source: workingdir
}))
.then(result => {
apiId = result.api.id;
});
},
setUpTests = function (version) {
it('does not block access to methods without an authorizer', done => {
invoke(version + '/', {
method: 'GET',
resolveErrors: false
})
.then(response => expect(JSON.parse(response.body)).toEqual('OK'))
.then(done, done.fail);
});
it('blocks access to methods without an authorizer without authentication headers', done => {
invoke(version + '/locked', {
method: 'GET',
resolveErrors: true
})
.then(response => {
expect(response.statusCode).toEqual(401);
expect(response.headers['x-amzn-errortype']).toEqual('UnauthorizedException');
expect(JSON.parse(response.body)).toEqual({ message: 'Unauthorized' });
})
.then(done, done.fail);
});
it('respects IAM policy for unauthorized users', done => {
invoke(version + '/locked', {
method: 'GET',
headers: {'Authorization': 'Bob-123'},
resolveErrors: true
})
.then(response => {
expect(response.statusCode).toEqual(403);
expect(response.headers['x-amzn-errortype']).toEqual('AccessDeniedException');
expect(JSON.parse(response.body)).toEqual({ Message: 'User is not authorized to access this resource' });
})
.then(done, done.fail);
});
it('respects IAM policy for authorized users', done => {
invoke(version + '/unlocked', {
method: 'GET',
headers: {'Authorization': 'Bob-123'},
resolveErrors: false
})
.then(response => expect(JSON.parse(response.body)).toEqual('OK for Bob'))
.then(done, done.fail);
});
};
beforeAll(done => {
console.log('creating custom authorizer examples');
createTestFixture()
.then(() => waitUntilDeployed('original'))
.then(() => console.log('created'))
.then(done, done.fail);
});
afterAll(done => {
console.log('destroying custom authorizer examples');
destroy({ config: path.join(workingdir, 'claudia-auth.json')})
.then(() => destroy({config: path.join(workingdir, 'claudia-api.json')}))
.then(() => fsUtil.rmDir(workingdir))
.then(() => console.log('destroyed'))
.catch(err => console.log('error cleaning up', err))
.then(done);
});
describe('create wires up authorizers intially', () => {
setUpTests('original');
});
describe('update creates a new version', () => {
beforeAll(done => {
console.log('updating custom authorizer examples');
setVersion({ source: workingdir, config: path.join(workingdir, 'claudia-auth.json'), version: 'new' })
.then(() => update({ source: workingdir, config: path.join(workingdir, 'claudia-api.json'), version: 'new' }))
.then(() => waitUntilDeployed('new'))
.then(() => console.log('updated'))
.then(done, err => {
console.log('failed to update custom authorizer examples', err);
done.fail();
});
});
setUpTests('new');
});
});