1
0
mirror of https://github.com/Foltik/Shimapan synced 2025-01-19 13:36:11 -05:00

Add stats tests

This commit is contained in:
Jack Foltz 2018-09-19 13:06:56 -04:00
parent 2ad3148a08
commit a8cbb6b0cf
Signed by: foltik
GPG Key ID: 303F88F996E95541
2 changed files with 114 additions and 2 deletions

View File

@ -934,4 +934,99 @@ describe('Users', () => {
}); });
}); });
describe('Stats', () => {
const setupUploadsAndViews = async () => {
const oneDay = 1000 * 60 * 60 * 24;
const currentDate = new Date();
const get_uid = i => {
return 'abcde' + String.fromCharCode(i + 97)
};
for (let i = 0; i < 8; i++) {
await util.insertUpload({
uid: get_uid(i),
views: 0,
uploader: 'user',
uploaderKey: null,
date: new Date(currentDate - i * oneDay),
file: {
size: 1
}
});
}
await util.insertUpload({
uid: 'zyxwvu',
uploader: 'someguy',
date: new Date(currentDate - 3 * oneDay),
file: {
size: 1
}
});
for (let i = 0; i < 8; i++) {
await util.insertView({
uid: get_uid(i),
uploader: 'user',
remoteAddress: '::1',
userAgent: 'fiyerfocks',
date: new Date(currentDate - i * oneDay),
});
}
await util.insertView({
uid: 'zyxwvu',
uploader: 'someguy',
remoteAddress: '::1',
userAgent: 'fiyerfocks',
date: new Date(currentDate - 3 * oneDay)
});
};
describe('/GET week', () => {
describe('0 Valid Request', () => {
it('must return valid stats for the past week', async () => {
await setupUploadsAndViews();
const oneDay = 1000 * 60 * 60 * 24;
const currentDate = new Date();
await util.createSession(agent, ['stats.get'], 'user');
const stats = (await util.getStatsWeek(agent)).body;
console.log(stats);
for (let i = 0; i < 7; i++) {
let date = new Date(currentDate - i * oneDay).toISOString();
let dateStr = date.substr(5, 2) + '-' + date.substr(8, 2);
let dayStats = stats[dateStr];
dayStats.should.be.a('object', 'Stats should exist for the day ' + dateStr);
dayStats.uploads.should.equal(1, 'Should be only one upload for the day ' + dateStr);
dayStats.size.should.equal(1, 'Should be only one byte uploaded for the day ' + dateStr);
dayStats.views.should.equal(1, 'Should be only one view for the day ' + dateStr);
}
let pastDate = new Date(currentDate - 7 * oneDay).toISOString();
let pastDateStr = pastDate.substr(5, 2) + '-' + pastDate.substr(8, 2);
stats.should.not.have.property(pastDateStr, 'No stats should exist past 1 week ago');
return util.logout(agent);
});
});
});
describe('/GET all', () => {
describe('0 Valid Request', () => {
it('must return valid stats for all time', async () => {
await setupUploadsAndViews();
await util.createSession(agent, ['stats.get'], 'user');
const stats = await util.getStatsAll(agent);
console.log(stats.body);
await util.logout(agent);
});
});
});
});
after(() => server.close(() => process.exit(0))); after(() => server.close(() => process.exit(0)));

View File

@ -8,6 +8,7 @@ const User = require(ModelPath + 'User.js');
const Upload = require(ModelPath + 'Upload.js'); const Upload = require(ModelPath + 'Upload.js');
const Key = require(ModelPath + 'Key.js'); const Key = require(ModelPath + 'Key.js');
const Invite = require(ModelPath + 'Invite.js'); const Invite = require(ModelPath + 'Invite.js');
const View = require(ModelPath + 'View.js');
const Buffer = require('buffer').Buffer; const Buffer = require('buffer').Buffer;
const crypto = require('crypto'); const crypto = require('crypto');
@ -35,7 +36,8 @@ exports.clearDatabase = () =>
User.remove({}), User.remove({}),
Invite.remove({}), Invite.remove({}),
Key.remove({}), Key.remove({}),
Upload.remove({}) Upload.remove({}),
View.remove({})
]); ]);
exports.insertInvite = invite => exports.insertInvite = invite =>
@ -44,6 +46,12 @@ exports.insertInvite = invite =>
exports.insertKey = key => exports.insertKey = key =>
Key.create(key); Key.create(key);
exports.insertUpload = upload =>
Upload.create(upload);
exports.insertView = view =>
View.create(view);
exports.setBanned = (username, banned) => exports.setBanned = (username, banned) =>
User.updateOne({username: username}, {banned: banned}); User.updateOne({username: username}, {banned: banned});
@ -201,3 +209,12 @@ exports.ban = (username, agent) =>
exports.unban = (username, agent) => exports.unban = (username, agent) =>
agent.post('/api/users/unban') agent.post('/api/users/unban')
.send({username: username}); .send({username: username});
//---------------- Stats ----------------//
exports.getStatsWeek = agent =>
agent.get('/api/stats/week');
exports.getStatsAll = agent =>
agent.get('/api/stats/all');