1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-15 17:18:05 -05:00
shimapan/test/testUtil.js

229 lines
6.1 KiB
JavaScript
Raw Normal View History

2017-10-13 18:08:13 -04:00
process.env.NODE_ENV = 'test';
const chai = require('chai');
chai.use(require('chai-http'));
2017-10-13 18:08:13 -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-09-19 13:06:56 -04:00
const View = require(ModelPath + 'View.js');
2018-07-26 13:17:37 -04:00
const Buffer = require('buffer').Buffer;
const crypto = require('crypto');
const fs = require('fs');
const fsPromises = fs.promises;
2018-07-29 20:09:09 -04:00
const path = require('path');
2018-07-26 13:17:37 -04:00
//---------------- RESPONSE VERIFICATION ----------------//
exports.verifyResponse = (res, status, message) => {
res.should.have.status(status);
res.body.should.be.a('object');
res.body.should.have.property('message').equal(message);
};
exports.verifyResponseObj = (res, status, obj) => {
res.should.have.status(status);
res.body.should.deep.equal(obj);
};
2017-10-13 18:08:13 -04:00
//---------------- DATABASE UTIL ----------------//
exports.clearDatabase = () =>
2018-07-25 18:45:38 -04:00
Promise.all([
2018-12-26 11:20:26 -05:00
User.deleteMany(),
Invite.deleteMany(),
Key.deleteMany(),
Upload.deleteMany(),
View.deleteMany()
2018-07-25 18:45:38 -04:00
]);
exports.insertInvite = invite =>
Invite.create(invite);
exports.insertKey = key =>
Key.create(key);
2018-09-19 13:06:56 -04:00
exports.insertUpload = upload =>
Upload.create(upload);
exports.insertView = view =>
View.create(view);
2018-08-12 05:30:50 -04:00
exports.setBanned = (username, banned) =>
User.updateOne({username: username}, {banned: banned});
2018-07-25 18:45:38 -04:00
//---------------- API ROUTES ----------------//
exports.login = (credentials, agent) =>
agent.post('/api/auth/login')
2018-07-25 18:45:38 -04:00
.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');
2017-10-13 18:08:13 -04:00
exports.registerUser = (user, agent) =>
agent.post('/api/auth/register')
2018-07-25 18:45:38 -04:00
.send(user);
2017-10-13 18:08:13 -04:00
2018-08-12 05:30:50 -04:00
exports.whoami = (agent, key) =>
agent.get('/api/auth/whoami')
2018-08-12 05:30:50 -04:00
.send({key: key});
2018-07-25 18:45:38 -04:00
//---------------- TEST ENTRY CREATION ----------------//
2017-10-13 18:08:13 -04:00
exports.createTestInvite = () =>
exports.insertInvite({code: 'code', scope: ['file.upload'], issuer: 'Mocha'});
2017-10-13 18:08:13 -04:00
exports.createTestInvites = (n) =>
2018-07-26 13:17:37 -04:00
Promise.all(
Array.from(new Array(n), (val, index) => 'code' + index)
.map(code => exports.insertInvite({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();
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);
return exports.login({displayname: 'user', password: 'pass'}, agent);
2017-10-13 18:08:13 -04:00
};
exports.createSession = async (agent, scope, displayname) => {
await exports.insertInvite({code: 'code', scope: scope, issuer: 'Mocha'});
await exports.registerUser({displayname: displayname ? displayname : 'user', password: 'pass', invite: 'code'}, agent);
await exports.login({displayname: displayname ? displayname : 'user', password: 'pass'}, agent);
await Invite.deleteOne({code: 'code'});
};
exports.createTestFile = (size, name) =>
fsPromises.writeFile(name, Buffer.allocUnsafe(size));
2017-10-13 18:08:13 -04:00
exports.createTestKey = scope =>
exports.insertKey({key: 'key', identifier: 'test', scope: scope, issuer: 'Mocha'});
//---------------- 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')));
});
2018-08-01 20:25:27 -04:00
exports.bufferHash = buffer =>
crypto.createHash('MD5')
.update(buffer)
.digest('hex');
exports.directoryFileCount = async dir =>
(await fsPromises.readdir(dir)).length;
2017-10-13 18:08:13 -04:00
2018-07-29 20:09:09 -04:00
exports.clearDirectory = async dir => {
const files = await fsPromises.readdir(dir);
const promises = files.map(file => fsPromises.unlink(path.join(dir, file)));
return Promise.all(promises);
};
2018-07-26 13:17:37 -04:00
//---------------- UPLOADS ----------------//
2017-10-13 18:08:13 -04:00
exports.upload = (file, agent, key) => {
const request = agent.post('/api/upload');
if (key)
request.field('key', key);
return request.attach('file', file);
};
//---------------- Invites ----------------//
exports.createInvite = (invite, agent) =>
agent.post('/api/invites/create')
.send(invite);
exports.deleteInvite = (code, agent) =>
agent.post('/api/invites/delete')
.send({code: code});
exports.getInvites = (query, agent) =>
agent.get('/api/invites/get')
.send(query);
2018-08-01 18:58:30 -04:00
2018-08-01 20:25:27 -04:00
//---------------- Keys ----------------//
2018-08-01 18:58:30 -04:00
exports.createKey = (key, agent) =>
agent.post('/api/keys/create')
.send(key);
exports.deleteKey = (key, agent) =>
agent.post('/api/keys/delete')
.send({key: key});
exports.getKeys = (query, agent) =>
agent.get('/api/keys/get')
.send(query);
2018-08-01 20:25:27 -04:00
//---------------- Viewing ----------------//
exports.binaryFileParser = (res, cb) => {
res.setEncoding('binary');
res.data = '';
res.on("data", function (chunk) {
res.data += chunk;
});
res.on('end', function () {
cb(null, Buffer.from(res.data, 'binary'));
});
};
exports.view = (id, agent) =>
agent.get('/v/' + id)
2018-08-13 05:52:54 -04:00
.buffer();
//---------------- Users ----------------//
exports.getUsers = (query, agent) =>
agent.get('/api/users/get')
.send(query);
exports.ban = (username, agent) =>
agent.post('/api/users/ban')
.send({username: username});
exports.unban = (username, agent) =>
agent.post('/api/users/unban')
2018-09-19 13:06:56 -04:00
.send({username: username});
//---------------- Stats ----------------//
2019-01-01 16:45:49 -05:00
exports.getStatsUploads = (query, agent) =>
agent.get('/api/stats/uploads')
.send(query);
exports.getStatsViews = (query, agent) =>
agent.get('/api/stats/views')
.send(query);
2018-09-19 13:06:56 -04:00
exports.getStatsWeek = agent =>
agent.get('/api/stats/week');
exports.getStatsAll = agent =>
agent.get('/api/stats/all');