From 256b407dd2d4f48a41f31922115c5b1fc4666ccb Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Tue, 7 Jul 2020 21:02:11 +1200 Subject: [PATCH] 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 --- test/api-acceptance/admin/webhooks_spec.js | 106 ++++++++------------- 1 file changed, 42 insertions(+), 64 deletions(-) diff --git a/test/api-acceptance/admin/webhooks_spec.js b/test/api-acceptance/admin/webhooks_spec.js index b7b0a0726e..35834c16a2 100644 --- a/test/api-acceptance/admin/webhooks_spec.js +++ b/test/api-acceptance/admin/webhooks_spec.js @@ -21,7 +21,7 @@ describe('Webhooks API', function () { }); }); - it('Can creates a webhook', function (done) { + it('Can creates a webhook', function () { let webhookData = { event: 'test.create', target_url: 'http://example.com/webhooks/test/extra/1', @@ -30,17 +30,13 @@ describe('Webhooks API', function () { api_version: 'v2' }; - request.post(localUtils.API.getApiQuery('webhooks/')) + 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(201) - .end(function (err, res) { - if (err) { - return done(err); - } - + .then((res) => { const jsonResponse = res.body; should.exist(jsonResponse.webhooks); @@ -52,13 +48,14 @@ describe('Webhooks API', function () { 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); - - done(); }); }); - it('Can edit a webhook', function (done) { - request.post(localUtils.API.getApiQuery('integrations/')) + it('Can edit a webhook', function () { + let createdIntegration; + let createdWebhook; + + return request.post(localUtils.API.getApiQuery('integrations/')) .set('Origin', config.get('url')) .send({ integrations: [{ @@ -66,14 +63,10 @@ describe('Webhooks API', function () { }] }) .expect(201) - .end(function (err, {body}) { - if (err) { - return done(err); - } + .then(({body}) => { + [createdIntegration] = body.integrations; - const [createdIntegration] = body.integrations; - - request.post(localUtils.API.getApiQuery('webhooks/')) + return request.post(localUtils.API.getApiQuery('webhooks/')) .set('Origin', config.get('url')) .send({ webhooks: [{ @@ -84,40 +77,35 @@ describe('Webhooks API', function () { }] }) .expect(201) - .end(function (err, {body}) { - if (err) { - return done(err); - } + }) + .then(({body}) => { + [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}/`)) - .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; - - 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(); - }); - }); + 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); }); }); - it('Can delete a webhook', function (done) { + it('Can delete a webhook', function () { const newWebhook = { event: 'test.create', // 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 - request.post(localUtils.API.getApiQuery('webhooks/')) + return request.post(localUtils.API.getApiQuery('webhooks/')) .set('Origin', config.get('url')) .send({webhooks: [newWebhook]}) .expect('Content-Type', /json/) .expect('Cache-Control', testUtils.cacheRules.private) .expect(201) - .end(function (err, res) { - if (err) { - return done(err); - } - + .then((res) => { const location = res.headers.location; const jsonResponse = res.body; @@ -145,18 +129,12 @@ describe('Webhooks API', function () { jsonResponse.webhooks[0].target_url.should.equal(newWebhook.target_url); // 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')) - .expect(204) - .end(function (err, res) { - if (err) { - return done(err); - } - - res.body.should.be.empty(); - - done(); - }); + .expect(204); + }) + .then((res) => { + res.body.should.be.empty(); }); }); });