forked from dvsa/cvs-tsk-update-test-stations
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from dvsa/chore/cb2-11281
chore(cb2-11281): upgrade aws sdk v2 to v3
- Loading branch information
Showing
8 changed files
with
3,376 additions
and
680 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,24 +1,25 @@ | ||
import * as AWS from 'aws-sdk'; | ||
import config from '../config'; | ||
import IDynamoRecord, { ResourceType } from './IDynamoRecord'; | ||
import { DynamoDBDocumentClient, QueryCommandInput, QueryCommand } from "@aws-sdk/lib-dynamodb" | ||
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; | ||
|
||
const dynamo = new AWS.DynamoDB.DocumentClient(); | ||
const dynamo = DynamoDBDocumentClient.from(new DynamoDBClient()); | ||
|
||
export const getDynamoMembers: () => Promise<IDynamoRecord[]> = async () => { | ||
const result = await dynamo | ||
.query({ | ||
TableName: config.aws.dynamoTable, | ||
KeyConditionExpression: 'resourceType = :type', | ||
FilterExpression: 'attribute_not_exists(#ttl_key) or #ttl_key = :null', | ||
ExpressionAttributeValues: { | ||
':type': ResourceType.User, | ||
':null': null, | ||
}, | ||
ExpressionAttributeNames: { | ||
'#ttl_key': 'ttl', | ||
}, | ||
} as AWS.DynamoDB.DocumentClient.QueryInput) | ||
.promise(); | ||
|
||
return result.Items as IDynamoRecord[]; | ||
.send(new QueryCommand( | ||
{ | ||
TableName: config.aws.dynamoTable, | ||
KeyConditionExpression: 'resourceType = :type', | ||
FilterExpression: 'attribute_not_exists(#ttl_key) or #ttl_key = :null', | ||
ExpressionAttributeValues: { | ||
':type': ResourceType.User, | ||
':null': null, | ||
}, | ||
ExpressionAttributeNames: { | ||
'#ttl_key': 'ttl', | ||
}, | ||
} as QueryCommandInput | ||
)); | ||
return result.Items as unknown as IDynamoRecord[]; | ||
}; |
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 |
---|---|---|
@@ -1,56 +1,42 @@ | ||
import { DynamoDBDocumentClient, QueryCommandOutput, QueryCommand } from '@aws-sdk/lib-dynamodb'; | ||
import { mockClient } from 'aws-sdk-client-mock'; | ||
import config from '../../src/config'; | ||
import { getDynamoMembers } from '../../src/dynamo/getDynamoRecords'; | ||
import { ResourceType } from '../../src/dynamo/IDynamoRecord'; | ||
import { getDynamoMembers } from '../../src/dynamo/getDynamoRecords'; | ||
|
||
// has to be 'var' as jest "hoists" execution behind the scenes and let/const cause errors | ||
/* tslint:disable */ | ||
var mockDynamoQuery: jest.Mock; | ||
/* tslint:enable */ | ||
|
||
jest.mock('aws-sdk', () => { | ||
mockDynamoQuery = jest.fn().mockImplementation(() => ({ | ||
promise: jest.fn().mockResolvedValue({ | ||
Items: [ | ||
{ | ||
resourceType: ResourceType.User, | ||
resourceKey: '6adbf131-c6c2-4bc6-b1e9-b62f812bed29', | ||
name: 'test user', | ||
email: '[email protected]', | ||
}, | ||
{ | ||
resourceType: ResourceType.User, | ||
resourceKey: '7d9e8e38-78d5-46ad-9fd0-6adad882161b', | ||
name: 'test user 2', | ||
email: '[email protected]', | ||
}, | ||
], | ||
} as AWS.DynamoDB.DocumentClient.QueryOutput), | ||
})); | ||
class FakeDynamoDb { | ||
query = mockDynamoQuery; | ||
} | ||
|
||
const AWS = { | ||
DynamoDB: { | ||
DocumentClient: FakeDynamoDb, | ||
const mockItems = { | ||
Items: [ | ||
{ | ||
resourceType: ResourceType.User, | ||
resourceKey: '6adbf131-c6c2-4bc6-b1e9-b62f812bed29', | ||
name: 'test user', | ||
email: '[email protected]', | ||
}, | ||
{ | ||
resourceType: ResourceType.User, | ||
resourceKey: '7d9e8e38-78d5-46ad-9fd0-6adad882161b', | ||
name: 'test user 2', | ||
email: '[email protected]', | ||
}, | ||
}; | ||
], | ||
} | ||
|
||
return AWS; | ||
}); | ||
const client = mockClient(DynamoDBDocumentClient); | ||
|
||
describe('getDynamoMembers', () => { | ||
beforeEach(() => { | ||
config.aws.dynamoTable = 'testTable'; | ||
client.reset() | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call dynamo query', async () => { | ||
client.on(QueryCommand).resolves(mockItems as unknown as QueryCommandOutput) | ||
await getDynamoMembers(); | ||
expect(mockDynamoQuery).toBeCalledWith({ | ||
expect(client.call(0).firstArg.input).toEqual({ | ||
TableName: 'testTable', | ||
KeyConditionExpression: 'resourceType = :type', | ||
FilterExpression: 'attribute_not_exists(#ttl_key) or #ttl_key = :null', | ||
|
@@ -61,6 +47,6 @@ describe('getDynamoMembers', () => { | |
ExpressionAttributeNames: { | ||
'#ttl_key': 'ttl', | ||
}, | ||
} as AWS.DynamoDB.DocumentClient.QueryInput); | ||
}) | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb"; | ||
import { mockClient } from 'aws-sdk-client-mock'; | ||
import IMemberDetails, { MemberType } from '../../src/aad/IMemberDetails'; | ||
import IDynamoRecord, { ResourceType } from '../../src/dynamo/IDynamoRecord'; | ||
import { handler } from '../../src/handler'; | ||
|
@@ -6,7 +8,6 @@ import { handler } from '../../src/handler'; | |
/* tslint:disable */ | ||
var mockMemberDetails: jest.Mock; | ||
var mockDynamoRecords: jest.Mock; | ||
var mockDynamoPut: jest.Mock; | ||
var emptyMemberDetails = true; | ||
var emptyDynamoDetails = true; | ||
/* tslint:enable */ | ||
|
@@ -16,14 +17,14 @@ jest.mock('../../src/aad/getMemberDetails', () => { | |
emptyMemberDetails | ||
? new Array<IMemberDetails>() | ||
: [ | ||
{ | ||
'@odata.type': MemberType.User, | ||
id: '5afcf0b5-fb7f-4b83-98cc-851a8b27025c', | ||
displayName: 'Test User', | ||
mail: '[email protected]', | ||
userPrincipalName: '[email protected]', | ||
} as IMemberDetails, | ||
], | ||
{ | ||
'@odata.type': MemberType.User, | ||
id: '5afcf0b5-fb7f-4b83-98cc-851a8b27025c', | ||
displayName: 'Test User', | ||
mail: '[email protected]', | ||
userPrincipalName: '[email protected]', | ||
} as IMemberDetails, | ||
], | ||
); | ||
return { getMemberDetails: mockMemberDetails }; | ||
}); | ||
|
@@ -33,35 +34,23 @@ jest.mock('../../src/dynamo/getDynamoRecords', () => { | |
emptyDynamoDetails | ||
? new Array<IDynamoRecord>() | ||
: [ | ||
{ | ||
resourceType: ResourceType.User, | ||
resourceKey: '932a98cb-8946-4796-8291-c7bcf4badb50', | ||
email: '[email protected]', | ||
name: 'Deleted User', | ||
} as IDynamoRecord, | ||
], | ||
{ | ||
resourceType: ResourceType.User, | ||
resourceKey: '932a98cb-8946-4796-8291-c7bcf4badb50', | ||
email: '[email protected]', | ||
name: 'Deleted User', | ||
} as IDynamoRecord, | ||
], | ||
); | ||
return { getDynamoMembers: mockDynamoRecords }; | ||
}); | ||
|
||
jest.mock('aws-sdk', () => { | ||
mockDynamoPut = jest.fn().mockImplementation(() => ({ | ||
promise: jest.fn().mockResolvedValue({} as AWS.DynamoDB.DocumentClient.PutItemOutput), | ||
})); | ||
class FakeDynamoDb { | ||
put = mockDynamoPut; | ||
} | ||
|
||
const AWS = { | ||
DynamoDB: { | ||
DocumentClient: FakeDynamoDb, | ||
}, | ||
}; | ||
|
||
return AWS; | ||
}); | ||
const client = mockClient(DynamoDBDocumentClient) | ||
|
||
describe('Handler', () => { | ||
beforeEach(() => { | ||
client.reset(); | ||
}) | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
emptyMemberDetails = true; | ||
|
@@ -79,8 +68,9 @@ describe('Handler', () => { | |
|
||
it('should run a dynamo put for an active record', async () => { | ||
emptyMemberDetails = false; | ||
client.on(PutCommand).resolves({}); | ||
await handler(); | ||
expect(mockDynamoPut).toBeCalledWith({ | ||
expect(client.call(0).firstArg.input).toEqual({ | ||
TableName: '', | ||
Item: { | ||
resourceType: ResourceType.User, | ||
|
@@ -93,8 +83,9 @@ describe('Handler', () => { | |
|
||
it('should run a dynamo put with a ttl for an inactive record', async () => { | ||
emptyDynamoDetails = false; | ||
client.on(PutCommand).resolves({}); | ||
await handler(); | ||
expect(mockDynamoPut).toHaveBeenCalledWith( | ||
expect(client.call(1).firstArg.input).toEqual( | ||
expect.objectContaining({ | ||
TableName: '', | ||
Item: { | ||
|