-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'staging' into fix-asset-uploader-error-logging
- Loading branch information
Showing
10 changed files
with
263 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Fetches all usage plans, combining all pages into a single array. | ||
* | ||
* @param apiGateway | ||
* an instance of `AWS.APIGateway` to use for API calls | ||
* | ||
* @param paramOverrides | ||
* a parameter object passed in calls to `APIGateway.getUsagePlans` | ||
* | ||
* @returns | ||
* a Promise resolving with an array of items returned from | ||
* `APIGateway.getUsagePlans` calls | ||
*/ | ||
async function getAllUsagePlans(apiGateway, paramOverrides = {}) { | ||
// The maximum allowed value of `limit` is 500 according to | ||
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html#getUsagePlans-property | ||
const defaultParams = {limit: 500, ...paramOverrides} | ||
|
||
console.log('Fetching first page of usage plans') | ||
let response = await apiGateway.getUsagePlans(defaultParams).promise() | ||
const usagePlans = response.items | ||
|
||
while (response.position) { | ||
console.log(`Fetching next page of usage plans, at position=[${response.position}]`) | ||
const nextParams = {...defaultParams, position: response.position} | ||
response = await apiGateway.getUsagePlans(nextParams).promise() | ||
usagePlans.push(...response.items) | ||
} | ||
|
||
return usagePlans | ||
} | ||
|
||
module.exports = { getAllUsagePlans } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Fetches all usage plans, combining all pages into a single array. | ||
* | ||
* @param apiGateway | ||
* an instance of `AWS.APIGateway` to use for API calls | ||
* | ||
* @param paramOverrides | ||
* a parameter object passed in calls to `APIGateway.getUsagePlans` | ||
* | ||
* @returns | ||
* a Promise resolving with an array of items returned from | ||
* `APIGateway.getUsagePlans` calls | ||
*/ | ||
async function getAllUsagePlans(apiGateway, paramOverrides = {}) { | ||
// The maximum allowed value of `limit` is 500 according to | ||
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html#getUsagePlans-property | ||
const defaultParams = {limit: 500, ...paramOverrides} | ||
|
||
console.log('Fetching first page of usage plans') | ||
let response = await apiGateway.getUsagePlans(defaultParams).promise() | ||
const usagePlans = response.items | ||
|
||
while (response.position) { | ||
console.log(`Fetching next page of usage plans, at position=[${response.position}]`) | ||
const nextParams = {...defaultParams, position: response.position} | ||
response = await apiGateway.getUsagePlans(nextParams).promise() | ||
usagePlans.push(...response.items) | ||
} | ||
|
||
return usagePlans | ||
} | ||
|
||
module.exports = { getAllUsagePlans } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
const { getAllUsagePlans } = require('../get-all-usage-plans') | ||
|
||
const promiser = require('../../setup-jest').promiser | ||
|
||
const mockUsagePlanItem = () => ({ | ||
id: '1a2b3c', | ||
name: '1a2b3c', | ||
apiStages: [ | ||
{ | ||
apiId: 'anmlcrckrs', | ||
stage: 'prod', | ||
}, | ||
{ | ||
apiId: 'jlpnochips', | ||
stage: 'beta', | ||
}, | ||
], | ||
throttle: { | ||
burstLimit: 10, | ||
rateLimit: 10, | ||
}, | ||
quota: { | ||
limit: 10000, | ||
offset: 0, | ||
period: 'DAY', | ||
} | ||
}) | ||
|
||
describe('getAllUsagePlans', () => { | ||
test('returns all usage plans, when none exist', async () => { | ||
const mockApiGateway = { | ||
getUsagePlans: jest.fn().mockReturnValueOnce(promiser({ | ||
items: [] | ||
})) | ||
} | ||
|
||
const result = await getAllUsagePlans(mockApiGateway) | ||
const mocked = mockApiGateway.getUsagePlans.mock | ||
expect(mocked.calls.length).toBe(1) | ||
expect(mocked.calls[0][0]).not.toHaveProperty('position') | ||
expect(result).toHaveLength(0) | ||
}) | ||
|
||
test('returns all usage plans, when only one page of usage plans exists', async () => { | ||
const mockApiGateway = { | ||
getUsagePlans: jest.fn().mockReturnValueOnce(promiser({ | ||
items: [ | ||
mockUsagePlanItem(), | ||
mockUsagePlanItem(), | ||
mockUsagePlanItem(), | ||
mockUsagePlanItem(), | ||
] | ||
})) | ||
} | ||
|
||
const result = await getAllUsagePlans(mockApiGateway) | ||
const mocked = mockApiGateway.getUsagePlans.mock | ||
expect(mocked.calls.length).toBe(1) | ||
expect(mocked.calls[0][0]).not.toHaveProperty('position') | ||
expect(result).toHaveLength(4) | ||
}) | ||
|
||
test('returns all usage plans, when multiple pages of usage plans exist', async () => { | ||
const mockApiGateway = { | ||
getUsagePlans: jest.fn().mockReturnValueOnce(promiser({ | ||
items: [ | ||
mockUsagePlanItem(), | ||
mockUsagePlanItem(), | ||
mockUsagePlanItem(), | ||
mockUsagePlanItem(), | ||
], | ||
position: 'qwertyuiopasdf%3D%3D', | ||
})).mockReturnValueOnce(promiser({ | ||
items: [ | ||
mockUsagePlanItem(), | ||
mockUsagePlanItem(), | ||
mockUsagePlanItem(), | ||
mockUsagePlanItem(), | ||
], | ||
position: 'zxcvbnm1234567%3D%3D', | ||
})).mockReturnValueOnce(promiser({ | ||
items: [ | ||
mockUsagePlanItem(), | ||
mockUsagePlanItem(), | ||
], | ||
})) | ||
} | ||
|
||
const result = await getAllUsagePlans(mockApiGateway) | ||
const mocked = mockApiGateway.getUsagePlans.mock | ||
expect(mocked.calls.length).toBe(3) | ||
expect(mocked.calls[0][0]).not.toHaveProperty('position') | ||
expect(mocked.calls[1][0]).toHaveProperty('position', 'qwertyuiopasdf%3D%3D') | ||
expect(mocked.calls[2][0]).toHaveProperty('position', 'zxcvbnm1234567%3D%3D') | ||
expect(result).toHaveLength(10) | ||
}) | ||
|
||
test('passes through an API Gateway request error', async () => { | ||
const expectedError = {} | ||
const mockApiGateway = { | ||
getUsagePlans: jest.fn().mockReturnValueOnce(promiser(null, expectedError)) | ||
} | ||
|
||
await expect(getAllUsagePlans(mockApiGateway)).rejects.toStrictEqual(expectedError) | ||
}) | ||
}) |
Oops, something went wrong.