mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
Updated chat tests with new async/await syntax
This commit is contained in:
@@ -5,12 +5,13 @@ import {
|
|||||||
import { find } from 'lodash';
|
import { find } from 'lodash';
|
||||||
|
|
||||||
describe('POST /chat/:chatId/flag', () => {
|
describe('POST /chat/:chatId/flag', () => {
|
||||||
let user, admin, group;
|
let user, admin, anotherUser, group;
|
||||||
const TEST_MESSAGE = 'Test Message';
|
const TEST_MESSAGE = 'Test Message';
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
user = await generateUser({balance: 1});
|
user = await generateUser({balance: 1});
|
||||||
admin = await generateUser({balance: 1, 'contributor.admin': true});
|
admin = await generateUser({balance: 1, 'contributor.admin': true});
|
||||||
|
anotherUser = await generateUser();
|
||||||
|
|
||||||
group = await user.post('/groups', {
|
group = await user.post('/groups', {
|
||||||
name: 'Test Guild',
|
name: 'Test Guild',
|
||||||
@@ -20,7 +21,7 @@ describe('POST /chat/:chatId/flag', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Returns an error when chat message is not found', async () => {
|
it('Returns an error when chat message is not found', async () => {
|
||||||
return expect(user.post(`/groups/${group._id}/chat/incorrectMessage/flag`))
|
await expect(user.post(`/groups/${group._id}/chat/incorrectMessage/flag`))
|
||||||
.to.eventually.be.rejected.and.eql({
|
.to.eventually.be.rejected.and.eql({
|
||||||
code: 404,
|
code: 404,
|
||||||
error: 'NotFound',
|
error: 'NotFound',
|
||||||
@@ -29,79 +30,55 @@ describe('POST /chat/:chatId/flag', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Returns an error when user tries to flag their own message', async () => {
|
it('Returns an error when user tries to flag their own message', async () => {
|
||||||
return user.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE})
|
let message = await user.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE });
|
||||||
.then((result) => {
|
await expect(user.post(`/groups/${group._id}/chat/${message.message.id}/flag`))
|
||||||
return expect(user.post(`/groups/${group._id}/chat/${result.message.id}/flag`))
|
.to.eventually.be.rejected.and.eql({
|
||||||
.to.eventually.be.rejected.and.eql({
|
code: 404,
|
||||||
code: 404,
|
error: 'NotFound',
|
||||||
error: 'NotFound',
|
message: t('messageGroupChatFlagOwnMessage'),
|
||||||
message: t('messageGroupChatFlagOwnMessage'),
|
});
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Flags a chat', async () => {
|
it('Flags a chat', async () => {
|
||||||
let message;
|
let message = await anotherUser.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE});
|
||||||
|
message = message.message;
|
||||||
|
|
||||||
return generateUser().then((anotherUser) => {
|
let flagResult = await user.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
||||||
return anotherUser.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE});
|
expect(flagResult.flags[user._id]).to.equal(true);
|
||||||
})
|
expect(flagResult.flagCount).to.equal(1);
|
||||||
.then((result) => {
|
|
||||||
message = result.message;
|
let groupWithFlags = await admin.get(`/groups/${group._id}`);
|
||||||
return user.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
|
||||||
})
|
let messageToCheck = find(groupWithFlags.chat, {id: message.id});
|
||||||
.then((result) => {
|
expect(messageToCheck.flags[user._id]).to.equal(true);
|
||||||
expect(result.flags[user._id]).to.equal(true);
|
|
||||||
expect(result.flagCount).to.equal(1);
|
|
||||||
return admin.get(`/groups/${group._id}`);
|
|
||||||
})
|
|
||||||
.then((updatedGroup) => {
|
|
||||||
let messageToCheck = find(updatedGroup.chat, {id: message.id});
|
|
||||||
expect(messageToCheck.flags[user._id]).to.equal(true);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Flags a chat with a higher flag acount when an admin flags the message', async () => {
|
it('Flags a chat with a higher flag acount when an admin flags the message', async () => {
|
||||||
let secondUser;
|
let message = await user.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE});
|
||||||
let message;
|
message = message.message;
|
||||||
|
|
||||||
return generateUser({'contributor.admin': true}).then((generatedUser) => {
|
let flagResult = await admin.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
||||||
secondUser = generatedUser;
|
expect(flagResult.flags[admin._id]).to.equal(true);
|
||||||
return user.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE});
|
expect(flagResult.flagCount).to.equal(5);
|
||||||
})
|
|
||||||
.then((result) => {
|
let groupWithFlags = await admin.get(`/groups/${group._id}`);
|
||||||
message = result.message;
|
|
||||||
return secondUser.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
let messageToCheck = find(groupWithFlags.chat, {id: message.id});
|
||||||
})
|
expect(messageToCheck.flags[admin._id]).to.equal(true);
|
||||||
.then((result) => {
|
expect(messageToCheck.flagCount).to.equal(5);
|
||||||
expect(result.flags[secondUser._id]).to.equal(true);
|
|
||||||
expect(result.flagCount).to.equal(5);
|
|
||||||
return admin.get(`/groups/${group._id}`);
|
|
||||||
})
|
|
||||||
.then((updatedGroup) => {
|
|
||||||
let messageToCheck = find(updatedGroup.chat, {id: message.id});
|
|
||||||
expect(messageToCheck.flags[secondUser._id]).to.equal(true);
|
|
||||||
expect(messageToCheck.flagCount).to.equal(5);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Returns an error when user tries to flag a message that is already flagged', async () => {
|
it('Returns an error when user tries to flag a message that is already flagged', async () => {
|
||||||
let message;
|
let message = await anotherUser.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE});
|
||||||
|
message = message.message;
|
||||||
|
|
||||||
return generateUser().then((anotherUser) => {
|
await user.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
||||||
return anotherUser.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE});
|
|
||||||
})
|
await expect(user.post(`/groups/${group._id}/chat/${message.id}/flag`))
|
||||||
.then((result) => {
|
.to.eventually.be.rejected.and.eql({
|
||||||
message = result.message;
|
code: 404,
|
||||||
return user.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
error: 'NotFound',
|
||||||
})
|
message: t('messageGroupChatFlagAlreadyReported'),
|
||||||
.then(() => {
|
});
|
||||||
return expect(user.post(`/groups/${group._id}/chat/${message.id}/flag`))
|
|
||||||
.to.eventually.be.rejected.and.eql({
|
|
||||||
code: 404,
|
|
||||||
error: 'NotFound',
|
|
||||||
message: t('messageGroupChatFlagAlreadyReported'),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,36 +1,32 @@
|
|||||||
import {
|
import {
|
||||||
generateUser,
|
createAndPopulateGroup,
|
||||||
translate as t,
|
translate as t,
|
||||||
} from '../../../../helpers/api-v3-integration.helper';
|
} from '../../../../helpers/api-v3-integration.helper';
|
||||||
import { find } from 'lodash';
|
import { find } from 'lodash';
|
||||||
|
|
||||||
describe('POST /chat/:chatId/like', () => {
|
describe('POST /chat/:chatId/like', () => {
|
||||||
let user;
|
let user;
|
||||||
let group;
|
let groupWithChat;
|
||||||
let testMessage = 'Test Message';
|
let testMessage = 'Test Message';
|
||||||
|
let anotherUser;
|
||||||
|
|
||||||
before(() => {
|
before(async () => {
|
||||||
let groupName = 'Test Guild';
|
let { group, groupLeader, members } = await createAndPopulateGroup({
|
||||||
let groupType = 'guild';
|
groupDetails: {
|
||||||
let groupPrivacy = 'public';
|
name: 'Test Guild',
|
||||||
|
type: 'guild',
|
||||||
return generateUser({balance: 1}).then((generatedUser) => {
|
privacy: 'public',
|
||||||
user = generatedUser;
|
},
|
||||||
})
|
members: 1,
|
||||||
.then(() => {
|
|
||||||
return user.post('/groups', {
|
|
||||||
name: groupName,
|
|
||||||
type: groupType,
|
|
||||||
privacy: groupPrivacy,
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.then((generatedGroup) => {
|
|
||||||
group = generatedGroup;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
user = groupLeader;
|
||||||
|
groupWithChat = group;
|
||||||
|
anotherUser = members[0];
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Returns an error when chat message is not found', () => {
|
it('Returns an error when chat message is not found', async () => {
|
||||||
return expect(user.post(`/groups/${group._id}/chat/incorrectMessage/like`))
|
await expect(user.post(`/groups/${groupWithChat._id}/chat/incorrectMessage/like`))
|
||||||
.to.eventually.be.rejected.and.eql({
|
.to.eventually.be.rejected.and.eql({
|
||||||
code: 404,
|
code: 404,
|
||||||
error: 'NotFound',
|
error: 'NotFound',
|
||||||
@@ -38,59 +34,42 @@ describe('POST /chat/:chatId/like', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Returns an error when user tries to like their own message', () => {
|
it('Returns an error when user tries to like their own message', async () => {
|
||||||
return user.post(`/groups/${group._id}/chat`, { message: testMessage})
|
let message = await user.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
|
||||||
.then((result) => {
|
|
||||||
return expect(user.post(`/groups/${group._id}/chat/${result.message.id}/like`))
|
await expect(user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`))
|
||||||
.to.eventually.be.rejected.and.eql({
|
.to.eventually.be.rejected.and.eql({
|
||||||
code: 404,
|
code: 404,
|
||||||
error: 'NotFound',
|
error: 'NotFound',
|
||||||
message: t('messageGroupChatLikeOwnMessage'),
|
message: t('messageGroupChatLikeOwnMessage'),
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Likes a chat', () => {
|
it('Likes a chat', async () => {
|
||||||
let message;
|
let message = await anotherUser.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
|
||||||
|
|
||||||
return generateUser().then((anotherUser) => {
|
let likeResult = await user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`);
|
||||||
return anotherUser.post(`/groups/${group._id}/chat`, { message: testMessage});
|
|
||||||
})
|
expect(likeResult.likes[user._id]).to.equal(true);
|
||||||
.then((result) => {
|
|
||||||
message = result.message;
|
let groupWithChatLikes = await user.get(`/groups/${groupWithChat._id}`);
|
||||||
return user.post(`/groups/${group._id}/chat/${message.id}/like`);
|
|
||||||
})
|
let messageToCheck = find(groupWithChatLikes.chat, {id: message.message.id});
|
||||||
.then((result) => {
|
expect(messageToCheck.likes[user._id]).to.equal(true);
|
||||||
expect(result.likes[user._id]).to.equal(true);
|
|
||||||
return user.get(`/groups/${group._id}`);
|
|
||||||
})
|
|
||||||
.then((updatedGroup) => {
|
|
||||||
let messageToCheck = find(updatedGroup.chat, {id: message.id});
|
|
||||||
expect(messageToCheck.likes[user._id]).to.equal(true);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Unlikes a chat', () => {
|
it('Unlikes a chat', async () => {
|
||||||
let message;
|
let message = await anotherUser.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
|
||||||
|
|
||||||
return generateUser().then((anotherUser) => {
|
let likeResult = await user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`);
|
||||||
return anotherUser.post(`/groups/${group._id}/chat`, { message: testMessage});
|
expect(likeResult.likes[user._id]).to.equal(true);
|
||||||
})
|
|
||||||
.then((result) => {
|
let unlikeResult = await user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`);
|
||||||
message = result.message;
|
expect(unlikeResult.likes[user._id]).to.equal(false);
|
||||||
return user.post(`/groups/${group._id}/chat/${message.id}/like`);
|
|
||||||
})
|
let groupWithoutChatLikes = await user.get(`/groups/${groupWithChat._id}`);
|
||||||
.then((result) => {
|
|
||||||
expect(result.likes[user._id]).to.equal(true);
|
let messageToCheck = find(groupWithoutChatLikes.chat, {id: message.message.id});
|
||||||
return user.post(`/groups/${group._id}/chat/${message.id}/like`);
|
expect(messageToCheck.likes[user._id]).to.equal(false);
|
||||||
})
|
|
||||||
.then((result) => {
|
|
||||||
expect(result.likes[user._id]).to.equal(false);
|
|
||||||
return user.get(`/groups/${group._id}`);
|
|
||||||
})
|
|
||||||
.then((updatedGroup) => {
|
|
||||||
let messageToCheck = find(updatedGroup.chat, {id: message.id});
|
|
||||||
expect(messageToCheck.likes[user._id]).to.equal(false);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,95 +1,81 @@
|
|||||||
import {
|
import {
|
||||||
generateUser,
|
createAndPopulateGroup,
|
||||||
translate as t,
|
translate as t,
|
||||||
} from '../../../../helpers/api-v3-integration.helper';
|
} from '../../../../helpers/api-v3-integration.helper';
|
||||||
|
|
||||||
describe('POST /chat', () => {
|
describe('POST /chat', () => {
|
||||||
let user;
|
let user, groupWithChat, userWithChatRevoked, member;
|
||||||
|
let testMessage = 'Test Message';
|
||||||
|
|
||||||
before(() => {
|
before(async () => {
|
||||||
return generateUser().then((generatedUser) => {
|
let { group, groupLeader, members } = await createAndPopulateGroup({
|
||||||
user = generatedUser;
|
groupDetails: {
|
||||||
|
name: 'Test Guild',
|
||||||
|
type: 'guild',
|
||||||
|
privacy: 'public',
|
||||||
|
},
|
||||||
|
members: 2,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
user = groupLeader;
|
||||||
|
groupWithChat = group;
|
||||||
|
userWithChatRevoked = await members[0].update({'flags.chatRevoked': true});
|
||||||
|
member = members[0];
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Returns an error when no message is provided', () => {
|
it('Returns an error when no message is provided', async () => {
|
||||||
let groupName = 'Test Guild';
|
await expect(user.post(`/groups/${groupWithChat._id}/chat`, { message: ''}))
|
||||||
let groupType = 'guild';
|
.to.eventually.be.rejected.and.eql({
|
||||||
let groupPrivacy = 'public';
|
code: 400,
|
||||||
let testMessage = '';
|
error: 'BadRequest',
|
||||||
|
message: t('invalidReqParams'),
|
||||||
return generateUser({balance: 1}).then((anotherUser) => {
|
|
||||||
return anotherUser.post('/groups', {
|
|
||||||
name: groupName,
|
|
||||||
type: groupType,
|
|
||||||
privacy: groupPrivacy,
|
|
||||||
});
|
});
|
||||||
})
|
|
||||||
.then((group) => {
|
|
||||||
return expect(user.post(`/groups/${group._id}/chat`, { message: testMessage}))
|
|
||||||
.to.eventually.be.rejected.and.eql({
|
|
||||||
code: 400,
|
|
||||||
error: 'BadRequest',
|
|
||||||
message: t('invalidReqParams'),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Returns an error when group is not found', () => {
|
it('Returns an error when group is not found', async () => {
|
||||||
let testMessage = 'Test Message';
|
await expect(user.post('/groups/invalidID/chat', { message: testMessage})).to.eventually.be.rejected.and.eql({
|
||||||
return expect(user.post('/groups/nvalidID/chat', { message: testMessage})).to.eventually.be.rejected.and.eql({
|
|
||||||
code: 404,
|
code: 404,
|
||||||
error: 'NotFound',
|
error: 'NotFound',
|
||||||
message: t('groupNotFound'),
|
message: t('groupNotFound'),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Returns an error when chat privileges are revoked', () => {
|
it('Returns an error when chat privileges are revoked', async () => {
|
||||||
let groupName = 'Test Guild';
|
await expect(userWithChatRevoked.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage})).to.eventually.be.rejected.and.eql({
|
||||||
let groupType = 'guild';
|
code: 404,
|
||||||
let groupPrivacy = 'public';
|
error: 'NotFound',
|
||||||
let testMessage = 'Test Message';
|
message: 'Your chat privileges have been revoked.',
|
||||||
let userWithoutChat;
|
|
||||||
|
|
||||||
return generateUser({balance: 1, 'flags.chatRevoked': true}).then((generatedUser) => {
|
|
||||||
userWithoutChat = generatedUser;
|
|
||||||
|
|
||||||
return userWithoutChat.post('/groups', {
|
|
||||||
name: groupName,
|
|
||||||
type: groupType,
|
|
||||||
privacy: groupPrivacy,
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.then((group) => {
|
|
||||||
return expect(userWithoutChat.post(`/groups/${group._id}/chat`, { message: testMessage})).to.eventually.be.rejected.and.eql({
|
|
||||||
code: 404,
|
|
||||||
error: 'NotFound',
|
|
||||||
message: 'Your chat privileges have been revoked.',
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates a chat', () => {
|
it('creates a chat', async () => {
|
||||||
let groupName = 'Test Guild';
|
let message = await user.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
|
||||||
let groupType = 'guild';
|
|
||||||
let groupPrivacy = 'public';
|
|
||||||
let testMessage = 'Test Message';
|
|
||||||
let anotherUser;
|
|
||||||
|
|
||||||
return generateUser({balance: 1}).then((generatedUser) => {
|
expect(message.message.id).to.exist;
|
||||||
anotherUser = generatedUser;
|
});
|
||||||
|
|
||||||
return anotherUser.post('/groups', {
|
it('notifies other users of new messages for a guild', async () => {
|
||||||
name: groupName,
|
let message = await user.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
|
||||||
type: groupType,
|
let memberWithNotification = await member.get('/user');
|
||||||
privacy: groupPrivacy,
|
|
||||||
});
|
expect(message.message.id).to.exist;
|
||||||
})
|
expect(memberWithNotification.newMessages[`${groupWithChat._id}`]).to.exist;
|
||||||
.then((group) => {
|
});
|
||||||
return anotherUser.post(`/groups/${group._id}/chat`, { message: testMessage});
|
|
||||||
})
|
it('notifies other users of new messages for a party', async () => {
|
||||||
.then((result) => {
|
let { group, groupLeader, members } = await createAndPopulateGroup({
|
||||||
expect(result.message.id).to.exist;
|
groupDetails: {
|
||||||
|
name: 'Test Party',
|
||||||
|
type: 'party',
|
||||||
|
privacy: 'private',
|
||||||
|
},
|
||||||
|
members: 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let message = await groupLeader.post(`/groups/${group._id}/chat`, { message: testMessage});
|
||||||
|
let memberWithNotification = await members[0].get('/user');
|
||||||
|
|
||||||
|
expect(message.message.id).to.exist;
|
||||||
|
expect(memberWithNotification.newMessages[`${group._id}`]).to.exist;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user