2017-07-11 21:47:40 +02:00
|
|
|
const assert = require('assert');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const proxyquire = require('proxyquire');
|
|
|
|
const request = require('supertest');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
const logStub = {};
|
|
|
|
logStub.info = sinon.stub();
|
|
|
|
logStub.error = sinon.stub();
|
|
|
|
|
|
|
|
const storage = proxyquire('../../server/storage', {
|
|
|
|
'./log.js': function() {
|
|
|
|
return logStub;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
storage.flushall();
|
|
|
|
|
|
|
|
describe('Server integration tests', function() {
|
|
|
|
let server;
|
|
|
|
let storage;
|
|
|
|
let uuid;
|
2017-07-11 21:49:24 +02:00
|
|
|
let fileId;
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
before(function() {
|
2017-07-11 21:34:49 +02:00
|
|
|
const app = proxyquire('../../server/server', {
|
2017-07-11 21:47:40 +02:00
|
|
|
'./log.js': function() {
|
|
|
|
return logStub;
|
|
|
|
}
|
2017-07-11 21:34:49 +02:00
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
server = app.server;
|
|
|
|
storage = app.storage;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
|
|
|
storage.flushall();
|
|
|
|
storage.quit();
|
|
|
|
server.close();
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
function upload() {
|
2017-08-03 01:50:58 +02:00
|
|
|
return request(server)
|
|
|
|
.post('/upload')
|
|
|
|
.field('fname', 'test_upload.txt')
|
|
|
|
.set(
|
|
|
|
'X-File-Metadata',
|
|
|
|
JSON.stringify({
|
|
|
|
id: '111111111111111111111111',
|
|
|
|
filename: 'test_upload.txt'
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.attach('file', './test/test_upload.txt');
|
2017-07-11 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
it('Responds with a 200 when the service is up', function() {
|
2017-09-13 21:01:55 +02:00
|
|
|
return request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(200);
|
2017-07-11 21:47:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Rejects with a 404 when a file id is not valid', function() {
|
2017-08-03 01:50:58 +02:00
|
|
|
return request(server)
|
|
|
|
.post('/upload/123')
|
|
|
|
.field('fname', 'test_upload.txt')
|
|
|
|
.set(
|
|
|
|
'X-File-Metadata',
|
|
|
|
JSON.stringify({
|
|
|
|
silly: 'text'
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.attach('file', './test/test_upload.txt')
|
|
|
|
.expect(404);
|
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
it('Accepts a file and stores it when properly uploaded', function(done) {
|
|
|
|
upload().then(res => {
|
2017-08-03 01:50:58 +02:00
|
|
|
assert(res.body.hasOwnProperty('delete'));
|
|
|
|
uuid = res.body.delete;
|
|
|
|
assert(res.body.hasOwnProperty('url'));
|
|
|
|
assert(res.body.hasOwnProperty('id'));
|
|
|
|
fileId = res.body.id;
|
|
|
|
fs.access('./static/' + fileId, fs.constants.F_OK, err => {
|
|
|
|
if (err) {
|
|
|
|
done(new Error('The file does not exist'));
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
it('Responds with a 200 if a file exists', function() {
|
2017-09-13 21:01:55 +02:00
|
|
|
return request(server)
|
|
|
|
.get('/exists/' + fileId)
|
|
|
|
.expect(200);
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
it('Exists in the redis server', function() {
|
2017-08-03 01:50:58 +02:00
|
|
|
return storage
|
|
|
|
.exists(fileId)
|
|
|
|
.then(() => assert(1))
|
|
|
|
.catch(err => assert.fail());
|
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
it('Fails delete if the delete token does not match', function() {
|
2017-08-03 01:50:58 +02:00
|
|
|
return request(server)
|
|
|
|
.post('/delete/' + fileId)
|
|
|
|
.send({ delete_token: 11 })
|
|
|
|
.expect(404);
|
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
it('Fails delete if the id is invalid', function() {
|
2017-09-13 21:01:55 +02:00
|
|
|
return request(server)
|
|
|
|
.post('/delete/1')
|
|
|
|
.expect(404);
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
|
|
|
|
2018-01-24 19:23:13 +01:00
|
|
|
it('Successfully deletes if the id is valid and the delete token matches', function(done) {
|
2017-08-03 01:50:58 +02:00
|
|
|
request(server)
|
|
|
|
.post('/delete/' + fileId)
|
|
|
|
.send({ delete_token: uuid })
|
|
|
|
.expect(200)
|
|
|
|
.then(() => {
|
|
|
|
fs.access('./static/' + fileId, fs.constants.F_OK, err => {
|
|
|
|
if (err) {
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
done(new Error('The file does not exist'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
it('Responds with a 404 if a file does not exist', function() {
|
2017-09-13 21:01:55 +02:00
|
|
|
return request(server)
|
|
|
|
.get('/exists/notfound')
|
|
|
|
.expect(404);
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
it('Uploads properly after a delete', function(done) {
|
|
|
|
upload().then(res => {
|
2017-08-03 01:50:58 +02:00
|
|
|
assert(res.body.hasOwnProperty('delete'));
|
|
|
|
uuid = res.body.delete;
|
|
|
|
assert(res.body.hasOwnProperty('url'));
|
|
|
|
assert(res.body.hasOwnProperty('id'));
|
|
|
|
fileId = res.body.id;
|
|
|
|
fs.access('./static/' + fileId, fs.constants.F_OK, err => {
|
|
|
|
if (err) {
|
|
|
|
done(new Error('The file does not exist'));
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
it('Responds with a 200 for the download page', function() {
|
2017-09-13 21:01:55 +02:00
|
|
|
return request(server)
|
|
|
|
.get('/download/' + fileId)
|
|
|
|
.expect(200);
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
it('Downloads a file properly', function() {
|
2017-09-13 21:01:55 +02:00
|
|
|
return request(server)
|
|
|
|
.get('/assets/download/' + fileId)
|
|
|
|
.then(res => {
|
|
|
|
assert(res.header.hasOwnProperty('content-disposition'));
|
|
|
|
assert(res.header.hasOwnProperty('content-type'));
|
|
|
|
assert(res.header.hasOwnProperty('content-length'));
|
|
|
|
assert(res.header.hasOwnProperty('x-file-metadata'));
|
|
|
|
assert.equal(
|
|
|
|
res.header['content-disposition'],
|
|
|
|
'attachment; filename=test_upload.txt'
|
|
|
|
);
|
|
|
|
assert.equal(res.header['content-type'], 'application/octet-stream');
|
|
|
|
});
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
it('The file is deleted after one download', function() {
|
|
|
|
assert(!fs.existsSync('./static/' + fileId));
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
2017-07-11 21:47:40 +02:00
|
|
|
|
|
|
|
it('No longer exists in the redis server', function() {
|
2017-08-03 01:50:58 +02:00
|
|
|
return storage
|
|
|
|
.exists(fileId)
|
|
|
|
.then(() => assert.fail())
|
|
|
|
.catch(err => assert(1));
|
|
|
|
});
|
2017-07-11 21:34:49 +02:00
|
|
|
});
|