Fix bug where updated webhook options failed to save (fixes #12336) (#12342)

* Fix bug where updated webhook options failed to save

This bug was caused by Mongoose not creating getters/setters for array
elements (https://mongoosejs.com/docs/faq.html#array-changes-not-saved).
So, although the webhook was being updated properly, Mongoose was not
actually committing it to the database. Telling Mongoose that the array
of webhooks has changed via `markModified` fixes the issue.

Additionally, the relevant API test case was only checking whether or
not the webhook returned from the PUT endpoint matched the expected
update. Since the endpoint was returning the updated webhook without
querying the database again, this test case would pass. It has been
updated to check both the returned webhook as well as the version of the
webhook that is saved to the database against the expected. In other
words:

`assert returned === saved === expected`

Fixes #12336

* Call markModified on webhook.options instead of user.webhooks

This tells Mongoose that only the modified webhook's options changed
instead of telling it that the entire user.webhooks array changed,
saving a costly DB update.
This commit is contained in:
Robert Whitaker
2020-07-03 10:48:45 -04:00
committed by GitHub
parent 680e86d2c9
commit af1d13d3a2
2 changed files with 20 additions and 9 deletions

View File

@@ -127,19 +127,26 @@ describe('PUT /user/webhook/:id', () => {
it('can update taskActivity options', async () => {
const type = 'taskActivity';
const options = {
checklistScored: true,
updated: false,
deleted: true,
scored: false,
};
const webhook = await user.put(`/user/webhook/${webhookToUpdate.id}`, { type, options });
expect(webhook.options).to.eql({
checklistScored: false, // starting value
const expected = {
checklistScored: true,
created: true, // starting value
updated: false,
deleted: true,
scored: true, // default value
});
deleted: false, // starting value
scored: false,
};
const returnedWebhook = await user.put(`/user/webhook/${webhookToUpdate.id}`, { type, options });
await user.sync();
const savedWebhook = user.webhooks.find(hook => webhookToUpdate.id === hook.id);
expect(returnedWebhook.options).to.eql(expected);
expect(savedWebhook.options).to.eql(expected);
});
it('errors if taskActivity option is not a boolean', async () => {