Webhooks v2 (and other fixes) (#10265)

* begin implementing global webhooks

* add checklist item scored webhook

* add pet hatched and mount raised webhooks (no tests)

* fix typo

* add lvl up webhooks, remove corrupt notifications and reorganize pre-save hook

* fix typo

* add some tests, globalActivity webhook

* fix bug in global activiy webhook and add more tests

* add tests and fix typo for petHatched and mountRaised webhooks

* fix errors and add tests for level up webhook

* wip: add default data to all webhooks, change signature for WebhookSender.send (missing tests)

* remove unused code

* fix unit tests

* fix chat webhooks

* remove console

* fix lint

* add and fix webhook tests

* add questStarted webhook and questActivity type

* add unit tests

* add finial tests and features
This commit is contained in:
Matteo Pagliazzi
2018-04-29 20:07:14 +02:00
committed by GitHub
parent cf274310a8
commit 5f0ef2d8f0
19 changed files with 1034 additions and 114 deletions

View File

@@ -24,6 +24,7 @@ describe('Webhook Model', () => {
updated: true,
deleted: true,
scored: true,
checklistScored: true,
},
};
});
@@ -36,6 +37,7 @@ describe('Webhook Model', () => {
wh.formatOptions(res);
expect(wh.options).to.eql({
checklistScored: false,
created: false,
updated: false,
deleted: false,
@@ -51,6 +53,7 @@ describe('Webhook Model', () => {
wh.formatOptions(res);
expect(wh.options).to.eql({
checklistScored: true,
created: false,
updated: true,
deleted: true,
@@ -67,6 +70,7 @@ describe('Webhook Model', () => {
expect(wh.options.foo).to.not.exist;
expect(wh.options).to.eql({
checklistScored: true,
created: true,
updated: true,
deleted: true,
@@ -74,7 +78,155 @@ describe('Webhook Model', () => {
});
});
['created', 'updated', 'deleted', 'scored'].forEach((option) => {
['created', 'updated', 'deleted', 'scored', 'checklistScored'].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 userActivity', () => {
let config;
beforeEach(() => {
config = {
type: 'userActivity',
url: 'https//exmaple.com/endpoint',
options: {
petHatched: true,
mountRaised: true,
leveledUp: true,
},
};
});
it('it provides default values for options', () => {
delete config.options;
let wh = new Webhook(config);
wh.formatOptions(res);
expect(wh.options).to.eql({
petHatched: false,
mountRaised: false,
leveledUp: false,
});
});
it('provides missing user options', () => {
delete config.options.petHatched;
let wh = new Webhook(config);
wh.formatOptions(res);
expect(wh.options).to.eql({
petHatched: false,
mountRaised: true,
leveledUp: 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({
petHatched: true,
mountRaised: true,
leveledUp: true,
});
});
['petHatched', 'petHatched', 'leveledUp'].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 questActivity', () => {
let config;
beforeEach(() => {
config = {
type: 'questActivity',
url: 'https//exmaple.com/endpoint',
options: {
questStarted: true,
questFinished: true,
},
};
});
it('it provides default values for options', () => {
delete config.options;
let wh = new Webhook(config);
wh.formatOptions(res);
expect(wh.options).to.eql({
questStarted: false,
questFinished: false,
});
});
it('provides missing user options', () => {
delete config.options.questStarted;
let wh = new Webhook(config);
wh.formatOptions(res);
expect(wh.options).to.eql({
questStarted: false,
questFinished: 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({
questStarted: true,
questFinished: true,
});
});
['questStarted', 'questFinished'].forEach((option) => {
it(`validates that ${option} is a boolean`, (done) => {
config.options[option] = 'not a boolean';
@@ -141,6 +293,30 @@ describe('Webhook Model', () => {
}
});
});
context('type is globalActivity', () => {
let config;
beforeEach(() => {
config = {
type: 'globalActivity',
url: 'https//exmaple.com/endpoint',
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({});
});
});
});
});
});