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({
22 text
: 'please give me ETH'
26 let chatSpamDetection
;
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
44 it('should mark messages from hellbanned user as spam', async () => {
45 fixture
.user1
.hellbanned
= true;
46 const isSpammy
= await chatSpamDetection
.detect({
48 room
: fixture
.troupe1
,
50 text
: 'Message from a banned user'
56 it('should use duplicate chat detector', function() {
58 for (var i
= 0; i
< 12; i
++) {
62 return Promise
.each(COUNTER
, function(v
, index
) {
63 return chatSpamDetection
66 room
: fixture
.troupe1
,
68 text
: '0123456789012345678912'
71 .then(function(isSpammy
) {
72 var expected
= index
>= 10;
73 assert
.strictEqual(isSpammy
, expected
);
77 return userService
.findById(fixture
.user1
._id
);
79 .then(function(user
) {
80 assert
.strictEqual(user
.hellbanned
, true);
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({
91 room
: fixture
.troupe1
,
93 text
: '0x1ea1F277E1A85961c337007556F1c23e5794262b'
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);