Skip to content

Commit 0cc42bf

Browse files
committed
Add more index tests
1 parent 18b8ead commit 0cc42bf

File tree

2 files changed

+92
-25
lines changed

2 files changed

+92
-25
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ module.exports = function(options) {
6060
const authSource = getObjectPath(['authorizer', 'identitySource'], e);
6161
const authValidatorExp = getObjectPath(['authorizer', 'identityValidationExpression'], e);
6262
const authCacheTtl = getObjectPath(['authorizer', 'resultTtlInSeconds'], e);
63+
const authorizerArn = getObjectPath(['authorizer', 'arn'], e);
64+
const authorizerName = getObjectPath(['authorizer', 'name'], e);
6365
const authorizerFunction = (
64-
getObjectPath(getObjectPath(['authorizer', 'arn'], e) || '', httpConfig.authorizers) ||
65-
getObjectPath(getObjectPath(['authorizer', 'name'], e) || '', slsHandlers)
66+
(authorizerArn && getObjectPath(authorizerArn, httpConfig.authorizers)) ||
67+
(authorizerName && getObjectPath(authorizerName, slsHandlers)) ||
68+
(authorizerName && getObjectPath(`${funcConf.handler.split('.')[0]}.${authorizerName}`, slsHandlers))
6669
);
6770
// Prepare callback
6871
const callbacks = [

tests/index.test.js

Lines changed: 87 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,37 @@ describe('serverlessify', () => {
2121
let req;
2222
let res;
2323
let next;
24-
const testConf = {
25-
service: 'test-service',
26-
functions: {
27-
'test-function': {
28-
handler: 'test-handler.test-function-handler',
29-
events: [
30-
{
31-
http: {
32-
path: 'test-path',
33-
method: 'GET',
34-
},
35-
},
36-
],
37-
},
38-
},
39-
};
40-
const testHandler = sinon.spy();
41-
const testHandlers = {
42-
'test-handler': {
43-
'test-function-handler': testHandler,
44-
},
45-
};
24+
let testConf;
25+
let testHandler;
26+
let testHandlers;
4627

4728
beforeEach(() => {
4829
httpEventHandler = sinon.spy();
4930
sls = serverlessify({
5031
http: { eventHandler: httpEventHandler },
5132
});
52-
testHandler.reset();
33+
testConf = {
34+
service: 'test-service',
35+
functions: {
36+
'test-function': {
37+
handler: 'test-handler.test-function-handler',
38+
events: [
39+
{
40+
http: {
41+
path: 'test-path',
42+
method: 'GET',
43+
},
44+
},
45+
],
46+
},
47+
},
48+
};
49+
testHandler = sinon.spy();
50+
testHandlers = {
51+
'test-handler': {
52+
'test-function-handler': testHandler,
53+
},
54+
};
5355
req = {
5456
method: 'test-req-method',
5557
headers: { 'test-req-header': 'test-req-header-value' },
@@ -115,5 +117,67 @@ describe('serverlessify', () => {
115117
reqDecorateCb(req, res, next);
116118
expect(req.lambda.event.principalId).to.equal('test-principal-id');
117119
});
120+
121+
it('should wrap a lambda with a wrapper function', () => {
122+
const wrappedLambda = sinon.spy();
123+
const wrapLambda = sinon.stub().returns(wrappedLambda);
124+
sls = serverlessify({
125+
http: {
126+
eventHandler: httpEventHandler,
127+
wrapLambda,
128+
},
129+
});
130+
sls(testConf, testHandlers);
131+
const callbacks = httpEventHandler.firstCall.args[2];
132+
const lambdaCb = callbacks[1];
133+
req.lambda = {
134+
event: 'test-req-event',
135+
context: 'test-req-context',
136+
};
137+
lambdaCb(req, res);
138+
expect(wrapLambda).to.have.been.calledWith(testHandler);
139+
expect(wrappedLambda).to.have.been.calledWith(
140+
req.lambda.event,
141+
req.lambda.context
142+
);
143+
expect(testHandler).to.have.callCount(0);
144+
});
145+
146+
it('should add an authorization step with a local function', () => {
147+
testConf.functions['test-function'].events[0].http.authorizer = {
148+
name: 'test-authorizer',
149+
};
150+
const testAuthorizer = sinon.spy();
151+
testHandlers['test-handler']['test-authorizer'] = testAuthorizer;
152+
sls(testConf, testHandlers);
153+
const callbacks = httpEventHandler.firstCall.args[2];
154+
expect(callbacks).to.be.an('array');
155+
expect(callbacks).to.have.lengthOf(4);
156+
});
157+
158+
it('should add an authorization step with a global authorizer', () => {
159+
testConf.functions['test-function'].events[0].http.authorizer = {
160+
arn: 'test-authorizer-arn',
161+
};
162+
const testAuthorizer = sinon.spy();
163+
sls = serverlessify({
164+
http: {
165+
eventHandler: httpEventHandler,
166+
authorizers: {
167+
'test-authorizer-arn': testAuthorizer,
168+
},
169+
},
170+
});
171+
sls(testConf, testHandlers);
172+
const callbacks = httpEventHandler.firstCall.args[2];
173+
expect(callbacks).to.be.an('array');
174+
expect(callbacks).to.have.lengthOf(4);
175+
});
176+
177+
it('should expose added functions via getFunction', () => {
178+
sls(testConf, testHandlers);
179+
const f = sls.getFunction('test-handler', 'test-function');
180+
expect(f).to.exists;
181+
});
118182
});
119183
});

0 commit comments

Comments
 (0)