mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-15 05:37:22 +01:00
Webhook improvements (#7879)
* refactor: Move translate test utility to helpers directory * Add kind property to webhooks * feat: Add options to create webhook route * refactor: Move webhook ops into single file * refactor: Create webhook objects for specific webhook behavior * chore(tests): Add default sleep helper value of 1 second * feat(api): Add method for groups to send out webhook * feat(api): Add taskCreated webhook task creation * feat(api): Send chat webhooks after a chat is sent * refactor: Move webhook routes to own controller * lint: Correct linting errors * fix(api): Correct taskCreated webhook method * fix(api): Fix webhook logging to only log when there is an error * fix: Update groupChatRecieved webhook creation * chore: Add integration tests for webhooks * fix: Set webhook creation response to 201 * fix: Correct how task scored webhook data is sent * Revert group chat recieved webhook to only support one group * Remove quest activity option for webhooks * feat: Send webhook for each task created * feat: Allow webhooks without a type to default to taskScored * feat: Add logic for adding ids to webhook * feat: optimize webhook url check by shortcircuiting if no url is passed * refactor: Use full name for webhook variable * feat: Add missing params to client webhook * lint: Add missing semicolon * chore(tests): Fix inccorect webhook tests * chore: Add migration to update task scored webhooks * feat: Allow default value of webhook add route to be enabled * chore: Update webhook documentation * chore: Remove special handling for v2 * refactor: adjust addComputedStatsToJSONObject to work for webhooks * refactor: combine taskScored and taskActivity webhooks * feat(api): Add task activity to task update and delete routes * chore: Change references to taskScored to taskActivity * fix: Correct stats object being passed in for transform * chore: Remove extra line break * fix: Pass in the language to use for the translations * refactor(api): Move webhooks from user.preferences.webhooks to user.webhooks * chore: Update migration to set webhook array * lint: Correct brace spacing * chore: convert webhook lib to use user.webhooks * refactor(api): Consolidate filters * chore: clarify migration instructions * fix(test): Correct user creation in user anonymized tests * chore: add test that webhooks cannot be updated via PUT /user * refactor: Simplify default webhook id value * refactor(client): Push newly created webhook instead of doing a sync * chore(test): Add test file for webhook model * refactor: Remove webhook validation * refactor: Remove need for watch on webhooks * refactor(client): Update webhooks object without syncing * chore: update webhook documentation * Fix migrations issues * chore: remove v2 test helper * fix(api): Provide webhook type in task scored webhook * fix(client): Fix webhook deletion appearing to delete all webhooks * feat(api): add optional label field for webhooks * feat: provide empty string as default for webhook label * chore: Update webhook migration * chore: update webhook migration name
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
|
||||
let user, webhookToDelete;
|
||||
let endpoint = '/user/webhook';
|
||||
|
||||
describe('DELETE /user/webhook', () => {
|
||||
beforeEach(async () => {
|
||||
user = await generateUser();
|
||||
|
||||
webhookToDelete = await user.post('/user/webhook', {
|
||||
url: 'http://some-url.com',
|
||||
enabled: true,
|
||||
});
|
||||
await user.post('/user/webhook', {
|
||||
url: 'http://some-other-url.com',
|
||||
enabled: false,
|
||||
});
|
||||
|
||||
await user.sync();
|
||||
});
|
||||
|
||||
it('deletes a webhook', async () => {
|
||||
expect(user.webhooks).to.have.a.lengthOf(2);
|
||||
await user.del(`${endpoint}/${webhookToDelete.id}`);
|
||||
|
||||
await user.sync();
|
||||
|
||||
expect(user.webhooks).to.have.a.lengthOf(1);
|
||||
});
|
||||
|
||||
it('returns the remaining webhooks', async () => {
|
||||
let [remainingWebhook] = await user.del(`${endpoint}/${webhookToDelete.id}`);
|
||||
|
||||
await user.sync();
|
||||
|
||||
let webhook = user.webhooks[0];
|
||||
|
||||
expect(remainingWebhook.id).to.eql(webhook.id);
|
||||
expect(remainingWebhook.url).to.eql(webhook.url);
|
||||
expect(remainingWebhook.type).to.eql(webhook.type);
|
||||
expect(remainingWebhook.options).to.eql(webhook.options);
|
||||
});
|
||||
|
||||
it('returns an error if webhook with id does not exist', async () => {
|
||||
await expect(user.del(`${endpoint}/id-that-does-not-exist`)).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('noWebhookWithId', {id: 'id-that-does-not-exist'}),
|
||||
});
|
||||
});
|
||||
});
|
||||
211
test/api/v3/integration/webhook/POST-user_add_webhook.test.js
Normal file
211
test/api/v3/integration/webhook/POST-user_add_webhook.test.js
Normal file
@@ -0,0 +1,211 @@
|
||||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
import { v4 as generateUUID } from 'uuid';
|
||||
|
||||
describe('POST /user/webhook', () => {
|
||||
let user, body;
|
||||
|
||||
beforeEach(async () => {
|
||||
user = await generateUser();
|
||||
body = {
|
||||
id: generateUUID(),
|
||||
url: 'https://example.com/endpoint',
|
||||
type: 'taskActivity',
|
||||
enabled: false,
|
||||
};
|
||||
});
|
||||
|
||||
it('requires a url', async () => {
|
||||
delete body.url;
|
||||
|
||||
await expect(user.post('/user/webhook', body)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: 'User validation failed',
|
||||
});
|
||||
});
|
||||
|
||||
it('requires custom id to be a uuid', async () => {
|
||||
body.id = 'not-a-uuid';
|
||||
|
||||
await expect(user.post('/user/webhook', body)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: 'User validation failed',
|
||||
});
|
||||
});
|
||||
|
||||
it('defaults id to a uuid', async () => {
|
||||
delete body.id;
|
||||
|
||||
let webhook = await user.post('/user/webhook', body);
|
||||
|
||||
expect(webhook.id).to.exist;
|
||||
});
|
||||
|
||||
it('requires type to be of an accetable type', async () => {
|
||||
body.type = 'not a valid type';
|
||||
|
||||
await expect(user.post('/user/webhook', body)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: 'User validation failed',
|
||||
});
|
||||
});
|
||||
|
||||
it('defaults enabled to true', async () => {
|
||||
delete body.enabled;
|
||||
|
||||
let webhook = await user.post('/user/webhook', body);
|
||||
|
||||
expect(webhook.enabled).to.be.true;
|
||||
});
|
||||
|
||||
it('can pass a label', async () => {
|
||||
body.label = 'Custom Label';
|
||||
|
||||
let webhook = await user.post('/user/webhook', body);
|
||||
|
||||
expect(webhook.label).to.equal('Custom Label');
|
||||
});
|
||||
|
||||
it('defaults type to taskActivity', async () => {
|
||||
delete body.type;
|
||||
|
||||
let webhook = await user.post('/user/webhook', body);
|
||||
|
||||
expect(webhook.type).to.eql('taskActivity');
|
||||
});
|
||||
|
||||
it('successfully adds the webhook', async () => {
|
||||
expect(user.webhooks).to.eql([]);
|
||||
|
||||
let response = await user.post('/user/webhook', body);
|
||||
|
||||
expect(response.id).to.eql(body.id);
|
||||
expect(response.type).to.eql(body.type);
|
||||
expect(response.url).to.eql(body.url);
|
||||
expect(response.enabled).to.eql(body.enabled);
|
||||
|
||||
await user.sync();
|
||||
|
||||
expect(user.webhooks).to.not.eql([]);
|
||||
|
||||
let webhook = user.webhooks[0];
|
||||
|
||||
expect(webhook.enabled).to.be.false;
|
||||
expect(webhook.type).to.eql('taskActivity');
|
||||
expect(webhook.url).to.eql(body.url);
|
||||
});
|
||||
|
||||
it('defaults taskActivity options', async () => {
|
||||
body.type = 'taskActivity';
|
||||
|
||||
let webhook = await user.post('/user/webhook', body);
|
||||
|
||||
expect(webhook.options).to.eql({
|
||||
created: false,
|
||||
updated: false,
|
||||
deleted: false,
|
||||
scored: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('can set taskActivity options', async () => {
|
||||
body.type = 'taskActivity';
|
||||
body.options = {
|
||||
created: true,
|
||||
updated: true,
|
||||
deleted: true,
|
||||
scored: false,
|
||||
};
|
||||
|
||||
let webhook = await user.post('/user/webhook', body);
|
||||
|
||||
expect(webhook.options).to.eql({
|
||||
created: true,
|
||||
updated: true,
|
||||
deleted: true,
|
||||
scored: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('discards extra properties in taskActivity options', async () => {
|
||||
body.type = 'taskActivity';
|
||||
body.options = {
|
||||
created: true,
|
||||
updated: true,
|
||||
deleted: true,
|
||||
scored: false,
|
||||
foo: 'bar',
|
||||
};
|
||||
|
||||
let webhook = await user.post('/user/webhook', body);
|
||||
|
||||
expect(webhook.options.foo).to.not.exist;
|
||||
expect(webhook.options).to.eql({
|
||||
created: true,
|
||||
updated: true,
|
||||
deleted: true,
|
||||
scored: false,
|
||||
});
|
||||
});
|
||||
|
||||
['created', 'updated', 'deleted', 'scored'].forEach((option) => {
|
||||
it(`requires taskActivity option ${option} to be a boolean`, async () => {
|
||||
body.type = 'taskActivity';
|
||||
body.options = {
|
||||
[option]: 'not a boolean',
|
||||
};
|
||||
|
||||
await expect(user.post('/user/webhook', body)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('webhookBooleanOption', { option }),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('can set groupChatReceived options', async () => {
|
||||
body.type = 'groupChatReceived';
|
||||
body.options = {
|
||||
groupId: generateUUID(),
|
||||
};
|
||||
|
||||
let webhook = await user.post('/user/webhook', body);
|
||||
|
||||
expect(webhook.options).to.eql({
|
||||
groupId: body.options.groupId,
|
||||
});
|
||||
});
|
||||
|
||||
it('groupChatReceived options requires a uuid for the groupId', async () => {
|
||||
body.type = 'groupChatReceived';
|
||||
body.options = {
|
||||
groupId: 'not-a-uuid',
|
||||
};
|
||||
|
||||
await expect(user.post('/user/webhook', body)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('groupIdRequired'),
|
||||
});
|
||||
});
|
||||
|
||||
it('discards extra properties in groupChatReceived options', async () => {
|
||||
body.type = 'groupChatReceived';
|
||||
body.options = {
|
||||
groupId: generateUUID(),
|
||||
foo: 'bar',
|
||||
};
|
||||
|
||||
let webhook = await user.post('/user/webhook', body);
|
||||
|
||||
expect(webhook.options.foo).to.not.exist;
|
||||
expect(webhook.options).to.eql({
|
||||
groupId: body.options.groupId,
|
||||
});
|
||||
});
|
||||
});
|
||||
132
test/api/v3/integration/webhook/PUT-user_update_webhook.test.js
Normal file
132
test/api/v3/integration/webhook/PUT-user_update_webhook.test.js
Normal file
@@ -0,0 +1,132 @@
|
||||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
import { v4 as generateUUID} from 'uuid';
|
||||
|
||||
describe('PUT /user/webhook/:id', () => {
|
||||
let user, webhookToUpdate;
|
||||
|
||||
beforeEach(async () => {
|
||||
user = await generateUser();
|
||||
|
||||
webhookToUpdate = await user.post('/user/webhook', {
|
||||
url: 'http://some-url.com',
|
||||
label: 'Original Label',
|
||||
enabled: true,
|
||||
type: 'taskActivity',
|
||||
options: { created: true, scored: true },
|
||||
});
|
||||
await user.post('/user/webhook', {
|
||||
url: 'http://some-other-url.com',
|
||||
enabled: false,
|
||||
});
|
||||
|
||||
await user.sync();
|
||||
});
|
||||
|
||||
it('returns an error if webhook with id does not exist', async () => {
|
||||
await expect(user.put('/user/webhook/id-that-does-not-exist')).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('noWebhookWithId', {id: 'id-that-does-not-exist'}),
|
||||
});
|
||||
});
|
||||
|
||||
it('returns an error if validation fails', async () => {
|
||||
await expect(user.put(`/user/webhook/${webhookToUpdate.id}`, { url: 'foo', enabled: true })).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: 'User validation failed',
|
||||
});
|
||||
});
|
||||
|
||||
it('updates a webhook', async () => {
|
||||
let url = 'http://a-new-url.com';
|
||||
let type = 'groupChatReceived';
|
||||
let label = 'New Label';
|
||||
let options = { groupId: generateUUID() };
|
||||
|
||||
await user.put(`/user/webhook/${webhookToUpdate.id}`, {url, type, options, label});
|
||||
|
||||
await user.sync();
|
||||
|
||||
let webhook = user.webhooks.find(hook => webhookToUpdate.id === hook.id);
|
||||
|
||||
expect(webhook.url).to.equal(url);
|
||||
expect(webhook.label).to.equal(label);
|
||||
expect(webhook.type).to.equal(type);
|
||||
expect(webhook.options).to.eql(options);
|
||||
});
|
||||
|
||||
it('returns the updated webhook', async () => {
|
||||
let url = 'http://a-new-url.com';
|
||||
let type = 'groupChatReceived';
|
||||
let options = { groupId: generateUUID() };
|
||||
|
||||
let response = await user.put(`/user/webhook/${webhookToUpdate.id}`, {url, type, options});
|
||||
|
||||
expect(response.url).to.eql(url);
|
||||
expect(response.type).to.eql(type);
|
||||
expect(response.options).to.eql(options);
|
||||
});
|
||||
|
||||
it('cannot update the id', async () => {
|
||||
let id = generateUUID();
|
||||
let url = 'http://a-new-url.com';
|
||||
|
||||
await user.put(`/user/webhook/${webhookToUpdate.id}`, {url, id});
|
||||
|
||||
await user.sync();
|
||||
|
||||
let webhook = user.webhooks.find(hook => webhookToUpdate.id === hook.id);
|
||||
|
||||
expect(webhook.id).to.eql(webhookToUpdate.id);
|
||||
expect(webhook.url).to.eql(url);
|
||||
});
|
||||
|
||||
it('can update taskActivity options', async () => {
|
||||
let type = 'taskActivity';
|
||||
let options = {
|
||||
updated: false,
|
||||
deleted: true,
|
||||
};
|
||||
|
||||
let webhook = await user.put(`/user/webhook/${webhookToUpdate.id}`, {type, options});
|
||||
|
||||
expect(webhook.options).to.eql({
|
||||
created: true, // starting value
|
||||
updated: false,
|
||||
deleted: true,
|
||||
scored: true, // default value
|
||||
});
|
||||
});
|
||||
|
||||
it('errors if taskActivity option is not a boolean', async () => {
|
||||
let type = 'taskActivity';
|
||||
let options = {
|
||||
created: 'not a boolean',
|
||||
updated: false,
|
||||
deleted: true,
|
||||
};
|
||||
|
||||
await expect(user.put(`/user/webhook/${webhookToUpdate.id}`, {type, options})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('webhookBooleanOption', { option: 'created' }),
|
||||
});
|
||||
});
|
||||
|
||||
it('errors if groupChatRecieved groupId option is not a uuid', async () => {
|
||||
let type = 'groupChatReceived';
|
||||
let options = {
|
||||
groupId: 'not-a-uuid',
|
||||
};
|
||||
|
||||
await expect(user.put(`/user/webhook/${webhookToUpdate.id}`, {type, options})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('groupIdRequired'),
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user