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';
|
||||
|
||||
describe('POST /chat/:chatId/flag', () => {
|
||||
let user, admin, group;
|
||||
let user, admin, anotherUser, group;
|
||||
const TEST_MESSAGE = 'Test Message';
|
||||
|
||||
before(async () => {
|
||||
user = await generateUser({balance: 1});
|
||||
admin = await generateUser({balance: 1, 'contributor.admin': true});
|
||||
anotherUser = await generateUser();
|
||||
|
||||
group = await user.post('/groups', {
|
||||
name: 'Test Guild',
|
||||
@@ -20,7 +21,7 @@ describe('POST /chat/:chatId/flag', () => {
|
||||
});
|
||||
|
||||
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({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
@@ -29,79 +30,55 @@ describe('POST /chat/:chatId/flag', () => {
|
||||
});
|
||||
|
||||
it('Returns an error when user tries to flag their own message', async () => {
|
||||
return user.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE})
|
||||
.then((result) => {
|
||||
return expect(user.post(`/groups/${group._id}/chat/${result.message.id}/flag`))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('messageGroupChatFlagOwnMessage'),
|
||||
});
|
||||
});
|
||||
let message = await user.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE });
|
||||
await expect(user.post(`/groups/${group._id}/chat/${message.message.id}/flag`))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('messageGroupChatFlagOwnMessage'),
|
||||
});
|
||||
});
|
||||
|
||||
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) => {
|
||||
return anotherUser.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE});
|
||||
})
|
||||
.then((result) => {
|
||||
message = result.message;
|
||||
return user.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
||||
})
|
||||
.then((result) => {
|
||||
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);
|
||||
});
|
||||
let flagResult = await user.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
||||
expect(flagResult.flags[user._id]).to.equal(true);
|
||||
expect(flagResult.flagCount).to.equal(1);
|
||||
|
||||
let groupWithFlags = await admin.get(`/groups/${group._id}`);
|
||||
|
||||
let messageToCheck = find(groupWithFlags.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 () => {
|
||||
let secondUser;
|
||||
let message;
|
||||
let message = await user.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE});
|
||||
message = message.message;
|
||||
|
||||
return generateUser({'contributor.admin': true}).then((generatedUser) => {
|
||||
secondUser = generatedUser;
|
||||
return user.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE});
|
||||
})
|
||||
.then((result) => {
|
||||
message = result.message;
|
||||
return secondUser.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
||||
})
|
||||
.then((result) => {
|
||||
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);
|
||||
});
|
||||
let flagResult = await admin.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
||||
expect(flagResult.flags[admin._id]).to.equal(true);
|
||||
expect(flagResult.flagCount).to.equal(5);
|
||||
|
||||
let groupWithFlags = await admin.get(`/groups/${group._id}`);
|
||||
|
||||
let messageToCheck = find(groupWithFlags.chat, {id: message.id});
|
||||
expect(messageToCheck.flags[admin._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 () => {
|
||||
let message;
|
||||
let message = await anotherUser.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE});
|
||||
message = message.message;
|
||||
|
||||
return generateUser().then((anotherUser) => {
|
||||
return anotherUser.post(`/groups/${group._id}/chat`, { message: TEST_MESSAGE});
|
||||
})
|
||||
.then((result) => {
|
||||
message = result.message;
|
||||
return user.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
||||
})
|
||||
.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'),
|
||||
});
|
||||
});
|
||||
await user.post(`/groups/${group._id}/chat/${message.id}/flag`);
|
||||
|
||||
await 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 {
|
||||
generateUser,
|
||||
createAndPopulateGroup,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-v3-integration.helper';
|
||||
import { find } from 'lodash';
|
||||
|
||||
describe('POST /chat/:chatId/like', () => {
|
||||
let user;
|
||||
let group;
|
||||
let groupWithChat;
|
||||
let testMessage = 'Test Message';
|
||||
let anotherUser;
|
||||
|
||||
before(() => {
|
||||
let groupName = 'Test Guild';
|
||||
let groupType = 'guild';
|
||||
let groupPrivacy = 'public';
|
||||
|
||||
return generateUser({balance: 1}).then((generatedUser) => {
|
||||
user = generatedUser;
|
||||
})
|
||||
.then(() => {
|
||||
return user.post('/groups', {
|
||||
name: groupName,
|
||||
type: groupType,
|
||||
privacy: groupPrivacy,
|
||||
});
|
||||
})
|
||||
.then((generatedGroup) => {
|
||||
group = generatedGroup;
|
||||
before(async () => {
|
||||
let { group, groupLeader, members } = await createAndPopulateGroup({
|
||||
groupDetails: {
|
||||
name: 'Test Guild',
|
||||
type: 'guild',
|
||||
privacy: 'public',
|
||||
},
|
||||
members: 1,
|
||||
});
|
||||
|
||||
user = groupLeader;
|
||||
groupWithChat = group;
|
||||
anotherUser = members[0];
|
||||
});
|
||||
|
||||
it('Returns an error when chat message is not found', () => {
|
||||
return expect(user.post(`/groups/${group._id}/chat/incorrectMessage/like`))
|
||||
it('Returns an error when chat message is not found', async () => {
|
||||
await expect(user.post(`/groups/${groupWithChat._id}/chat/incorrectMessage/like`))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
@@ -38,59 +34,42 @@ describe('POST /chat/:chatId/like', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('Returns an error when user tries to like their own message', () => {
|
||||
return user.post(`/groups/${group._id}/chat`, { message: testMessage})
|
||||
.then((result) => {
|
||||
return expect(user.post(`/groups/${group._id}/chat/${result.message.id}/like`))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('messageGroupChatLikeOwnMessage'),
|
||||
});
|
||||
});
|
||||
it('Returns an error when user tries to like their own message', async () => {
|
||||
let message = await user.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
|
||||
|
||||
await expect(user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('messageGroupChatLikeOwnMessage'),
|
||||
});
|
||||
});
|
||||
|
||||
it('Likes a chat', () => {
|
||||
let message;
|
||||
it('Likes a chat', async () => {
|
||||
let message = await anotherUser.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
|
||||
|
||||
return generateUser().then((anotherUser) => {
|
||||
return anotherUser.post(`/groups/${group._id}/chat`, { message: testMessage});
|
||||
})
|
||||
.then((result) => {
|
||||
message = result.message;
|
||||
return user.post(`/groups/${group._id}/chat/${message.id}/like`);
|
||||
})
|
||||
.then((result) => {
|
||||
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);
|
||||
});
|
||||
let likeResult = await user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`);
|
||||
|
||||
expect(likeResult.likes[user._id]).to.equal(true);
|
||||
|
||||
let groupWithChatLikes = await user.get(`/groups/${groupWithChat._id}`);
|
||||
|
||||
let messageToCheck = find(groupWithChatLikes.chat, {id: message.message.id});
|
||||
expect(messageToCheck.likes[user._id]).to.equal(true);
|
||||
});
|
||||
|
||||
it('Unlikes a chat', () => {
|
||||
let message;
|
||||
it('Unlikes a chat', async () => {
|
||||
let message = await anotherUser.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
|
||||
|
||||
return generateUser().then((anotherUser) => {
|
||||
return anotherUser.post(`/groups/${group._id}/chat`, { message: testMessage});
|
||||
})
|
||||
.then((result) => {
|
||||
message = result.message;
|
||||
return user.post(`/groups/${group._id}/chat/${message.id}/like`);
|
||||
})
|
||||
.then((result) => {
|
||||
expect(result.likes[user._id]).to.equal(true);
|
||||
return user.post(`/groups/${group._id}/chat/${message.id}/like`);
|
||||
})
|
||||
.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);
|
||||
});
|
||||
let likeResult = await user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`);
|
||||
expect(likeResult.likes[user._id]).to.equal(true);
|
||||
|
||||
let unlikeResult = await user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`);
|
||||
expect(unlikeResult.likes[user._id]).to.equal(false);
|
||||
|
||||
let groupWithoutChatLikes = await user.get(`/groups/${groupWithChat._id}`);
|
||||
|
||||
let messageToCheck = find(groupWithoutChatLikes.chat, {id: message.message.id});
|
||||
expect(messageToCheck.likes[user._id]).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,95 +1,81 @@
|
||||
import {
|
||||
generateUser,
|
||||
createAndPopulateGroup,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-v3-integration.helper';
|
||||
|
||||
describe('POST /chat', () => {
|
||||
let user;
|
||||
let user, groupWithChat, userWithChatRevoked, member;
|
||||
let testMessage = 'Test Message';
|
||||
|
||||
before(() => {
|
||||
return generateUser().then((generatedUser) => {
|
||||
user = generatedUser;
|
||||
before(async () => {
|
||||
let { group, groupLeader, members } = await createAndPopulateGroup({
|
||||
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', () => {
|
||||
let groupName = 'Test Guild';
|
||||
let groupType = 'guild';
|
||||
let groupPrivacy = 'public';
|
||||
let testMessage = '';
|
||||
|
||||
return generateUser({balance: 1}).then((anotherUser) => {
|
||||
return anotherUser.post('/groups', {
|
||||
name: groupName,
|
||||
type: groupType,
|
||||
privacy: groupPrivacy,
|
||||
it('Returns an error when no message is provided', async () => {
|
||||
await expect(user.post(`/groups/${groupWithChat._id}/chat`, { message: ''}))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('invalidReqParams'),
|
||||
});
|
||||
})
|
||||
.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', () => {
|
||||
let testMessage = 'Test Message';
|
||||
return expect(user.post('/groups/nvalidID/chat', { message: testMessage})).to.eventually.be.rejected.and.eql({
|
||||
it('Returns an error when group is not found', async () => {
|
||||
await expect(user.post('/groups/invalidID/chat', { message: testMessage})).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('groupNotFound'),
|
||||
});
|
||||
});
|
||||
|
||||
it('Returns an error when chat privileges are revoked', () => {
|
||||
let groupName = 'Test Guild';
|
||||
let groupType = 'guild';
|
||||
let groupPrivacy = 'public';
|
||||
let testMessage = 'Test Message';
|
||||
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('Returns an error when chat privileges are revoked', async () => {
|
||||
await expect(userWithChatRevoked.post(`/groups/${groupWithChat._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', () => {
|
||||
let groupName = 'Test Guild';
|
||||
let groupType = 'guild';
|
||||
let groupPrivacy = 'public';
|
||||
let testMessage = 'Test Message';
|
||||
let anotherUser;
|
||||
it('creates a chat', async () => {
|
||||
let message = await user.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
|
||||
|
||||
return generateUser({balance: 1}).then((generatedUser) => {
|
||||
anotherUser = generatedUser;
|
||||
expect(message.message.id).to.exist;
|
||||
});
|
||||
|
||||
return anotherUser.post('/groups', {
|
||||
name: groupName,
|
||||
type: groupType,
|
||||
privacy: groupPrivacy,
|
||||
});
|
||||
})
|
||||
.then((group) => {
|
||||
return anotherUser.post(`/groups/${group._id}/chat`, { message: testMessage});
|
||||
})
|
||||
.then((result) => {
|
||||
expect(result.message.id).to.exist;
|
||||
it('notifies other users of new messages for a guild', async () => {
|
||||
let message = await user.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
|
||||
let memberWithNotification = await member.get('/user');
|
||||
|
||||
expect(message.message.id).to.exist;
|
||||
expect(memberWithNotification.newMessages[`${groupWithChat._id}`]).to.exist;
|
||||
});
|
||||
|
||||
it('notifies other users of new messages for a party', async () => {
|
||||
let { group, groupLeader, members } = await createAndPopulateGroup({
|
||||
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