mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 06:37:23 +01:00
Mandatory pagination for GET /challenges/user (#12792)
* BREAKING: require pagination for GET /challenges/user * fix tests * remove unused test
This commit is contained in:
@@ -56,7 +56,7 @@ describe('GET challenges/user', () => {
|
|||||||
});
|
});
|
||||||
context('all challenges', () => {
|
context('all challenges', () => {
|
||||||
it('should return challenges user has joined', async () => {
|
it('should return challenges user has joined', async () => {
|
||||||
const challenges = await nonMember.get('/challenges/user');
|
const challenges = await nonMember.get('/challenges/user?page=0');
|
||||||
|
|
||||||
const foundChallenge = _.find(challenges, { _id: challenge._id });
|
const foundChallenge = _.find(challenges, { _id: challenge._id });
|
||||||
expect(foundChallenge).to.exist;
|
expect(foundChallenge).to.exist;
|
||||||
@@ -65,14 +65,14 @@ describe('GET challenges/user', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should not return challenges a non-member has not joined', async () => {
|
it('should not return challenges a non-member has not joined', async () => {
|
||||||
const challenges = await nonMember.get('/challenges/user');
|
const challenges = await nonMember.get('/challenges/user?page=0');
|
||||||
|
|
||||||
const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
|
const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
|
||||||
expect(foundChallenge2).to.not.exist;
|
expect(foundChallenge2).to.not.exist;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return challenges user has created', async () => {
|
it('should return challenges user has created', async () => {
|
||||||
const challenges = await user.get('/challenges/user');
|
const challenges = await user.get('/challenges/user?page=0');
|
||||||
|
|
||||||
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
||||||
expect(foundChallenge1).to.exist;
|
expect(foundChallenge1).to.exist;
|
||||||
@@ -85,7 +85,7 @@ describe('GET challenges/user', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return challenges in user\'s group', async () => {
|
it('should return challenges in user\'s group', async () => {
|
||||||
const challenges = await member.get('/challenges/user');
|
const challenges = await member.get('/challenges/user?page=0');
|
||||||
|
|
||||||
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
||||||
expect(foundChallenge1).to.exist;
|
expect(foundChallenge1).to.exist;
|
||||||
@@ -98,7 +98,7 @@ describe('GET challenges/user', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return newest challenges first', async () => {
|
it('should return newest challenges first', async () => {
|
||||||
let challenges = await user.get('/challenges/user');
|
let challenges = await user.get('/challenges/user?page=0');
|
||||||
|
|
||||||
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id });
|
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id });
|
||||||
expect(foundChallengeIndex).to.eql(0);
|
expect(foundChallengeIndex).to.eql(0);
|
||||||
@@ -106,7 +106,7 @@ describe('GET challenges/user', () => {
|
|||||||
const newChallenge = await generateChallenge(user, publicGuild);
|
const newChallenge = await generateChallenge(user, publicGuild);
|
||||||
await user.post(`/challenges/${newChallenge._id}/join`);
|
await user.post(`/challenges/${newChallenge._id}/join`);
|
||||||
|
|
||||||
challenges = await user.get('/challenges/user');
|
challenges = await user.get('/challenges/user?page=0');
|
||||||
|
|
||||||
foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
|
foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
|
||||||
expect(foundChallengeIndex).to.eql(0);
|
expect(foundChallengeIndex).to.eql(0);
|
||||||
@@ -125,7 +125,7 @@ describe('GET challenges/user', () => {
|
|||||||
const privateChallenge = await generateChallenge(groupLeader, group);
|
const privateChallenge = await generateChallenge(groupLeader, group);
|
||||||
await groupLeader.post(`/challenges/${privateChallenge._id}/join`);
|
await groupLeader.post(`/challenges/${privateChallenge._id}/join`);
|
||||||
|
|
||||||
const challenges = await nonMember.get('/challenges/user');
|
const challenges = await nonMember.get('/challenges/user?page=0');
|
||||||
|
|
||||||
const foundChallenge = _.find(challenges, { _id: privateChallenge._id });
|
const foundChallenge = _.find(challenges, { _id: privateChallenge._id });
|
||||||
expect(foundChallenge).to.not.exist;
|
expect(foundChallenge).to.not.exist;
|
||||||
@@ -149,7 +149,7 @@ describe('GET challenges/user', () => {
|
|||||||
});
|
});
|
||||||
await groupLeader.post(`/challenges/${privateChallenge._id}/join`);
|
await groupLeader.post(`/challenges/${privateChallenge._id}/join`);
|
||||||
|
|
||||||
const challenges = await nonMember.get('/challenges/user?categories=academics&owned=not_owned');
|
const challenges = await nonMember.get('/challenges/user?page=0&categories=academics&owned=not_owned');
|
||||||
|
|
||||||
const foundChallenge = _.find(challenges, { _id: privateChallenge._id });
|
const foundChallenge = _.find(challenges, { _id: privateChallenge._id });
|
||||||
expect(foundChallenge).to.not.exist;
|
expect(foundChallenge).to.not.exist;
|
||||||
@@ -158,7 +158,7 @@ describe('GET challenges/user', () => {
|
|||||||
|
|
||||||
context('my challenges', () => {
|
context('my challenges', () => {
|
||||||
it('should return challenges user has joined', async () => {
|
it('should return challenges user has joined', async () => {
|
||||||
const challenges = await nonMember.get(`/challenges/user?member=${true}`);
|
const challenges = await nonMember.get(`/challenges/user?page=0&member=${true}`);
|
||||||
|
|
||||||
const foundChallenge = _.find(challenges, { _id: challenge._id });
|
const foundChallenge = _.find(challenges, { _id: challenge._id });
|
||||||
expect(foundChallenge).to.exist;
|
expect(foundChallenge).to.exist;
|
||||||
@@ -167,7 +167,7 @@ describe('GET challenges/user', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return challenges user has created', async () => {
|
it('should return challenges user has created', async () => {
|
||||||
const challenges = await user.get(`/challenges/user?member=${true}`);
|
const challenges = await user.get(`/challenges/user?page=0&member=${true}`);
|
||||||
|
|
||||||
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
||||||
expect(foundChallenge1).to.exist;
|
expect(foundChallenge1).to.exist;
|
||||||
@@ -180,7 +180,7 @@ describe('GET challenges/user', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return challenges user has created if filter by owned', async () => {
|
it('should return challenges user has created if filter by owned', async () => {
|
||||||
const challenges = await user.get(`/challenges/user?member=${true}&owned=owned`);
|
const challenges = await user.get(`/challenges/user?member=${true}&owned=owned&page=0`);
|
||||||
|
|
||||||
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
||||||
expect(foundChallenge1).to.exist;
|
expect(foundChallenge1).to.exist;
|
||||||
@@ -193,7 +193,7 @@ describe('GET challenges/user', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should not return challenges user has created if filter by not owned', async () => {
|
it('should not return challenges user has created if filter by not owned', async () => {
|
||||||
const challenges = await user.get(`/challenges/user?owned=not_owned&member=${true}`);
|
const challenges = await user.get(`/challenges/user?page=0&owned=not_owned&member=${true}`);
|
||||||
|
|
||||||
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
||||||
expect(foundChallenge1).to.not.exist;
|
expect(foundChallenge1).to.not.exist;
|
||||||
@@ -202,7 +202,7 @@ describe('GET challenges/user', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should not return challenges in user groups', async () => {
|
it('should not return challenges in user groups', async () => {
|
||||||
const challenges = await member.get(`/challenges/user?member=${true}`);
|
const challenges = await member.get(`/challenges/user?page=0&member=${true}`);
|
||||||
|
|
||||||
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
|
||||||
expect(foundChallenge1).to.not.exist;
|
expect(foundChallenge1).to.not.exist;
|
||||||
@@ -253,7 +253,7 @@ describe('GET challenges/user', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return official challenges first', async () => {
|
it('should return official challenges first', async () => {
|
||||||
const challenges = await user.get('/challenges/user');
|
const challenges = await user.get('/challenges/user?page=0');
|
||||||
|
|
||||||
const foundChallengeIndex = _.findIndex(challenges, { _id: officialChallenge._id });
|
const foundChallengeIndex = _.findIndex(challenges, { _id: officialChallenge._id });
|
||||||
expect(foundChallengeIndex).to.eql(0);
|
expect(foundChallengeIndex).to.eql(0);
|
||||||
@@ -274,7 +274,7 @@ describe('GET challenges/user', () => {
|
|||||||
const newChallenge = await generateChallenge(user, publicGuild);
|
const newChallenge = await generateChallenge(user, publicGuild);
|
||||||
await user.post(`/challenges/${newChallenge._id}/join`);
|
await user.post(`/challenges/${newChallenge._id}/join`);
|
||||||
|
|
||||||
challenges = await user.get('/challenges/user');
|
challenges = await user.get('/challenges/user?page=0');
|
||||||
|
|
||||||
const foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
|
const foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
|
||||||
expect(foundChallengeIndex).to.eql(1);
|
expect(foundChallengeIndex).to.eql(1);
|
||||||
@@ -314,18 +314,12 @@ describe('GET challenges/user', () => {
|
|||||||
it('returns public guilds filtered by category', async () => {
|
it('returns public guilds filtered by category', async () => {
|
||||||
const categoryChallenge = await generateChallenge(user, guild, { categories });
|
const categoryChallenge = await generateChallenge(user, guild, { categories });
|
||||||
await user.post(`/challenges/${categoryChallenge._id}/join`);
|
await user.post(`/challenges/${categoryChallenge._id}/join`);
|
||||||
const challenges = await user.get(`/challenges/user?categories=${categories[0].slug}`);
|
const challenges = await user.get(`/challenges/user?page=0&categories=${categories[0].slug}`);
|
||||||
|
|
||||||
expect(challenges[0]._id).to.eql(categoryChallenge._id);
|
expect(challenges[0]._id).to.eql(categoryChallenge._id);
|
||||||
expect(challenges.length).to.eql(1);
|
expect(challenges.length).to.eql(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not page challenges if page parameter is absent', async () => {
|
|
||||||
const challenges = await user.get('/challenges/user');
|
|
||||||
|
|
||||||
expect(challenges.length).to.be.above(11);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('paginates challenges', async () => {
|
it('paginates challenges', async () => {
|
||||||
const challenges = await user.get('/challenges/user?page=0');
|
const challenges = await user.get('/challenges/user?page=0');
|
||||||
const challengesPaged = await user.get('/challenges/user?page=1&owned=owned');
|
const challengesPaged = await user.get('/challenges/user?page=1&owned=owned');
|
||||||
@@ -335,7 +329,7 @@ describe('GET challenges/user', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('filters by owned', async () => {
|
it('filters by owned', async () => {
|
||||||
const challenges = await member.get('/challenges/user?owned=owned');
|
const challenges = await member.get('/challenges/user?page=0&owned=owned');
|
||||||
|
|
||||||
expect(challenges.length).to.eql(0);
|
expect(challenges.length).to.eql(0);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -367,6 +367,11 @@ api.getUserChallenges = {
|
|||||||
url: '/challenges/user',
|
url: '/challenges/user',
|
||||||
middlewares: [authWithHeaders()],
|
middlewares: [authWithHeaders()],
|
||||||
async handler (req, res) {
|
async handler (req, res) {
|
||||||
|
req.checkQuery('page').notEmpty().isInt({ min: 0 }, apiError('queryPageInteger'));
|
||||||
|
|
||||||
|
const validationErrors = req.validationErrors();
|
||||||
|
if (validationErrors) throw validationErrors;
|
||||||
|
|
||||||
const CHALLENGES_PER_PAGE = 10;
|
const CHALLENGES_PER_PAGE = 10;
|
||||||
const { page } = req.query;
|
const { page } = req.query;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user