Filter challenge by owned (#12527)

* when filtering by owned challenges, challenges that a user owns but has not joined will be included

* add tests for filtering challenges by owned

* fix lint
This commit is contained in:
Kirsty
2020-09-07 14:30:01 +01:00
committed by GitHub
parent e5ccb634e0
commit d37c156fa0
2 changed files with 169 additions and 199 deletions

View File

@@ -6,8 +6,8 @@ import {
describe('GET challenges/user', () => { describe('GET challenges/user', () => {
context('no official challenges', () => { context('no official challenges', () => {
let user; let member; let nonMember; let challenge; let challenge2; let let user; let member; let nonMember; let challenge; let challenge2;
publicGuild; let publicGuild; let userData; let groupData;
before(async () => { before(async () => {
const { group, groupLeader, members } = await createAndPopulateGroup({ const { group, groupLeader, members } = await createAndPopulateGroup({
@@ -19,225 +19,197 @@ describe('GET challenges/user', () => {
members: 1, members: 1,
}); });
user = groupLeader;
publicGuild = group; publicGuild = group;
groupData = {
_id: publicGuild._id,
categories: [],
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
summary: publicGuild.name,
leader: publicGuild.leader._id,
};
user = groupLeader;
userData = {
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: { name: user.profile.name },
auth: {
local: {
username: user.auth.local.username,
},
},
flags: {
verifiedUsername: true,
},
};
member = members[0]; // eslint-disable-line prefer-destructuring member = members[0]; // eslint-disable-line prefer-destructuring
nonMember = await generateUser(); nonMember = await generateUser();
challenge = await generateChallenge(user, group); challenge = await generateChallenge(user, group);
await user.post(`/challenges/${challenge._id}/join`);
challenge2 = await generateChallenge(user, group); challenge2 = await generateChallenge(user, group);
await user.post(`/challenges/${challenge2._id}/join`);
});
it('should return challenges user has joined', async () => {
await nonMember.post(`/challenges/${challenge._id}/join`); await nonMember.post(`/challenges/${challenge._id}/join`);
});
context('all challenges', () => {
it('should return challenges user has joined', async () => {
const challenges = await nonMember.get('/challenges/user');
const challenges = await nonMember.get('/challenges/user'); const foundChallenge = _.find(challenges, { _id: challenge._id });
expect(foundChallenge).to.exist;
expect(foundChallenge.leader).to.eql(userData);
expect(foundChallenge.group).to.eql(groupData);
});
const foundChallenge = _.find(challenges, { _id: challenge._id }); it('should not return challenges a non-member has not joined', async () => {
expect(foundChallenge).to.exist; const challenges = await nonMember.get('/challenges/user');
expect(foundChallenge.leader).to.eql({
_id: publicGuild.leader._id, const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
id: publicGuild.leader._id, expect(foundChallenge2).to.not.exist;
profile: { name: user.profile.name }, });
auth: {
local: { it('should return challenges user has created', async () => {
username: user.auth.local.username, const challenges = await user.get('/challenges/user');
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
expect(foundChallenge1).to.exist;
expect(foundChallenge1.leader).to.eql(userData);
expect(foundChallenge1.group).to.eql(groupData);
const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql(userData);
expect(foundChallenge2.group).to.eql(groupData);
});
it('should return challenges in user\'s group', async () => {
const challenges = await member.get('/challenges/user');
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
expect(foundChallenge1).to.exist;
expect(foundChallenge1.leader).to.eql(userData);
expect(foundChallenge1.group).to.eql(groupData);
const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql(userData);
expect(foundChallenge2.group).to.eql(groupData);
});
it('should return newest challenges first', async () => {
let challenges = await user.get('/challenges/user');
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id });
expect(foundChallengeIndex).to.eql(0);
const newChallenge = await generateChallenge(user, publicGuild);
await user.post(`/challenges/${newChallenge._id}/join`);
challenges = await user.get('/challenges/user');
foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
expect(foundChallengeIndex).to.eql(0);
});
it('should not return challenges user doesn\'t have access to', async () => {
const { group, groupLeader } = await createAndPopulateGroup({
groupDetails: {
name: 'TestPrivateGuild',
summary: 'summary for TestPrivateGuild',
type: 'guild',
privacy: 'private',
}, },
}, });
flags: {
verifiedUsername: true,
},
});
expect(foundChallenge.group).to.eql({
_id: publicGuild._id,
categories: [],
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
summary: publicGuild.name,
leader: publicGuild.leader._id,
});
});
it('should return challenges user has created', async () => { const privateChallenge = await generateChallenge(groupLeader, group);
const challenges = await user.get('/challenges/user'); await groupLeader.post(`/challenges/${privateChallenge._id}/join`);
const foundChallenge1 = _.find(challenges, { _id: challenge._id }); const challenges = await nonMember.get('/challenges/user');
expect(foundChallenge1).to.exist;
expect(foundChallenge1.leader).to.eql({ const foundChallenge = _.find(challenges, { _id: privateChallenge._id });
_id: publicGuild.leader._id, expect(foundChallenge).to.not.exist;
id: publicGuild.leader._id, });
profile: { name: user.profile.name },
auth: { it('should not return challenges user doesn\'t have access to, even with query parameters', async () => {
local: { const { group, groupLeader } = await createAndPopulateGroup({
username: user.auth.local.username, groupDetails: {
name: 'TestPrivateGuild',
summary: 'summary for TestPrivateGuild',
type: 'guild',
privacy: 'private',
}, },
}, });
flags: {
verifiedUsername: true, const privateChallenge = await generateChallenge(groupLeader, group, {
}, categories: [{
}); name: 'academics',
expect(foundChallenge1.group).to.eql({ slug: 'academics',
_id: publicGuild._id, }],
categories: [], });
id: publicGuild._id, await groupLeader.post(`/challenges/${privateChallenge._id}/join`);
type: publicGuild.type,
privacy: publicGuild.privacy, const challenges = await nonMember.get('/challenges/user?categories=academics&owned=not_owned');
name: publicGuild.name,
summary: publicGuild.name, const foundChallenge = _.find(challenges, { _id: privateChallenge._id });
leader: publicGuild.leader._id, expect(foundChallenge).to.not.exist;
});
const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: { name: user.profile.name },
auth: {
local: {
username: user.auth.local.username,
},
},
flags: {
verifiedUsername: true,
},
});
expect(foundChallenge2.group).to.eql({
_id: publicGuild._id,
categories: [],
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
summary: publicGuild.name,
leader: publicGuild.leader._id,
}); });
}); });
it('should return challenges in user\'s group', async () => { context('my challenges', () => {
const challenges = await member.get('/challenges/user'); it('should return challenges user has joined', async () => {
const challenges = await nonMember.get(`/challenges/user?member=${true}`);
const foundChallenge1 = _.find(challenges, { _id: challenge._id }); const foundChallenge = _.find(challenges, { _id: challenge._id });
expect(foundChallenge1).to.exist; expect(foundChallenge).to.exist;
expect(foundChallenge1.leader).to.eql({ expect(foundChallenge.leader).to.eql(userData);
_id: publicGuild.leader._id, expect(foundChallenge.group).to.eql(groupData);
id: publicGuild.leader._id,
profile: { name: user.profile.name },
auth: {
local: {
username: user.auth.local.username,
},
},
flags: {
verifiedUsername: true,
},
});
expect(foundChallenge1.group).to.eql({
_id: publicGuild._id,
categories: [],
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
summary: publicGuild.name,
leader: publicGuild.leader._id,
});
const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: { name: user.profile.name },
auth: {
local: {
username: user.auth.local.username,
},
},
flags: {
verifiedUsername: true,
},
});
expect(foundChallenge2.group).to.eql({
_id: publicGuild._id,
categories: [],
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
summary: publicGuild.name,
leader: publicGuild.leader._id,
});
});
it('should not return challenges in user groups if we send member true param', async () => {
const challenges = await member.get(`/challenges/user?member=${true}`);
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
expect(foundChallenge1).to.not.exist;
const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.not.exist;
});
it('should return newest challenges first', async () => {
let challenges = await user.get('/challenges/user');
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id });
expect(foundChallengeIndex).to.eql(0);
const newChallenge = await generateChallenge(user, publicGuild);
await user.post(`/challenges/${newChallenge._id}/join`);
challenges = await user.get('/challenges/user');
foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
expect(foundChallengeIndex).to.eql(0);
});
it('should not return challenges user doesn\'t have access to', async () => {
const { group, groupLeader } = await createAndPopulateGroup({
groupDetails: {
name: 'TestPrivateGuild',
summary: 'summary for TestPrivateGuild',
type: 'guild',
privacy: 'private',
},
}); });
const privateChallenge = await generateChallenge(groupLeader, group); it('should return challenges user has created', async () => {
await groupLeader.post(`/challenges/${privateChallenge._id}/join`); const challenges = await user.get(`/challenges/user?member=${true}`);
const challenges = await nonMember.get('/challenges/user'); const foundChallenge1 = _.find(challenges, { _id: challenge._id });
expect(foundChallenge1).to.exist;
const foundChallenge = _.find(challenges, { _id: privateChallenge._id }); expect(foundChallenge1.leader).to.eql(userData);
expect(foundChallenge).to.not.exist; expect(foundChallenge1.group).to.eql(groupData);
}); const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.exist;
it('should not return challenges user doesn\'t have access to, even with query parameters', async () => { expect(foundChallenge2.leader).to.eql(userData);
const { group, groupLeader } = await createAndPopulateGroup({ expect(foundChallenge2.group).to.eql(groupData);
groupDetails: {
name: 'TestPrivateGuild',
summary: 'summary for TestPrivateGuild',
type: 'guild',
privacy: 'private',
},
}); });
const privateChallenge = await generateChallenge(groupLeader, group, { it('should return challenges user has created if filter by owned', async () => {
categories: [{ const challenges = await user.get(`/challenges/user?member=${true}&owned=owned`);
name: 'academics',
slug: 'academics', const foundChallenge1 = _.find(challenges, { _id: challenge._id });
}], expect(foundChallenge1).to.exist;
expect(foundChallenge1.leader).to.eql(userData);
expect(foundChallenge1.group).to.eql(groupData);
const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql(userData);
expect(foundChallenge2.group).to.eql(groupData);
}); });
await groupLeader.post(`/challenges/${privateChallenge._id}/join`);
const challenges = await nonMember.get('/challenges/user?categories=academics&owned=not_owned'); 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 foundChallenge = _.find(challenges, { _id: privateChallenge._id }); const foundChallenge1 = _.find(challenges, { _id: challenge._id });
expect(foundChallenge).to.not.exist; expect(foundChallenge1).to.not.exist;
const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.not.exist;
});
it('should not return challenges in user groups', async () => {
const challenges = await member.get(`/challenges/user?member=${true}`);
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
expect(foundChallenge1).to.not.exist;
const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.not.exist;
});
}); });
}); });

View File

@@ -371,10 +371,7 @@ api.getUserChallenges = {
{ _id: { $in: user.challenges } }, // Challenges where the user is participating { _id: { $in: user.challenges } }, // Challenges where the user is participating
]; ];
const { owned } = req.query; orOptions.push({ leader: user._id });
if (!owned) {
orOptions.push({ leader: user._id });
}
if (!req.query.member) { if (!req.query.member) {
orOptions.push({ orOptions.push({
@@ -386,6 +383,7 @@ api.getUserChallenges = {
$and: [{ $or: orOptions }], $and: [{ $or: orOptions }],
}; };
const { owned } = req.query;
if (owned) { if (owned) {
if (owned === 'not_owned') { if (owned === 'not_owned') {
query.$and.push({ leader: { $ne: user._id } }); query.$and.push({ leader: { $ne: user._id } });