Files
habitica/test/api/v3/unit/models/webhook.test.js
negue a42cb0e3ab moving developer-only strings to api messages (#10188)
* move translatable string to apiMessages

* use apiMessages instead of res.t for groupIdRequired / keepOrRemove

* move pageMustBeNumber to apiMessages

* change apimessages

* move missingKeyParam to apiMessages

* move more strings to apiMessages

* fix lint

* revert lodash imports to fix tests

* fix webhook test

* fix test

* rollback key change of `keepOrRemove`

* remove unneeded `req.language` param

*  extract more messages from i18n

* add missing `missingTypeParam` message
2018-04-14 16:13:13 +02:00

148 lines
3.7 KiB
JavaScript

import { model as Webhook } from '../../../../../website/server/models/webhook';
import { BadRequest } from '../../../../../website/server/libs/errors';
import { v4 as generateUUID } from 'uuid';
import apiMessages from '../../../../../website/server/libs/apiMessages';
describe('Webhook Model', () => {
context('Instance Methods', () => {
describe('#formatOptions', () => {
let res;
beforeEach(() => {
res = {
t: sandbox.spy(),
};
});
context('type is taskActivity', () => {
let config;
beforeEach(() => {
config = {
type: 'taskActivity',
url: 'https//exmaple.com/endpoint',
options: {
created: true,
updated: true,
deleted: true,
scored: true,
},
};
});
it('it provides default values for options', () => {
delete config.options;
let wh = new Webhook(config);
wh.formatOptions(res);
expect(wh.options).to.eql({
created: false,
updated: false,
deleted: false,
scored: true,
});
});
it('provides missing task options', () => {
delete config.options.created;
let wh = new Webhook(config);
wh.formatOptions(res);
expect(wh.options).to.eql({
created: false,
updated: true,
deleted: true,
scored: true,
});
});
it('discards additional options', () => {
config.options.foo = 'another option';
let wh = new Webhook(config);
wh.formatOptions(res);
expect(wh.options.foo).to.not.exist;
expect(wh.options).to.eql({
created: true,
updated: true,
deleted: true,
scored: true,
});
});
['created', 'updated', 'deleted', 'scored'].forEach((option) => {
it(`validates that ${option} is a boolean`, (done) => {
config.options[option] = 'not a boolean';
try {
let wh = new Webhook(config);
wh.formatOptions(res);
} catch (err) {
expect(err).to.be.an.instanceOf(BadRequest);
expect(res.t).to.be.calledOnce;
expect(res.t).to.be.calledWith('webhookBooleanOption', { option });
done();
}
});
});
});
context('type is groupChatReceived', () => {
let config;
beforeEach(() => {
config = {
type: 'groupChatReceived',
url: 'https//exmaple.com/endpoint',
options: {
groupId: generateUUID(),
},
};
});
it('creates options', () => {
let wh = new Webhook(config);
wh.formatOptions(res);
expect(wh.options).to.eql(config.options);
});
it('discards additional objects', () => {
config.options.foo = 'another thing';
let wh = new Webhook(config);
wh.formatOptions(res);
expect(wh.options.foo).to.not.exist;
expect(wh.options).to.eql({
groupId: config.options.groupId,
});
});
it('requires groupId option to be a uuid', (done) => {
config.options.groupId = 'not a uuid';
try {
let wh = new Webhook(config);
wh.formatOptions(res);
} catch (err) {
expect(err).to.be.an.instanceOf(BadRequest);
expect(err.message).to.eql(apiMessages('groupIdRequired'));
done();
}
});
});
});
});
});