Skip to content

Commit

Permalink
update quest leave route
Browse files Browse the repository at this point in the history
  • Loading branch information
paglias committed Feb 11, 2016
1 parent c6c852c commit 354b6bc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,85 +1,124 @@
import {
createAndPopulateGroup,
translate as t,
generateUser,
} from '../../../../helpers/api-v3-integration.helper';
import { v4 as generateUUID } from 'uuid';

describe('POST /groups/:groupId/quests/leave', () => {
let questingGroup, member, leader;
let questingGroup;
let partyMembers;
let user;
let leader;

const PET_QUEST = 'whale';
let userQuestUpdate = {
items: {
quests: {},
},
'party.quest.RSVPNeeded': true,
'party.quest.key': PET_QUEST,
};

before(async () => {

beforeEach(async () => {
let { group, groupLeader, members } = await createAndPopulateGroup({
groupDetails: { type: 'party', privacy: 'private' },
members: 1,
members: 2,
});

leader = groupLeader;
questingGroup = group;
member = members[0];
leader = groupLeader;
partyMembers = members;

userQuestUpdate.items.quests[PET_QUEST] = 1;
await leader.update({
[`items.quests.${PET_QUEST}`]: 1,
});
user = await generateUser();
});

it('returns an error when group is not found', async () => {
await expect(member.post(`/groups/${generateUUID()}/quests/leave`))
context('failure conditions', () => {
it('returns an error when group is not found', async () => {
await expect(partyMembers[0].post(`/groups/${generateUUID()}/quests/leave`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('groupNotFound'),
});
});

it('returns an error for a group in which user is not a member', async () => {
await expect(user.post(`/groups/${questingGroup._id}/quests/leave`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('groupNotFound'),
});
});
});

it('returns an error when quest is not active', async () => {
await expect(member.post(`/groups/${questingGroup._id}/quests/leave`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('noActiveQuestToLeave'),
it('returns an error when group is a guild', async () => {
let { group: guild, groupLeader: guildLeader } = await createAndPopulateGroup({
groupDetails: { type: 'guild', privacy: 'private' },
});
});

it('returns an error when quest leader attempts to leave', async () => {
await questingGroup.update({quest: {key: PET_QUEST, active: true, leader: leader._id}});

await expect(leader.post(`/groups/${questingGroup._id}/quests/leave`))
await expect(guildLeader.post(`/groups/${guild._id}/quests/leave`))
.to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: t('questLeaderCannotLeaveQuest'),
message: t('guildQuestsNotSupported'),
});
});
});

it('returns an error when quest is not active', async () => {
await expect(partyMembers[0].post(`/groups/${questingGroup._id}/quests/leave`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('noActiveQuestToLeave'),
});
});

it('returns an error when quest leader attempts to leave', async () => {
await leader.post(`/groups/${questingGroup._id}/quests/invite/${PET_QUEST}`);
await partyMembers[0].post(`/groups/${questingGroup._id}/quests/accept`);
await partyMembers[1].post(`/groups/${questingGroup._id}/quests/accept`);

it('returns an error when non quest member attempts to leave', async () => {
await expect(member.post(`/groups/${questingGroup._id}/quests/leave`))
await expect(leader.post(`/groups/${questingGroup._id}/quests/leave`))
.to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: t('questLeaderCannotLeaveQuest'),
});
});

it('returns an error when non quest member attempts to leave', async () => {
await leader.post(`/groups/${questingGroup._id}/quests/invite/${PET_QUEST}`);
await partyMembers[0].post(`/groups/${questingGroup._id}/quests/accept`);
await partyMembers[1].post(`/groups/${questingGroup._id}/quests/reject`);

await expect(partyMembers[0].post(`/groups/${questingGroup._id}/quests/leave`))
.to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: t('notPartOfQuest'),
});
});
});

it('leaves a quest', async () => {
await member.update(userQuestUpdate);

let questMembers = {};
questMembers[member._id] = true;
await questingGroup.update({'quest.members': questMembers});
await leader.post(`/groups/${questingGroup._id}/quests/invite/${PET_QUEST}`);
await partyMembers[0].post(`/groups/${questingGroup._id}/quests/accept`);
await partyMembers[1].post(`/groups/${questingGroup._id}/quests/accept`);

let leaveResult = await member.post(`/groups/${questingGroup._id}/quests/leave`);
let userThatLeft = await member.get('/user');
let updatedGroup = await member.get(`/groups/${questingGroup._id}`);
let leaveResult = await partyMembers[0].post(`/groups/${questingGroup._id}/quests/leave`);
await Promise.all([
partyMembers[0].sync(),
questingGroup.sync(),
]);

expect(userThatLeft.party.quest.key).to.be.null;
expect(userThatLeft.party.quest.RSVPNeeded).to.be.false;
expect(updatedGroup.quest.members[member._id]).to.be.false;
expect(updatedGroup.quest).to.deep.equal(leaveResult);
expect(partyMembers[0].party.quest).to.eql({
key: null,
progress: {
up: 0,
down: 0,
collect: {},
},
completed: null,
RSVPNeeded: false,
});
expect(questingGroup.quest).to.deep.equal(leaveResult);
expect(questingGroup.quest.members[partyMembers[0]._id]).to.be.false;
});
});
22 changes: 7 additions & 15 deletions website/src/controllers/api-v3/quests.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
sendTxn as sendTxnEmail,
} from '../../libs/api-v3/email';
import { quests as questScrolls } from '../../../../common/script/content';
import Q from 'q';

function canStartQuestAutomatically (group) {
// If all members are either true (accepted) or false (rejected) return true
Expand Down Expand Up @@ -184,14 +183,14 @@ api.acceptQuest = {
};

/**
* @api {post} /groups/:groupId/quests/leave Leaves a quest
* @api {post} /groups/:groupId/quests/leave Leaves the active quest
* @apiVersion 3.0.0
* @apiName LeaveQuest
* @apiGroup Group
*
* @apiParam {string} groupId The group _id (or 'party')
*
* @apiSuccess {Object} Empty Object
* @apiSuccess {Object} quest Quest Object
*/
api.leaveQuest = {
method: 'POST',
Expand All @@ -207,19 +206,12 @@ api.leaveQuest = {
if (validationErrors) throw validationErrors;

let group = await Group.getGroup({user, groupId, fields: 'type quest'});
if (!group) throw new NotFound(res.t('groupNotFound'));

if (!(group.quest && group.quest.active)) {
throw new NotFound(res.t('noActiveQuestToLeave'));
}

if (group.quest.leader === user._id) {
throw new NotAuthorized(res.t('questLeaderCannotLeaveQuest'));
}

if (!(group.quest.members && group.quest.members[user._id])) {
throw new NotAuthorized(res.t('notPartOfQuest'));
}
if (!group) throw new NotFound(res.t('groupNotFound'));
if (group.type !== 'party') throw new NotAuthorized(res.t('guildQuestsNotSupported'));
if (!group.quest.active) throw new NotFound(res.t('noActiveQuestToLeave'));
if (group.quest.leader === user._id) throw new NotAuthorized(res.t('questLeaderCannotLeaveQuest'));
if (!group.quest.members[user._id]) throw new NotAuthorized(res.t('notPartOfQuest'));

group.quest.members[user._id] = false;
group.markModified('quest.members');
Expand Down

0 comments on commit 354b6bc

Please sign in to comment.