Merge master

This commit is contained in:
Mendel Konikov
2018-05-01 20:25:05 -04:00
225 changed files with 5840 additions and 2389 deletions
@@ -0,0 +1,11 @@
let values = {};
const getScores = () => values.getScores;
const isToxic = () => values.isToxic;
const setValues = newValues => {
values = newValues;
};
module.exports = { getScores, isToxic, setValues };
@@ -1,12 +1,16 @@
const { APIError } = require('errors');
const { TalkError } = require('errors');
// ErrToxic is sent during a `CreateComment` mutation where
// `input.checkToxicity` is set to true and the comment contains
// toxic language as determined by the perspective service.
const ErrToxic = new APIError('Comment is toxic', {
status: 400,
translation_key: 'COMMENT_IS_TOXIC',
});
class ErrToxic extends TalkError {
constructor() {
super('Comment is toxic', {
status: 400,
translation_key: 'COMMENT_IS_TOXIC',
});
}
}
module.exports = {
ErrToxic,
@@ -1,11 +1,6 @@
const { getScores, isToxic } = require('./perspective');
const { ErrToxic } = require('./errors');
// We don't add the hooks during _test_ as the perspective API is not available.
if (process.env.NODE_ENV === 'test') {
return null;
}
module.exports = {
RootMutation: {
createComment: {
@@ -16,7 +11,7 @@ module.exports = {
scores = await getScores(input.body);
} catch (err) {
// Warn and let mutation pass.
console.trace(err);
console.trace(err); // TODO: log/handle this differently?
return;
}
@@ -27,7 +22,7 @@ module.exports = {
if (isToxic(scores)) {
if (input.checkToxicity) {
throw ErrToxic;
throw new ErrToxic();
}
input.status = 'SYSTEM_WITHHELD';
@@ -0,0 +1,31 @@
const hooks = require('./hooks');
const { ErrToxic } = require('./errors');
// Mock out the perspective api call.
jest.mock('./perspective');
describe('talk-plugin-toxic-comments', () => {
describe('hooks', () => {
beforeEach(() => {
require('./perspective').setValues({ isToxic: true });
});
it('sets the correct values for a toxic comment', async () => {
let input = { body: 'This is a body.', checkToxicity: false };
await hooks.RootMutation.createComment.pre(null, { input }, null, null);
expect(input).toHaveProperty('status', 'SYSTEM_WITHHELD');
});
it('throws an error when a toxic comment is sent', async () => {
expect.assertions(1);
await expect(
hooks.RootMutation.createComment.pre(
null,
{ input: { checkToxicity: true } },
null,
null
)
).rejects.toBeInstanceOf(ErrToxic);
});
});
});