-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.test.js
114 lines (96 loc) · 2.54 KB
/
publish.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const { CreateTableCommand, DeleteTableCommand, DescribeTableCommand } = require('@aws-sdk/client-dynamodb');
const { DynamoDBDocumentClient, GetCommand } = require('@aws-sdk/lib-dynamodb');
const { testSha, testDatetime, testMessage } = require('./testdata/env');
const { publishPayload, ddbClient } = require('./publish');
const TABLE_NAME = 'metadata';
const TABLE_ARN = `arn:aws:dynamodb:local:000000000000:table/${TABLE_NAME}`;
const REGION = 'local';
const PAYLOAD = {
commit_branch: 'main',
commit_datetime: testDatetime,
commit_message: testMessage,
commit_sha: testSha,
repository: 'babbel/publish-build-metadata',
slices: ['api', 'consumer'],
ttl: 1000,
};
beforeEach(async () => {
// setup dynamodb endpoint
process.env.AWS_ENDPOINT_URL = 'http://localhost:8000';
// setup inputs
await createDynamoDBTable();
});
afterEach(async () => {
await deleteDynamoDBTable();
});
test('content should be published', async () => {
const publishResult = await publishPayload(TABLE_ARN, PAYLOAD);
expect(publishResult['$metadata'].httpStatusCode).toEqual(200);
const docClient = DynamoDBDocumentClient.from(client());
const result = await docClient.send(new GetCommand(
{
TableName: TABLE_NAME,
Key: {
repository: PAYLOAD.repository,
commit_sha: PAYLOAD.commit_sha,
}
}
));
expect(result.Item).toEqual(PAYLOAD);
});
//
// Test Helpers
//
function client() {
return ddbClient(REGION);
}
async function createDynamoDBTable() {
const params = {
TableName: TABLE_NAME,
KeySchema: [
{
AttributeName: 'repository',
KeyType: 'HASH',
},
{
AttributeName: 'commit_sha',
KeyType: 'RANGE',
},
],
AttributeDefinitions: [
{
AttributeName: 'repository',
AttributeType: 'S',
},
{
AttributeName: 'commit_sha',
AttributeType: 'S',
},
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
StreamSpecification: {
StreamEnabled: false,
},
};
try {
await client().send(new CreateTableCommand(params));
} catch(err) {
if (err == "ResourceInUseException") {
await deleteDynamoDBTable();
await createDynamoDBTable();
}
}
const describeParams = {
TableName: TABLE_NAME
};
return await client().send(new DescribeTableCommand(describeParams));
}
async function deleteDynamoDBTable() {
const params = {
TableName: TABLE_NAME
};
return await client().send(new DeleteTableCommand(params));
}