|
| 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('#validateKinesisServiceProxy()', () => { |
| 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 validate the "streamName" property', async () => { |
| 29 | + serverlessApigatewayServiceProxy.validated = { |
| 30 | + events: [ |
| 31 | + { |
| 32 | + serviceName: 'kinesis', |
| 33 | + http: { |
| 34 | + path: 'kinesis', |
| 35 | + method: 'post' |
| 36 | + } |
| 37 | + } |
| 38 | + ] |
| 39 | + } |
| 40 | + |
| 41 | + await expect(serverlessApigatewayServiceProxy.validateKinesisServiceProxy()).to.be.rejectedWith( |
| 42 | + 'Missing "streamName" property in Kinesis Service Proxy' |
| 43 | + ) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should validate the "streamName" property if it is string or AWS intrinsic function', async () => { |
| 47 | + serverlessApigatewayServiceProxy.validated = { |
| 48 | + events: [ |
| 49 | + { |
| 50 | + serviceName: 'kinesis', |
| 51 | + http: { |
| 52 | + streamName: ['xx', 'yy'], |
| 53 | + path: 'kinesis', |
| 54 | + method: 'post' |
| 55 | + } |
| 56 | + } |
| 57 | + ] |
| 58 | + } |
| 59 | + |
| 60 | + await expect(serverlessApigatewayServiceProxy.validateKinesisServiceProxy()).to.be.rejectedWith( |
| 61 | + 'You can only set "string" or the AWS intrinsic function "Ref" like { Ref: \'KinesisStreamResourceId\' } as a streamName property' |
| 62 | + ) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should validate the "streamName" property if AWS intrinsic function is correct syntax', async () => { |
| 66 | + serverlessApigatewayServiceProxy.validated = { |
| 67 | + events: [ |
| 68 | + { |
| 69 | + serviceName: 'kinesis', |
| 70 | + http: { |
| 71 | + streamName: { xxx: 'KinesisStreamResourceId' }, |
| 72 | + path: 'kinesis', |
| 73 | + method: 'post' |
| 74 | + } |
| 75 | + } |
| 76 | + ] |
| 77 | + } |
| 78 | + |
| 79 | + await expect(serverlessApigatewayServiceProxy.validateKinesisServiceProxy()).to.be.rejectedWith( |
| 80 | + 'the AWS intrinsic function need "Ref" property like { Ref: \'KinesisStreamResourceId\' } as a streamName' |
| 81 | + ) |
| 82 | + }) |
| 83 | + |
| 84 | + it('should not show error if streamName is correct', async () => { |
| 85 | + serverlessApigatewayServiceProxy.validated = { |
| 86 | + events: [ |
| 87 | + { |
| 88 | + serviceName: 'kinesis', |
| 89 | + http: { |
| 90 | + streamName: { Ref: 'KinesisStreamResourceId' }, |
| 91 | + path: 'kinesis', |
| 92 | + method: 'post' |
| 93 | + } |
| 94 | + } |
| 95 | + ] |
| 96 | + } |
| 97 | + |
| 98 | + await expect(serverlessApigatewayServiceProxy.validateKinesisServiceProxy()).to.be.fulfilled |
| 99 | + |
| 100 | + serverlessApigatewayServiceProxy.validated = { |
| 101 | + events: [ |
| 102 | + { |
| 103 | + serviceName: 'kinesis', |
| 104 | + http: { |
| 105 | + streamName: 'yourStream', |
| 106 | + path: 'kinesis', |
| 107 | + method: 'post' |
| 108 | + } |
| 109 | + } |
| 110 | + ] |
| 111 | + } |
| 112 | + |
| 113 | + await expect(serverlessApigatewayServiceProxy.validateKinesisServiceProxy()).to.be.fulfilled |
| 114 | + }) |
| 115 | +}) |
0 commit comments