Selaa lähdekoodia

Add stats tests

production
Jack Foltz 5 vuotta sitten
vanhempi
commit
a8cbb6b0cf
Allekirjoittanut: foltik <jack@foltz.io> GPG Key ID: 303F88F996E95541
2 muutettua tiedostoa jossa 114 lisäystä ja 2 poistoa
  1. +95
    -0
      test/api.js
  2. +19
    -2
      test/testUtil.js

+ 95
- 0
test/api.js Näytä tiedosto

@@ -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)));

+ 19
- 2
test/testUtil.js Näytä tiedosto

@@ -8,6 +8,7 @@ const User = require(ModelPath + 'User.js');
const Upload = require(ModelPath + 'Upload.js');
const Key = require(ModelPath + 'Key.js');
const Invite = require(ModelPath + 'Invite.js');
const View = require(ModelPath + 'View.js');

const Buffer = require('buffer').Buffer;
const crypto = require('crypto');
@@ -35,7 +36,8 @@ exports.clearDatabase = () =>
User.remove({}),
Invite.remove({}),
Key.remove({}),
Upload.remove({})
Upload.remove({}),
View.remove({})
]);

exports.insertInvite = invite =>
@@ -44,6 +46,12 @@ exports.insertInvite = invite =>
exports.insertKey = key =>
Key.create(key);

exports.insertUpload = upload =>
Upload.create(upload);

exports.insertView = view =>
View.create(view);

exports.setBanned = (username, banned) =>
User.updateOne({username: username}, {banned: banned});

@@ -200,4 +208,13 @@ exports.ban = (username, agent) =>

exports.unban = (username, agent) =>
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');


Loading…
Peruuta
Tallenna