|
| 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 | + const options = { |
| 15 | + stage: 'dev', |
| 16 | + region: 'us-east-1', |
| 17 | + }; |
| 18 | + serverless.setProvider('aws', new AwsProvider(serverless)); |
| 19 | + serverless.service.provider.apiKeys = ['1234567890']; |
| 20 | + serverless.service.provider.compiledCloudFormationTemplate = { |
| 21 | + Resources: {}, |
| 22 | + }; |
| 23 | + serverlessStepFunctions = new ServerlessStepFunctions(serverless, options); |
| 24 | + serverlessStepFunctions.serverless.service.stepFunctions = { |
| 25 | + stateMachines: { |
| 26 | + first: {}, |
| 27 | + }, |
| 28 | + }; |
| 29 | + serverlessStepFunctions.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi'; |
| 30 | + serverlessStepFunctions.apiGatewayDeploymentLogicalId = 'ApiGatewayDeploymentTest'; |
| 31 | + }); |
| 32 | + |
| 33 | + it('should compile api key resource', () => |
| 34 | + serverlessStepFunctions.compileApiKeys().then(() => { |
| 35 | + expect( |
| 36 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 37 | + .Resources[ |
| 38 | + serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1) |
| 39 | + ].Type |
| 40 | + ).to.equal('AWS::ApiGateway::ApiKey'); |
| 41 | + |
| 42 | + expect( |
| 43 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 44 | + .Resources[ |
| 45 | + serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1) |
| 46 | + ].Properties.Enabled |
| 47 | + ).to.equal(true); |
| 48 | + |
| 49 | + expect( |
| 50 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 51 | + .Resources[ |
| 52 | + serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1) |
| 53 | + ].Properties.Name |
| 54 | + ).to.equal('1234567890'); |
| 55 | + |
| 56 | + expect( |
| 57 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 58 | + .Resources[ |
| 59 | + serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1) |
| 60 | + ].Properties.StageKeys[0].RestApiId.Ref |
| 61 | + ).to.equal('ApiGatewayRestApi'); |
| 62 | + |
| 63 | + expect( |
| 64 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 65 | + .Resources[ |
| 66 | + serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1) |
| 67 | + ].Properties.StageKeys[0].StageName |
| 68 | + ).to.equal('dev'); |
| 69 | + |
| 70 | + expect( |
| 71 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 72 | + .Resources[ |
| 73 | + serverlessStepFunctions.provider.naming.getApiKeyLogicalId(1) |
| 74 | + ].DependsOn |
| 75 | + ).to.equal('ApiGatewayDeploymentTest'); |
| 76 | + }) |
| 77 | + ); |
| 78 | + |
| 79 | + it('throw error if apiKey property is not an array', () => { |
| 80 | + serverlessStepFunctions.serverless.service.provider.apiKeys = 2; |
| 81 | + expect(() => serverlessStepFunctions.compileApiKeys()).to.throw(Error); |
| 82 | + }); |
| 83 | + |
| 84 | + it('throw error if an apiKey is not a string', () => { |
| 85 | + serverlessStepFunctions.serverless.service.provider.apiKeys = [2]; |
| 86 | + expect(() => serverlessStepFunctions.compileApiKeys()).to.throw(Error); |
| 87 | + }); |
| 88 | +}); |
0 commit comments