|
| 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('#compileUsagePlan()', () => { |
| 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.service.service = 'first-service'; |
| 19 | + serverless.setProvider('aws', new AwsProvider(serverless)); |
| 20 | + serverless.service.provider.apiKeys = ['1234567890']; |
| 21 | + serverless.service.provider.compiledCloudFormationTemplate = { |
| 22 | + Resources: {}, |
| 23 | + }; |
| 24 | + serverlessStepFunctions = new ServerlessStepFunctions(serverless, options); |
| 25 | + serverlessStepFunctions.serverless.service.stepFunctions = { |
| 26 | + stateMachines: { |
| 27 | + first: {}, |
| 28 | + }, |
| 29 | + }; |
| 30 | + serverlessStepFunctions.apiGatewayDeploymentLogicalId = 'ApiGatewayDeploymentTest'; |
| 31 | + serverlessStepFunctions.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi'; |
| 32 | + }); |
| 33 | + |
| 34 | + it('should compile default usage plan resource', () => { |
| 35 | + serverless.service.provider.apiKeys = ['1234567890']; |
| 36 | + return serverlessStepFunctions.compileUsagePlan().then(() => { |
| 37 | + expect( |
| 38 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 39 | + .Resources[ |
| 40 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 41 | + ].Type |
| 42 | + ).to.equal('AWS::ApiGateway::UsagePlan'); |
| 43 | + |
| 44 | + expect( |
| 45 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 46 | + .Resources[ |
| 47 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 48 | + ].DependsOn |
| 49 | + ).to.equal('ApiGatewayDeploymentTest'); |
| 50 | + |
| 51 | + expect( |
| 52 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 53 | + .Resources[ |
| 54 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 55 | + ].Properties.ApiStages[0].ApiId.Ref |
| 56 | + ).to.equal('ApiGatewayRestApi'); |
| 57 | + |
| 58 | + expect( |
| 59 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 60 | + .Resources[ |
| 61 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 62 | + ].Properties.ApiStages[0].Stage |
| 63 | + ).to.equal('dev'); |
| 64 | + |
| 65 | + expect( |
| 66 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 67 | + .Resources[ |
| 68 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 69 | + ].Properties.Description |
| 70 | + ).to.equal('Usage plan for first-service dev stage'); |
| 71 | + |
| 72 | + expect( |
| 73 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 74 | + .Resources[ |
| 75 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 76 | + ].Properties.UsagePlanName |
| 77 | + ).to.equal('first-service-dev'); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should compile custom usage plan resource', () => { |
| 82 | + serverless.service.provider.usagePlan = { |
| 83 | + quota: { |
| 84 | + limit: 500, |
| 85 | + offset: 10, |
| 86 | + period: 'MONTH', |
| 87 | + }, |
| 88 | + throttle: { |
| 89 | + burstLimit: 200, |
| 90 | + rateLimit: 100, |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + return serverlessStepFunctions.compileUsagePlan().then(() => { |
| 95 | + expect( |
| 96 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 97 | + .Resources[ |
| 98 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 99 | + ].Type |
| 100 | + ).to.equal('AWS::ApiGateway::UsagePlan'); |
| 101 | + |
| 102 | + expect( |
| 103 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 104 | + .Resources[ |
| 105 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 106 | + ].DependsOn |
| 107 | + ).to.equal('ApiGatewayDeploymentTest'); |
| 108 | + |
| 109 | + expect( |
| 110 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 111 | + .Resources[ |
| 112 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 113 | + ].Properties.ApiStages[0].ApiId.Ref |
| 114 | + ).to.equal('ApiGatewayRestApi'); |
| 115 | + |
| 116 | + expect( |
| 117 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 118 | + .Resources[ |
| 119 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 120 | + ].Properties.ApiStages[0].Stage |
| 121 | + ).to.equal('dev'); |
| 122 | + |
| 123 | + expect( |
| 124 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 125 | + .Resources[ |
| 126 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 127 | + ].Properties.Description |
| 128 | + ).to.equal('Usage plan for first-service dev stage'); |
| 129 | + |
| 130 | + expect( |
| 131 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 132 | + .Resources[ |
| 133 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 134 | + ].Properties.Quota |
| 135 | + ).to.deep.equal({ |
| 136 | + Limit: 500, |
| 137 | + Offset: 10, |
| 138 | + Period: 'MONTH', |
| 139 | + }); |
| 140 | + |
| 141 | + expect( |
| 142 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 143 | + .Resources[ |
| 144 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 145 | + ].Properties.Throttle |
| 146 | + ).to.deep.equal({ |
| 147 | + BurstLimit: 200, |
| 148 | + RateLimit: 100, |
| 149 | + }); |
| 150 | + |
| 151 | + expect( |
| 152 | + serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate |
| 153 | + .Resources[ |
| 154 | + serverlessStepFunctions.provider.naming.getUsagePlanLogicalId() |
| 155 | + ].Properties.UsagePlanName |
| 156 | + ).to.equal('first-service-dev'); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments