File size: 3,747 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
 * Tests for the chat monitor (word filters).
 *
 * @author Annika
 */
'use strict';

const assert = require('assert').strict;
const { makeUser } = require('../../users-utils');

const { Filters } = require('../../../dist/server/chat-plugins/chat-monitor');

describe('Chat monitor', () => {
	describe('regex generator', () => {
		it('should generate case-insensitive regexes', () => {
			const regex = Filters.generateRegex('slur');
			assert(regex.flags.includes('i'));
		});

		it('should use word boundaries for URL shortener regexes', () => {
			const regex = Filters.generateRegex('bit.ly/', false, true);
			assert(String(regex).startsWith('/\\b'));
		});

		it('should correctly strip word boundaries', () => {
			const regex = /\btest\b/iu;
			assert.deepEqual(Filters.stripWordBoundaries(regex), /test/iu);
		});

		describe('evasion regexes', function () {
			before(() => {
				this.evasionRegex = Filters.generateRegex('slur', true);
			});

			it('should account for stretching', () => {
				assert(this.evasionRegex.test('slur'));
				assert(this.evasionRegex.test('slurrrr'));
				assert(this.evasionRegex.test('sssllluurrrr'));
			});

			it('should account for non-English characters', () => {
				assert(this.evasionRegex.test('sⱠür'));
				assert(this.evasionRegex.test('s𝓵𝓾r'));
			});

			it('should account for periods', () => {
				assert(this.evasionRegex.test('s.l.u.r'));
			});
		});
	});

	describe('in-room tests', function () {
		before(() => {
			this.room = Rooms.get('lobby');
			this.user = makeUser("Unit Tester");
			this.connection = this.user.connections[0];
			this.user.joinRoom(this.room.roomid, this.connection);

			Chat.loadPlugins();
			this.parse = async function (message) {
				const context = new Chat.CommandContext({
					message,
					room: this.room,
					user: this.user,
					connection: this.connection,
				});
				return context.parse();
			};
		});

		beforeEach(() => Punishments.unlock(this.user.id));

		it('should lock users who use autolock phrases', async () => {
			assert(!this.user.locked);
			Filters.add({
				word: 'autolock',
				list: 'autolock',
			});

			await this.parse("haha autolock me pls");

			assert(this.user.locked);
			assert.notEqual(this.room.log.log.pop(), "haha autolock me pls");
		});

		it('should lock users who evade evasion phrases', async () => {
			assert(!this.user.locked);
			Filters.add({
				word: 'slur',
				list: 'evasion',
			});

			await this.parse("sl ur");
			assert(this.user.locked);
			assert.notEqual(this.room.log.log.pop(), "sl ur");
		});

		it('should replace words filtered to other words', async () => {
			assert(!this.user.locked);
			Filters.add({
				word: 'replace me',
				list: 'wordfilter',
				replacement: 'i got replaced',
			});

			await this.parse("Hello! replace me pls! thanks, and remember to replace me.");
			assert.equal(
				this.room.log.log.pop().replace(/^\|c:\|[0-9]+\| Unit Tester\|/, ''),
				"Hello! i got replaced pls! thanks, and remember to i got replaced."
			);
		});

		it('should prevent filtered words from being said', async () => {
			assert(!this.user.locked);
			Filters.add({
				word: 'mild slur',
				list: 'warn',
			});

			await this.parse("mild slur");
			assert.notEqual(this.room.log.log.pop(), "mild slur");
		});

		it('should prevent banwords and evasion banwords from being used in usernames', () => {
			Filters.add({
				word: 'nameslur',
				list: 'warn',
			});

			Filters.add({
				word: 'strongnameslur',
				list: 'evasion',
			});

			assert.equal(Chat.namefilter('anameslurtest', this.user), '');
			assert.equal(Chat.namefilter('strongnameslur', this.user), '');
			assert.equal(Chat.namefilter('stroñgñameslur', this.user), '');
		});
	});
});