forked from jeremydaly/lambda-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetLink.js
More file actions
212 lines (173 loc) · 7.91 KB
/
Copy pathgetLink.js
File metadata and controls
212 lines (173 loc) · 7.91 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'use strict';
const Promise = require('bluebird') // Promise library
const expect = require('chai').expect // Assertion library
// Require Sinon.js library
const sinon = require('sinon')
const AWS = require('aws-sdk') // AWS SDK (automatically available in Lambda)
// AWS.config.credentials = new AWS.SharedIniFileCredentials({profile: 'madlucas'})
const S3 = require('../lib/s3-service') // Init S3 Service
// Init API instance
const api = require('../index')({ version: 'v1.0' })
// NOTE: Set test to true
api._test = true;
let event = {
httpMethod: 'get',
path: '/test',
body: {},
multiValueHeaders: {
'Content-Type': 'application/json'
}
}
/******************************************************************************/
/*** DEFINE TEST ROUTES ***/
/******************************************************************************/
api.get('/s3Link', async function(req,res) {
stub.callsArgWith(2, null, 'https://s3.amazonaws.com/my-test-bucket/test/test.txt?AWSAccessKeyId=AKXYZ&Expires=1534290845&Signature=XYZ')
let url = await res.getLink('s3://my-test-bucket/test/test.txt')
res.send(url)
})
api.get('/s3LinkExpire', async function(req,res) {
stub.callsArgWith(2, null, 'https://s3.amazonaws.com/my-test-bucket/test/test.txt?AWSAccessKeyId=AKXYZ&Expires=1534290845&Signature=XYZ')
let url = await res.getLink('s3://my-test-bucket/test/test.txt',60)
res.send(url)
})
api.get('/s3LinkInvalidExpire', async function(req,res) {
stub.callsArgWith(2, null, 'https://s3.amazonaws.com/my-test-bucket/test/test.txt?AWSAccessKeyId=AKXYZ&Expires=1534290845&Signature=XYZ')
let url = await res.getLink('s3://my-test-bucket/test/test.txt','test')
res.send(url)
})
api.get('/s3LinkExpireFloat', async function(req,res) {
stub.callsArgWith(2, null, 'https://s3.amazonaws.com/my-test-bucket/test/test.txt?AWSAccessKeyId=AKXYZ&Expires=1534290845&Signature=XYZ')
let url = await res.getLink('s3://my-test-bucket/test/test.txt',3.145)
res.send(url)
})
api.get('/s3LinkError', async function(req,res) {
stub.callsArgWith(2, 'getSignedUrl error', null)
let url = await res.getLink('s3://my-test-bucket/test/test.txt', async (e) => {
return await Promise.delay(100).then(() => {})
})
res.send(url)
})
api.get('/s3LinkErrorCustom', async function(req,res) {
stub.callsArgWith(2, 'getSignedUrl error', null)
let url = await res.getLink('s3://my-test-bucket/test/test.txt', 60 ,async (e) => {
return await Promise.delay(100).then(() => {
res.error('Custom error')
})
})
res.send(url)
})
api.get('/s3LinkErrorStandard', async function(req,res) {
stub.callsArgWith(2, 'getSignedUrl error', null)
let url = await res.getLink('s3://my-test-bucket/test/test.txt', 900)
res.send(url)
})
api.get('/s3LinkInvalid', async function(req,res) {
//stub.callsArgWith(2, 'getSignedUrl error', null)
let url = await res.getLink('s3://my-test-bucket', 900)
res.send(url)
})
/******************************************************************************/
/*** BEGIN TESTS ***/
/******************************************************************************/
let stub
describe('getLink Tests:', function() {
this.slow(300)
before(function() {
// Stub getSignedUrl
stub = sinon.stub(S3,'getSignedUrl')
})
it('Simple path', async function() {
let _event = Object.assign({},event,{ path: '/s3Link' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
multiValueHeaders: { 'content-type': ['application/json'] },
statusCode: 200,
body: 'https://s3.amazonaws.com/my-test-bucket/test/test.txt?AWSAccessKeyId=AKXYZ&Expires=1534290845&Signature=XYZ',
isBase64Encoded: false
})
expect(stub.lastCall.args[1]).to.deep.equal({ Bucket: 'my-test-bucket', Key: 'test/test.txt', Expires: 900 })
}) // end it
it('Simple path (with custom expiration)', async function() {
let _event = Object.assign({},event,{ path: '/s3LinkExpire' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
multiValueHeaders: { 'content-type': ['application/json'] },
statusCode: 200,
body: 'https://s3.amazonaws.com/my-test-bucket/test/test.txt?AWSAccessKeyId=AKXYZ&Expires=1534290845&Signature=XYZ',
isBase64Encoded: false
})
// console.log(stub);
expect(stub.lastCall.args[1]).to.deep.equal({ Bucket: 'my-test-bucket', Key: 'test/test.txt', Expires: 60 })
}) // end it
it('Simple path (with invalid expiration)', async function() {
let _event = Object.assign({},event,{ path: '/s3LinkInvalidExpire' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
multiValueHeaders: { 'content-type': ['application/json'] },
statusCode: 200,
body: 'https://s3.amazonaws.com/my-test-bucket/test/test.txt?AWSAccessKeyId=AKXYZ&Expires=1534290845&Signature=XYZ',
isBase64Encoded: false
})
// console.log(stub);
expect(stub.lastCall.args[1]).to.deep.equal({ Bucket: 'my-test-bucket', Key: 'test/test.txt', Expires: 900 })
}) // end it
it('Simple path (with float expiration)', async function() {
let _event = Object.assign({},event,{ path: '/s3LinkExpireFloat' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
multiValueHeaders: { 'content-type': ['application/json'] },
statusCode: 200,
body: 'https://s3.amazonaws.com/my-test-bucket/test/test.txt?AWSAccessKeyId=AKXYZ&Expires=1534290845&Signature=XYZ',
isBase64Encoded: false
})
// console.log(stub);
expect(stub.lastCall.args[1]).to.deep.equal({ Bucket: 'my-test-bucket', Key: 'test/test.txt', Expires: 3 })
}) // end it
it('Error (with delayed callback)', async function() {
let _event = Object.assign({},event,{ path: '/s3LinkError' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
multiValueHeaders: { 'content-type': ['application/json'] },
statusCode: 500,
body: '{"error":"getSignedUrl error"}',
isBase64Encoded: false
})
expect(stub.lastCall.args[1]).to.deep.equal({ Bucket: 'my-test-bucket', Key: 'test/test.txt', Expires: 900 })
}) // end it
it('Custom Error (with delayed callback)', async function() {
let _event = Object.assign({},event,{ path: '/s3LinkErrorCustom' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
multiValueHeaders: { 'content-type': ['application/json'] },
statusCode: 500,
body: '{"error":"Custom error"}',
isBase64Encoded: false
})
expect(stub.lastCall.args[1]).to.deep.equal({ Bucket: 'my-test-bucket', Key: 'test/test.txt', Expires: 60 })
}) // end it
it('Error (with default callback)', async function() {
let _event = Object.assign({},event,{ path: '/s3LinkErrorStandard' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
multiValueHeaders: { 'content-type': ['application/json'] },
statusCode: 500,
body: '{"error":"getSignedUrl error"}',
isBase64Encoded: false
})
expect(stub.lastCall.args[1]).to.deep.equal({ Bucket: 'my-test-bucket', Key: 'test/test.txt', Expires: 900 })
}) // end it
it('Error (invalid S3 path)', async function() {
let _event = Object.assign({},event,{ path: '/s3LinkInvalid' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
multiValueHeaders: { 'content-type': ['application/json'] },
statusCode: 500,
body: '{"error":"Invalid S3 path"}',
isBase64Encoded: false
})
}) // end it
after(function() {
stub.restore()
})
}) // end getLink tests