Skip to content

Commit

Permalink
refactors filesAdapter tests in factories
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Mar 1, 2016
1 parent 0b990b6 commit 9287afc
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 39 deletions.
40 changes: 22 additions & 18 deletions spec/FilesController.spec.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
var FilesController = require('../src/Controllers/FilesController').FilesController;
var GridStoreAdapter = require("../src/Adapters/Files/GridStoreAdapter").GridStoreAdapter;
var S3Adapter = require("../src/Adapters/Files/S3Adapter").S3Adapter;
var Config = require("../src/Config");

var FCTestFactory = require("./FilesControllerTestFactory");


// Small additional tests to improve overall coverage
describe("FilesController",()=>{

it("should properly expand objects", (done) => {
var config = new Config(Parse.applicationId);
var adapter = new GridStoreAdapter();
var filesController = new FilesController(adapter);
var result = filesController.expandFilesInObject(config, function(){});
// Test the grid store adapter
var gridStoreAdapter = new GridStoreAdapter();
FCTestFactory.testAdapter("GridStoreAdapter", gridStoreAdapter);

if (process.env.S3_ACCESS_KEY && process.env.S3_SECRET_KEY) {

// Test the S3 Adapter
var s3Adapter = new S3Adapter(process.env.S3_ACCESS_KEY, process.env.S3_SECRET_KEY, 'parse.server.tests');

expect(result).toBeUndefined();
FCTestFactory.testAdapter("S3Adapter",s3Adapter);

var fullFile = {
type: '__type',
url: "http://an.url"
}
// Test S3 with direct access
var s3DirectAccessAdapter = new S3Adapter(process.env.S3_ACCESS_KEY, process.env.S3_SECRET_KEY, 'parse.server.tests', {
directAccess: true
});

var anObject = {
aFile: fullFile
}
filesController.expandFilesInObject(config, anObject);
expect(anObject.aFile.url).toEqual("http://an.url");
FCTestFactory.testAdapter("S3AdapterDirect", s3DirectAccessAdapter);

done();
})
})
} else if (!process.env.TRAVIS) {
console.log("set S3_ACCESS_KEY and S3_SECRET_KEY to test S3Adapter")
}
});
73 changes: 73 additions & 0 deletions spec/FilesControllerTestFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

var FilesController = require('../src/Controllers/FilesController').FilesController;
var Config = require("../src/Config");

var testAdapter = function(name, adapter) {
// Small additional tests to improve overall coverage

var config = new Config(Parse.applicationId);
var filesController = new FilesController(adapter);

describe("FilesController with "+name,()=>{

it("should properly expand objects", (done) => {

var result = filesController.expandFilesInObject(config, function(){});

expect(result).toBeUndefined();

var fullFile = {
type: '__type',
url: "http://an.url"
}

var anObject = {
aFile: fullFile
}
filesController.expandFilesInObject(config, anObject);
expect(anObject.aFile.url).toEqual("http://an.url");

done();
})

it("should properly create, read, delete files", (done) => {
var filename;
filesController.createFile(config, "file.txt", "hello world").then( (result) => {
ok(result.url);
ok(result.name);
filename = result.name;
expect(result.name.match(/file.txt/)).not.toBe(null);
return filesController.getFileData(config, filename);
}, (err) => {
fail("The adapter should create the file");
console.error(err);
done();
}).then((result) => {
expect(result instanceof Buffer).toBe(true);
expect(result.toString('utf-8')).toEqual("hello world");
return filesController.deleteFile(config, filename);
}, (err) => {
fail("The adapter should get the file");
console.error(err);
done();
}).then((result) => {

filesController.getFileData(config, filename).then((res) => {
fail("the file should be deleted");
done();
}, (err) => {
done();
});

}, (err) => {
fail("The adapter should delete the file");
console.error(err);
done();
});
}, 5000); // longer tests
});
}

module.exports = {
testAdapter: testAdapter
}
68 changes: 47 additions & 21 deletions src/Adapters/Files/S3Adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ export class S3Adapter extends FilesAdapter {
};
AWS.config._region = this._region;
this._s3Client = new AWS.S3(s3Options);
this._hasBucket = false;
}

createBucket() {
var promise;
if (this._hasBucket) {
promise = Promise.resolve();
} else {
promise = new Promise((resolve, reject) => {
this._s3Client.createBucket(() => {
this._hasBucket = true;
resolve();
});
});
}
return promise;
}

// For a given config object, filename, and data, store a file in S3
Expand All @@ -60,26 +76,30 @@ export class S3Adapter extends FilesAdapter {
if (this._directAccess) {
params.ACL = "public-read"
}
return new Promise((resolve, reject) => {
this._s3Client.upload(params, (err, data) => {
if (err !== null) {
return reject(err);
}
resolve(data);
return this.createBucket().then(() => {
return new Promise((resolve, reject) => {
this._s3Client.upload(params, (err, data) => {
if (err !== null) {
return reject(err);
}
resolve(data);
});
});
});
}

deleteFile(config, filename) {
return new Promise((resolve, reject) => {
let params = {
Key: this._bucketPrefix + filename
};
this._s3Client.deleteObject(params, (err, data) =>{
if(err !== null) {
return reject(err);
}
resolve(data);
return this.createBucket().then(() => {
return new Promise((resolve, reject) => {
let params = {
Key: this._bucketPrefix + filename
};
this._s3Client.deleteObject(params, (err, data) =>{
if(err !== null) {
return reject(err);
}
resolve(data);
});
});
});
}
Expand All @@ -88,12 +108,18 @@ export class S3Adapter extends FilesAdapter {
// Returns a promise that succeeds with the buffer result from S3
getFileData(config, filename) {
let params = {Key: this._bucketPrefix + filename};
return new Promise((resolve, reject) => {
this._s3Client.getObject(params, (err, data) => {
if (err !== null) {
return reject(err);
}
resolve(data.Body);
return this.createBucket().then(() => {
return new Promise((resolve, reject) => {
this._s3Client.getObject(params, (err, data) => {
if (err !== null) {
return reject(err);
}
// Something happend here...
if (data && !data.Body) {
return reject(data);
}
resolve(data.Body);
});
});
});
}
Expand Down

0 comments on commit 9287afc

Please sign in to comment.