2017-10-13 18:08:13 -04:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
2018-07-25 18:45:38 -04:00
|
|
|
const app = require('../server');
|
|
|
|
const server = app.server;
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-25 21:34:16 -04:00
|
|
|
const chai = require('chai');
|
|
|
|
chai.use(require('chai-http'));
|
|
|
|
const should = chai.should();
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-25 21:34:16 -04:00
|
|
|
const User = require('../app/models/User.js');
|
|
|
|
const Invite = require('../app/models/Invite.js');
|
|
|
|
const Upload = require('../app/models/Upload.js');
|
2018-07-25 01:44:45 -04:00
|
|
|
|
2017-10-13 18:08:13 -04:00
|
|
|
//---------------- DATABASE UTIL ----------------//
|
|
|
|
|
2018-07-25 18:45:38 -04:00
|
|
|
exports.clearDatabase = async () =>
|
|
|
|
Promise.all([
|
|
|
|
User.remove({}),
|
|
|
|
Invite.remove({}),
|
|
|
|
Upload.remove({})
|
|
|
|
]);
|
|
|
|
|
|
|
|
//---------------- API ROUTES ----------------//
|
|
|
|
|
2018-07-25 21:34:16 -04:00
|
|
|
exports.login = async (credentials, agent) => {
|
|
|
|
return (agent ? agent : chai.request(server))
|
2018-07-25 18:45:38 -04:00
|
|
|
.post('/api/auth/login')
|
|
|
|
.send(credentials);
|
2018-07-25 21:34:16 -04:00
|
|
|
};
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-25 18:45:38 -04:00
|
|
|
exports.createInvite = async (invite) => {
|
2018-07-24 19:39:55 -04:00
|
|
|
if (!invite.code) invite.code = 'code';
|
|
|
|
if (!invite.scope) invite.scope = ['test.perm', 'file.upload'];
|
|
|
|
if (!invite.issuer) invite.issuer = 'Mocha';
|
|
|
|
if (!invite.issued) invite.issued = new Date();
|
2018-07-25 18:45:38 -04:00
|
|
|
return Invite.create(invite);
|
2018-07-24 19:39:55 -04:00
|
|
|
};
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-25 18:45:38 -04:00
|
|
|
exports.registerUser = async (user) => {
|
|
|
|
if (!user.username) user.username = 'user';
|
|
|
|
if (!user.password) user.password = 'pass';
|
|
|
|
if (!user.invite) user.invite = 'code';
|
|
|
|
return chai.request(server)
|
2017-10-13 18:08:13 -04:00
|
|
|
.post('/api/auth/register')
|
2018-07-25 18:45:38 -04:00
|
|
|
.send(user);
|
2017-10-13 18:08:13 -04:00
|
|
|
};
|
|
|
|
|
2018-07-25 21:34:16 -04:00
|
|
|
exports.ping = async (agent) =>
|
|
|
|
(agent ? agent : chai.request(server))
|
|
|
|
.get('/api/auth/ping')
|
|
|
|
.send();
|
|
|
|
|
2018-07-25 18:45:38 -04:00
|
|
|
//---------------- TEST ENTRY CREATION ----------------//
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-25 18:45:38 -04:00
|
|
|
exports.createTestInvite = async () =>
|
|
|
|
exports.createInvite({});
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-25 18:45:38 -04:00
|
|
|
exports.createTestInvites = async (n) => {
|
|
|
|
const codes = Array.from(new Array(n), (val, index) => 'code' + index);
|
|
|
|
return Promise.all(codes.map(code => exports.createInvite({code: code})));
|
2017-10-13 18:08:13 -04:00
|
|
|
};
|
|
|
|
|
2018-07-25 18:45:38 -04:00
|
|
|
exports.createTestUser = async () => {
|
|
|
|
await exports.createTestInvite();
|
|
|
|
return exports.registerUser({});
|
2017-10-13 18:08:13 -04:00
|
|
|
};
|
|
|
|
|
2018-07-25 18:45:38 -04:00
|
|
|
//---------------- UPLOAD API ----------------//
|
2017-10-13 18:08:13 -04:00
|
|
|
|
|
|
|
var upload = function(token, file, cb) {
|
|
|
|
chai.request(server)
|
|
|
|
.post('/api/upload')
|
|
|
|
.attach('file', file)
|
|
|
|
.set('Authorization', 'Bearer ' + token)
|
|
|
|
.end(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
var loginUpload = function(user, cb) {
|
|
|
|
login(user, function(err, res) {
|
|
|
|
upload(res.body.token, 'test/test.png', cb);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var loginUploadFile = function(user, file, cb) {
|
|
|
|
login(user, function(err, res) {
|
|
|
|
upload(res.body.token, file, cb);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var verifySuccessfulUpload = function(user, done) {
|
|
|
|
loginUpload(user, function(err, res) {
|
|
|
|
res.should.have.status(200);
|
|
|
|
res.body.should.have.be.a('object');
|
2017-10-14 15:15:27 -04:00
|
|
|
res.body.should.have.property('url');
|
2017-10-13 18:08:13 -04:00
|
|
|
res.body.should.have.property('name');
|
2017-10-14 15:15:27 -04:00
|
|
|
expect(res.body.name).to.match(/^[a-z]{6}$/);
|
2017-10-13 18:08:13 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var verifyFailedSizeUpload = function(user, done) {
|
|
|
|
loginUploadFile(user, 'test/large.bin', function(err, res) {
|
|
|
|
res.should.have.status(413);
|
|
|
|
res.body.should.be.a('object');
|
|
|
|
res.body.should.have.property('message').eql('File too large.');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var verifyFailedPermissionUpload = function(user, done) {
|
|
|
|
loginUpload(user, function(err, res) {
|
|
|
|
res.should.have.status(403);
|
|
|
|
res.body.should.be.a('object');
|
|
|
|
res.body.should.have.property('message').eql('Permission error.');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var verifyFailedAuthUpload = function(done) {
|
|
|
|
async.parallel([
|
|
|
|
function(cb) {
|
2018-07-24 19:39:55 -04:00
|
|
|
upload('bogus', 'test/test.png', function(err, res) {
|
2017-10-13 18:08:13 -04:00
|
|
|
res.should.have.status(401);
|
|
|
|
res.body.should.be.a('object');
|
|
|
|
res.body.should.have.property('message').eql('UnauthorizedError: jwt malformed');
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(cb) {
|
|
|
|
upload('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.' +
|
|
|
|
'eyJpc3MiOiJzaGltYXBhbi5yb2NrcyIsImlhd' +
|
|
|
|
'CI6MTUwNzkyNTAyNSwiZXhwIjoxNTM5NDYxMD' +
|
|
|
|
'I1LCJhdWQiOiJ3d3cuc2hpbWFwYW4ucm9ja3M' +
|
|
|
|
'iLCJzdWIiOiJUZXN0VXNlciIsInVzZXJuYW1l' +
|
|
|
|
'IjoiVGVzdFVzZXIiLCJzY29wZSI6ImZpbGUud' +
|
|
|
|
'XBsb2FkIn0.e746_BNNuxlbXKESKKYsxl6e5j' +
|
|
|
|
'8JwmEFxO3zRf66tWo',
|
|
|
|
'test/test.png',
|
|
|
|
function(err, res) {
|
2018-07-24 19:39:55 -04:00
|
|
|
res.should.have.status(401);
|
|
|
|
res.body.should.be.a('object');
|
|
|
|
res.body.should.have.property('message').eql('UnauthorizedError: invalid signature');
|
|
|
|
cb();
|
|
|
|
})
|
2017-10-13 18:08:13 -04:00
|
|
|
}
|
|
|
|
], function(err, res) {
|
|
|
|
if (err) console.log(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|