mirror of
https://github.com/Foltik/Shimapan
synced 2025-01-07 08:42:49 -05:00
Add new stats tests
This commit is contained in:
parent
d1610db1fe
commit
c6011b751c
278
test/api.js
278
test/api.js
@ -45,7 +45,10 @@ describe('Authentication', () => {
|
|||||||
const userCount = await User.countDocuments({displayname: user.displayname});
|
const userCount = await User.countDocuments({displayname: user.displayname});
|
||||||
userCount.should.equal(1, 'The user should have be created in the database');
|
userCount.should.equal(1, 'The user should have be created in the database');
|
||||||
|
|
||||||
const inviteCount = await Invite.countDocuments({code: user.invite, recipient: canonicalize(user.displayname)});
|
const inviteCount = await Invite.countDocuments({
|
||||||
|
code: user.invite,
|
||||||
|
recipient: canonicalize(user.displayname)
|
||||||
|
});
|
||||||
inviteCount.should.equal(1, 'The invite should be marked as used by the user');
|
inviteCount.should.equal(1, 'The invite should be marked as used by the user');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +72,10 @@ describe('Authentication', () => {
|
|||||||
const res = await (util.registerUser(user, agent));
|
const res = await (util.registerUser(user, agent));
|
||||||
util.verifyResponse(res, 422, message);
|
util.verifyResponse(res, 422, message);
|
||||||
|
|
||||||
const inviteCount = await Invite.countDocuments({code: user.invite, recipient: canonicalize(user.displayname)});
|
const inviteCount = await Invite.countDocuments({
|
||||||
|
code: user.invite,
|
||||||
|
recipient: canonicalize(user.displayname)
|
||||||
|
});
|
||||||
inviteCount.should.equal(0, 'Invite should not be marked as used or received by the user');
|
inviteCount.should.equal(0, 'Invite should not be marked as used or received by the user');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +97,10 @@ describe('Authentication', () => {
|
|||||||
const res = await util.registerUser(user, agent);
|
const res = await util.registerUser(user, agent);
|
||||||
util.verifyResponse(res, code, message);
|
util.verifyResponse(res, code, message);
|
||||||
|
|
||||||
const inviteCount = await Invite.countDocuments({code: user.invite, recipient: canonicalize(user.displayname)});
|
const inviteCount = await Invite.countDocuments({
|
||||||
|
code: user.invite,
|
||||||
|
recipient: canonicalize(user.displayname)
|
||||||
|
});
|
||||||
inviteCount.should.equal(0, 'The invite should not be inserted into the database after rejection');
|
inviteCount.should.equal(0, 'The invite should not be inserted into the database after rejection');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -936,8 +945,8 @@ describe('Users', () => {
|
|||||||
|
|
||||||
describe('Stats', () => {
|
describe('Stats', () => {
|
||||||
const setupUploadsAndViews = async () => {
|
const setupUploadsAndViews = async () => {
|
||||||
const oneDay = 1000 * 60 * 60 * 24;
|
|
||||||
const currentDate = new Date();
|
const currentDate = new Date();
|
||||||
|
const oneDay = 1000 * 60 * 60 * 24;
|
||||||
|
|
||||||
const get_uid = i => {
|
const get_uid = i => {
|
||||||
return 'abcde' + String.fromCharCode(i + 97)
|
return 'abcde' + String.fromCharCode(i + 97)
|
||||||
@ -984,6 +993,267 @@ describe('Stats', () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
describe('/GET uploads', () => {
|
||||||
|
describe('0 Valid Request', () => {
|
||||||
|
const currentDate = new Date();
|
||||||
|
const oneDay = 1000 * 60 * 60 * 24;
|
||||||
|
|
||||||
|
it('must return valid upload stats', async () => {
|
||||||
|
await Promise.all([
|
||||||
|
util.insertUpload({
|
||||||
|
uid: 'uvwxyz',
|
||||||
|
uploader: 'user',
|
||||||
|
date: new Date(currentDate - 3 * oneDay),
|
||||||
|
file: {
|
||||||
|
originalName: 'lol.png',
|
||||||
|
size: 1,
|
||||||
|
mime: 'image/png'
|
||||||
|
}
|
||||||
|
}), util.insertUpload({
|
||||||
|
uid: 'abcdef',
|
||||||
|
uploader: 'user',
|
||||||
|
date: new Date(currentDate - 3 * oneDay),
|
||||||
|
file: {
|
||||||
|
originalName: 'lol.png',
|
||||||
|
size: 1,
|
||||||
|
mime: 'image/png'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
|
||||||
|
await util.createSession(agent, ['stats.get'], 'user');
|
||||||
|
|
||||||
|
const res = await util.getStatsUploads({}, agent);
|
||||||
|
res.should.have.status(200);
|
||||||
|
|
||||||
|
const stats = res.body;
|
||||||
|
stats.should.be.a('Array');
|
||||||
|
stats.should.have.length(2, 'There should be exactly two uploads returned');
|
||||||
|
|
||||||
|
const stat = stats[0];
|
||||||
|
stat.should.have.property('date');
|
||||||
|
stat.should.have.property('uid');
|
||||||
|
stat.should.have.property('key');
|
||||||
|
stat.should.have.property('file');
|
||||||
|
|
||||||
|
return util.logout(agent);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('must return an empty set when there are no stats', async () => {
|
||||||
|
await util.createSession(agent, ['stats.get'], 'user');
|
||||||
|
|
||||||
|
const res = await util.getStatsUploads({}, agent);
|
||||||
|
res.should.have.status(200);
|
||||||
|
|
||||||
|
const stats = res.body;
|
||||||
|
stats.should.be.a('Array');
|
||||||
|
stats.should.have.length(0, 'No uploads should be returned');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('must constrain results by date', async () => {
|
||||||
|
await Promise.all([
|
||||||
|
util.insertUpload({
|
||||||
|
uid: 'uvwxyz',
|
||||||
|
uploader: 'user',
|
||||||
|
date: new Date(currentDate - 8 * oneDay),
|
||||||
|
file: {
|
||||||
|
originalName: 'lol.png',
|
||||||
|
size: 1,
|
||||||
|
mime: 'image/png'
|
||||||
|
}
|
||||||
|
}), util.insertUpload({
|
||||||
|
uid: 'lmnopq',
|
||||||
|
uploader: 'user',
|
||||||
|
date: new Date(currentDate - 6 * oneDay),
|
||||||
|
file: {
|
||||||
|
originalName: 'lol.png',
|
||||||
|
size: 1,
|
||||||
|
mime: 'image/png'
|
||||||
|
}
|
||||||
|
}), util.insertUpload({
|
||||||
|
uid: 'abcdef',
|
||||||
|
uploader: 'user',
|
||||||
|
date: new Date(currentDate - 4 * oneDay),
|
||||||
|
file: {
|
||||||
|
originalName: 'lol.png',
|
||||||
|
size: 1,
|
||||||
|
mime: 'image/png'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
|
||||||
|
await util.createSession(agent, ['stats.get'], 'user');
|
||||||
|
|
||||||
|
const res = await util.getStatsUploads({
|
||||||
|
before: new Date(currentDate - 5 * oneDay),
|
||||||
|
after: new Date(currentDate - 7 * oneDay)
|
||||||
|
}, agent);
|
||||||
|
res.should.have.status(200);
|
||||||
|
|
||||||
|
const stats = res.body;
|
||||||
|
stats.should.be.a('Array');
|
||||||
|
stats.should.have.length(1, 'There should be exactly one upload returned');
|
||||||
|
|
||||||
|
return util.logout(agent);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('must limit results', async () => {
|
||||||
|
await Promise.all([
|
||||||
|
util.insertUpload({
|
||||||
|
uid: 'uvwxyz',
|
||||||
|
uploader: 'user',
|
||||||
|
date: new Date(currentDate - 3 * oneDay),
|
||||||
|
file: {
|
||||||
|
originalName: 'lol.png',
|
||||||
|
size: 1,
|
||||||
|
mime: 'image/png'
|
||||||
|
}
|
||||||
|
}), util.insertUpload({
|
||||||
|
uid: 'abcdef',
|
||||||
|
uploader: 'user',
|
||||||
|
date: new Date(currentDate - 3 * oneDay),
|
||||||
|
file: {
|
||||||
|
originalName: 'lol.png',
|
||||||
|
size: 1,
|
||||||
|
mime: 'image/png'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
|
||||||
|
await util.createSession(agent, ['stats.get'], 'user');
|
||||||
|
|
||||||
|
const res = await util.getStatsUploads({limit: 1}, agent);
|
||||||
|
res.should.have.status(200);
|
||||||
|
|
||||||
|
const stats = res.body;
|
||||||
|
stats.should.be.a('Array');
|
||||||
|
stats.should.have.length(1, 'There should be exactly one upload returned');
|
||||||
|
|
||||||
|
return util.logout(agent);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('/GET views', () => {
|
||||||
|
describe('0 Valid Request', () => {
|
||||||
|
const currentDate = new Date();
|
||||||
|
const oneDay = 1000 * 60 * 60 * 24;
|
||||||
|
|
||||||
|
it('must return valid view stats', async () => {
|
||||||
|
await Promise.all([
|
||||||
|
util.insertView({
|
||||||
|
uid: 'abcdef',
|
||||||
|
uploader: 'user',
|
||||||
|
remoteAddress: '::1',
|
||||||
|
userAgent: 'fiyerfocks',
|
||||||
|
date: new Date(currentDate - 3 * oneDay),
|
||||||
|
}), util.insertView({
|
||||||
|
uid: 'uvwxyz',
|
||||||
|
uploader: 'user',
|
||||||
|
remoteAddress: '::1',
|
||||||
|
userAgent: 'fiyerfocks',
|
||||||
|
date: new Date(currentDate - 3 * oneDay),
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
|
||||||
|
await util.createSession(agent, ['stats.get'], 'user');
|
||||||
|
|
||||||
|
const res = await util.getStatsViews({}, agent);
|
||||||
|
res.should.have.status(200);
|
||||||
|
|
||||||
|
const stats = res.body;
|
||||||
|
stats.should.be.a('Array');
|
||||||
|
stats.should.have.length(2, 'There should be exactly two views returned');
|
||||||
|
|
||||||
|
const stat = stats[0];
|
||||||
|
stat.should.have.property('date');
|
||||||
|
stat.should.have.property('uid');
|
||||||
|
stat.should.not.have.property('remoteAddress');
|
||||||
|
stat.should.not.have.property('userAgent');
|
||||||
|
|
||||||
|
return util.logout(agent);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('must return an empty set when there are no stats', async () => {
|
||||||
|
await util.createSession(agent, ['stats.get'], 'user');
|
||||||
|
|
||||||
|
const res = await util.getStatsViews({}, agent);
|
||||||
|
res.should.have.status(200);
|
||||||
|
|
||||||
|
const stats = res.body;
|
||||||
|
stats.should.be.a('Array');
|
||||||
|
stats.should.have.length(0, 'No views should be returned');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('must constrain results by date', async () => {
|
||||||
|
await Promise.all([
|
||||||
|
util.insertView({
|
||||||
|
uid: 'abcdef',
|
||||||
|
uploader: 'user',
|
||||||
|
remoteAddress: '::1',
|
||||||
|
userAgent: 'fiyerfocks',
|
||||||
|
date: new Date(currentDate - 8 * oneDay),
|
||||||
|
}), util.insertView({
|
||||||
|
uid: 'uvwxyz',
|
||||||
|
uploader: 'user',
|
||||||
|
remoteAddress: '::1',
|
||||||
|
userAgent: 'fiyerfocks',
|
||||||
|
date: new Date(currentDate - 6 * oneDay),
|
||||||
|
}), util.insertView({
|
||||||
|
uid: 'lmnopq',
|
||||||
|
uploader: 'user',
|
||||||
|
remoteAddress: '::1',
|
||||||
|
userAgent: 'fiyerfocks',
|
||||||
|
date: new Date(currentDate - 4 * oneDay),
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
|
||||||
|
await util.createSession(agent, ['stats.get'], 'user');
|
||||||
|
|
||||||
|
const res = await util.getStatsViews({
|
||||||
|
before: new Date(currentDate - 5 * oneDay),
|
||||||
|
after: new Date(currentDate - 7 * oneDay)
|
||||||
|
}, agent);
|
||||||
|
res.should.have.status(200);
|
||||||
|
|
||||||
|
const stats = res.body;
|
||||||
|
stats.should.be.a('Array');
|
||||||
|
stats.should.have.length(1, 'There should be exactly one view returned');
|
||||||
|
|
||||||
|
return util.logout(agent);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('must limit results', async () => {
|
||||||
|
await Promise.all([
|
||||||
|
util.insertView({
|
||||||
|
uid: 'abcdef',
|
||||||
|
uploader: 'user',
|
||||||
|
remoteAddress: '::1',
|
||||||
|
userAgent: 'fiyerfocks',
|
||||||
|
date: new Date(currentDate - 3 * oneDay),
|
||||||
|
}), util.insertView({
|
||||||
|
uid: 'uvwxyz',
|
||||||
|
uploader: 'user',
|
||||||
|
remoteAddress: '::1',
|
||||||
|
userAgent: 'fiyerfocks',
|
||||||
|
date: new Date(currentDate - 3 * oneDay),
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
|
||||||
|
await util.createSession(agent, ['stats.get'], 'user');
|
||||||
|
|
||||||
|
const res = await util.getStatsViews({limit: 1}, agent);
|
||||||
|
res.should.have.status(200);
|
||||||
|
|
||||||
|
const stats = res.body;
|
||||||
|
stats.should.be.a('Array');
|
||||||
|
stats.should.have.length(1, 'There should be exactly one view returned');
|
||||||
|
|
||||||
|
return util.logout(agent);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('/GET week', () => {
|
describe('/GET week', () => {
|
||||||
describe('0 Valid Request', () => {
|
describe('0 Valid Request', () => {
|
||||||
it('must return valid stats for the past week', async () => {
|
it('must return valid stats for the past week', async () => {
|
||||||
|
@ -212,6 +212,14 @@ exports.unban = (username, agent) =>
|
|||||||
|
|
||||||
//---------------- Stats ----------------//
|
//---------------- Stats ----------------//
|
||||||
|
|
||||||
|
exports.getStatsUploads = (query, agent) =>
|
||||||
|
agent.get('/api/stats/uploads')
|
||||||
|
.send(query);
|
||||||
|
|
||||||
|
exports.getStatsViews = (query, agent) =>
|
||||||
|
agent.get('/api/stats/views')
|
||||||
|
.send(query);
|
||||||
|
|
||||||
exports.getStatsWeek = agent =>
|
exports.getStatsWeek = agent =>
|
||||||
agent.get('/api/stats/week');
|
agent.get('/api/stats/week');
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user