0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/test/e2e-api/admin/webhooks.test.js
Hannah Wolfe 401fe46380
Changed webhooks to store safe version
refs: https://github.com/TryGhost/Toolbox/issues/229

- We're getting rid of the concept of api versions inside of Ghost
- Instead of storing the supposed api version a webhook was created with, store the current ghost version
- This way we can determine if anything signicant changes in future and we need to update webhooks or something
2022-05-06 12:43:14 +01:00

112 lines
3.4 KiB
JavaScript

const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {anyEtag, anyErrorId, anyObjectId, anyISODate, stringMatching} = matchers;
const webhookMatcher = {
id: anyObjectId,
api_version: stringMatching(/v\d+\.\d+/),
integration_id: anyObjectId,
created_at: anyISODate,
updated_at: anyISODate
};
describe('Webhooks API', function () {
let agent;
let createdWebhookId;
let webhookData;
before(async function () {
agent = await agentProvider.getAdminAPIAgent();
await fixtureManager.init('integrations');
await agent.loginAsOwner();
// create a webhook linked to a real integration
webhookData = {
event: 'test.create',
target_url: 'http://example.com/webhooks/test/extra/1',
name: 'test',
secret: 'thisissecret',
integration_id: fixtureManager.get('integrations', 0).id
};
});
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
})
.matchBodySnapshot({
webhooks: [webhookMatcher]
});
// Store an id for use in future tests. Not the best pattern but does keep the tests readable.
createdWebhookId = body.webhooks[0].id;
});
it('Fails nicely when adding a duplicate webhook', async function () {
await agent.post('/webhooks/')
.body({webhooks: [webhookData]})
.expectStatus(422)
.matchHeaderSnapshot({
etag: anyEtag
})
.matchBodySnapshot({
errors: [{
id: anyErrorId
}]
});
});
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
}]
});
});
it('Can edit a webhook', async function () {
await agent.put(`/webhooks/${createdWebhookId}/`)
.body({
webhooks: [{
name: 'Edit Test',
event: 'subscriber.added',
target_url: 'https://example.com/new-subscriber',
integration_id: 'ignore_me'
}]
})
.expectStatus(200)
.matchHeaderSnapshot({
etag: anyEtag
})
.matchBodySnapshot({
webhooks: [webhookMatcher]
});
});
it('Can delete a webhook', async function () {
await agent
.delete(`/webhooks/${createdWebhookId}/`)
.expectStatus(204)
.expectEmptyBody()
.matchHeaderSnapshot({
etag: anyEtag
});
});
});