|
| 1 | +describe('ServerlessAWSDocumentation', function () { |
| 2 | + let objectUnderTest; |
| 3 | + |
| 4 | + beforeEach(() => { |
| 5 | + objectUnderTest = require('./downloadDocumentation.js'); |
| 6 | + objectUnderTest.fs = { |
| 7 | + writeFileSync: jasmine.createSpy('fs') |
| 8 | + }; |
| 9 | + objectUnderTest.serverless = { |
| 10 | + providers: { |
| 11 | + aws: { |
| 12 | + naming: { |
| 13 | + getStackName: () => { |
| 14 | + return 'testStackName'; |
| 15 | + } |
| 16 | + }, |
| 17 | + request: jasmine.createSpy('aws request'), |
| 18 | + } |
| 19 | + }, |
| 20 | + service: { |
| 21 | + provider: { |
| 22 | + stage: 'testStage', |
| 23 | + region: 'testRegion', |
| 24 | + } |
| 25 | + } |
| 26 | + }; |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + delete require.cache[require.resolve('./downloadDocumentation.js')]; |
| 31 | + }); |
| 32 | + |
| 33 | + describe('downloadDocumentation', () => { |
| 34 | + it('should successfully download documentation, unknown file extension', (done) => { |
| 35 | + objectUnderTest.options = { |
| 36 | + outputFileName: 'test.txt', |
| 37 | + }; |
| 38 | + objectUnderTest._getRestApiId = () => { |
| 39 | + return Promise.resolve('testRestApiId') |
| 40 | + }; |
| 41 | + |
| 42 | + objectUnderTest.serverless.providers.aws.request.and.returnValue(Promise.resolve({ |
| 43 | + body: 'some body', |
| 44 | + })); |
| 45 | + return objectUnderTest.downloadDocumentation().then(() => { |
| 46 | + expect(objectUnderTest.serverless.providers.aws.request).toHaveBeenCalledWith('APIGateway', 'getExport', { |
| 47 | + stageName: 'testStage', |
| 48 | + restApiId: 'testRestApiId', |
| 49 | + exportType: 'swagger', |
| 50 | + accepts: 'application/json', |
| 51 | + }); |
| 52 | + expect(objectUnderTest.fs.writeFileSync).toHaveBeenCalledWith('test.txt', 'some body'); |
| 53 | + |
| 54 | + done(); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should successfully download documentation, yaml extension', (done) => { |
| 59 | + objectUnderTest.options = { |
| 60 | + outputFileName: 'test.yml', |
| 61 | + }; |
| 62 | + objectUnderTest._getRestApiId = () => { |
| 63 | + return Promise.resolve('testRestApiId') |
| 64 | + }; |
| 65 | + |
| 66 | + objectUnderTest.serverless.providers.aws.request.and.returnValue(Promise.resolve({ |
| 67 | + body: 'some body', |
| 68 | + })); |
| 69 | + return objectUnderTest.downloadDocumentation().then(() => { |
| 70 | + expect(objectUnderTest.serverless.providers.aws.request).toHaveBeenCalledWith('APIGateway', 'getExport', { |
| 71 | + stageName: 'testStage', |
| 72 | + restApiId: 'testRestApiId', |
| 73 | + exportType: 'swagger', |
| 74 | + accepts: 'application/yaml', |
| 75 | + }); |
| 76 | + expect(objectUnderTest.fs.writeFileSync).toHaveBeenCalledWith('test.yml', 'some body'); |
| 77 | + |
| 78 | + done(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should throw an error', (done) => { |
| 83 | + objectUnderTest.options = { |
| 84 | + outputFileName: 'test.json', |
| 85 | + }; |
| 86 | + objectUnderTest._getRestApiId = () => { |
| 87 | + return Promise.resolve('testRestApiId'); |
| 88 | + }; |
| 89 | + objectUnderTest.serverless.providers.aws.request.and.returnValue(Promise.reject('reason')); |
| 90 | + return objectUnderTest.downloadDocumentation().catch(() => { |
| 91 | + done(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should get rest api id', (done) => { |
| 96 | + objectUnderTest.serverless.providers.aws.request.and.returnValue(Promise.resolve({ |
| 97 | + Stacks: [{ |
| 98 | + Outputs: [{ |
| 99 | + OutputKey: 'some-key-1', |
| 100 | + OutputValue: 'some-value-1', |
| 101 | + }, { |
| 102 | + OutputKey: 'AwsDocApiId', |
| 103 | + OutputValue: 'testRestApiId', |
| 104 | + }, { |
| 105 | + OutputKey: 'some-key-2', |
| 106 | + OutputValue: 'some-value-2', |
| 107 | + }] |
| 108 | + }] |
| 109 | + })); |
| 110 | + |
| 111 | + return objectUnderTest._getRestApiId('testStackName').then((restApiId) => { |
| 112 | + expect(restApiId).toBe('testRestApiId'); |
| 113 | + expect(objectUnderTest.serverless.providers.aws.request).toHaveBeenCalledWith( |
| 114 | + 'CloudFormation', |
| 115 | + 'describeStacks', |
| 116 | + jasmine.objectContaining({StackName: 'testStackName'}), 'testStage', 'testRegion' |
| 117 | + ); |
| 118 | + |
| 119 | + done(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments