2014-01-14 18:26:09 +00:00
|
|
|
var request = require('supertest');
|
|
|
|
var loopback = require('loopback');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
var app = loopback();
|
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
// expose a rest api
|
|
|
|
app.use(loopback.rest());
|
|
|
|
|
|
|
|
var ds = loopback.createDataSource({
|
|
|
|
connector: require('../lib/storage-connector'),
|
|
|
|
provider: 'filesystem',
|
|
|
|
root: path.join(__dirname, 'images')
|
|
|
|
});
|
|
|
|
|
|
|
|
var Container = ds.createModel('container');
|
|
|
|
app.model(Container);
|
|
|
|
|
2014-03-31 16:49:42 +00:00
|
|
|
/*!
|
|
|
|
* Verify that the JSON response has the correct metadata properties.
|
|
|
|
* Please note the metadata vary by storage providers. This test assumes
|
|
|
|
* the 'filesystem' provider.
|
|
|
|
*
|
|
|
|
* @param {String} containerOrFile The container/file object
|
|
|
|
* @param {String} [name] The name to be checked if not undefined
|
|
|
|
*/
|
|
|
|
function verifyMetadata(containerOrFile, name) {
|
|
|
|
assert(containerOrFile);
|
|
|
|
|
|
|
|
// Name
|
|
|
|
if (name) {
|
|
|
|
assert.equal(containerOrFile.name, name);
|
|
|
|
}
|
|
|
|
// No sensitive information
|
|
|
|
assert(containerOrFile.uid === undefined);
|
|
|
|
assert(containerOrFile.gid === undefined);
|
|
|
|
|
|
|
|
// Timestamps
|
|
|
|
assert(containerOrFile.atime);
|
|
|
|
assert(containerOrFile.ctime);
|
|
|
|
assert(containerOrFile.mtime);
|
|
|
|
|
|
|
|
// Size
|
|
|
|
assert.equal(typeof containerOrFile.size, 'number');
|
|
|
|
}
|
|
|
|
|
2014-01-14 18:26:09 +00:00
|
|
|
describe('storage service', function () {
|
|
|
|
var server = null;
|
|
|
|
before(function (done) {
|
|
|
|
server = app.listen(3000, function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
2014-02-03 18:58:37 +00:00
|
|
|
it('should create a container', function (done) {
|
|
|
|
|
|
|
|
request('http://localhost:3000')
|
|
|
|
.post('/containers')
|
|
|
|
.send({name: 'test-container'})
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, function (err, res) {
|
2014-03-31 16:49:42 +00:00
|
|
|
verifyMetadata(res.body, 'test-container');
|
2014-02-03 18:58:37 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get a container', function (done) {
|
|
|
|
|
|
|
|
request('http://localhost:3000')
|
|
|
|
.get('/containers/test-container')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, function (err, res) {
|
2014-03-31 16:49:42 +00:00
|
|
|
verifyMetadata(res.body, 'test-container');
|
2014-02-03 18:58:37 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should list containers', function (done) {
|
|
|
|
|
|
|
|
request('http://localhost:3000')
|
|
|
|
.get('/containers')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, function (err, res) {
|
|
|
|
assert(Array.isArray(res.body));
|
|
|
|
assert.equal(res.body.length, 2);
|
2014-03-31 16:49:42 +00:00
|
|
|
res.body.forEach(function(c) {
|
|
|
|
verifyMetadata(c);
|
|
|
|
});
|
2014-02-03 18:58:37 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete a container', function (done) {
|
|
|
|
|
|
|
|
request('http://localhost:3000')
|
|
|
|
.del('/containers/test-container')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, function (err, res) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should list containers after delete', function (done) {
|
|
|
|
|
|
|
|
request('http://localhost:3000')
|
|
|
|
.get('/containers')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, function (err, res) {
|
|
|
|
assert(Array.isArray(res.body));
|
|
|
|
assert.equal(res.body.length, 1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should list files', function (done) {
|
|
|
|
|
|
|
|
request('http://localhost:3000')
|
|
|
|
.get('/containers/album1/files')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, function (err, res) {
|
|
|
|
assert(Array.isArray(res.body));
|
2014-03-31 16:49:42 +00:00
|
|
|
res.body.forEach(function(f) {
|
|
|
|
verifyMetadata(f);
|
|
|
|
});
|
2014-02-03 18:58:37 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-14 18:26:09 +00:00
|
|
|
it('uploads files', function (done) {
|
|
|
|
|
|
|
|
request('http://localhost:3000')
|
|
|
|
.post('/containers/album1/upload')
|
|
|
|
.attach('image', path.join(__dirname, '../example/test.jpg'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, function (err, res) {
|
|
|
|
assert.deepEqual(res.body, {"result": {"files": {"image": [
|
|
|
|
{"container": "album1", "name": "test.jpg", "type": "image/jpeg"}
|
|
|
|
]}, "fields": {}}});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-03 18:58:37 +00:00
|
|
|
it('should get file by name', function (done) {
|
|
|
|
|
|
|
|
request('http://localhost:3000')
|
|
|
|
.get('/containers/album1/files/test.jpg')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, function (err, res) {
|
2014-03-31 16:49:42 +00:00
|
|
|
verifyMetadata(res.body, 'test.jpg');
|
2014-02-03 18:58:37 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-14 18:26:09 +00:00
|
|
|
it('downloads files', function (done) {
|
|
|
|
|
|
|
|
request('http://localhost:3000')
|
|
|
|
.get('/containers/album1/download/test.jpg')
|
2014-01-30 06:39:01 +00:00
|
|
|
.expect('Content-Type', 'image/jpeg')
|
2014-01-14 18:26:09 +00:00
|
|
|
.expect(200, function (err, res) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-03 18:58:37 +00:00
|
|
|
it('should delete a file', function (done) {
|
|
|
|
|
|
|
|
request('http://localhost:3000')
|
|
|
|
.del('/containers/album1/files/test.jpg')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200, function (err, res) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-14 18:26:09 +00:00
|
|
|
it('reports errors if it fails to find the file to download', function (done) {
|
|
|
|
|
|
|
|
request('http://localhost:3000')
|
|
|
|
.get('/containers/album1/download/test_not_exist.jpg')
|
2014-01-30 06:39:01 +00:00
|
|
|
.expect('Content-Type', /json/)
|
2014-01-14 18:26:09 +00:00
|
|
|
.expect(500, function (err, res) {
|
|
|
|
assert(res.body.error);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|