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
describe ( 'Webhooks API' , function ( ) {
2020-11-30 17:25:22 +03:00
let request ;
2021-03-04 03:06:16 +03:00
const API _VERSION = 'canary' ;
2018-10-11 21:52:38 +03:00
2020-11-30 17:25:22 +03:00
before ( async function ( ) {
await testUtils . startGhost ( ) ;
request = supertest . agent ( config . get ( 'url' ) ) ;
await localUtils . doAuth ( request , 'integrations' ) ;
2019-02-04 17:16:24 +03:00
} ) ;
2018-10-17 14:17:13 +03:00
2020-11-30 17:25:22 +03:00
it ( 'Can create a webhook' , async function ( ) {
const webhookData = {
2019-02-04 17:16:24 +03:00
event : 'test.create' ,
target _url : 'http://example.com/webhooks/test/extra/1' ,
name : 'test' ,
secret : 'thisissecret' ,
2021-03-04 03:06:16 +03:00
api _version : API _VERSION ,
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-11-30 17:25:22 +03:00
const res = await 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 )
2020-11-30 17:25:22 +03:00
. expect ( 201 ) ;
2019-02-04 17:16:24 +03:00
2020-11-30 17:25:22 +03:00
const jsonResponse = res . body ;
2019-02-04 17:16:24 +03:00
2020-11-30 17:25:22 +03:00
should . exist ( jsonResponse . webhooks ) ;
2019-02-04 17:16:24 +03:00
2020-11-30 17:25:22 +03:00
localUtils . API . checkResponse ( jsonResponse . webhooks [ 0 ] , 'webhook' ) ;
2020-09-14 13:33:37 +03:00
2020-11-30 17:25:22 +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 ) ;
jsonResponse . webhooks [ 0 ] . integration _id . should . equal ( webhookData . integration _id ) ;
should . not . exist ( res . headers . location ) ;
await 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 ) ;
2019-02-04 17:16:24 +03:00
} ) ;
2021-03-23 19:15:21 +03:00
it ( 'Fails nicely when creating an orphaned webhook' , async function ( ) {
const webhookData = {
event : 'test.create' ,
target _url : 'http://example.com/webhooks/test/extra/10' ,
name : 'test' ,
secret : 'thisissecret' ,
api _version : API _VERSION ,
integration _id : ` fake-integration `
} ;
const res = await 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 ) ;
const jsonResponse = res . body ;
should . exist ( jsonResponse . errors ) ;
jsonResponse . errors [ 0 ] . type . should . equal ( 'ValidationError' ) ;
jsonResponse . errors [ 0 ] . context . should . equal ( ` Validation failed for 'integration_id'. 'integration_id' value does not match any existing integration. ` ) ;
} ) ;
2020-11-30 17:25:22 +03:00
it ( 'Can edit a webhook' , async function ( ) {
2020-07-07 12:02:11 +03:00
let createdIntegration ;
let createdWebhook ;
2020-11-30 17:25:22 +03:00
const res = await 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'
} ]
} )
2020-11-30 17:25:22 +03:00
. expect ( 201 ) ;
[ createdIntegration ] = res . body . integrations ;
const res2 = await request . post ( localUtils . API . getApiQuery ( 'webhooks/' ) )
. set ( 'Origin' , config . get ( 'url' ) )
. send ( {
webhooks : [ {
name : 'Testing' ,
event : 'site.changed' ,
target _url : 'https://example.com/rebuild' ,
integration _id : createdIntegration . id
} ]
2020-07-07 12:02:11 +03:00
} )
2020-11-30 17:25:22 +03:00
. expect ( 201 ) ;
[ createdWebhook ] = res2 . body . webhooks ;
const res3 = await 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' ,
integration _id : 'ignore_me'
} ]
2020-07-07 12:02:11 +03:00
} )
2020-11-30 17:25:22 +03:00
. expect ( 200 )
. expect ( 'Content-Type' , /json/ )
. expect ( 'Cache-Control' , testUtils . cacheRules . private ) ;
const [ updatedWebhook ] = res3 . 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 ) ;
2019-02-04 17:16:24 +03:00
} ) ;
2018-10-11 21:52:38 +03:00
2020-11-30 17:25:22 +03:00
it ( 'Can delete a webhook' , async 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-11-30 17:25:22 +03:00
const res = await 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 )
2020-11-30 17:25:22 +03:00
. expect ( 201 ) ;
const jsonResponse = res . body ;
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
const res2 = await request . del ( localUtils . API . getApiQuery ( 'webhooks/' + jsonResponse . webhooks [ 0 ] . id + '/' ) )
. set ( 'Origin' , config . get ( 'url' ) )
. expect ( 204 ) ;
res2 . body . should . be . empty ( ) ;
2018-10-11 21:52:38 +03:00
} ) ;
} ) ;