|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const expect = require('chai').expect; |
| 4 | +const Serverless = require('serverless/lib/Serverless'); |
| 5 | +const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider'); |
| 6 | +const ServerlessStepFunctions = require('./../../../index'); |
| 7 | + |
| 8 | +describe('#methods()', () => { |
| 9 | + let serverless; |
| 10 | + let serverlessStepFunctions; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + serverless = new Serverless(); |
| 14 | + serverless.setProvider('aws', new AwsProvider(serverless)); |
| 15 | + serverless.service.provider.compiledCloudFormationTemplate = { |
| 16 | + Resources: {}, |
| 17 | + }; |
| 18 | + |
| 19 | + const options = { |
| 20 | + stage: 'dev', |
| 21 | + region: 'us-east-1', |
| 22 | + }; |
| 23 | + serverlessStepFunctions = new ServerlessStepFunctions(serverless, options); |
| 24 | + serverlessStepFunctions.serverless.service.stepFunctions = { |
| 25 | + stateMachines: { |
| 26 | + first: {}, |
| 27 | + }, |
| 28 | + }; |
| 29 | + serverlessStepFunctions.apiGatewayResourceLogicalIds = { |
| 30 | + 'users/create': 'ApiGatewayResourceUsersCreate', |
| 31 | + 'users/list': 'ApiGatewayResourceUsersList', |
| 32 | + 'users/update': 'ApiGatewayResourceUsersUpdate', |
| 33 | + 'users/delete': 'ApiGatewayResourceUsersDelete', |
| 34 | + 'users/any': 'ApiGatewayResourceUsersAny', |
| 35 | + }; |
| 36 | + serverlessStepFunctions.apiGatewayResourceNames = { |
| 37 | + 'users/create': 'UsersCreate', |
| 38 | + 'users/list': 'UsersList', |
| 39 | + 'users/update': 'UsersUpdate', |
| 40 | + 'users/delete': 'UsersDelete', |
| 41 | + 'users/any': 'UsersAny', |
| 42 | + }; |
| 43 | + serverlessStepFunctions.pluginhttpValidated = {}; |
| 44 | + }); |
| 45 | + |
| 46 | + it('should create preflight method for CORS enabled resource', () => { |
| 47 | + serverlessStepFunctions.pluginhttpValidated.corsPreflight = { |
| 48 | + 'users/update': { |
| 49 | + origin: 'http://example.com', |
| 50 | + headers: ['*'], |
| 51 | + methods: ['OPTIONS', 'PUT'], |
| 52 | + allowCredentials: false, |
| 53 | + }, |
| 54 | + 'users/create': { |
| 55 | + origins: ['*', 'http://example.com'], |
| 56 | + headers: ['*'], |
| 57 | + methods: ['OPTIONS', 'POST'], |
| 58 | + allowCredentials: true, |
| 59 | + }, |
| 60 | + 'users/delete': { |
| 61 | + origins: ['*'], |
| 62 | + headers: ['CustomHeaderA', 'CustomHeaderB'], |
| 63 | + methods: ['OPTIONS', 'DELETE'], |
| 64 | + allowCredentials: false, |
| 65 | + }, |
| 66 | + 'users/any': { |
| 67 | + origins: ['http://example.com'], |
| 68 | + headers: ['*'], |
| 69 | + methods: ['OPTIONS', 'ANY'], |
| 70 | + allowCredentials: false, |
| 71 | + }, |
| 72 | + }; |
| 73 | + return serverlessStepFunctions.compileCors().then(() => { |
| 74 | + // users/create |
| 75 | + expect( |
| 76 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 77 | + .Resources.ApiGatewayMethodUsersCreateOptions |
| 78 | + .Properties.Integration.IntegrationResponses[0] |
| 79 | + .ResponseParameters['method.response.header.Access-Control-Allow-Origin'] |
| 80 | + ).to.equal('\'*,http://example.com\''); |
| 81 | + |
| 82 | + expect( |
| 83 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 84 | + .Resources.ApiGatewayMethodUsersCreateOptions |
| 85 | + .Properties.Integration.IntegrationResponses[0] |
| 86 | + .ResponseParameters['method.response.header.Access-Control-Allow-Headers'] |
| 87 | + ).to.equal('\'*\''); |
| 88 | + |
| 89 | + expect( |
| 90 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 91 | + .Resources.ApiGatewayMethodUsersCreateOptions |
| 92 | + .Properties.Integration.IntegrationResponses[0] |
| 93 | + .ResponseParameters['method.response.header.Access-Control-Allow-Methods'] |
| 94 | + ).to.equal('\'OPTIONS,POST\''); |
| 95 | + |
| 96 | + expect( |
| 97 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 98 | + .Resources.ApiGatewayMethodUsersCreateOptions |
| 99 | + .Properties.Integration.IntegrationResponses[0] |
| 100 | + .ResponseParameters['method.response.header.Access-Control-Allow-Credentials'] |
| 101 | + ).to.equal('\'true\''); |
| 102 | + |
| 103 | + // users/update |
| 104 | + expect( |
| 105 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 106 | + .Resources.ApiGatewayMethodUsersUpdateOptions |
| 107 | + .Properties.Integration.IntegrationResponses[0] |
| 108 | + .ResponseParameters['method.response.header.Access-Control-Allow-Origin'] |
| 109 | + ).to.equal('\'http://example.com\''); |
| 110 | + |
| 111 | + expect( |
| 112 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 113 | + .Resources.ApiGatewayMethodUsersUpdateOptions |
| 114 | + .Properties.Integration.IntegrationResponses[0] |
| 115 | + .ResponseParameters['method.response.header.Access-Control-Allow-Methods'] |
| 116 | + ).to.equal('\'OPTIONS,PUT\''); |
| 117 | + |
| 118 | + expect( |
| 119 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 120 | + .Resources.ApiGatewayMethodUsersUpdateOptions |
| 121 | + .Properties.Integration.IntegrationResponses[0] |
| 122 | + .ResponseParameters['method.response.header.Access-Control-Allow-Credentials'] |
| 123 | + ).to.equal('\'false\''); |
| 124 | + |
| 125 | + // users/delete |
| 126 | + expect( |
| 127 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 128 | + .Resources.ApiGatewayMethodUsersDeleteOptions |
| 129 | + .Properties.Integration.IntegrationResponses[0] |
| 130 | + .ResponseParameters['method.response.header.Access-Control-Allow-Origin'] |
| 131 | + ).to.equal('\'*\''); |
| 132 | + |
| 133 | + expect( |
| 134 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 135 | + .Resources.ApiGatewayMethodUsersDeleteOptions |
| 136 | + .Properties.Integration.IntegrationResponses[0] |
| 137 | + .ResponseParameters['method.response.header.Access-Control-Allow-Headers'] |
| 138 | + ).to.equal('\'CustomHeaderA,CustomHeaderB\''); |
| 139 | + |
| 140 | + expect( |
| 141 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 142 | + .Resources.ApiGatewayMethodUsersDeleteOptions |
| 143 | + .Properties.Integration.IntegrationResponses[0] |
| 144 | + .ResponseParameters['method.response.header.Access-Control-Allow-Methods'] |
| 145 | + ).to.equal('\'OPTIONS,DELETE\''); |
| 146 | + |
| 147 | + expect( |
| 148 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 149 | + .Resources.ApiGatewayMethodUsersDeleteOptions |
| 150 | + .Properties.Integration.IntegrationResponses[0] |
| 151 | + .ResponseParameters['method.response.header.Access-Control-Allow-Credentials'] |
| 152 | + ).to.equal('\'false\''); |
| 153 | + |
| 154 | + // users/any |
| 155 | + expect( |
| 156 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 157 | + .Resources.ApiGatewayMethodUsersAnyOptions |
| 158 | + .Properties.Integration.IntegrationResponses[0] |
| 159 | + .ResponseParameters['method.response.header.Access-Control-Allow-Origin'] |
| 160 | + ).to.equal('\'http://example.com\''); |
| 161 | + |
| 162 | + expect( |
| 163 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 164 | + .Resources.ApiGatewayMethodUsersAnyOptions |
| 165 | + .Properties.Integration.IntegrationResponses[0] |
| 166 | + .ResponseParameters['method.response.header.Access-Control-Allow-Headers'] |
| 167 | + ).to.equal('\'*\''); |
| 168 | + |
| 169 | + expect( |
| 170 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 171 | + .Resources.ApiGatewayMethodUsersAnyOptions |
| 172 | + .Properties.Integration.IntegrationResponses[0] |
| 173 | + .ResponseParameters['method.response.header.Access-Control-Allow-Methods'] |
| 174 | + ).to.equal('\'OPTIONS,DELETE,GET,HEAD,PATCH,POST,PUT\''); |
| 175 | + |
| 176 | + expect( |
| 177 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 178 | + .Resources.ApiGatewayMethodUsersAnyOptions |
| 179 | + .Properties.Integration.IntegrationResponses[0] |
| 180 | + .ResponseParameters['method.response.header.Access-Control-Allow-Credentials'] |
| 181 | + ).to.equal('\'false\''); |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments