|
| 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('#compileResources()', () => { |
| 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 = { Resources: {} }; |
| 16 | + serverlessStepFunctions = new ServerlessStepFunctions(serverless); |
| 17 | + serverlessStepFunctions.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi'; |
| 18 | + serverlessStepFunctions.pluginhttpValidated = {}; |
| 19 | + }); |
| 20 | + |
| 21 | + // sorted makes parent refs easier |
| 22 | + it('should construct the correct (sorted) resourcePaths array', () => { |
| 23 | + serverlessStepFunctions.pluginhttpValidated.events = [ |
| 24 | + { |
| 25 | + http: { |
| 26 | + path: '', |
| 27 | + method: 'GET', |
| 28 | + }, |
| 29 | + }, |
| 30 | + { |
| 31 | + http: { |
| 32 | + path: 'foo/bar', |
| 33 | + method: 'POST', |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + http: { |
| 38 | + path: 'bar/-', |
| 39 | + method: 'GET', |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + http: { |
| 44 | + path: 'bar/foo', |
| 45 | + method: 'GET', |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + http: { |
| 50 | + path: 'bar/{id}/foobar', |
| 51 | + method: 'GET', |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + http: { |
| 56 | + path: 'bar/{id}', |
| 57 | + method: 'GET', |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + http: { |
| 62 | + path: 'bar/{foo_id}', |
| 63 | + method: 'GET', |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + http: { |
| 68 | + path: 'bar/{foo_id}/foobar', |
| 69 | + method: 'GET', |
| 70 | + }, |
| 71 | + }, |
| 72 | + ]; |
| 73 | + expect(serverlessStepFunctions.getResourcePaths()).to.deep.equal([ |
| 74 | + 'foo', |
| 75 | + 'bar', |
| 76 | + 'foo/bar', |
| 77 | + 'bar/-', |
| 78 | + 'bar/foo', |
| 79 | + 'bar/{id}', |
| 80 | + 'bar/{foo_id}', |
| 81 | + 'bar/{id}/foobar', |
| 82 | + 'bar/{foo_id}/foobar', |
| 83 | + ]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should reference the appropriate ParentId', () => { |
| 87 | + serverlessStepFunctions.pluginhttpValidated.events = [ |
| 88 | + { |
| 89 | + http: { |
| 90 | + path: 'foo/bar', |
| 91 | + method: 'POST', |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + http: { |
| 96 | + path: 'bar/-', |
| 97 | + method: 'GET', |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + http: { |
| 102 | + path: 'bar/foo', |
| 103 | + method: 'GET', |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + http: { |
| 108 | + path: 'bar/{id}/foobar', |
| 109 | + method: 'GET', |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + http: { |
| 114 | + path: 'bar/{id}', |
| 115 | + method: 'GET', |
| 116 | + }, |
| 117 | + }, |
| 118 | + ]; |
| 119 | + return serverlessStepFunctions.compileResources().then(() => { |
| 120 | + expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 121 | + .Resources.ApiGatewayResourceFoo.Properties.ParentId['Fn::GetAtt'][0]) |
| 122 | + .to.equal('ApiGatewayRestApi'); |
| 123 | + expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 124 | + .Resources.ApiGatewayResourceFoo.Properties.ParentId['Fn::GetAtt'][1]) |
| 125 | + .to.equal('RootResourceId'); |
| 126 | + expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 127 | + .Resources.ApiGatewayResourceFooBar.Properties.ParentId.Ref) |
| 128 | + .to.equal('ApiGatewayResourceFoo'); |
| 129 | + expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 130 | + .Resources.ApiGatewayResourceBarIdVar.Properties.ParentId.Ref) |
| 131 | + .to.equal('ApiGatewayResourceBar'); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should construct the correct resourceLogicalIds object', () => { |
| 136 | + serverlessStepFunctions.pluginhttpValidated.events = [ |
| 137 | + { |
| 138 | + http: { |
| 139 | + path: '', |
| 140 | + method: 'POST', |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + http: { |
| 145 | + path: 'foo', |
| 146 | + method: 'GET', |
| 147 | + }, |
| 148 | + }, |
| 149 | + { |
| 150 | + http: { |
| 151 | + path: 'foo/{foo_id}/bar', |
| 152 | + method: 'GET', |
| 153 | + }, |
| 154 | + }, |
| 155 | + { |
| 156 | + http: { |
| 157 | + path: 'baz/foo', |
| 158 | + method: 'GET', |
| 159 | + }, |
| 160 | + }, |
| 161 | + ]; |
| 162 | + return serverlessStepFunctions.compileResources().then(() => { |
| 163 | + expect(serverlessStepFunctions.apiGatewayResourceLogicalIds).to.deep.equal({ |
| 164 | + baz: 'ApiGatewayResourceBaz', |
| 165 | + 'baz/foo': 'ApiGatewayResourceBazFoo', |
| 166 | + foo: 'ApiGatewayResourceFoo', |
| 167 | + 'foo/{foo_id}': 'ApiGatewayResourceFooFooidVar', |
| 168 | + 'foo/{foo_id}/bar': 'ApiGatewayResourceFooFooidVarBar', |
| 169 | + }); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should construct resourceLogicalIds that do not collide', () => { |
| 174 | + serverlessStepFunctions.pluginhttpValidated.events = [ |
| 175 | + { |
| 176 | + http: { |
| 177 | + path: 'foo/bar', |
| 178 | + method: 'POST', |
| 179 | + }, |
| 180 | + }, |
| 181 | + { |
| 182 | + http: { |
| 183 | + path: 'foo/{bar}', |
| 184 | + method: 'GET', |
| 185 | + }, |
| 186 | + }, |
| 187 | + ]; |
| 188 | + return serverlessStepFunctions.compileResources().then(() => { |
| 189 | + expect(serverlessStepFunctions.apiGatewayResourceLogicalIds).to.deep.equal({ |
| 190 | + foo: 'ApiGatewayResourceFoo', |
| 191 | + 'foo/bar': 'ApiGatewayResourceFooBar', |
| 192 | + 'foo/{bar}': 'ApiGatewayResourceFooBarVar', |
| 193 | + }); |
| 194 | + }); |
| 195 | + }); |
| 196 | + |
| 197 | + it('should set the appropriate Pathpart', () => { |
| 198 | + serverlessStepFunctions.pluginhttpValidated.events = [ |
| 199 | + { |
| 200 | + http: { |
| 201 | + path: 'foo/{bar}', |
| 202 | + method: 'GET', |
| 203 | + }, |
| 204 | + }, |
| 205 | + { |
| 206 | + http: { |
| 207 | + path: 'foo/bar', |
| 208 | + method: 'GET', |
| 209 | + }, |
| 210 | + }, |
| 211 | + { |
| 212 | + http: { |
| 213 | + path: 'foo/{bar}/baz', |
| 214 | + method: 'GET', |
| 215 | + }, |
| 216 | + }, |
| 217 | + ]; |
| 218 | + return serverlessStepFunctions.compileResources().then(() => { |
| 219 | + expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 220 | + .Resources.ApiGatewayResourceFooBar.Properties.PathPart) |
| 221 | + .to.equal('bar'); |
| 222 | + expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 223 | + .Resources.ApiGatewayResourceFooBarVar.Properties.PathPart) |
| 224 | + .to.equal('{bar}'); |
| 225 | + expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 226 | + .Resources.ApiGatewayResourceFooBarVarBaz.Properties.PathPart) |
| 227 | + .to.equal('baz'); |
| 228 | + }); |
| 229 | + }); |
| 230 | + |
| 231 | + it('should handle root resource references', () => { |
| 232 | + serverlessStepFunctions.pluginhttpValidated.events = [ |
| 233 | + { |
| 234 | + http: { |
| 235 | + path: '', |
| 236 | + method: 'GET', |
| 237 | + }, |
| 238 | + }, |
| 239 | + ]; |
| 240 | + return serverlessStepFunctions.compileResources().then(() => { |
| 241 | + expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 242 | + .Resources).to.deep.equal({}); |
| 243 | + }); |
| 244 | + }); |
| 245 | + |
| 246 | + describe('#getResourceName()', () => { |
| 247 | + it('should return empty if empty string gives to argument', () => { |
| 248 | + expect(serverlessStepFunctions.getResourceName('')) |
| 249 | + .to.equal(''); |
| 250 | + }); |
| 251 | + }); |
| 252 | +}); |
0 commit comments