Fix challenge sorting

Closes #7543
Closes #7507
This commit is contained in:
Shervin Sarain
2016-05-30 19:04:13 +02:00
committed by Blade Barringer
parent a11a999839
commit de21487038
3 changed files with 244 additions and 110 deletions

View File

@@ -64,6 +64,20 @@ describe('GET challenges/group/:groupId', () => {
profile: {name: user.profile.name}, profile: {name: user.profile.name},
}); });
}); });
it('should return newest challenges first', async () => {
let challenges = await user.get(`/challenges/groups/${publicGuild._id}`);
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id });
expect(foundChallengeIndex).to.eql(0);
let newChallenge = await generateChallenge(user, publicGuild);
challenges = await user.get(`/challenges/groups/${publicGuild._id}`);
foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
expect(foundChallengeIndex).to.eql(0);
});
}); });
context('Private Guild', () => { context('Private Guild', () => {
@@ -115,4 +129,56 @@ describe('GET challenges/group/:groupId', () => {
}); });
}); });
}); });
context('official challenge is present', () => {
let publicGuild, user, officialChallenge, challenge, challenge2;
before(async () => {
let { group, groupLeader } = await createAndPopulateGroup({
groupDetails: {
name: 'TestGuild',
type: 'guild',
privacy: 'public',
},
});
user = groupLeader;
publicGuild = group;
await user.update({
'contributor.admin': true,
});
officialChallenge = await generateChallenge(user, group, {
official: true,
});
challenge = await generateChallenge(user, group);
challenge2 = await generateChallenge(user, group);
});
it('should return official challenges first', async () => {
let challenges = await user.get(`/challenges/groups/${publicGuild._id}`);
let foundChallengeIndex = _.findIndex(challenges, { _id: officialChallenge._id });
expect(foundChallengeIndex).to.eql(0);
});
it('should return newest challenges first, after official ones', async () => {
let challenges = await user.get(`/challenges/groups/${publicGuild._id}`);
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge._id });
expect(foundChallengeIndex).to.eql(2);
foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id });
expect(foundChallengeIndex).to.eql(1);
let newChallenge = await generateChallenge(user, publicGuild);
challenges = await user.get(`/challenges/groups/${publicGuild._id}`);
foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
expect(foundChallengeIndex).to.eql(1);
});
});
}); });

View File

@@ -5,128 +5,196 @@ import {
} from '../../../../helpers/api-v3-integration.helper'; } from '../../../../helpers/api-v3-integration.helper';
describe('GET challenges/user', () => { describe('GET challenges/user', () => {
let user, member, nonMember, challenge, challenge2, publicGuild; context('no official challenges', () => {
let user, member, nonMember, challenge, challenge2, publicGuild;
before(async () => { before(async () => {
let { group, groupLeader, members } = await createAndPopulateGroup({ let { group, groupLeader, members } = await createAndPopulateGroup({
groupDetails: { groupDetails: {
name: 'TestGuild', name: 'TestGuild',
type: 'guild', type: 'guild',
privacy: 'public', privacy: 'public',
}, },
members: 1, members: 1,
});
user = groupLeader;
publicGuild = group;
member = members[0];
nonMember = await generateUser();
challenge = await generateChallenge(user, group);
challenge2 = await generateChallenge(user, group);
}); });
user = groupLeader; it('should return challenges user has joined', async () => {
publicGuild = group; await nonMember.post(`/challenges/${challenge._id}/join`);
member = members[0];
nonMember = await generateUser();
challenge = await generateChallenge(user, group); let challenges = await nonMember.get('/challenges/user');
challenge2 = await generateChallenge(user, group);
});
it('should return challenges user has joined', async () => { let foundChallenge = _.find(challenges, { _id: challenge._id });
await nonMember.post(`/challenges/${challenge._id}/join`); expect(foundChallenge).to.exist;
expect(foundChallenge.leader).to.eql({
let challenges = await nonMember.get('/challenges/user'); _id: publicGuild.leader._id,
id: publicGuild.leader._id,
let foundChallenge = _.find(challenges, { _id: challenge._id }); profile: {name: user.profile.name},
expect(foundChallenge).to.exist; });
expect(foundChallenge.leader).to.eql({ expect(foundChallenge.group).to.eql({
_id: publicGuild.leader._id, _id: publicGuild._id,
id: publicGuild.leader._id, id: publicGuild._id,
profile: {name: user.profile.name}, type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
});
}); });
expect(foundChallenge.group).to.eql({
_id: publicGuild._id, it('should return challenges user has created', async () => {
id: publicGuild._id, let challenges = await user.get('/challenges/user');
type: publicGuild.type,
privacy: publicGuild.privacy, let foundChallenge1 = _.find(challenges, { _id: challenge._id });
name: publicGuild.name, expect(foundChallenge1).to.exist;
expect(foundChallenge1.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
expect(foundChallenge1.group).to.eql({
_id: publicGuild._id,
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
});
let 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},
});
expect(foundChallenge2.group).to.eql({
_id: publicGuild._id,
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
});
});
it('should return challenges in user\'s group', async () => {
let challenges = await member.get('/challenges/user');
let foundChallenge1 = _.find(challenges, { _id: challenge._id });
expect(foundChallenge1).to.exist;
expect(foundChallenge1.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
expect(foundChallenge1.group).to.eql({
_id: publicGuild._id,
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
});
let 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},
});
expect(foundChallenge2.group).to.eql({
_id: publicGuild._id,
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
});
});
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);
let newChallenge = await generateChallenge(user, publicGuild);
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 () => {
let { group, groupLeader } = await createAndPopulateGroup({
groupDetails: {
name: 'TestPrivateGuild',
type: 'guild',
privacy: 'private',
},
});
let privateChallenge = await generateChallenge(groupLeader, group);
let challenges = await nonMember.get('/challenges/user');
let foundChallenge = _.find(challenges, { _id: privateChallenge._id });
expect(foundChallenge).to.not.exist;
}); });
}); });
it('should return challenges user has created', async () => { context('official challenge is present', () => {
let challenges = await user.get('/challenges/user'); let user, officialChallenge, challenge, challenge2, publicGuild;
let foundChallenge1 = _.find(challenges, { _id: challenge._id }); before(async () => {
expect(foundChallenge1).to.exist; let { group, groupLeader } = await createAndPopulateGroup({
expect(foundChallenge1.leader).to.eql({ groupDetails: {
_id: publicGuild.leader._id, name: 'TestGuild',
id: publicGuild.leader._id, type: 'guild',
profile: {name: user.profile.name}, privacy: 'public',
}); },
expect(foundChallenge1.group).to.eql({ });
_id: publicGuild._id,
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
});
let 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},
});
expect(foundChallenge2.group).to.eql({
_id: publicGuild._id,
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
});
});
it('should return challenges in user\'s group', async () => { user = groupLeader;
let challenges = await member.get('/challenges/user'); publicGuild = group;
let foundChallenge1 = _.find(challenges, { _id: challenge._id }); await user.update({
expect(foundChallenge1).to.exist; 'contributor.admin': true,
expect(foundChallenge1.leader).to.eql({ });
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
expect(foundChallenge1.group).to.eql({
_id: publicGuild._id,
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
});
let 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},
});
expect(foundChallenge2.group).to.eql({
_id: publicGuild._id,
id: publicGuild._id,
type: publicGuild.type,
privacy: publicGuild.privacy,
name: publicGuild.name,
});
});
it('should not return challenges user doesn\'t have access to', async () => { officialChallenge = await generateChallenge(user, group, {
let { group, groupLeader } = await createAndPopulateGroup({ official: true,
groupDetails: { });
name: 'TestPrivateGuild',
type: 'guild', challenge = await generateChallenge(user, group);
privacy: 'private', challenge2 = await generateChallenge(user, group);
},
}); });
let privateChallenge = await generateChallenge(groupLeader, group); it('should return official challenges first', async () => {
let challenges = await user.get('/challenges/user');
let challenges = await nonMember.get('/challenges/user'); let foundChallengeIndex = _.findIndex(challenges, { _id: officialChallenge._id });
expect(foundChallengeIndex).to.eql(0);
});
let foundChallenge = _.find(challenges, { _id: privateChallenge._id }); it('should return newest challenges first, after official ones', async () => {
expect(foundChallenge).to.not.exist; let challenges = await user.get('/challenges/user');
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge._id });
expect(foundChallengeIndex).to.eql(2);
foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id });
expect(foundChallengeIndex).to.eql(1);
let newChallenge = await generateChallenge(user, publicGuild);
challenges = await user.get('/challenges/user');
foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
expect(foundChallengeIndex).to.eql(1);
});
}); });
}); });

View File

@@ -201,7 +201,7 @@ api.leaveChallenge = {
* @apiName GetUserChallenges * @apiName GetUserChallenges
* @apiGroup Challenge * @apiGroup Challenge
* *
* @apiSuccess {Array} data An array of challenges * @apiSuccess {Array} data An array of challenges sorted with official challenges first, followed by the challenges in order from newest to oldest
*/ */
api.getUserChallenges = { api.getUserChallenges = {
method: 'GET', method: 'GET',
@@ -218,7 +218,7 @@ api.getUserChallenges = {
], ],
_id: {$ne: '95533e05-1ff9-4e46-970b-d77219f199e9'}, // remove the Spread the Word Challenge for now, will revisit when we fix the closing-challenge bug TODO revisit _id: {$ne: '95533e05-1ff9-4e46-970b-d77219f199e9'}, // remove the Spread the Word Challenge for now, will revisit when we fix the closing-challenge bug TODO revisit
}) })
.sort('-official -timestamp') .sort('-official -createdAt')
// see below why we're not using populate // see below why we're not using populate
// .populate('group', basicGroupFields) // .populate('group', basicGroupFields)
// .populate('leader', nameFields) // .populate('leader', nameFields)
@@ -249,7 +249,7 @@ api.getUserChallenges = {
* *
* @apiParam {groupId} groupId The group _id * @apiParam {groupId} groupId The group _id
* *
* @apiSuccess {Array} data An array of challenges * @apiSuccess {Array} data An array of challenges sorted with official challenges first, followed by the challenges in order from newest to oldest
*/ */
api.getGroupChallenges = { api.getGroupChallenges = {
method: 'GET', method: 'GET',
@@ -268,7 +268,7 @@ api.getGroupChallenges = {
if (!group) throw new NotFound(res.t('groupNotFound')); if (!group) throw new NotFound(res.t('groupNotFound'));
let challenges = await Challenge.find({group: groupId}) let challenges = await Challenge.find({group: groupId})
.sort('-official -timestamp') .sort('-official -createdAt')
// .populate('leader', nameFields) // Only populate the leader as the group is implicit // .populate('leader', nameFields) // Only populate the leader as the group is implicit
.exec(); .exec();