feat(webhook): Created webhook at startup

This commit is contained in:
Daniel Gonzalez
2021-07-28 18:32:54 -04:00
parent afb9e98127
commit 297320eb89
4 changed files with 120 additions and 1 deletions
+12
View File
@@ -15,6 +15,10 @@ const config = require('../config') // this first.
// const IPFSLib = require('../src/lib/ipfs')
const AdminLib = require('../src/adapters/admin')
const adminLib = new AdminLib()
const WebHookLib = require('../src/adapters/webhook')
const webHookLib = new WebHookLib()
// const JSONRPC = require('../src/rpc')
// const rpc = new JSONRPC()
@@ -72,6 +76,14 @@ async function startServer () {
const success = await adminLib.createSystemUser()
if (success) console.log('System admin user created.')
// Create webhook
try {
await webHookLib.createWebHook('https://test.com/my-webhook')
console.log('Webhook created')
} catch (error) {
console.log('Webhook cant be created')
}
return app
}
// startServer()
+3 -1
View File
@@ -35,5 +35,7 @@ module.exports = {
name: 'Permissionless Software Foundation',
url: 'https://PSFoundation.cash'
}
}
},
// P2WDB webhook endpoint
webhookService: process.env.WEBHOOKSERVICE ? process.env.WEBHOOKSERVICE : 'http://localhost:5001/webhook'
}
+37
View File
@@ -0,0 +1,37 @@
const config = require('../../config')
const axios = require('axios')
let _this
class WebHook {
constructor () {
_this = this
_this.config = config
_this.axios = axios
}
// REST petition to create a webhook in p2wdb-service
async createWebHook (url) {
try {
if (!url || typeof url !== 'string') {
throw new Error('url must be a string')
}
const endpoint = _this.config.webhookService
const obj = {
appId: 'torlist',
url
}
const result = await axios.post(endpoint, obj)
return result.data
} catch (err) {
console.log('Error in adapters/webhook/createWebHook()')
throw err
}
}
}
module.exports = WebHook
@@ -0,0 +1,68 @@
/*
Unit tests for the webhook adapter.
*/
const assert = require('chai').assert
const sinon = require('sinon')
// Set the environment variable to signal this is a test.
process.env.SVC_ENV = 'test'
const WebHook = require('../../../src/adapters/webhook')
describe('#Webhook-Adapter', () => {
let uut
let sandbox
beforeEach(async () => {
uut = new WebHook()
sandbox = sinon.createSandbox()
})
afterEach(() => sandbox.restore())
describe('#createWebHook', () => {
it('should throw error if input is not provided', async () => {
try {
await uut.createWebHook()
assert.fail('unexpected code path')
} catch (err) {
assert.include(err.message, 'url must be a string')
}
})
it('should throw error if input is not string', async () => {
try {
await uut.createWebHook(1)
assert.fail('unexpected code path')
} catch (err) {
assert.include(err.message, 'url must be a string')
}
})
it('should handle error', async () => {
try {
sandbox
.stub(uut.axios, 'post')
.throws(new Error('test error'))
await uut.createWebHook('https://test.com/my-webhook')
assert.fail('unexpected code path')
} catch (err) {
assert.include(err.message, 'test error')
}
})
it('should return response', async () => {
sandbox
.stub(uut.axios, 'post')
.resolves({ data: { success: true, id: '61018c8c9a71973a596cdccb' } })
const url = 'https://test.com/my-webhook'
const result = await uut.createWebHook(url)
assert.isObject(result)
assert.property(result, 'success')
assert.property(result, 'id')
assert.isBoolean(result.success)
assert.isString(result.id)
})
})
})