2022-05-06 12:17:32 +01:00
|
|
|
const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
|
2022-05-06 12:40:55 +01:00
|
|
|
const {anyEtag, anyErrorId, anyObjectId, anyISODate, stringMatching} = matchers;
|
2022-05-06 12:17:32 +01:00
|
|
|
|
|
|
|
const webhookMatcher = {
|
|
|
|
id: anyObjectId,
|
2022-05-06 12:40:55 +01:00
|
|
|
api_version: stringMatching(/v\d+\.\d+/),
|
2022-05-06 12:17:32 +01:00
|
|
|
integration_id: anyObjectId,
|
|
|
|
created_at: anyISODate,
|
|
|
|
updated_at: anyISODate
|
|
|
|
};
|
2019-09-20 17:02:45 +02:00
|
|
|
|
2018-10-12 00:22:38 +05:30
|
|
|
describe('Webhooks API', function () {
|
2022-05-06 12:17:32 +01:00
|
|
|
let agent;
|
|
|
|
let createdWebhookId;
|
|
|
|
let webhookData;
|
2018-10-12 00:22:38 +05:30
|
|
|
|
2020-11-30 14:25:22 +00:00
|
|
|
before(async function () {
|
2022-05-06 12:17:32 +01:00
|
|
|
agent = await agentProvider.getAdminAPIAgent();
|
|
|
|
await fixtureManager.init('integrations');
|
|
|
|
await agent.loginAsOwner();
|
2018-10-17 16:47:13 +05:30
|
|
|
|
2022-05-06 12:17:32 +01:00
|
|
|
// create a webhook linked to a real integration
|
|
|
|
webhookData = {
|
2019-02-04 15:16:24 +01:00
|
|
|
event: 'test.create',
|
|
|
|
target_url: 'http://example.com/webhooks/test/extra/1',
|
|
|
|
name: 'test',
|
|
|
|
secret: 'thisissecret',
|
2022-05-06 12:17:32 +01:00
|
|
|
integration_id: fixtureManager.get('integrations', 0).id
|
2019-02-04 15:16:24 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-05-06 12:17:32 +01: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
|
|
|
|
etag: anyEtag
|
2021-03-23 17:15:21 +01:00
|
|
|
|
2022-05-06 12:17:32 +01:00
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
webhooks: [webhookMatcher]
|
|
|
|
});
|
2021-03-23 17:15:21 +01:00
|
|
|
|
2022-05-06 12:17:32 +01: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 17:15:21 +01:00
|
|
|
});
|
|
|
|
|
2022-05-06 12:17:32 +01: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 15:16:24 +01:00
|
|
|
})
|
2022-05-06 12:17:32 +01:00
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
2020-11-30 14:25:22 +00:00
|
|
|
}]
|
2022-05-06 12:17:32 +01:00
|
|
|
});
|
|
|
|
});
|
2020-11-30 14:25:22 +00:00
|
|
|
|
2022-05-06 12:17:32 +01: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 14:25:22 +00:00
|
|
|
|
2022-05-06 12:17:32 +01:00
|
|
|
it('Can edit a webhook', async function () {
|
|
|
|
await agent.put(`/webhooks/${createdWebhookId}/`)
|
|
|
|
.body({
|
2020-11-30 14:25:22 +00:00
|
|
|
webhooks: [{
|
|
|
|
name: 'Edit Test',
|
|
|
|
event: 'subscriber.added',
|
|
|
|
target_url: 'https://example.com/new-subscriber',
|
|
|
|
integration_id: 'ignore_me'
|
|
|
|
}]
|
2020-07-07 21:02:11 +12:00
|
|
|
})
|
2022-05-06 12:17:32 +01:00
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
webhooks: [webhookMatcher]
|
|
|
|
});
|
2019-02-04 15:16:24 +01:00
|
|
|
});
|
2018-10-12 00:22:38 +05:30
|
|
|
|
2020-11-30 14:25:22 +00:00
|
|
|
it('Can delete a webhook', async function () {
|
2022-05-06 12:17:32 +01:00
|
|
|
await agent
|
|
|
|
.delete(`/webhooks/${createdWebhookId}/`)
|
|
|
|
.expectStatus(204)
|
|
|
|
.expectEmptyBody()
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
2018-10-12 00:22:38 +05:30
|
|
|
});
|
|
|
|
});
|