mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-14 05:07:22 +01:00
124
test/api/unit/libs/slack.js
Normal file
124
test/api/unit/libs/slack.js
Normal file
@@ -0,0 +1,124 @@
|
||||
/* 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;
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user