2018-10-11 21:52:38 +03:00
|
|
|
const should = require('should');
|
|
|
|
const supertest = require('supertest');
|
2019-09-20 18:02:45 +03:00
|
|
|
const testUtils = require('../../utils');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../core/shared/config');
|
2018-10-11 21:52:38 +03:00
|
|
|
const localUtils = require('./utils');
|
2019-09-20 18:02:45 +03:00
|
|
|
|
2018-10-11 21:52:38 +03:00
|
|
|
const ghost = testUtils.startGhost;
|
|
|
|
let request;
|
|
|
|
|
|
|
|
describe('Webhooks API', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let ghostServer;
|
2018-10-11 21:52:38 +03:00
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
before(function () {
|
|
|
|
return ghost()
|
|
|
|
.then(function (_ghostServer) {
|
|
|
|
ghostServer = _ghostServer;
|
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
})
|
|
|
|
.then(function () {
|
2020-09-24 04:55:25 +03:00
|
|
|
return localUtils.doAuth(request, 'integrations');
|
2018-10-11 21:52:38 +03:00
|
|
|
});
|
2019-02-04 17:16:24 +03:00
|
|
|
});
|
2018-10-17 14:17:13 +03:00
|
|
|
|
2020-09-24 04:55:25 +03:00
|
|
|
it('Can create a webhook', function () {
|
2019-02-04 17:16:24 +03:00
|
|
|
let webhookData = {
|
|
|
|
event: 'test.create',
|
|
|
|
target_url: 'http://example.com/webhooks/test/extra/1',
|
|
|
|
name: 'test',
|
|
|
|
secret: 'thisissecret',
|
2020-07-17 08:37:14 +03:00
|
|
|
api_version: 'v2',
|
2020-09-24 04:55:25 +03:00
|
|
|
integration_id: testUtils.DataGenerator.Content.integrations[0].id
|
2019-02-04 17:16:24 +03:00
|
|
|
};
|
|
|
|
|
2020-07-07 12:02:11 +03:00
|
|
|
return request.post(localUtils.API.getApiQuery('webhooks/'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({webhooks: [webhookData]})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(201)
|
2020-07-07 12:02:11 +03:00
|
|
|
.then((res) => {
|
2020-04-29 18:44:27 +03:00
|
|
|
const jsonResponse = res.body;
|
2019-02-04 17:16:24 +03:00
|
|
|
|
|
|
|
should.exist(jsonResponse.webhooks);
|
|
|
|
|
2019-02-07 22:01:47 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.webhooks[0], 'webhook');
|
2019-02-04 17:16:24 +03:00
|
|
|
|
|
|
|
jsonResponse.webhooks[0].event.should.equal(webhookData.event);
|
|
|
|
jsonResponse.webhooks[0].target_url.should.equal(webhookData.target_url);
|
|
|
|
jsonResponse.webhooks[0].secret.should.equal(webhookData.secret);
|
|
|
|
jsonResponse.webhooks[0].name.should.equal(webhookData.name);
|
|
|
|
jsonResponse.webhooks[0].api_version.should.equal(webhookData.api_version);
|
2020-07-17 08:37:14 +03:00
|
|
|
jsonResponse.webhooks[0].integration_id.should.equal(webhookData.integration_id);
|
2020-09-14 13:33:37 +03:00
|
|
|
|
|
|
|
should.not.exist(res.headers.location);
|
2020-09-24 04:55:25 +03:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return request.post(localUtils.API.getApiQuery('webhooks/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({webhooks: [webhookData]})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(422);
|
2018-10-17 14:17:13 +03:00
|
|
|
});
|
2019-02-04 17:16:24 +03:00
|
|
|
});
|
|
|
|
|
2020-07-07 12:02:11 +03:00
|
|
|
it('Can edit a webhook', function () {
|
|
|
|
let createdIntegration;
|
|
|
|
let createdWebhook;
|
|
|
|
|
|
|
|
return request.post(localUtils.API.getApiQuery('integrations/'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
name: 'Rubbish Integration Name'
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expect(201)
|
2020-07-07 12:02:11 +03:00
|
|
|
.then(({body}) => {
|
|
|
|
[createdIntegration] = body.integrations;
|
2018-10-11 21:52:38 +03:00
|
|
|
|
2020-07-07 12:02:11 +03:00
|
|
|
return request.post(localUtils.API.getApiQuery('webhooks/'))
|
2018-10-19 20:35:55 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
2019-02-04 17:16:24 +03:00
|
|
|
webhooks: [{
|
|
|
|
name: 'Testing',
|
|
|
|
event: 'site.changed',
|
|
|
|
target_url: 'https://example.com/rebuild',
|
|
|
|
integration_id: createdIntegration.id
|
2018-10-19 20:35:55 +03:00
|
|
|
}]
|
|
|
|
})
|
2020-07-07 12:07:16 +03:00
|
|
|
.expect(201);
|
2020-07-07 12:02:11 +03:00
|
|
|
})
|
|
|
|
.then(({body}) => {
|
|
|
|
[createdWebhook] = body.webhooks;
|
|
|
|
|
|
|
|
return request.put(localUtils.API.getApiQuery(`webhooks/${createdWebhook.id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
webhooks: [{
|
|
|
|
name: 'Edit Test',
|
|
|
|
event: 'subscriber.added',
|
2020-09-24 07:24:02 +03:00
|
|
|
target_url: 'https://example.com/new-subscriber',
|
|
|
|
integration_id: 'ignore_me'
|
2020-07-07 12:02:11 +03:00
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private);
|
|
|
|
})
|
|
|
|
.then(({body}) => {
|
|
|
|
const [updatedWebhook] = body.webhooks;
|
|
|
|
|
|
|
|
should.equal(updatedWebhook.id, createdWebhook.id);
|
|
|
|
should.equal(updatedWebhook.name, 'Edit Test');
|
|
|
|
should.equal(updatedWebhook.event, 'subscriber.added');
|
|
|
|
should.equal(updatedWebhook.target_url, 'https://example.com/new-subscriber');
|
|
|
|
should.equal(updatedWebhook.integration_id, createdIntegration.id);
|
2018-10-19 20:35:55 +03:00
|
|
|
});
|
2019-02-04 17:16:24 +03:00
|
|
|
});
|
2018-10-11 21:52:38 +03:00
|
|
|
|
2020-07-07 12:02:11 +03:00
|
|
|
it('Can delete a webhook', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const newWebhook = {
|
2019-02-04 17:16:24 +03:00
|
|
|
event: 'test.create',
|
|
|
|
// a different target_url from above is needed to avoid an "already exists" error
|
2020-07-17 08:37:14 +03:00
|
|
|
target_url: 'http://example.com/webhooks/test/2',
|
2020-09-24 04:55:25 +03:00
|
|
|
integration_id: testUtils.DataGenerator.Content.integrations[0].id
|
2019-02-04 17:16:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// create the webhook that is to be deleted
|
2020-07-07 12:02:11 +03:00
|
|
|
return request.post(localUtils.API.getApiQuery('webhooks/'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({webhooks: [newWebhook]})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(201)
|
2020-07-07 12:02:11 +03:00
|
|
|
.then((res) => {
|
2020-04-29 18:44:27 +03:00
|
|
|
const jsonResponse = res.body;
|
2019-02-04 17:16:24 +03:00
|
|
|
|
|
|
|
should.exist(jsonResponse.webhooks);
|
|
|
|
localUtils.API.checkResponse(jsonResponse.webhooks[0], 'webhook');
|
|
|
|
jsonResponse.webhooks[0].event.should.equal(newWebhook.event);
|
|
|
|
jsonResponse.webhooks[0].target_url.should.equal(newWebhook.target_url);
|
|
|
|
|
|
|
|
// begin delete test
|
2020-07-07 12:02:11 +03:00
|
|
|
return request.del(localUtils.API.getApiQuery('webhooks/' + jsonResponse.webhooks[0].id + '/'))
|
2018-10-11 21:52:38 +03:00
|
|
|
.set('Origin', config.get('url'))
|
2020-07-07 12:02:11 +03:00
|
|
|
.expect(204);
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
res.body.should.be.empty();
|
2018-10-11 21:52:38 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|