Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / modules / spam-detection / test / chat-spam-detection-test.js
blobc697370829218f76745668127b1e4d0a540819e8
1 'use strict';
3 const env = require('gitter-web-env');
4 var assert = require('assert');
5 var Promise = require('bluebird');
6 var proxyquireNoCallThru = require('proxyquire').noCallThru();
7 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
8 const userService = require('gitter-web-users');
9 const chatService = require('gitter-web-chats');
11 describe('chat-spam-detection', function() {
12 describe('integration tests #slow', function() {
13 var fixture = fixtureLoader.setupEach({
14 user1: {},
15 groupDirty1: {},
16 troupe1: {
17 group: 'groupDirty1'
19 message1: {
20 user: 'user1',
21 troupe: 'troupe1',
22 text: 'please give me ETH'
24 });
26 let chatSpamDetection;
27 beforeEach(() => {
28 const mockEnv = {
29 ...env,
30 config: {
31 get: key => {
32 if (key === 'spam-detection:ethereum-dirty-group-list') {
33 return [fixture.groupDirty1.id];
39 chatSpamDetection = proxyquireNoCallThru('../lib/chat-spam-detection', {
40 'gitter-web-env': mockEnv
41 });
42 });
44 it('should mark messages from hellbanned user as spam', async () => {
45 fixture.user1.hellbanned = true;
46 const isSpammy = await chatSpamDetection.detect({
47 user: fixture.user1,
48 room: fixture.troupe1,
49 parsedMessage: {
50 text: 'Message from a banned user'
52 });
53 assert(isSpammy);
54 });
56 it('should use duplicate chat detector', function() {
57 var COUNTER = [];
58 for (var i = 0; i < 12; i++) {
59 COUNTER.push(i);
62 return Promise.each(COUNTER, function(v, index) {
63 return chatSpamDetection
64 .detect({
65 user: fixture.user1,
66 room: fixture.troupe1,
67 parsedMessage: {
68 text: '0123456789012345678912'
71 .then(function(isSpammy) {
72 var expected = index >= 10;
73 assert.strictEqual(isSpammy, expected);
74 });
76 .then(function() {
77 return userService.findById(fixture.user1._id);
79 .then(function(user) {
80 assert.strictEqual(user.hellbanned, true);
81 });
82 });
84 it('should use ethereum spam detector', async () => {
85 // Ensure the message exists before we remove it
86 const beforeChatMessage = await chatService.findById(fixture.message1._id);
87 assert(beforeChatMessage);
89 const isSpammy = await chatSpamDetection.detect({
90 user: fixture.user1,
91 room: fixture.troupe1,
92 parsedMessage: {
93 text: '0x1ea1F277E1A85961c337007556F1c23e5794262b'
95 });
97 assert.strictEqual(isSpammy, true);
99 // Make sure the user is hellbanned and can no longer send mesages
100 const user = await userService.findById(fixture.user1._id);
101 assert.strictEqual(user.hellbanned, true);
103 // The spammy user's chat messages are removed from the room
104 const afterChatMessage = await chatService.findById(fixture.message1._id);
105 assert.strictEqual(afterChatMessage, null);