Repository: serverless/serverless (JavaScript)
Date: 2016-11-01 23:58:08+00:00
Task type: Bug Fix

Issue:
resultTtlInSeconds defaults to 300 when set to 0
# This is a Bug Report

## Description

* What went wrong?

`resultTtlInSeconds` defaults to 300 when set to 0.

This is oviously due to [this line](https://github.com/serverless/serverless/blob/f817933909cb524dcde729348bf229ae355e15ac/lib/plugins/aws/deploy/compile/events/apiGateway/lib/authorizers.js#L54
) (since 0 is falesy...)



Issue discussion:
Yup thats a bug that needs to be resolved. Thanks


Solution patch:
diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js
index 9dc087e83b0..99a99502c4e 100644
--- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js
+++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js
@@ -118,9 +118,8 @@ module.exports = {
         throw new this.serverless.classes.Error('Please provide either an authorizer name or ARN');
       }
 
-      if (authorizer.resultTtlInSeconds) {
-        resultTtlInSeconds = Number.parseInt(authorizer.resultTtlInSeconds, 10);
-      }
+      resultTtlInSeconds = Number.parseInt(authorizer.resultTtlInSeconds, 10);
+      resultTtlInSeconds = Number.isNaN(resultTtlInSeconds) ? 300 : resultTtlInSeconds;
 
       identitySource = authorizer.identitySource;
       identityValidationExpression = authorizer.identityValidationExpression;
@@ -134,10 +133,6 @@ module.exports = {
       throw new this.serverless.classes.Error(errorMessage);
     }
 
-    if (typeof resultTtlInSeconds === 'undefined') {
-      resultTtlInSeconds = 300;
-    }
-
     if (typeof identitySource === 'undefined') {
       identitySource = 'method.request.header.Authorization';
     }


Test patch:
diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/validate.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/validate.js
index 39a7b479a61..f7fa2860679 100644
--- a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/validate.js
+++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/validate.js
@@ -284,4 +284,32 @@ describe('#validate()', () => {
     expect(authorizer.identitySource).to.equal('method.request.header.Custom');
     expect(authorizer.identityValidationExpression).to.equal('foo');
   });
+
+  it('should accept authorizer config when resultTtlInSeconds is 0', () => {
+    awsCompileApigEvents.serverless.service.functions = {
+      foo: {},
+      first: {
+        events: [
+          {
+            http: {
+              method: 'GET',
+              path: 'foo/bar',
+              authorizer: {
+                name: 'foo',
+                resultTtlInSeconds: 0,
+                identitySource: 'method.request.header.Custom',
+                identityValidationExpression: 'foo',
+              },
+            },
+          },
+        ],
+      },
+    };
+
+    const validated = awsCompileApigEvents.validate();
+    const authorizer = validated.events[0].http.authorizer;
+    expect(authorizer.resultTtlInSeconds).to.equal(0);
+    expect(authorizer.identitySource).to.equal('method.request.header.Custom');
+    expect(authorizer.identityValidationExpression).to.equal('foo');
+  });
 });