Skip to content

Commit 0b79646

Browse files
committed
test: add single-integration for dynamodb
1 parent f7671b0 commit 0b79646

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
service: dynamodb-proxy
2+
3+
provider:
4+
name: aws
5+
runtime: nodejs10.x
6+
7+
plugins:
8+
localPath: './../../../../../../'
9+
modules:
10+
- serverless-apigateway-service-proxy
11+
12+
custom:
13+
apiGatewayServiceProxies:
14+
- dynamodb:
15+
path: /dynamodb
16+
method: post
17+
tableName:
18+
Ref: MyTestTable
19+
hashKey: 'id'
20+
cors: true
21+
22+
resources:
23+
Resources:
24+
MyTestTable:
25+
Type: AWS::DynamoDB::Table
26+
Properties:
27+
TableName: MyTestTable
28+
AttributeDefinitions:
29+
-
30+
AttributeName: id
31+
AttributeType: S
32+
KeySchema:
33+
-
34+
AttributeName: id
35+
KeyType: HASH
36+
ProvisionedThroughput:
37+
ReadCapacityUnits: 1
38+
WriteCapacityUnits: 1
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
const expect = require('chai').expect
4+
const fetch = require('node-fetch')
5+
const { deployWithRandomStage, removeService, getDynamodbItem } = require('../../../utils')
6+
7+
describe('Single dynamodb Proxy Integration Test', () => {
8+
let endpoint
9+
let stage
10+
const tableName = 'MyTestTable'
11+
const config = '__tests__/integration/dynamodb/single-integration/service/serverless.yml'
12+
13+
beforeAll(async () => {
14+
const result = await deployWithRandomStage(config)
15+
16+
stage = result.stage
17+
endpoint = result.endpoint
18+
})
19+
20+
afterAll(async () => {
21+
removeService(stage, config)
22+
})
23+
24+
it('should get correct response from dynamodb proxy endpoint', async () => {
25+
const testEndpoint = `${endpoint}/dynamodb`
26+
27+
const response = await fetch(testEndpoint, {
28+
method: 'POST',
29+
headers: { 'Content-Type': 'application/json' },
30+
body: JSON.stringify({ item1: { S: 'item1' }, item2: { S: 'item2' } })
31+
})
32+
expect(response.headers.get('access-control-allow-origin')).to.deep.equal('*')
33+
expect(response.status).to.be.equal(200)
34+
const body = await response.json()
35+
36+
const item = await getDynamodbItem(tableName, body.id)
37+
expect(item).to.be.deep.equal({
38+
Item: {
39+
id: { S: body.id },
40+
item1: { S: 'item1' },
41+
item2: { S: 'item2' }
42+
}
43+
})
44+
})
45+
})

__tests__/utils.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const aws = require('aws-sdk')
99
const s3 = new aws.S3()
1010

1111
const region = 'us-east-1'
12+
const dynamodb = new aws.DynamoDB({ region })
1213
const cloudformation = new aws.CloudFormation({ region })
1314

1415
function getApiGatewayEndpoint(outputs) {
@@ -25,6 +26,19 @@ async function getStackOutputs(stackName) {
2526
return _.zipObject(keys, values)
2627
}
2728

29+
async function getDynamodbItem(tableName, hashKey) {
30+
return await dynamodb
31+
.getItem({
32+
Key: {
33+
id: {
34+
S: hashKey
35+
}
36+
},
37+
TableName: tableName
38+
})
39+
.promise()
40+
}
41+
2842
async function getS3Object(bucket, key) {
2943
const resp = await s3
3044
.getObject({
@@ -77,5 +91,6 @@ module.exports = {
7791
removeService,
7892
deployWithRandomStage,
7993
getS3Object,
80-
deleteS3Object
94+
deleteS3Object,
95+
getDynamodbItem
8196
}

0 commit comments

Comments
 (0)