2017-10-13 18:08:13 -04:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
2018-07-25 21:34:16 -04:00
|
|
|
const chai = require('chai');
|
|
|
|
chai.use(require('chai-http'));
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-26 19:40:42 -04:00
|
|
|
const ModelPath = '../app/models/';
|
|
|
|
const User = require(ModelPath + 'User.js');
|
|
|
|
const Upload = require(ModelPath + 'Upload.js');
|
|
|
|
const Key = require(ModelPath + 'Key.js');
|
|
|
|
const Invite = require(ModelPath + 'Invite.js');
|
2018-07-25 01:44:45 -04:00
|
|
|
|
2018-07-26 13:17:37 -04:00
|
|
|
const Buffer = require('buffer').Buffer;
|
|
|
|
const crypto = require('crypto');
|
2018-07-26 16:54:08 -04:00
|
|
|
const fs = require('fs');
|
|
|
|
const fsPromises = fs.promises;
|
2018-07-26 13:17:37 -04:00
|
|
|
|
2017-10-13 18:08:13 -04:00
|
|
|
//---------------- DATABASE UTIL ----------------//
|
|
|
|
|
2018-07-26 16:54:08 -04:00
|
|
|
exports.clearDatabase = () =>
|
2018-07-25 18:45:38 -04:00
|
|
|
Promise.all([
|
|
|
|
User.remove({}),
|
|
|
|
Invite.remove({}),
|
2018-07-26 13:19:47 -04:00
|
|
|
Key.remove({}),
|
2018-07-25 18:45:38 -04:00
|
|
|
Upload.remove({})
|
|
|
|
]);
|
|
|
|
|
|
|
|
//---------------- API ROUTES ----------------//
|
|
|
|
|
2018-07-26 16:54:08 -04:00
|
|
|
exports.login = (credentials, agent) =>
|
2018-07-26 13:17:37 -04:00
|
|
|
agent
|
2018-07-25 18:45:38 -04:00
|
|
|
.post('/api/auth/login')
|
|
|
|
.send(credentials);
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-26 13:17:37 -04:00
|
|
|
exports.logout = agent =>
|
|
|
|
agent
|
|
|
|
.post('/api/auth/logout');
|
|
|
|
|
2018-07-26 16:54:08 -04:00
|
|
|
exports.createInvite = (invite) =>
|
2018-07-26 13:17:37 -04:00
|
|
|
Invite.create(invite);
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-26 16:54:08 -04:00
|
|
|
exports.registerUser = (user, agent) =>
|
2018-07-26 13:17:37 -04:00
|
|
|
agent
|
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-26 16:54:08 -04:00
|
|
|
exports.whoami = (agent) =>
|
2018-07-26 13:17:37 -04:00
|
|
|
agent
|
|
|
|
.get('/api/auth/whoami')
|
2018-07-25 21:34:16 -04:00
|
|
|
.send();
|
|
|
|
|
2018-07-25 18:45:38 -04:00
|
|
|
//---------------- TEST ENTRY CREATION ----------------//
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-26 16:54:08 -04:00
|
|
|
exports.createTestInvite = () =>
|
2018-07-26 19:40:42 -04:00
|
|
|
exports.createInvite({code: 'code', scope: ['file.upload'], issuer: 'Mocha'});
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-26 16:54:08 -04:00
|
|
|
exports.createTestInvites = (n) =>
|
2018-07-26 13:17:37 -04:00
|
|
|
Promise.all(
|
|
|
|
Array.from(new Array(n), (val, index) => 'code' + index)
|
2018-07-26 19:40:42 -04:00
|
|
|
.map(code => exports.createInvite({code: code, scope: ['file.upload'], issuer: 'Mocha'}))
|
2018-07-26 13:17:37 -04:00
|
|
|
);
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-26 13:17:37 -04:00
|
|
|
exports.createTestUser = async agent => {
|
2018-07-25 18:45:38 -04:00
|
|
|
await exports.createTestInvite();
|
2018-07-26 19:40:42 -04:00
|
|
|
return exports.registerUser({displayname: 'user', password: 'pass', invite: 'code'}, agent);
|
2017-10-13 18:08:13 -04:00
|
|
|
};
|
|
|
|
|
2018-07-26 13:17:37 -04:00
|
|
|
exports.createTestSession = async agent => {
|
|
|
|
await exports.createTestUser(agent);
|
2018-07-26 19:40:42 -04:00
|
|
|
return exports.login({displayname: 'user', password: 'pass'}, agent);
|
2017-10-13 18:08:13 -04:00
|
|
|
};
|
|
|
|
|
2018-07-26 16:54:08 -04:00
|
|
|
exports.createTestFile = (size, name) =>
|
|
|
|
fsPromises.writeFile(name, Buffer.allocUnsafe(size));
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-26 16:54:08 -04:00
|
|
|
//---------------- FILESYSTEM ----------------//
|
|
|
|
|
|
|
|
exports.deleteFile = file =>
|
|
|
|
fsPromises.unlink(file);
|
|
|
|
|
|
|
|
exports.fileExists = file =>
|
|
|
|
fsPromises.access(file, fs.constants.R_OK);
|
|
|
|
|
|
|
|
exports.fileSize = async file =>
|
|
|
|
(await fsPromises.stat(file)).size;
|
|
|
|
|
|
|
|
exports.fileHash = file =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const hash = crypto.createHash('MD5');
|
|
|
|
fs.createReadStream(file)
|
|
|
|
.on('error', reject)
|
|
|
|
.on('data', chunk => hash.update(chunk))
|
|
|
|
.on('end', () => resolve(hash.digest('hex')));
|
|
|
|
});
|
|
|
|
|
|
|
|
exports.directoryFileCount = async dir =>
|
|
|
|
(await fsPromises.readdir(dir)).length;
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-26 13:17:37 -04:00
|
|
|
//---------------- UPLOADS ----------------//
|
2017-10-13 18:08:13 -04:00
|
|
|
|
2018-07-26 13:17:37 -04:00
|
|
|
exports.upload = (file, agent) =>
|
|
|
|
agent
|
|
|
|
.post('/api/upload')
|
|
|
|
.attach('file', file);
|