|
| 1 | +var expect = require('chai').expect; |
| 2 | +var apigatewayV2AccessLogging = require('./apigatewayV2AccessLogging'); |
| 3 | + |
| 4 | +const createCache = (getApis, getStages) => { |
| 5 | + if (getApis && getApis.length && getApis[0].ApiId) var restApiId = getApis[0].ApiId; |
| 6 | + return { |
| 7 | + apigatewayv2: { |
| 8 | + getApis: { |
| 9 | + 'us-east-1': { |
| 10 | + data: getApis |
| 11 | + } |
| 12 | + }, |
| 13 | + getStages: { |
| 14 | + 'us-east-1': { |
| 15 | + [restApiId]: { |
| 16 | + data: { |
| 17 | + Items: getStages |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + }; |
| 24 | +}; |
| 25 | + |
| 26 | +const createErrorCache = () => { |
| 27 | + return { |
| 28 | + apigatewayv2: { |
| 29 | + getApis: { |
| 30 | + 'us-east-1': { |
| 31 | + err: { |
| 32 | + message: 'error fetching API Gateway v2 APIs' |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + getStages: { |
| 37 | + 'us-east-1': { |
| 38 | + err: { |
| 39 | + message: 'error fetching API Gateway v2 stages' |
| 40 | + }, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + }, |
| 45 | + }; |
| 46 | +}; |
| 47 | + |
| 48 | +const createUnknownForStage = (api) => { |
| 49 | + return { |
| 50 | + apigatewayv2: { |
| 51 | + getApis: { |
| 52 | + 'us-east-1': { |
| 53 | + data: api |
| 54 | + } |
| 55 | + }, |
| 56 | + getStages: { |
| 57 | + 'us-east-1': 'err' |
| 58 | + } |
| 59 | + } |
| 60 | + }; |
| 61 | +}; |
| 62 | + |
| 63 | +describe('apigatewayV2AccessLogging', function () { |
| 64 | + describe('run', function () { |
| 65 | + it('should return UNKNOWN if unable to query for API Gateway v2 APIs', function (done) { |
| 66 | + const cache = createErrorCache(); |
| 67 | + apigatewayV2AccessLogging.run(cache, {} , (err, results) => { |
| 68 | + expect(results.length).to.equal(1); |
| 69 | + expect(results[0].status).to.equal(3); |
| 70 | + expect(results[0].region).to.equal('us-east-1'); |
| 71 | + expect (results[0].message).to.include('Unable to query for API Gateway V2 APIs:'); |
| 72 | + done(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return PASS if no API Gateway Rest APIs found', function (done) { |
| 77 | + const cache = createCache([]); |
| 78 | + apigatewayV2AccessLogging.run(cache, {}, (err, results) => { |
| 79 | + expect(results.length).to.equal(1); |
| 80 | + expect(results[0].status).to.equal(0); |
| 81 | + expect(results[0].region).to.equal('us-east-1'); |
| 82 | + expect (results[0].message).to.include('No API Gateway V2 APIs found'); |
| 83 | + done(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should return PASS if no stages found', function (done) { |
| 88 | + const getApis = [ |
| 89 | + { |
| 90 | + ApiId: 'api-id', |
| 91 | + name: 'TestAPI', |
| 92 | + description: 'Test API', |
| 93 | + createdDate: 1621916018, |
| 94 | + apiKeySource: 'HEADER', |
| 95 | + endpointConfiguration: { |
| 96 | + types: ['REGIONAL'] |
| 97 | + } |
| 98 | + } |
| 99 | + ]; |
| 100 | + const getStages = []; |
| 101 | + const cache = createCache(getApis, getStages); |
| 102 | + apigatewayV2AccessLogging.run(cache,{}, (err, results) => { |
| 103 | + expect(results.length).to.equal(1); |
| 104 | + expect(results[0].status).to.equal(0); |
| 105 | + expect(results[0].region).to.equal('us-east-1'); |
| 106 | + expect (results[0].message).to.include('No API Gateway V2 API Stages found'); |
| 107 | + done(); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should return PASS if Api gateway v2 stage has access logging enabled', function (done) { |
| 112 | + const getApis = [ |
| 113 | + { |
| 114 | + ApiId: 'api-id', |
| 115 | + name: 'TestAPI', |
| 116 | + description: 'Test API', |
| 117 | + createdDate: 1621916018, |
| 118 | + apiKeySource: 'HEADER', |
| 119 | + endpointConfiguration: { |
| 120 | + types: ['REGIONAL'] |
| 121 | + } |
| 122 | + } |
| 123 | + ]; |
| 124 | + const getStages = [ |
| 125 | + { |
| 126 | + "AutoDeploy": true, |
| 127 | + "CreatedDate": "2023-12-11T20:07:28.000Z", |
| 128 | + "DefaultRouteSettings": { |
| 129 | + "DetailedMetricsEnabled": false |
| 130 | + }, |
| 131 | + "DeploymentId": "biw5qf", |
| 132 | + "Description": "Created by AWS Lambda", |
| 133 | + "LastDeploymentStatusMessage": "Successfully deployed stage with deployment ID 'biw5qf'", |
| 134 | + "LastUpdatedDate": "2023-12-11T20:07:29.000Z", |
| 135 | + "RouteSettings": {}, |
| 136 | + "StageName": "default", |
| 137 | + "StageVariables": {}, |
| 138 | + "Tags": {}, |
| 139 | + "AccessLogSetting": { |
| 140 | + "LogArn": "arn:aws:1234:log" |
| 141 | + } |
| 142 | + } |
| 143 | + ]; |
| 144 | + const cache = createCache(getApis, getStages); |
| 145 | + apigatewayV2AccessLogging.run(cache,{}, (err, results) => { |
| 146 | + expect(results.length).to.equal(1); |
| 147 | + expect(results[0].status).to.equal(0); |
| 148 | + expect(results[0].region).to.equal('us-east-1'); |
| 149 | + expect(results[0].message).to.include('API Gateway V2 API stage has access logging enabled') |
| 150 | + done(); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should return PASS if Api gateway v2 stage has access logging enabled', function (done) { |
| 155 | + const getApis = [ |
| 156 | + { |
| 157 | + ApiId: 'api-id', |
| 158 | + name: 'TestAPI', |
| 159 | + description: 'Test API', |
| 160 | + createdDate: 1621916018, |
| 161 | + apiKeySource: 'HEADER', |
| 162 | + endpointConfiguration: { |
| 163 | + types: ['REGIONAL'] |
| 164 | + } |
| 165 | + } |
| 166 | + ]; |
| 167 | + const getStages = [ |
| 168 | + { |
| 169 | + "AutoDeploy": true, |
| 170 | + "CreatedDate": "2023-12-11T20:07:28.000Z", |
| 171 | + "DefaultRouteSettings": { |
| 172 | + "DetailedMetricsEnabled": false |
| 173 | + }, |
| 174 | + "DeploymentId": "biw5qf", |
| 175 | + "Description": "Created by AWS Lambda", |
| 176 | + "LastDeploymentStatusMessage": "Successfully deployed stage with deployment ID 'biw5qf'", |
| 177 | + "LastUpdatedDate": "2023-12-11T20:07:29.000Z", |
| 178 | + "RouteSettings": {}, |
| 179 | + "StageName": "default", |
| 180 | + "StageVariables": {}, |
| 181 | + "Tags": {}, |
| 182 | + } |
| 183 | + ]; |
| 184 | + const cache = createCache(getApis, getStages); |
| 185 | + apigatewayV2AccessLogging.run(cache,{}, (err, results) => { |
| 186 | + expect(results.length).to.equal(1); |
| 187 | + expect(results[0].status).to.equal(2); |
| 188 | + expect(results[0].region).to.equal('us-east-1'); |
| 189 | + expect(results[0].message).to.include('API Gateway V2 API stage does not have access logging enabled') |
| 190 | + done(); |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should return UNKNOWN if unable to query for api stages', function (done) { |
| 195 | + const getApis = [ |
| 196 | + { |
| 197 | + ApiId: 'api-id', |
| 198 | + name: 'TestAPI', |
| 199 | + description: 'Test API', |
| 200 | + createdDate: 1621916018, |
| 201 | + apiKeySource: 'HEADER', |
| 202 | + endpointConfiguration: { |
| 203 | + types: ['REGIONAL'] |
| 204 | + } |
| 205 | + } |
| 206 | + ]; |
| 207 | + |
| 208 | + const cache = createUnknownForStage(getApis); |
| 209 | + apigatewayV2AccessLogging.run(cache,{}, (err, results) => { |
| 210 | + expect(results.length).to.equal(1); |
| 211 | + expect(results[0].status).to.equal(3); |
| 212 | + expect(results[0].region).to.equal('us-east-1'); |
| 213 | + expect(results[0].message).to.include('Unable to query for API Gateway V2 API Stages:') |
| 214 | + done(); |
| 215 | + }); |
| 216 | + }); |
| 217 | + }); |
| 218 | +}); |
0 commit comments