|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const chai = require('chai') |
| 4 | +const Serverless = require('serverless/lib/Serverless') |
| 5 | +const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider') |
| 6 | +const ServerlessApigatewayServiceProxy = require('./../../index') |
| 7 | + |
| 8 | +chai.use(require('chai-as-promised')) |
| 9 | +const expect = require('chai').expect |
| 10 | + |
| 11 | +describe('#compileIamRoleToSqs()', () => { |
| 12 | + let serverless |
| 13 | + let serverlessApigatewayServiceProxy |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + serverless = new Serverless() |
| 17 | + serverless.servicePath = true |
| 18 | + serverless.service.service = 'apigw-service-proxy' |
| 19 | + const options = { |
| 20 | + stage: 'dev', |
| 21 | + region: 'us-east-1' |
| 22 | + } |
| 23 | + serverless.setProvider('aws', new AwsProvider(serverless)) |
| 24 | + serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} } |
| 25 | + serverlessApigatewayServiceProxy = new ServerlessApigatewayServiceProxy(serverless, options) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should create corresponding resources when sqs proxies are given', async () => { |
| 29 | + serverlessApigatewayServiceProxy.validated = { |
| 30 | + events: [ |
| 31 | + { |
| 32 | + serviceName: 'sqs', |
| 33 | + http: { |
| 34 | + queueName: 'myQueue', |
| 35 | + path: 'sqs', |
| 36 | + method: 'post' |
| 37 | + } |
| 38 | + } |
| 39 | + ] |
| 40 | + } |
| 41 | + serverlessApigatewayServiceProxy.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi' |
| 42 | + serverlessApigatewayServiceProxy.apiGatewayResources = { |
| 43 | + sqs: { |
| 44 | + name: 'sqs', |
| 45 | + resourceLogicalId: 'ApiGatewayResourceSqs' |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + await expect(serverlessApigatewayServiceProxy.compileMethodsToSqs()).to.be.fulfilled |
| 50 | + expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({ |
| 51 | + ApiGatewayMethodsqsPost: { |
| 52 | + Type: 'AWS::ApiGateway::Method', |
| 53 | + Properties: { |
| 54 | + HttpMethod: 'POST', |
| 55 | + RequestParameters: {}, |
| 56 | + AuthorizationType: 'NONE', |
| 57 | + ApiKeyRequired: false, |
| 58 | + ResourceId: { Ref: 'ApiGatewayResourceSqs' }, |
| 59 | + RestApiId: { Ref: 'ApiGatewayRestApi' }, |
| 60 | + Integration: { |
| 61 | + IntegrationHttpMethod: 'POST', |
| 62 | + Type: 'AWS', |
| 63 | + Credentials: { 'Fn::GetAtt': ['ApigatewayToSqsRole', 'Arn'] }, |
| 64 | + Uri: { |
| 65 | + 'Fn::Join': [ |
| 66 | + '', |
| 67 | + [ |
| 68 | + 'arn:aws:apigateway:', |
| 69 | + { Ref: 'AWS::Region' }, |
| 70 | + ':sqs:path//', |
| 71 | + { Ref: 'AWS::AccountId' }, |
| 72 | + '/', |
| 73 | + '"myQueue"' |
| 74 | + ] |
| 75 | + ] |
| 76 | + }, |
| 77 | + RequestParameters: { |
| 78 | + 'integration.request.querystring.Action': "'SendMessage'", |
| 79 | + 'integration.request.querystring.MessageBody': 'method.request.body.message' |
| 80 | + }, |
| 81 | + RequestTemplates: { 'application/json': '{statusCode:200}' }, |
| 82 | + IntegrationResponses: [ |
| 83 | + { |
| 84 | + StatusCode: 200, |
| 85 | + SelectionPattern: 200, |
| 86 | + ResponseParameters: {}, |
| 87 | + ResponseTemplates: {} |
| 88 | + }, |
| 89 | + { |
| 90 | + StatusCode: 400, |
| 91 | + SelectionPattern: 400, |
| 92 | + ResponseParameters: {}, |
| 93 | + ResponseTemplates: {} |
| 94 | + } |
| 95 | + ] |
| 96 | + }, |
| 97 | + MethodResponses: [ |
| 98 | + { ResponseParameters: {}, ResponseModels: {}, StatusCode: 200 }, |
| 99 | + { ResponseParameters: {}, ResponseModels: {}, StatusCode: 400 } |
| 100 | + ] |
| 101 | + } |
| 102 | + } |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should create corresponding resources when sqs proxies are given with cors', async () => { |
| 107 | + serverlessApigatewayServiceProxy.validated = { |
| 108 | + events: [ |
| 109 | + { |
| 110 | + serviceName: 'sqs', |
| 111 | + http: { |
| 112 | + queueName: 'myQueue', |
| 113 | + path: 'sqs', |
| 114 | + method: 'post', |
| 115 | + cors: { |
| 116 | + origins: ['*'], |
| 117 | + origin: '*', |
| 118 | + methods: ['OPTIONS', 'POST'], |
| 119 | + headers: [ |
| 120 | + 'Content-Type', |
| 121 | + 'X-Amz-Date', |
| 122 | + 'Authorization', |
| 123 | + 'X-Api-Key', |
| 124 | + 'X-Amz-Security-Token', |
| 125 | + 'X-Amz-User-Agent' |
| 126 | + ], |
| 127 | + allowCredentials: false |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + ] |
| 132 | + } |
| 133 | + serverlessApigatewayServiceProxy.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi' |
| 134 | + serverlessApigatewayServiceProxy.apiGatewayResources = { |
| 135 | + sqs: { |
| 136 | + name: 'sqs', |
| 137 | + resourceLogicalId: 'ApiGatewayResourceSqs' |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + await expect(serverlessApigatewayServiceProxy.compileMethodsToSqs()).to.be.fulfilled |
| 142 | + expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({ |
| 143 | + ApiGatewayMethodsqsPost: { |
| 144 | + Type: 'AWS::ApiGateway::Method', |
| 145 | + Properties: { |
| 146 | + HttpMethod: 'POST', |
| 147 | + RequestParameters: {}, |
| 148 | + AuthorizationType: 'NONE', |
| 149 | + ApiKeyRequired: false, |
| 150 | + ResourceId: { Ref: 'ApiGatewayResourceSqs' }, |
| 151 | + RestApiId: { Ref: 'ApiGatewayRestApi' }, |
| 152 | + Integration: { |
| 153 | + IntegrationHttpMethod: 'POST', |
| 154 | + Type: 'AWS', |
| 155 | + Credentials: { 'Fn::GetAtt': ['ApigatewayToSqsRole', 'Arn'] }, |
| 156 | + Uri: { |
| 157 | + 'Fn::Join': [ |
| 158 | + '', |
| 159 | + [ |
| 160 | + 'arn:aws:apigateway:', |
| 161 | + { Ref: 'AWS::Region' }, |
| 162 | + ':sqs:path//', |
| 163 | + { Ref: 'AWS::AccountId' }, |
| 164 | + '/', |
| 165 | + '"myQueue"' |
| 166 | + ] |
| 167 | + ] |
| 168 | + }, |
| 169 | + RequestParameters: { |
| 170 | + 'integration.request.querystring.Action': "'SendMessage'", |
| 171 | + 'integration.request.querystring.MessageBody': 'method.request.body.message' |
| 172 | + }, |
| 173 | + RequestTemplates: { 'application/json': '{statusCode:200}' }, |
| 174 | + IntegrationResponses: [ |
| 175 | + { |
| 176 | + StatusCode: 200, |
| 177 | + SelectionPattern: 200, |
| 178 | + ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" }, |
| 179 | + ResponseTemplates: {} |
| 180 | + }, |
| 181 | + { |
| 182 | + StatusCode: 400, |
| 183 | + SelectionPattern: 400, |
| 184 | + ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" }, |
| 185 | + ResponseTemplates: {} |
| 186 | + } |
| 187 | + ] |
| 188 | + }, |
| 189 | + MethodResponses: [ |
| 190 | + { |
| 191 | + ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" }, |
| 192 | + ResponseModels: {}, |
| 193 | + StatusCode: 200 |
| 194 | + }, |
| 195 | + { |
| 196 | + ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" }, |
| 197 | + ResponseModels: {}, |
| 198 | + StatusCode: 400 |
| 199 | + } |
| 200 | + ] |
| 201 | + } |
| 202 | + } |
| 203 | + }) |
| 204 | + }) |
| 205 | +}) |
0 commit comments