Skip to content

Commit

Permalink
Merge pull request #6563 from TheHollidayInn/api-v3-get-group-tests
Browse files Browse the repository at this point in the history
Added initial get group tests
  • Loading branch information
paglias committed Jan 24, 2016
2 parents f8274ad + 2063070 commit c5d731a
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 3 deletions.
8 changes: 5 additions & 3 deletions test/api/v3/integration/chat/POST-chat.flag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
import { find } from 'lodash';

describe('POST /chat/:chatId/flag', () => {
let user, group;
let user, admin, group;
const TEST_MESSAGE = 'Test Message';

before(async () => {
user = await generateUser({balance: 1});
admin = await generateUser({balance: 1, 'contributor.admin': true});

group = await user.post('/groups', {
name: 'Test Guild',
type: 'guild',
Expand Down Expand Up @@ -51,7 +53,7 @@ describe('POST /chat/:chatId/flag', () => {
.then((result) => {
expect(result.flags[user._id]).to.equal(true);
expect(result.flagCount).to.equal(1);
return user.get(`/groups/${group._id}`);
return admin.get(`/groups/${group._id}`);
})
.then((updatedGroup) => {
let messageToCheck = find(updatedGroup.chat, {id: message.id});
Expand All @@ -74,7 +76,7 @@ describe('POST /chat/:chatId/flag', () => {
.then((result) => {
expect(result.flags[secondUser._id]).to.equal(true);
expect(result.flagCount).to.equal(5);
return user.get(`/groups/${group._id}`);
return admin.get(`/groups/${group._id}`);
})
.then((updatedGroup) => {
let messageToCheck = find(updatedGroup.chat, {id: message.id});
Expand Down
302 changes: 302 additions & 0 deletions test/api/v3/integration/groups/GET-groups_id.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
import {
generateUser,
createAndPopulateGroup,
translate as t,
} from '../../../../helpers/api-v3-integration.helper';

import {
each,
} from 'lodash';

describe('GET /groups/:id', () => {
let typesOfGroups = {};
typesOfGroups['public guild'] = { type: 'guild', privacy: 'public' };
typesOfGroups['private guild'] = { type: 'guild', privacy: 'private' };
typesOfGroups.party = { type: 'party', privacy: 'private' };

each(typesOfGroups, (groupDetails, groupType) => {
context(`Member of a ${groupType}`, () => {
let leader, member, createdGroup;

before(async () => {
let groupData = await createAndPopulateGroup({
members: 30,
groupDetails,
});

leader = groupData.groupLeader;
member = groupData.members[0];
createdGroup = groupData.group;
});

it('returns the group object', async () => {
let group = await member.get(`/groups/${createdGroup._id}`);

expect(group._id).to.eql(createdGroup._id);
expect(group.name).to.eql(createdGroup.name);
expect(group.type).to.eql(createdGroup.type);
expect(group.privacy).to.eql(createdGroup.privacy);
});

it('transforms leader id to leader object', async () => {
let group = await member.get(`/groups/${createdGroup._id}`);

expect(group.leader._id).to.eql(leader._id);
expect(group.leader.profile.name).to.eql(leader.profile.name);
expect(group.leader.items).to.exist;
expect(group.leader.stats).to.exist;
expect(group.leader.achievements).to.exist;
expect(group.leader.contributor).to.exist;
});
});
});

context('Non-member of a public guild', () => {
let nonMember, createdGroup;

before(async () => {
let groupData = await createAndPopulateGroup({
members: 1,
groupDetails: {
name: 'test guild',
type: 'guild',
privacy: 'public',
},
});

createdGroup = groupData.group;
nonMember = await generateUser();
});

it('returns the group object for a non-member', async () => {
let group = await nonMember.get(`/groups/${createdGroup._id}`);

expect(group._id).to.eql(createdGroup._id);
expect(group.name).to.eql(createdGroup.name);
expect(group.type).to.eql(createdGroup.type);
expect(group.privacy).to.eql(createdGroup.privacy);
});
});

context('Non-member of a private guild', () => {
let nonMember, createdGroup;

before(async () => {
let groupData = await createAndPopulateGroup({
members: 1,
groupDetails: {
name: 'test guild',
type: 'guild',
privacy: 'private',
},
});

createdGroup = groupData.group;
nonMember = await generateUser();
});

it('does not return the group object for a non-member', async () => {
await expect(nonMember.get(`/groups/${createdGroup._id}`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('groupNotFound'),
});
});
});

context('Non-member of a party', () => {
let nonMember, createdGroup;

before(async () => {
let groupData = await createAndPopulateGroup({
members: 1,
groupDetails: {
name: 'test party',
type: 'party',
privacy: 'private',
},
});

createdGroup = groupData.group;
nonMember = await generateUser();
});

it('does not return the group object for a non-member', async () => {
await expect(nonMember.get(`/groups/${createdGroup._id}`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('groupNotFound'),
});
});
});

context('Member of a party', () => {
let member, createdGroup;

before(async () => {
let groupData = await createAndPopulateGroup({
members: 1,
groupDetails: {
name: 'test party',
type: 'party',
privacy: 'private',
},
});

createdGroup = groupData.group;
member = groupData.members[0];
});

it('returns the user\'s party if an id of "party" is passed in', async () => {
let group = await member.get('/groups/party');

expect(group._id).to.eql(createdGroup._id);
expect(group.name).to.eql(createdGroup.name);
expect(group.type).to.eql(createdGroup.type);
expect(group.privacy).to.eql(createdGroup.privacy);
});
});

context('Non-existent group', () => {
let user;

beforeEach(async () => {
user = await generateUser();
});

it('returns error if group does not exist', async () => {
await expect(user.get('/groups/group-that-does-not-exist'))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('groupNotFound'),
});
});
});

context('Flagged messages', () => {
let group;

let chat1 = {
id: 'chat1',
text: 'chat 1',
flags: {},
};

let chat2 = {
id: 'chat2',
text: 'chat 2',
flags: {},
flagCount: 0,
};

let chat3 = {
id: 'chat3',
text: 'chat 3',
flags: {
'user-id': true,
},
flagCount: 1,
};

let chat4 = {
id: 'chat4',
text: 'chat 4',
flags: {
'user-id': true,
'other-user-id': true,
},
flagCount: 2,
};

let chat5 = {
id: 'chat5',
text: 'chat 5',
flags: {
'user-id': true,
'other-user-id': true,
'yet-another-user-id': true,
},
flagCount: 3,
};

beforeEach(async () => {
let groupData = await createAndPopulateGroup({
groupDetails: {
name: 'test guild',
type: 'guild',
privacy: 'public',
chat: [
chat1,
chat2,
chat3,
chat4,
chat5,
],
},
});

group = groupData.group;

await group.addChat([chat1, chat2, chat3, chat4, chat5]);
});

context('non-admin', () => {
let nonAdmin;

beforeEach(async () => {
nonAdmin = await generateUser();
});

it('does not include messages with a flag count of 2 or greater', async () => {
let fetchedGroup = await nonAdmin.get(`/groups/${group._id}`);

expect(fetchedGroup.chat).to.have.lengthOf(3);
expect(fetchedGroup.chat[0].id).to.eql(chat1.id);
expect(fetchedGroup.chat[1].id).to.eql(chat2.id);
expect(fetchedGroup.chat[2].id).to.eql(chat3.id);
});

it('does not include user ids in flags object', async () => {
let fetchedGroup = await nonAdmin.get(`/groups/${group._id}`);
let chatWithOneFlag = fetchedGroup.chat[2];

expect(chatWithOneFlag.id).to.eql(chat3.id);
expect(chat3.flags).to.eql({ 'user-id': true });
expect(chatWithOneFlag.flags).to.eql({});
});
});

context('admin', () => {
let admin;

beforeEach(async () => {
admin = await generateUser({
'contributor.admin': true,
});
});

it('includes all messages', async () => {
let fetchedGroup = await admin.get(`/groups/${group._id}`);

expect(fetchedGroup.chat).to.have.lengthOf(5);
expect(fetchedGroup.chat[0].id).to.eql(chat1.id);
expect(fetchedGroup.chat[1].id).to.eql(chat2.id);
expect(fetchedGroup.chat[2].id).to.eql(chat3.id);
expect(fetchedGroup.chat[3].id).to.eql(chat4.id);
expect(fetchedGroup.chat[4].id).to.eql(chat5.id);
});

it('includes user ids in flags object', async () => {
let fetchedGroup = await admin.get(`/groups/${group._id}`);
let chatWithOneFlag = fetchedGroup.chat[2];

expect(chatWithOneFlag.id).to.eql(chat3.id);
expect(chat3.flags).to.eql({ 'user-id': true });
expect(chatWithOneFlag.flags).to.eql(chat3.flags);
});
});
});
});
23 changes: 23 additions & 0 deletions test/helpers/api-integration/api-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ export class ApiGroup extends ApiObject {

this._docType = 'groups';
}

async addChat (chat) {
let group = this;

if (!chat) {
chat = {
id: 'Test_ID',
text: 'Test message',
flagCount: 0,
timestamp: Date(),
likes: {},
flags: {},
uuid: group.leader,
contributor: {},
backer: {},
user: group.leader,
};
}

let update = { chat };

return await this.update(update);
}
}

export class ApiChallenge extends ApiObject {
Expand Down
8 changes: 8 additions & 0 deletions website/src/controllers/api-v3/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ api.getGroup = {
let group = await Group.getGroup({user, groupId: req.params.groupId, populateLeader: true});
if (!group) throw new NotFound(res.t('groupNotFound'));

if (!user.contributor.admin) {
group = group.toJSON();
_.remove(group.chat, function removeChat (chat) {
chat.flags = {};
return chat.flagCount >= 2;
});
}

res.respond(200, group);
},
};
Expand Down

0 comments on commit c5d731a

Please sign in to comment.