Permalink
Browse files

update S3 tests with promise mock

  • Loading branch information...
jeremydaly committed Apr 25, 2018
1 parent 8eebc99 commit 728f8927f91124c4bbce1c8e5287885f278e3af4
Showing with 47 additions and 32 deletions.
  1. +20 −16 test/download.js
  2. +27 −16 test/sendFile.js
@@ -90,29 +90,33 @@ api.get('/download/all', function(req,res) {
// S3 file
api.get('/download/s3', function(req,res) {

stub.withArgs({Bucket: 'my-test-bucket', Key: 'test.txt'}).resolves({
AcceptRanges: 'bytes',
LastModified: new Date('2018-04-01T13:32:58.000Z'),
ContentLength: 23,
ETag: '"ae771fbbba6a74eeeb77754355831713"',
ContentType: 'text/plain',
Metadata: {},
Body: Buffer.from('Test file for sendFile\n')
stub.withArgs({Bucket: 'my-test-bucket', Key: 'test.txt'}).returns({
promise: () => { return {
AcceptRanges: 'bytes',
LastModified: new Date('2018-04-01T13:32:58.000Z'),
ContentLength: 23,
ETag: '"ae771fbbba6a74eeeb77754355831713"',
ContentType: 'text/plain',
Metadata: {},
Body: Buffer.from('Test file for sendFile\n')
}}
})

res.download('s3://my-test-bucket/test.txt')
})

api.get('/download/s3path', function(req,res) {

stub.withArgs({Bucket: 'my-test-bucket', Key: 'test/test.txt'}).resolves({
AcceptRanges: 'bytes',
LastModified: new Date('2018-04-01T13:32:58.000Z'),
ContentLength: 23,
ETag: '"ae771fbbba6a74eeeb77754355831713"',
ContentType: 'text/plain',
Metadata: {},
Body: Buffer.from('Test file for sendFile\n')
stub.withArgs({Bucket: 'my-test-bucket', Key: 'test/test.txt'}).returns({
promise: () => { return {
AcceptRanges: 'bytes',
LastModified: new Date('2018-04-01T13:32:58.000Z'),
ContentLength: 23,
ETag: '"ae771fbbba6a74eeeb77754355831713"',
ContentType: 'text/plain',
Metadata: {},
Body: Buffer.from('Test file for sendFile\n')
}}
})

res.download('s3://my-test-bucket/test/test.txt')
@@ -9,6 +9,9 @@ const fs = require('fs') // Require Node.js file system
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
@@ -126,29 +129,33 @@ api.get('/sendfile/custom-cache-control', function(req,res) {
// S3 file
api.get('/sendfile/s3', function(req,res) {

stub.withArgs({Bucket: 'my-test-bucket', Key: 'test.txt'}).resolves({
AcceptRanges: 'bytes',
LastModified: new Date('2018-04-01T13:32:58.000Z'),
ContentLength: 23,
ETag: '"ae771fbbba6a74eeeb77754355831713"',
ContentType: 'text/plain',
Metadata: {},
Body: Buffer.from('Test file for sendFile\n')
stub.withArgs({Bucket: 'my-test-bucket', Key: 'test.txt'}).returns({
promise: () => { return {
AcceptRanges: 'bytes',
LastModified: new Date('2018-04-01T13:32:58.000Z'),
ContentLength: 23,
ETag: '"ae771fbbba6a74eeeb77754355831713"',
ContentType: 'text/plain',
Metadata: {},
Body: Buffer.from('Test file for sendFile\n')
}}
})

res.sendFile('s3://my-test-bucket/test.txt')
})

api.get('/sendfile/s3path', function(req,res) {

stub.withArgs({Bucket: 'my-test-bucket', Key: 'test/test.txt'}).resolves({
AcceptRanges: 'bytes',
LastModified: new Date('2018-04-01T13:32:58.000Z'),
ContentLength: 23,
ETag: '"ae771fbbba6a74eeeb77754355831713"',
ContentType: 'text/plain',
Metadata: {},
Body: Buffer.from('Test file for sendFile\n')
stub.withArgs({Bucket: 'my-test-bucket', Key: 'test/test.txt'}).returns({
promise: () => { return {
AcceptRanges: 'bytes',
LastModified: new Date('2018-04-01T13:32:58.000Z'),
ContentLength: 23,
ETag: '"ae771fbbba6a74eeeb77754355831713"',
ContentType: 'text/plain',
Metadata: {},
Body: Buffer.from('Test file for sendFile\n')
}}
})

res.sendFile('s3://my-test-bucket/test/test.txt')
@@ -180,6 +187,10 @@ describe('SendFile Tests:', function() {
before(function() {
// Stub getObjectAsync
stub = sinon.stub(S3,'getObject')
//stub.prototype.promise = function() { console.log('test') }
//console.log('proto:',);
//S3.getObject.promise = () => { }
//stub.promise = () => {}
})

it('Bad path', async function() {

0 comments on commit 728f892

Please sign in to comment.