0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Refactored webhook acceptance test suite

no issue

- Refactored a test as it was using outdated `done()` convention instead of relying on promises
- Removed linting warnings for variable shadowing
- This is precursor work for webhook regression test updates needed in near future work
This commit is contained in:
Nazar Gargol 2020-07-07 21:02:11 +12:00
parent 8d989bd3c3
commit 256b407dd2

View file

@ -21,7 +21,7 @@ describe('Webhooks API', function () {
}); });
}); });
it('Can creates a webhook', function (done) { it('Can creates a webhook', function () {
let webhookData = { let webhookData = {
event: 'test.create', event: 'test.create',
target_url: 'http://example.com/webhooks/test/extra/1', target_url: 'http://example.com/webhooks/test/extra/1',
@ -30,17 +30,13 @@ describe('Webhooks API', function () {
api_version: 'v2' api_version: 'v2'
}; };
request.post(localUtils.API.getApiQuery('webhooks/')) return request.post(localUtils.API.getApiQuery('webhooks/'))
.set('Origin', config.get('url')) .set('Origin', config.get('url'))
.send({webhooks: [webhookData]}) .send({webhooks: [webhookData]})
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private) .expect('Cache-Control', testUtils.cacheRules.private)
.expect(201) .expect(201)
.end(function (err, res) { .then((res) => {
if (err) {
return done(err);
}
const jsonResponse = res.body; const jsonResponse = res.body;
should.exist(jsonResponse.webhooks); should.exist(jsonResponse.webhooks);
@ -52,13 +48,14 @@ describe('Webhooks API', function () {
jsonResponse.webhooks[0].secret.should.equal(webhookData.secret); jsonResponse.webhooks[0].secret.should.equal(webhookData.secret);
jsonResponse.webhooks[0].name.should.equal(webhookData.name); jsonResponse.webhooks[0].name.should.equal(webhookData.name);
jsonResponse.webhooks[0].api_version.should.equal(webhookData.api_version); jsonResponse.webhooks[0].api_version.should.equal(webhookData.api_version);
done();
}); });
}); });
it('Can edit a webhook', function (done) { it('Can edit a webhook', function () {
request.post(localUtils.API.getApiQuery('integrations/')) let createdIntegration;
let createdWebhook;
return request.post(localUtils.API.getApiQuery('integrations/'))
.set('Origin', config.get('url')) .set('Origin', config.get('url'))
.send({ .send({
integrations: [{ integrations: [{
@ -66,14 +63,10 @@ describe('Webhooks API', function () {
}] }]
}) })
.expect(201) .expect(201)
.end(function (err, {body}) { .then(({body}) => {
if (err) { [createdIntegration] = body.integrations;
return done(err);
}
const [createdIntegration] = body.integrations; return request.post(localUtils.API.getApiQuery('webhooks/'))
request.post(localUtils.API.getApiQuery('webhooks/'))
.set('Origin', config.get('url')) .set('Origin', config.get('url'))
.send({ .send({
webhooks: [{ webhooks: [{
@ -84,40 +77,35 @@ describe('Webhooks API', function () {
}] }]
}) })
.expect(201) .expect(201)
.end(function (err, {body}) { })
if (err) { .then(({body}) => {
return done(err); [createdWebhook] = body.webhooks;
}
const [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',
target_url: 'https://example.com/new-subscriber'
}]
})
.expect(200)
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private);
})
.then(({body}) => {
const [updatedWebhook] = body.webhooks;
request.put(localUtils.API.getApiQuery(`webhooks/${createdWebhook.id}/`)) should.equal(updatedWebhook.id, createdWebhook.id);
.set('Origin', config.get('url')) should.equal(updatedWebhook.name, 'Edit Test');
.send({ should.equal(updatedWebhook.event, 'subscriber.added');
webhooks: [{ should.equal(updatedWebhook.target_url, 'https://example.com/new-subscriber');
name: 'Edit Test', should.equal(updatedWebhook.integration_id, createdIntegration.id);
event: 'subscriber.added',
target_url: 'https://example.com/new-subscriber'
}]
})
.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);
done();
});
});
}); });
}); });
it('Can delete a webhook', function (done) { it('Can delete a webhook', function () {
const newWebhook = { const newWebhook = {
event: 'test.create', event: 'test.create',
// a different target_url from above is needed to avoid an "already exists" error // a different target_url from above is needed to avoid an "already exists" error
@ -125,17 +113,13 @@ describe('Webhooks API', function () {
}; };
// create the webhook that is to be deleted // create the webhook that is to be deleted
request.post(localUtils.API.getApiQuery('webhooks/')) return request.post(localUtils.API.getApiQuery('webhooks/'))
.set('Origin', config.get('url')) .set('Origin', config.get('url'))
.send({webhooks: [newWebhook]}) .send({webhooks: [newWebhook]})
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private) .expect('Cache-Control', testUtils.cacheRules.private)
.expect(201) .expect(201)
.end(function (err, res) { .then((res) => {
if (err) {
return done(err);
}
const location = res.headers.location; const location = res.headers.location;
const jsonResponse = res.body; const jsonResponse = res.body;
@ -145,18 +129,12 @@ describe('Webhooks API', function () {
jsonResponse.webhooks[0].target_url.should.equal(newWebhook.target_url); jsonResponse.webhooks[0].target_url.should.equal(newWebhook.target_url);
// begin delete test // begin delete test
request.del(localUtils.API.getApiQuery('webhooks/' + jsonResponse.webhooks[0].id + '/')) return request.del(localUtils.API.getApiQuery('webhooks/' + jsonResponse.webhooks[0].id + '/'))
.set('Origin', config.get('url')) .set('Origin', config.get('url'))
.expect(204) .expect(204);
.end(function (err, res) { })
if (err) { .then((res) => {
return done(err); res.body.should.be.empty();
}
res.body.should.be.empty();
done();
});
}); });
}); });
}); });