2022-05-06 14:17:32 +03:00
|
|
|
const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
|
2022-05-19 08:50:44 +03:00
|
|
|
const {anyEtag, anyErrorId, anyObjectId, anyISODate, stringMatching, anyContentLength} = matchers;
|
2022-05-06 14:17:32 +03:00
|
|
|
|
|
|
|
const webhookMatcher = {
|
|
|
|
id: anyObjectId,
|
2022-05-06 14:40:55 +03:00
|
|
|
api_version: stringMatching(/v\d+\.\d+/),
|
2022-05-06 14:17:32 +03:00
|
|
|
integration_id: anyObjectId,
|
|
|
|
created_at: anyISODate,
|
|
|
|
updated_at: anyISODate
|
|
|
|
};
|
2019-09-20 18:02:45 +03:00
|
|
|
|
2018-10-11 21:52:38 +03:00
|
|
|
describe('Webhooks API', function () {
|
2022-05-06 14:17:32 +03:00
|
|
|
let agent;
|
|
|
|
let createdWebhookId;
|
|
|
|
let webhookData;
|
2018-10-11 21:52:38 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
before(async function () {
|
2022-05-06 14:17:32 +03:00
|
|
|
agent = await agentProvider.getAdminAPIAgent();
|
|
|
|
await fixtureManager.init('integrations');
|
|
|
|
await agent.loginAsOwner();
|
2018-10-17 14:17:13 +03:00
|
|
|
|
2022-05-06 14:17:32 +03:00
|
|
|
// create a webhook linked to a real integration
|
|
|
|
webhookData = {
|
2019-02-04 17:16:24 +03:00
|
|
|
event: 'test.create',
|
|
|
|
target_url: 'http://example.com/webhooks/test/extra/1',
|
|
|
|
name: 'test',
|
|
|
|
secret: 'thisissecret',
|
2022-05-06 14:17:32 +03:00
|
|
|
integration_id: fixtureManager.get('integrations', 0).id
|
2019-02-04 17:16:24 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-05-06 14:17:32 +03:00
|
|
|
it('Can create a webhook', async function () {
|
|
|
|
const {body} = await agent.post('/webhooks/')
|
|
|
|
.body({webhooks: [webhookData]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
// Note: No location header as there is no read method for webhooks
|
2022-05-19 08:50:44 +03:00
|
|
|
etag: anyEtag,
|
|
|
|
'content-length': anyContentLength
|
2021-03-23 19:15:21 +03:00
|
|
|
|
2022-05-06 14:17:32 +03:00
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
webhooks: [webhookMatcher]
|
|
|
|
});
|
2021-03-23 19:15:21 +03:00
|
|
|
|
2022-05-06 14:17:32 +03:00
|
|
|
// Store an id for use in future tests. Not the best pattern but does keep the tests readable.
|
|
|
|
createdWebhookId = body.webhooks[0].id;
|
2021-03-23 19:15:21 +03:00
|
|
|
});
|
|
|
|
|
2022-05-06 14:17:32 +03:00
|
|
|
it('Fails nicely when adding a duplicate webhook', async function () {
|
|
|
|
await agent.post('/webhooks/')
|
|
|
|
.body({webhooks: [webhookData]})
|
|
|
|
.expectStatus(422)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
2019-02-04 17:16:24 +03:00
|
|
|
})
|
2022-05-06 14:17:32 +03:00
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
2020-11-30 17:25:22 +03:00
|
|
|
}]
|
2022-05-06 14:17:32 +03:00
|
|
|
});
|
|
|
|
});
|
2020-11-30 17:25:22 +03:00
|
|
|
|
2022-05-06 14:17:32 +03:00
|
|
|
it('Fails nicely when creating an orphaned webhook', async function () {
|
|
|
|
await agent
|
|
|
|
.post('/webhooks/')
|
|
|
|
.body({webhooks: [{
|
|
|
|
event: 'test.create',
|
|
|
|
target_url: 'http://example.com/webhooks/test/extra/10',
|
|
|
|
name: 'test',
|
|
|
|
secret: 'thisissecret',
|
|
|
|
integration_id: `fake-integration`
|
|
|
|
}]})
|
|
|
|
.expectStatus(422)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
2020-11-30 17:25:22 +03:00
|
|
|
|
2022-05-06 14:17:32 +03:00
|
|
|
it('Can edit a webhook', async function () {
|
|
|
|
await agent.put(`/webhooks/${createdWebhookId}/`)
|
|
|
|
.body({
|
2020-11-30 17:25:22 +03:00
|
|
|
webhooks: [{
|
|
|
|
name: 'Edit Test',
|
2022-05-26 09:45:36 +03:00
|
|
|
event: 'member.added',
|
|
|
|
target_url: 'https://example.com/new-member',
|
2020-11-30 17:25:22 +03:00
|
|
|
integration_id: 'ignore_me'
|
|
|
|
}]
|
2020-07-07 12:02:11 +03:00
|
|
|
})
|
2022-05-06 14:17:32 +03:00
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
2022-05-19 08:50:44 +03:00
|
|
|
etag: anyEtag,
|
|
|
|
'content-length': anyContentLength
|
2022-05-06 14:17:32 +03:00
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
webhooks: [webhookMatcher]
|
|
|
|
});
|
2019-02-04 17:16:24 +03:00
|
|
|
});
|
2018-10-11 21:52:38 +03:00
|
|
|
|
2022-08-24 10:28:20 +03:00
|
|
|
it('Cannot edit a non-existent webhook', async function () {
|
|
|
|
await agent.put('/webhooks/abcd1234abcd1234abcd1234/')
|
|
|
|
.body({
|
|
|
|
webhooks: [{
|
|
|
|
name: 'Edit Test',
|
|
|
|
event: 'member.added',
|
|
|
|
target_url: 'https://example.com/new-member',
|
|
|
|
integration_id: 'ignore_me'
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(404)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can delete a webhook', async function () {
|
2022-05-06 14:17:32 +03:00
|
|
|
await agent
|
|
|
|
.delete(`/webhooks/${createdWebhookId}/`)
|
|
|
|
.expectStatus(204)
|
|
|
|
.expectEmptyBody()
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
2018-10-11 21:52:38 +03:00
|
|
|
});
|
2022-08-24 10:28:20 +03:00
|
|
|
|
|
|
|
it('Cannot delete a non-existent webhook', async function () {
|
|
|
|
await agent
|
|
|
|
.delete('/webhooks/abcd1234abcd1234abcd1234/')
|
|
|
|
.expectStatus(404)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
2018-10-11 21:52:38 +03:00
|
|
|
});
|