From 26123ac6aef57ab4d19234aebf7cab89c2fb6481 Mon Sep 17 00:00:00 2001 From: Mel Date: Wed, 25 Oct 2017 13:35:23 -0700 Subject: [PATCH] API - Get challenges for a group does not allow `party` or `habitrpg` (#8882) * test get party challenges by party ID * tavern challenge tests; failing tests with ID 'party' or 'habitrpg' * allow finding challenges by groupid 'party' or 'habitrpg' * use single quotes in strings --- .../GET-challenges_group_groupid.test.js | 120 ++++++++++++++++++ .../server/controllers/api-v3/challenges.js | 3 + 2 files changed, 123 insertions(+) diff --git a/test/api/v3/integration/challenges/GET-challenges_group_groupid.test.js b/test/api/v3/integration/challenges/GET-challenges_group_groupid.test.js index 99e3fa0bc1..5c290efc64 100644 --- a/test/api/v3/integration/challenges/GET-challenges_group_groupid.test.js +++ b/test/api/v3/integration/challenges/GET-challenges_group_groupid.test.js @@ -4,6 +4,7 @@ import { createAndPopulateGroup, translate as t, } from '../../../../helpers/api-v3-integration.helper'; +import { TAVERN_ID } from '../../../../../website/common/script/constants'; describe('GET challenges/groups/:groupId', () => { context('Public Guild', () => { @@ -181,4 +182,123 @@ describe('GET challenges/groups/:groupId', () => { expect(foundChallengeIndex).to.eql(1); }); }); + + context('Party', () => { + let party, user, nonMember, challenge, challenge2; + + before(async () => { + let { group, groupLeader } = await createAndPopulateGroup({ + groupDetails: { + name: 'TestParty', + type: 'party', + }, + }); + + party = group; + user = groupLeader; + + nonMember = await generateUser(); + + challenge = await generateChallenge(user, group); + challenge2 = await generateChallenge(user, group); + }); + + it('should prevent non-member from seeing challenges', async () => { + await expect(nonMember.get(`/challenges/groups/${party._id}`)) + .to.eventually.be.rejected.and.eql({ + code: 404, + error: 'NotFound', + message: t('groupNotFound'), + }); + }); + + it('should return group challenges for member with populated leader', async () => { + let challenges = await user.get(`/challenges/groups/${party._id}`); + + let foundChallenge1 = _.find(challenges, { _id: challenge._id }); + expect(foundChallenge1).to.exist; + expect(foundChallenge1.leader).to.eql({ + _id: party.leader._id, + id: party.leader._id, + profile: {name: user.profile.name}, + }); + let foundChallenge2 = _.find(challenges, { _id: challenge2._id }); + expect(foundChallenge2).to.exist; + expect(foundChallenge2.leader).to.eql({ + _id: party.leader._id, + id: party.leader._id, + profile: {name: user.profile.name}, + }); + }); + + it('should return group challenges for member using ID "party"', async () => { + let challenges = await user.get('/challenges/groups/party'); + + let foundChallenge1 = _.find(challenges, { _id: challenge._id }); + expect(foundChallenge1).to.exist; + expect(foundChallenge1.leader).to.eql({ + _id: party.leader._id, + id: party.leader._id, + profile: {name: user.profile.name}, + }); + let foundChallenge2 = _.find(challenges, { _id: challenge2._id }); + expect(foundChallenge2).to.exist; + expect(foundChallenge2.leader).to.eql({ + _id: party.leader._id, + id: party.leader._id, + profile: {name: user.profile.name}, + }); + }); + }); + + context('Tavern', () => { + let tavern, user, challenge, challenge2; + + before(async () => { + user = await generateUser(); + await user.update({balance: 0.5}); + tavern = await user.get(`/groups/${TAVERN_ID}`); + + challenge = await generateChallenge(user, tavern, {prize: 1}); + challenge2 = await generateChallenge(user, tavern, {prize: 1}); + }); + + it('should return tavern challenges with populated leader', async () => { + let challenges = await user.get(`/challenges/groups/${TAVERN_ID}`); + + let foundChallenge1 = _.find(challenges, { _id: challenge._id }); + expect(foundChallenge1).to.exist; + expect(foundChallenge1.leader).to.eql({ + _id: user._id, + id: user._id, + profile: {name: user.profile.name}, + }); + let foundChallenge2 = _.find(challenges, { _id: challenge2._id }); + expect(foundChallenge2).to.exist; + expect(foundChallenge2.leader).to.eql({ + _id: user._id, + id: user._id, + profile: {name: user.profile.name}, + }); + }); + + it('should return tavern challenges using ID "habitrpg', async () => { + let challenges = await user.get('/challenges/groups/habitrpg'); + + let foundChallenge1 = _.find(challenges, { _id: challenge._id }); + expect(foundChallenge1).to.exist; + expect(foundChallenge1.leader).to.eql({ + _id: user._id, + id: user._id, + profile: {name: user.profile.name}, + }); + let foundChallenge2 = _.find(challenges, { _id: challenge2._id }); + expect(foundChallenge2).to.exist; + expect(foundChallenge2.leader).to.eql({ + _id: user._id, + id: user._id, + profile: {name: user.profile.name}, + }); + }); + }); }); diff --git a/website/server/controllers/api-v3/challenges.js b/website/server/controllers/api-v3/challenges.js index 933ed40050..5ad31580f0 100644 --- a/website/server/controllers/api-v3/challenges.js +++ b/website/server/controllers/api-v3/challenges.js @@ -439,6 +439,9 @@ api.getGroupChallenges = { let validationErrors = req.validationErrors(); if (validationErrors) throw validationErrors; + if (groupId === 'party') groupId = user.party._id; + if (groupId === 'habitrpg') groupId = TAVERN_ID; + let group = await Group.getGroup({user, groupId}); if (!group) throw new NotFound(res.t('groupNotFound'));