fix(tests): update challenges

This commit is contained in:
SabreCat
2023-07-26 16:59:57 -05:00
parent d394858022
commit 7607c67070
7 changed files with 93 additions and 125 deletions

View File

@@ -72,18 +72,17 @@ describe('GET /challenges/:challengeId/members', () => {
}); });
it('populates only some fields', async () => { it('populates only some fields', async () => {
const anotherUser = await generateUser({ balance: 3 }); const group = await generateGroup(user, { type: 'party', privacy: 'private', name: generateUUID() });
const group = await generateGroup(anotherUser, { type: 'party', privacy: 'private', name: generateUUID() }); const challenge = await generateChallenge(user, group);
const challenge = await generateChallenge(anotherUser, group); await user.post(`/challenges/${challenge._id}/join`);
await anotherUser.post(`/challenges/${challenge._id}/join`);
const res = await user.get(`/challenges/${challenge._id}/members`); const res = await user.get(`/challenges/${challenge._id}/members`);
expect(res[0]).to.eql({ expect(res[0]).to.eql({
_id: anotherUser._id, _id: user._id,
id: anotherUser._id, id: user._id,
profile: { name: anotherUser.profile.name }, profile: { name: user.profile.name },
auth: { auth: {
local: { local: {
username: anotherUser.auth.local.username, username: user.auth.local.username,
}, },
}, },
flags: { flags: {

View File

@@ -72,20 +72,6 @@ describe('GET /challenges/:challengeId/members/:memberId', () => {
}); });
}); });
it('works with challenges belonging to a public guild', async () => {
const groupLeader = await generateUser({ balance: 4 });
const group = await generateGroup(groupLeader, { type: 'guild', privacy: 'public', name: generateUUID() });
const challenge = await generateChallenge(groupLeader, group);
await groupLeader.post(`/challenges/${challenge._id}/join`);
const taskText = 'Test Text';
await groupLeader.post(`/tasks/challenge/${challenge._id}`, [{ type: 'habit', text: taskText }]);
const memberProgress = await user.get(`/challenges/${challenge._id}/members/${groupLeader._id}`);
expect(memberProgress).to.have.all.keys(['_id', 'auth', 'flags', 'id', 'profile', 'tasks']);
expect(memberProgress.profile).to.have.all.keys(['name']);
expect(memberProgress.tasks.length).to.equal(1);
});
it('returns the member tasks for the challenges', async () => { it('returns the member tasks for the challenges', async () => {
const group = await generateGroup(user, { type: 'party', name: generateUUID() }); const group = await generateGroup(user, { type: 'party', name: generateUUID() });
const challenge = await generateChallenge(user, group); const challenge = await generateChallenge(user, group);

View File

@@ -271,6 +271,7 @@ describe('GET challenges/groups/:groupId', () => {
before(async () => { before(async () => {
await user.update({ await user.update({
'permissions.challengeAdmin': true, 'permissions.challengeAdmin': true,
'balance': 3,
}); });
officialChallenge = await generateChallenge(user, tavern, { officialChallenge = await generateChallenge(user, tavern, {

View File

@@ -3,38 +3,40 @@ import {
generateChallenge, generateChallenge,
createAndPopulateGroup, createAndPopulateGroup,
} from '../../../../helpers/api-integration/v3'; } from '../../../../helpers/api-integration/v3';
import { TAVERN_ID } from '../../../../../website/common/script/constants';
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 user; let member; let nonMember; let challenge; let challenge2; let publicChallenge;
let publicGuild; let userData; let groupData; let groupPlan; let userData; let groupData; let tavern; let tavernData;
before(async () => { before(async () => {
const { group, groupLeader, members } = await createAndPopulateGroup({ const { group, groupLeader, members } = await createAndPopulateGroup({
groupDetails: { groupDetails: {
name: 'TestGuild', name: 'TestGuild',
type: 'guild', type: 'guild',
privacy: 'public', privacy: 'private',
}, },
members: 1, members: 1,
upgradeToGroupPlan: true,
}); });
publicGuild = group; groupPlan = group;
groupData = { groupData = {
_id: publicGuild._id, _id: groupPlan._id,
categories: [], categories: [],
id: publicGuild._id, id: groupPlan._id,
type: publicGuild.type, type: groupPlan.type,
privacy: publicGuild.privacy, privacy: groupPlan.privacy,
name: publicGuild.name, name: groupPlan.name,
summary: publicGuild.name, summary: groupPlan.name,
leader: publicGuild.leader._id, leader: groupPlan.leader._id,
}; };
user = groupLeader; user = groupLeader;
userData = { userData = {
_id: publicGuild.leader._id, _id: groupPlan.leader._id,
id: publicGuild.leader._id, id: groupPlan.leader._id,
profile: { name: user.profile.name }, profile: { name: user.profile.name },
auth: { auth: {
local: { local: {
@@ -46,17 +48,31 @@ describe('GET challenges/user', () => {
}, },
}; };
tavern = await user.get(`/groups/${TAVERN_ID}`);
tavernData = {
_id: TAVERN_ID,
categories: [],
id: TAVERN_ID,
type: tavern.type,
privacy: tavern.privacy,
name: tavern.name,
summary: tavern.name,
leader: tavern.leader._id,
};
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);
challenge2 = await generateChallenge(user, group); challenge2 = await generateChallenge(user, group);
await user.update({ balance: 0.25 });
publicChallenge = await generateChallenge(user, tavern, { prize: 1 });
await nonMember.post(`/challenges/${challenge._id}/join`); await member.post(`/challenges/${challenge._id}/join`);
}); });
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?page=0'); const challenges = await member.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;
@@ -64,11 +80,13 @@ describe('GET challenges/user', () => {
expect(foundChallenge.group).to.eql(groupData); expect(foundChallenge.group).to.eql(groupData);
}); });
it('should not return challenges a non-member has not joined', async () => { it('should return public challenges', async () => {
const challenges = await nonMember.get('/challenges/user?page=0'); const challenges = await nonMember.get('/challenges/user?page=0');
const foundChallenge2 = _.find(challenges, { _id: challenge2._id }); const foundPublicChallenge = _.find(challenges, { _id: publicChallenge._id });
expect(foundChallenge2).to.not.exist; expect(foundPublicChallenge).to.exist;
expect(foundPublicChallenge.leader).to.eql(userData);
expect(foundPublicChallenge.group).to.eql(tavernData);
}); });
it('should return challenges user has created', async () => { it('should return challenges user has created', async () => {
@@ -100,10 +118,10 @@ 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?page=0'); let challenges = await user.get('/challenges/user?page=0');
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id }); let foundChallengeIndex = _.findIndex(challenges, { _id: publicChallenge._id });
expect(foundChallengeIndex).to.eql(0); expect(foundChallengeIndex).to.eql(0);
const newChallenge = await generateChallenge(user, publicGuild); const newChallenge = await generateChallenge(user, groupPlan);
await user.post(`/challenges/${newChallenge._id}/join`); await user.post(`/challenges/${newChallenge._id}/join`);
challenges = await user.get('/challenges/user?page=0'); challenges = await user.get('/challenges/user?page=0');
@@ -113,52 +131,23 @@ describe('GET challenges/user', () => {
}); });
it('should not return challenges user doesn\'t have access to', async () => { 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);
await groupLeader.post(`/challenges/${privateChallenge._id}/join`);
const challenges = await nonMember.get('/challenges/user?page=0'); const challenges = await nonMember.get('/challenges/user?page=0');
const foundChallenge = _.find(challenges, { _id: privateChallenge._id }); const foundChallenge = _.find(challenges, { _id: challenge._id });
expect(foundChallenge).to.not.exist; expect(foundChallenge).to.not.exist;
}); });
it('should not return challenges user doesn\'t have access to, even with query parameters', async () => { it('should not return challenges user doesn\'t have access to, even with query parameters', async () => {
const { group, groupLeader } = await createAndPopulateGroup({
groupDetails: {
name: 'TestPrivateGuild',
summary: 'summary for TestPrivateGuild',
type: 'guild',
privacy: 'private',
},
});
const privateChallenge = await generateChallenge(groupLeader, group, {
categories: [{
name: 'academics',
slug: 'academics',
}],
});
await groupLeader.post(`/challenges/${privateChallenge._id}/join`);
const challenges = await nonMember.get('/challenges/user?page=0&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: challenge._id });
expect(foundChallenge).to.not.exist; expect(foundChallenge).to.not.exist;
}); });
}); });
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?page=0&member=${true}`); const challenges = await member.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;
@@ -177,6 +166,10 @@ describe('GET challenges/user', () => {
expect(foundChallenge2).to.exist; expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql(userData); expect(foundChallenge2.leader).to.eql(userData);
expect(foundChallenge2.group).to.eql(groupData); expect(foundChallenge2.group).to.eql(groupData);
const foundPublicChallenge = _.find(challenges, { _id: publicChallenge._id });
expect(foundPublicChallenge).to.exist;
expect(foundPublicChallenge.leader).to.eql(userData);
expect(foundPublicChallenge.group).to.eql(tavernData);
}); });
it('should return challenges user has created if filter by owned', async () => { it('should return challenges user has created if filter by owned', async () => {
@@ -190,6 +183,10 @@ describe('GET challenges/user', () => {
expect(foundChallenge2).to.exist; expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql(userData); expect(foundChallenge2.leader).to.eql(userData);
expect(foundChallenge2.group).to.eql(groupData); expect(foundChallenge2.group).to.eql(groupData);
const foundPublicChallenge = _.find(challenges, { _id: publicChallenge._id });
expect(foundPublicChallenge).to.exist;
expect(foundPublicChallenge.leader).to.eql(userData);
expect(foundPublicChallenge.group).to.eql(tavernData);
}); });
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 () => {
@@ -199,23 +196,29 @@ describe('GET challenges/user', () => {
expect(foundChallenge1).to.not.exist; expect(foundChallenge1).to.not.exist;
const foundChallenge2 = _.find(challenges, { _id: challenge2._id }); const foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.not.exist; expect(foundChallenge2).to.not.exist;
const foundPublicChallenge = _.find(challenges, { _id: publicChallenge._id });
expect(foundPublicChallenge).to.not.exist;
}); });
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?page=0&member=${true}`); const challenges = await member.get(`/challenges/user?page=0&member=${true}`);
const foundChallenge1 = _.find(challenges, { _id: challenge._id });
expect(foundChallenge1).to.not.exist;
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 not return public challenges', async () => {
const challenges = await member.get(`/challenges/user?page=0&member=${true}`);
const foundPublicChallenge = _.find(challenges, { _id: publicChallenge._id });
expect(foundPublicChallenge).to.not.exist;
});
}); });
}); });
context('official challenge is present', () => { context('official challenge is present', () => {
let user; let officialChallenge; let unofficialChallenges; let let user; let officialChallenge; let unofficialChallenges; let
publicGuild; groupPlan;
before(async () => { before(async () => {
const { group, groupLeader } = await createAndPopulateGroup({ const { group, groupLeader } = await createAndPopulateGroup({
@@ -223,12 +226,13 @@ describe('GET challenges/user', () => {
name: 'TestGuild', name: 'TestGuild',
summary: 'summary for TestGuild', summary: 'summary for TestGuild',
type: 'guild', type: 'guild',
privacy: 'public', privacy: 'private',
}, },
upgradeToGroupPlan: true,
}); });
user = groupLeader; user = groupLeader;
publicGuild = group; groupPlan = group;
await user.update({ await user.update({
'permissions.challengeAdmin': true, 'permissions.challengeAdmin': true,
@@ -271,7 +275,7 @@ describe('GET challenges/user', () => {
} }
}); });
const newChallenge = await generateChallenge(user, publicGuild); const newChallenge = await generateChallenge(user, groupPlan);
await user.post(`/challenges/${newChallenge._id}/join`); await user.post(`/challenges/${newChallenge._id}/join`);
challenges = await user.get('/challenges/user?page=0'); challenges = await user.get('/challenges/user?page=0');
@@ -294,9 +298,10 @@ describe('GET challenges/user', () => {
groupDetails: { groupDetails: {
name: 'TestGuild', name: 'TestGuild',
type: 'guild', type: 'guild',
privacy: 'public', privacy: 'private',
}, },
members: 1, members: 1,
upgradeToGroupPlan: true,
}); });
user = groupLeader; user = groupLeader;

View File

@@ -79,6 +79,7 @@ describe('POST /challenges', () => {
}); });
groupLeader = await populatedGroup.groupLeader.sync(); groupLeader = await populatedGroup.groupLeader.sync();
await groupLeader.update({ permissions: {} });
group = populatedGroup.group; group = populatedGroup.group;
groupMember = populatedGroup.members[0]; // eslint-disable-line prefer-destructuring groupMember = populatedGroup.members[0]; // eslint-disable-line prefer-destructuring
}); });

View File

@@ -1,7 +1,6 @@
import { v4 as generateUUID } from 'uuid'; import { v4 as generateUUID } from 'uuid';
import { import {
createAndPopulateGroup, createAndPopulateGroup,
generateUser,
translate as t, translate as t,
} from '../../../../helpers/api-integration/v3'; } from '../../../../helpers/api-integration/v3';
@@ -10,27 +9,30 @@ describe('DELETE /groups/:groupId/chat/:chatId', () => {
admin; admin;
before(async () => { before(async () => {
const { group, groupLeader } = await createAndPopulateGroup({ const { group, groupLeader, members } = await createAndPopulateGroup({
groupDetails: { groupDetails: {
type: 'guild', type: 'guild',
privacy: 'public', privacy: 'private',
}, },
leaderDetails: { leaderDetails: {
'auth.timestamps.created': new Date('2022-01-01'), 'auth.timestamps.created': new Date('2022-01-01'),
balance: 10, balance: 10,
}, },
members: 2,
upgradeToGroupPlan: true,
}); });
groupWithChat = group; groupWithChat = group;
user = groupLeader; user = groupLeader;
message = await user.post(`/groups/${groupWithChat._id}/chat`, { message: 'Some message' }); message = await user.post(`/groups/${groupWithChat._id}/chat`, { message: 'Some message' });
message = message.message; message = message.message;
userThatDidNotCreateChat = await generateUser(); userThatDidNotCreateChat = members[0];
admin = await generateUser({ 'permissions.moderator': true }); admin = members[1];
await admin.update({ permissions: { moderator: true }});
}); });
context('Chat errors', () => { context('Chat errors', () => {
it('returns an error is message does not exist', async () => { it('returns an error if message does not exist', async () => {
const fakeChatId = generateUUID(); const fakeChatId = generateUUID();
await expect(user.del(`/groups/${groupWithChat._id}/chat/${fakeChatId}`)).to.eventually.be.rejected.and.eql({ await expect(user.del(`/groups/${groupWithChat._id}/chat/${fakeChatId}`)).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
@@ -56,7 +58,7 @@ describe('DELETE /groups/:groupId/chat/:chatId', () => {
nextMessage = nextMessage.message; nextMessage = nextMessage.message;
}); });
it('allows creator to delete a their message', async () => { it('allows creator to delete their message', async () => {
await user.del(`/groups/${groupWithChat._id}/chat/${nextMessage.id}`); await user.del(`/groups/${groupWithChat._id}/chat/${nextMessage.id}`);
const returnedMessages = await user.get(`/groups/${groupWithChat._id}/chat/`); const returnedMessages = await user.get(`/groups/${groupWithChat._id}/chat/`);

View File

@@ -1,6 +1,6 @@
import { import {
generateUser, generateUser,
generateGroup, createAndPopulateGroup,
translate as t, translate as t,
} from '../../../../helpers/api-integration/v3'; } from '../../../../helpers/api-integration/v3';
@@ -11,48 +11,22 @@ describe('GET /groups/:groupId/chat', () => {
user = await generateUser(); user = await generateUser();
}); });
context('public Guild', () => {
let group;
before(async () => {
const leader = await generateUser({ balance: 2 });
group = await generateGroup(leader, {
name: 'test group',
type: 'guild',
privacy: 'public',
}, {
chat: [
{ text: 'Hello', flags: {}, id: 1 },
{ text: 'Welcome to the Guild', flags: {}, id: 2 },
],
});
});
it('returns Guild chat', async () => {
const chat = await user.get(`/groups/${group._id}/chat`);
expect(chat[0].id).to.eql(group.chat[0].id);
expect(chat[1].id).to.eql(group.chat[1].id);
});
});
context('private Guild', () => { context('private Guild', () => {
let group; let group;
before(async () => { before(async () => {
const leader = await generateUser({ balance: 2 }); ({ group } = await createAndPopulateGroup({
groupDetails: {
group = await generateGroup(leader, {
name: 'test group', name: 'test group',
type: 'guild', type: 'guild',
privacy: 'private', privacy: 'private',
}, { },
members: 1,
upgradeToGroupPlan: true,
chat: [ chat: [
'Hello', 'Hello',
'Welcome to the Guild', 'Welcome to the Guild',
], ],
}); }));
}); });
it('returns error if user is not member of requested private group', async () => { it('returns error if user is not member of requested private group', async () => {