mirror of
https://github.com/Foltik/Shimapan
synced 2024-11-10 15:48:27 -05:00
104 lines
2.7 KiB
JavaScript
104 lines
2.7 KiB
JavaScript
process.env.NODE_ENV = 'test';
|
|
|
|
const chai = require('chai');
|
|
chai.use(require('chai-http'));
|
|
const should = chai.should();
|
|
|
|
const User = require('../app/models/User.js');
|
|
const Invite = require('../app/models/Invite.js');
|
|
const Upload = require('../app/models/Upload.js');
|
|
const Key = require('../app/models/Key.js');
|
|
|
|
const Buffer = require('buffer').Buffer;
|
|
const crypto = require('crypto');
|
|
const fs = require('fs');
|
|
const fsPromises = fs.promises;
|
|
|
|
//---------------- DATABASE UTIL ----------------//
|
|
|
|
exports.clearDatabase = () =>
|
|
Promise.all([
|
|
User.remove({}),
|
|
Invite.remove({}),
|
|
Key.remove({}),
|
|
Upload.remove({})
|
|
]);
|
|
|
|
//---------------- API ROUTES ----------------//
|
|
|
|
exports.login = (credentials, agent) =>
|
|
agent
|
|
.post('/api/auth/login')
|
|
.send(credentials);
|
|
|
|
exports.logout = agent =>
|
|
agent
|
|
.post('/api/auth/logout');
|
|
|
|
exports.createInvite = (invite) =>
|
|
Invite.create(invite);
|
|
|
|
exports.registerUser = (user, agent) =>
|
|
agent
|
|
.post('/api/auth/register')
|
|
.send(user);
|
|
|
|
exports.whoami = (agent) =>
|
|
agent
|
|
.get('/api/auth/whoami')
|
|
.send();
|
|
|
|
//---------------- TEST ENTRY CREATION ----------------//
|
|
|
|
exports.createTestInvite = () =>
|
|
exports.createInvite({code: 'code', scope: ['file.upload']});
|
|
|
|
exports.createTestInvites = (n) =>
|
|
Promise.all(
|
|
Array.from(new Array(n), (val, index) => 'code' + index)
|
|
.map(code => exports.createInvite({code: code}))
|
|
);
|
|
|
|
exports.createTestUser = async agent => {
|
|
await exports.createTestInvite();
|
|
return exports.registerUser({username: 'user', password: 'pass', invite: 'code'}, agent);
|
|
};
|
|
|
|
exports.createTestSession = async agent => {
|
|
await exports.createTestUser(agent);
|
|
return exports.login({username: 'user', password: 'pass'}, agent);
|
|
};
|
|
|
|
exports.createTestFile = (size, name) =>
|
|
fsPromises.writeFile(name, Buffer.allocUnsafe(size));
|
|
|
|
//---------------- 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;
|
|
|
|
//---------------- UPLOADS ----------------//
|
|
|
|
exports.upload = (file, agent) =>
|
|
agent
|
|
.post('/api/upload')
|
|
.attach('file', file);
|