Skip to content

Commit f5c9ce3

Browse files
test: added single-integration s3 test
1 parent 17ac6a2 commit f5c9ce3

File tree

3 files changed

+115
-8
lines changed

3 files changed

+115
-8
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
service: s3-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+
- s3:
15+
path: /s3/{key}
16+
method: post
17+
action: PutObject
18+
bucket:
19+
Ref: S3Bucket
20+
key:
21+
pathParam: key
22+
cors: true
23+
24+
resources:
25+
Resources:
26+
S3Bucket:
27+
Type: 'AWS::S3::Bucket'
28+
29+
Outputs:
30+
S3BucketName:
31+
Value:
32+
Ref: S3Bucket
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict'
2+
3+
const expect = require('chai').expect
4+
const fetch = require('node-fetch')
5+
const {
6+
deployWithRandomStage,
7+
removeService,
8+
getS3Object,
9+
deleteS3Object
10+
} = require('../../../utils')
11+
12+
describe('Single S3 Proxy Integration Test', () => {
13+
let endpoint
14+
let stage
15+
let bucket
16+
const config = '__tests__/integration/s3/single-integration/service/serverless.yml'
17+
const key = 'my-test-object.json'
18+
19+
beforeAll(async () => {
20+
const result = await deployWithRandomStage(config)
21+
22+
stage = result.stage
23+
endpoint = result.endpoint
24+
bucket = result.outputs.S3BucketName
25+
})
26+
27+
afterAll(async () => {
28+
await deleteS3Object(bucket, key)
29+
removeService(stage, config)
30+
})
31+
32+
it('should get correct response from s3 proxy endpoint', async () => {
33+
const testEndpoint = `${endpoint}/s3/${key}`
34+
35+
const response = await fetch(testEndpoint, {
36+
method: 'POST',
37+
headers: { 'Content-Type': 'application/json' },
38+
body: JSON.stringify({ message: 'test' })
39+
})
40+
expect(response.headers.get('access-control-allow-origin')).to.deep.equal('*')
41+
expect(response.status).to.be.equal(200)
42+
43+
const uploadedObject = await getS3Object(bucket, key)
44+
expect(uploadedObject.toString()).to.equal(JSON.stringify({ message: 'test' }))
45+
})
46+
})

__tests__/utils.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,41 @@ const fs = require('fs')
66
const path = require('path')
77
const execSync = require('child_process').execSync
88
const aws = require('aws-sdk')
9+
const s3 = new aws.S3()
910
const cloudformation = new aws.CloudFormation({ region: 'us-east-1' })
1011

11-
async function getApiGatewayEndpoint(stackName) {
12+
function getApiGatewayEndpoint(outputs) {
13+
return outputs.ServiceEndpoint.match(/https:\/\/.+\.execute-api\..+\.amazonaws\.com.+/)[0]
14+
}
15+
16+
async function getStackOutputs(stackName) {
1217
const result = await cloudformation.describeStacks({ StackName: stackName }).promise()
18+
const stack = result.Stacks[0]
19+
20+
const keys = stack.Outputs.map((x) => x.OutputKey)
21+
const values = stack.Outputs.map((x) => x.OutputValue)
22+
23+
return _.zipObject(keys, values)
24+
}
25+
26+
async function getS3Object(bucket, key) {
27+
const resp = await s3
28+
.getObject({
29+
Bucket: bucket,
30+
Key: key
31+
})
32+
.promise()
33+
34+
return resp.Body
35+
}
1336

14-
const endpointOutput = _.find(result.Stacks[0].Outputs, { OutputKey: 'ServiceEndpoint' })
15-
.OutputValue
16-
return endpointOutput.match(/https:\/\/.+\.execute-api\..+\.amazonaws\.com.+/)[0]
37+
async function deleteS3Object(bucket, key) {
38+
await s3
39+
.deleteObject({
40+
Bucket: bucket,
41+
Key: key
42+
})
43+
.promise()
1744
}
1845

1946
function deployService(stage, config) {
@@ -37,14 +64,16 @@ async function deployWithRandomStage(config) {
3764
.substring(2)
3865
const stackName = `${serviceName}-${stage}`
3966
deployService(stage, config)
40-
const endpoint = await getApiGatewayEndpoint(stackName)
67+
const outputs = await getStackOutputs(stackName)
68+
const endpoint = getApiGatewayEndpoint(outputs)
4169

42-
return { stage, endpoint }
70+
return { stackName, stage, outputs, endpoint }
4371
}
4472

4573
module.exports = {
46-
getApiGatewayEndpoint,
4774
deployService,
4875
removeService,
49-
deployWithRandomStage
76+
deployWithRandomStage,
77+
getS3Object,
78+
deleteS3Object
5079
}

0 commit comments

Comments
 (0)