-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from DFE-Digital/add-webhook-for-frameworks
Add webhook service for published frameworks
- Loading branch information
Showing
7 changed files
with
155 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const send = async (payload) => { | ||
const https = require('https') | ||
|
||
const data = JSON.stringify(payload) | ||
|
||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Content-Length': Buffer.byteLength(data), | ||
'Authorization': `Token ${process.env.GHBS_WEBHOOK_SECRET}` | ||
} | ||
} | ||
|
||
const req = https.request(process.env.GHBS_WEBHOOK_ENDPOINT, options, res => { | ||
console.log(`statusCode: ${res.statusCode}`) | ||
}) | ||
|
||
req.on('error', error => { | ||
console.error(error) | ||
}) | ||
|
||
req.write(data) | ||
req.end() | ||
} | ||
|
||
module.exports = { send } |
44 changes: 44 additions & 0 deletions
44
api/test/api.structure.populateFrameworkReferences.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const mongoose = require('mongoose') | ||
const chai = require('chai') | ||
|
||
const testData = require('./testdata/frameworks.json') | ||
const testProviderData = require('./testdata/providers.json') | ||
const testCategoryData = require('./testdata/categories.json') | ||
|
||
const expect = chai.expect | ||
const setup = require('./setup') | ||
|
||
describe('structureController', () => { | ||
let structureController = require('../controllers/structure') | ||
let testProviders = [] | ||
let testCategories = [] | ||
let testFrameworks = [] | ||
|
||
before(async () => { | ||
const setupDone = await setup() | ||
await setupDone.helpers.removeAllRecords('framework') | ||
await setupDone.helpers.removeAllRecords('provider') | ||
testProviders.push(await setupDone.helpers.createRecord('provider', testProviderData.spectre)) | ||
await setupDone.helpers.removeAllRecords('category') | ||
testCategories.push(await setupDone.helpers.createRecord('category', testCategoryData.construction)) | ||
structureController = structureController(setupDone.dataSource) | ||
|
||
const framework = JSON.parse(JSON.stringify(testData.builders)) | ||
framework.cat = mongoose.Types.ObjectId(testCategories[0].id) | ||
framework.provider = mongoose.Types.ObjectId(testProviders[0].id) | ||
testFrameworks.push(await setupDone.helpers.createRecord('framework', framework)) | ||
}) | ||
|
||
describe('populateFrameworkReferences', () => { | ||
it('should return referenced objects and remove ids', done => { | ||
let result = structureController.populateFrameworkReferences(testFrameworks.map(f => f.toJSON()), testCategories.map(c => c.toJSON()), testProviders.map(p => p.toJSON())) | ||
expect(result.length).to.equal(1) | ||
expect(result[0]).to.not.have.property('_id') | ||
expect(result[0].cat).to.be.an('object') | ||
expect(result[0].cat).to.not.have.property('_id') | ||
expect(result[0].provider).to.be.an('object') | ||
expect(result[0].provider).to.not.have.property('_id') | ||
done() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const sinon = require('sinon') | ||
|
||
const testData = require('./testdata/frameworks.json') | ||
|
||
describe('webhookService', () => { | ||
let https | ||
let webhookService | ||
let testFramework | ||
let stub | ||
let env | ||
|
||
before(() => { | ||
https = require('https') | ||
stub = sinon.stub(https, 'request').returns({ | ||
on() {}, | ||
write() {}, | ||
end() {} | ||
}) | ||
webhookService = require('../services/webhookService') | ||
testFramework = testData.builders | ||
env = process.env | ||
process.env = { | ||
GHBS_WEBHOOK_SECRET: 'secret', | ||
GHBS_WEBHOOK_ENDPOINT: 'endpoint' | ||
} | ||
}) | ||
|
||
describe('send', () => { | ||
it('sends a post request to the webhook endpoint', () => { | ||
webhookService.send(testFramework) | ||
|
||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Content-Length': 664, | ||
'Authorization': 'Token secret' | ||
} | ||
} | ||
|
||
sinon.assert.calledOnce(https.request) | ||
sinon.assert.calledWith(stub, 'endpoint', options, sinon.match.any) | ||
}) | ||
}) | ||
|
||
after(() => { | ||
process.env = env | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters