lists banned words in the chat error message - fixes https://github.com/HabitRPG/habitica/issues/8812 (#8858)

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* small fix for unnecessary empty array

* added test and did some small refactoring

* lint error fix

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* small fix for unnecessary empty array

* added test and did some small refactoring

* lint error fix

* add test to check the error message contains the banned words used

* improve banned words test

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* small fix for unnecessary empty array

* added test and did some small refactoring

* lint error fix

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* add test to check the error message contains the banned words used

* improve banned words test

* merge with develop - aligned banned slurs check with banned words check
This commit is contained in:
borisabramovich86
2017-08-02 22:43:22 +03:00
committed by Sabe Jones
parent 014a7197f0
commit 026014b8d6
3 changed files with 60 additions and 45 deletions

View File

@@ -10,6 +10,8 @@ import {
TAVERN_ID,
} from '../../../../../website/server/models/group';
import { v4 as generateUUID } from 'uuid';
import { getMatchesByWordArray, removePunctuationFromString } from '../../../../../website/server/libs/stringUtils';
import bannedWords from '../../../../../website/server/libs/bannedWords';
import * as email from '../../../../../website/server/libs/email';
import { IncomingWebhook } from '@slack/client';
import nconf from 'nconf';
@@ -21,6 +23,9 @@ describe('POST /chat', () => {
let testMessage = 'Test Message';
let testBannedWordMessage = 'TEST_PLACEHOLDER_SWEAR_WORD_HERE';
let testSlurMessage = 'message with TEST_PLACEHOLDER_SLUR_WORD_HERE';
let bannedWordErrorMessage = t('bannedWordUsed').split('.');
bannedWordErrorMessage[0] += ` (${removePunctuationFromString(testBannedWordMessage.toLowerCase())})`;
bannedWordErrorMessage = bannedWordErrorMessage.join('.');
before(async () => {
let { group, groupLeader, members } = await createAndPopulateGroup({
@@ -31,7 +36,6 @@ describe('POST /chat', () => {
},
members: 2,
});
user = groupLeader;
groupWithChat = group;
member = members[0];
@@ -85,11 +89,11 @@ describe('POST /chat', () => {
context('banned word', () => {
it('returns an error when chat message contains a banned word in tavern', async () => {
await expect(user.post('/groups/habitrpg/chat', { message: testBannedWordMessage}))
.to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: t('bannedWordUsed'),
});
.to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: bannedWordErrorMessage,
});
});
it('errors when word is part of a phrase', async () => {
@@ -98,7 +102,7 @@ describe('POST /chat', () => {
.to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: t('bannedWordUsed'),
message: bannedWordErrorMessage,
});
});
@@ -108,10 +112,26 @@ describe('POST /chat', () => {
.to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: t('bannedWordUsed'),
message: bannedWordErrorMessage,
});
});
it('checks error message has the banned words used', async () => {
let randIndex = Math.floor(Math.random() * (bannedWords.length + 1));
let testBannedWords = bannedWords.slice(randIndex, randIndex + 2).map((w) => w.replace(/\\/g, ''));
let chatMessage = `Mixing ${testBannedWords[0]} and ${testBannedWords[1]} is bad for you.`;
await expect(user.post('/groups/habitrpg/chat', { message: chatMessage}))
.to.eventually.be.rejected
.and.have.property('message')
.that.includes(testBannedWords.join(', '));
});
it('check all banned words are matched', async () => {
let message = bannedWords.join(',').replace(/\\/g, '');
let matches = getMatchesByWordArray(message, bannedWords);
expect(matches.length).to.equal(bannedWords.length);
});
it('does not error when bad word is suffix of a word', async () => {
let wordAsSuffix = `prefix${testBannedWordMessage}`;
let message = await user.post('/groups/habitrpg/chat', { message: wordAsSuffix});