mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 06:37:23 +01:00
* remove duplicate module.exports statement * remove commented-out footer in Slack slur notification There's no need for anything to replace this footer. * swap order of flag actions to put most critical first This causes moderators to be notified before the flagged message's flagCount is incremented, because if something happens to prevent the flagGroupMessage Promise from resolving, we still want to mods to see the notification. * limit chat message flagging ability for new players Players who created accounts less than three days ago can flag posts but that does not contribute to the posts' flagCount. This prevents a troll from maliciously hiding innocent messages by creating new accounts to flag them. * add tests * fix other tests
125 lines
3.6 KiB
JavaScript
125 lines
3.6 KiB
JavaScript
/* eslint-disable camelcase */
|
|
import { IncomingWebhook } from '@slack/client';
|
|
import requireAgain from 'require-again';
|
|
import slack from '../../../../website/server/libs/slack';
|
|
import logger from '../../../../website/server/libs/logger';
|
|
import { TAVERN_ID } from '../../../../website/server/models/group';
|
|
import nconf from 'nconf';
|
|
import moment from 'moment';
|
|
|
|
describe('slack', () => {
|
|
describe('sendFlagNotification', () => {
|
|
let data;
|
|
|
|
beforeEach(() => {
|
|
sandbox.stub(IncomingWebhook.prototype, 'send');
|
|
data = {
|
|
authorEmail: 'author@example.com',
|
|
flagger: {
|
|
id: 'flagger-id',
|
|
profile: {
|
|
name: 'flagger',
|
|
},
|
|
preferences: {
|
|
language: 'flagger-lang',
|
|
},
|
|
},
|
|
group: {
|
|
id: 'group-id',
|
|
privacy: 'private',
|
|
name: 'Some group',
|
|
type: 'guild',
|
|
},
|
|
message: {
|
|
id: 'chat-id',
|
|
user: 'Author',
|
|
uuid: 'author-id',
|
|
text: 'some text',
|
|
},
|
|
};
|
|
});
|
|
|
|
afterEach(() => {
|
|
IncomingWebhook.prototype.send.restore();
|
|
});
|
|
|
|
it('sends a slack webhook', () => {
|
|
slack.sendFlagNotification(data);
|
|
|
|
const timestamp = `${moment(data.message.timestamp).utc().format('YYYY-MM-DD HH:mm')} UTC`;
|
|
|
|
expect(IncomingWebhook.prototype.send).to.be.calledOnce;
|
|
expect(IncomingWebhook.prototype.send).to.be.calledWith({
|
|
text: 'flagger (flagger-id; language: flagger-lang) flagged a message',
|
|
attachments: [{
|
|
fallback: 'Flag Message',
|
|
color: 'danger',
|
|
author_name: `Author - author@example.com - author-id\n${timestamp}`,
|
|
title: 'Flag in Some group - (private guild)',
|
|
title_link: undefined,
|
|
text: 'some text',
|
|
footer: sandbox.match(/<.*?groupId=group-id&chatId=chat-id\|Flag this message.>/),
|
|
mrkdwn_in: [
|
|
'text',
|
|
],
|
|
}],
|
|
});
|
|
});
|
|
|
|
it('includes a title link if guild is public', () => {
|
|
data.group.privacy = 'public';
|
|
|
|
slack.sendFlagNotification(data);
|
|
|
|
expect(IncomingWebhook.prototype.send).to.be.calledWithMatch({
|
|
attachments: [sandbox.match({
|
|
title: 'Flag in Some group',
|
|
title_link: sandbox.match(/.*\/groups\/guild\/group-id/),
|
|
})],
|
|
});
|
|
});
|
|
|
|
it('links to tavern', () => {
|
|
data.group.privacy = 'public';
|
|
data.group.name = 'Tavern';
|
|
data.group.id = TAVERN_ID;
|
|
|
|
slack.sendFlagNotification(data);
|
|
|
|
expect(IncomingWebhook.prototype.send).to.be.calledWithMatch({
|
|
attachments: [sandbox.match({
|
|
title: 'Flag in Tavern',
|
|
title_link: sandbox.match(/.*\/groups\/tavern/),
|
|
})],
|
|
});
|
|
});
|
|
|
|
it('provides name for system message', () => {
|
|
data.message.uuid = 'system';
|
|
delete data.message.user;
|
|
|
|
slack.sendFlagNotification(data);
|
|
|
|
const timestamp = `${moment(data.message.timestamp).utc().format('YYYY-MM-DD HH:mm')} UTC`;
|
|
|
|
expect(IncomingWebhook.prototype.send).to.be.calledWithMatch({
|
|
attachments: [sandbox.match({
|
|
author_name: `System Message\n${timestamp}`,
|
|
})],
|
|
});
|
|
});
|
|
|
|
it('noops if no flagging url is provided', () => {
|
|
sandbox.stub(nconf, 'get').withArgs('SLACK:FLAGGING_URL').returns('');
|
|
sandbox.stub(logger, 'error');
|
|
let reRequiredSlack = requireAgain('../../../../website/server/libs/slack');
|
|
|
|
expect(logger.error).to.be.calledOnce;
|
|
|
|
reRequiredSlack.sendFlagNotification(data);
|
|
|
|
expect(IncomingWebhook.prototype.send).to.not.be.called;
|
|
});
|
|
});
|
|
});
|