diff --git a/docs/devguide/docs/swagger-docs.yaml b/docs/devguide/docs/swagger-docs.yaml index a4d967c5a..8f0e4c20a 100644 --- a/docs/devguide/docs/swagger-docs.yaml +++ b/docs/devguide/docs/swagger-docs.yaml @@ -1613,6 +1613,41 @@ paths: application/json: schema: $ref: '#/components/schemas/error_response' + /v1/webhooks/{webhook_id}/test: + get: + operationId: test-a-webhook + tags: + - Webhooks + summary: Tests that the webhook is configure correctly + description: Tests that the webhook is configure correctly by sending a message to the target webhook url. + parameters: + - in: path + name: webhook_id + description: The webhook id. + required: true + schema: + type: string + format: uuid + example: 4bf5d7ab-f310-4a64-8ec2-d65c06188ec1 + responses: + '200': + description: Success + content: + application/json: + schema: + $ref: '#/components/schemas/test_webhook_response' + '404': + description: Not found + content: + application/json: + schema: + $ref: '#/components/schemas/error_response' + '500': + description: Internal server error + content: + application/json: + schema: + $ref: '#/components/schemas/error_response' # Files /v1/files: @@ -2734,3 +2769,11 @@ components: - slack - json - teams + test_webhook_response: + type: object + required: + - webhook_status_code + properties: + webhook_status_code: + type: number + example: 400 diff --git a/docs/openapi3.yaml b/docs/openapi3.yaml index 30a0f8232..0f85d2663 100644 --- a/docs/openapi3.yaml +++ b/docs/openapi3.yaml @@ -1704,6 +1704,41 @@ paths: application/json: schema: $ref: '#/components/schemas/error_response' + /v1/webhooks/{webhook_id}/test: + get: + operationId: test-a-webhook + tags: + - Webhooks + summary: Tests that the webhook is configure correctly + description: Tests that the webhook is configure correctly by sending a message to the target webhook url. + parameters: + - in: path + name: webhook_id + description: The webhook id. + required: true + schema: + type: string + format: uuid + example: 4bf5d7ab-f310-4a64-8ec2-d65c06188ec1 + responses: + '200': + description: Success + content: + application/json: + schema: + $ref: '#/components/schemas/test_webhook_response' + '404': + description: Not found + content: + application/json: + schema: + $ref: '#/components/schemas/error_response' + '500': + description: Internal server error + content: + application/json: + schema: + $ref: '#/components/schemas/error_response' # Files /v1/files: @@ -2783,3 +2818,11 @@ components: - slack - json - teams + test_webhook_response: + type: object + required: + - webhook_status_code + properties: + webhook_status_code: + type: number + example: 400 diff --git a/src/common/consts.js b/src/common/consts.js index 34035ae1a..326944415 100644 --- a/src/common/consts.js +++ b/src/common/consts.js @@ -60,6 +60,7 @@ module.exports = { KUBERNETES: 'KUBERNETES', METRONOME: 'METRONOME', DOCKER: 'DOCKER', + WEBHOOK_TEST_MESSAGE: 'Hello From Predator! Wuff! Wuff!', CONFIG: { GRFANA_URL: 'grafana_url', DELAY_RUNNER_MS: 'delay_runner_ms', diff --git a/src/common/requestSender.js b/src/common/requestSender.js index da4987919..511a0b829 100644 --- a/src/common/requestSender.js +++ b/src/common/requestSender.js @@ -17,4 +17,4 @@ module.exports.send = async (options) => { logger.error({ method: options.method, url: options.url, error }, 'Error occurred sending request'); throw error; } -}; \ No newline at end of file +}; diff --git a/src/webhooks/controllers/webhooksController.js b/src/webhooks/controllers/webhooksController.js index 35f371060..aff4b5cb8 100644 --- a/src/webhooks/controllers/webhooksController.js +++ b/src/webhooks/controllers/webhooksController.js @@ -50,4 +50,15 @@ module.exports.updateWebhook = async function (req, res, next) { } catch (err) { return next(err); } -}; \ No newline at end of file +}; + +module.exports.testWebhook = async function(req, res, next) { + const { params: { webhook_id: webhookId } } = req; + try { + const webhookStatusCode = await webhookManager.testWebhook(webhookId); + return res.status(200).json({ webhook_status_code: webhookStatusCode }); + } + catch (err) { + return next(err); + } +}; diff --git a/src/webhooks/models/webhookManager.js b/src/webhooks/models/webhookManager.js index 49f552909..65171f018 100644 --- a/src/webhooks/models/webhookManager.js +++ b/src/webhooks/models/webhookManager.js @@ -50,12 +50,14 @@ async function getAllGlobalWebhooks() { async function fireSingleWebhook(webhook, payload) { try { - await requestSender.send({ + const response = await requestSender.send({ method: 'POST', url: webhook.url, - body: payload + body: payload, + resolveWithFullResponse: true }); logger.info(`Webhook fired successfully, url = ${webhook.url}`); + return response; } catch (requestError) { logger.error(`Webhook failed, url = ${webhook.url}`); throw requestError; @@ -81,11 +83,28 @@ async function fireWebhookByEvent(job, eventType, report, additionalInfo = {}, o await Promise.allSettled(webhooksPromises); } +async function testWebhook(webhookId) { + const webhook = await databaseConnector.getWebhook(webhookId); + if (!webhook) { + throw generateError(404, ERROR_MESSAGES.NOT_FOUND); + } + const payload = webhooksFormatter.formatSimpleMessage(webhook.format_type); + let webhookStatusCode = null; + try { + const response = await fireSingleWebhook(webhook, payload); + webhookStatusCode = response.statusCode; + } catch (requestError) { + webhookStatusCode = requestError.statusCode; + } + return webhookStatusCode; +} + module.exports = { getAllWebhooks, getWebhook, createWebhook, deleteWebhook, updateWebhook, + testWebhook, fireWebhookByEvent }; diff --git a/src/webhooks/models/webhooksFormatter.js b/src/webhooks/models/webhooksFormatter.js index 8c8d34544..d3917d39e 100644 --- a/src/webhooks/models/webhooksFormatter.js +++ b/src/webhooks/models/webhooksFormatter.js @@ -18,7 +18,8 @@ const { WEBHOOK_SLACK_DEFAULT_MESSAGE_ICON, WEBHOOK_SLACK_DEFAULT_REPORTER_NAME, WEBHOOK_EVENT_TYPE_IN_PROGRESS, - WEBHOOK_TEAMS_DEFAULT_THEME_COLOR + WEBHOOK_TEAMS_DEFAULT_THEME_COLOR, + WEBHOOK_TEST_MESSAGE } = require('../../common/consts'); const statsFormatter = require('./statsFormatter'); @@ -38,7 +39,7 @@ function getThresholdMessage(state, { isSlack, testName, benchmarkThreshold, las `.*\n${statsFormatter.getStatsFormatted('aggregate', aggregatedReport, { score })}\n`; } -function slackWebhookFormat(message, options) { +function slackWebhookFormat(message, options = {}) { return { text: message, icon_emoji: options.icon || WEBHOOK_SLACK_DEFAULT_MESSAGE_ICON, @@ -208,4 +209,22 @@ module.exports.format = function(format, eventType, jobId, testId, report, addit throw new Error(`Unrecognized webhook format: ${format}, available options: ${EVENT_FORMAT_TYPES.join()}`); } } -}; \ No newline at end of file +}; + +module.exports.formatSimpleMessage = function(format) { + const simpleMessage = WEBHOOK_TEST_MESSAGE; + switch (format) { + case EVENT_FORMAT_TYPE_SLACK: { + return slackWebhookFormat(simpleMessage); + } + case EVENT_FORMAT_TYPE_JSON: { + return { greeting: simpleMessage }; + } + case EVENT_FORMAT_TYPE_TEAMS: { + return teamsWebhookFormat(simpleMessage); + } + default: { + throw new Error(`Unrecognized webhook format: ${format}, available options: ${EVENT_FORMAT_TYPES.join()}`); + } + } +}; diff --git a/src/webhooks/routes/webhooksRouter.js b/src/webhooks/routes/webhooksRouter.js index a5c677560..826886128 100644 --- a/src/webhooks/routes/webhooksRouter.js +++ b/src/webhooks/routes/webhooksRouter.js @@ -9,6 +9,7 @@ const webhooksController = require('../controllers/webhooksController'); router.get('/', swaggerValidator.validate, webhooksController.getAllWebhooks); router.post('/', swaggerValidator.validate, webhooksController.createWebhook); router.get('/:webhook_id', swaggerValidator.validate, webhooksController.getWebhook); +router.get('/:webhook_id/test', swaggerValidator.validate, webhooksController.testWebhook); router.delete('/:webhook_id', swaggerValidator.validate, webhooksController.deleteWebhook); router.put('/:webhook_id', swaggerValidator.validate, webhooksController.updateWebhook); diff --git a/tests/integration-tests/webhooks/helpers/requestCreator.js b/tests/integration-tests/webhooks/helpers/requestCreator.js index 383421aea..6e0a90251 100644 --- a/tests/integration-tests/webhooks/helpers/requestCreator.js +++ b/tests/integration-tests/webhooks/helpers/requestCreator.js @@ -11,6 +11,7 @@ module.exports = { getWebhooks, getWebhook, deleteWebhook, + testWebhook, updateWebhook }; @@ -68,4 +69,10 @@ function updateWebhook(webhookId, webhook) { .expect(function (res) { return res; }); -} \ No newline at end of file +} + +function testWebhook(webhookId) { + return request(app) + .get(`${resourceUri}/${webhookId}/test`) + .set(headers); +} diff --git a/tests/integration-tests/webhooks/webhooks-test.js b/tests/integration-tests/webhooks/webhooks-test.js index 8af8b7b3f..3418e1d7e 100644 --- a/tests/integration-tests/webhooks/webhooks-test.js +++ b/tests/integration-tests/webhooks/webhooks-test.js @@ -1,7 +1,8 @@ const { expect } = require('chai'); const uuid = require('uuid'); +const nock = require('nock'); -const { WEBHOOK_EVENT_TYPES, EVENT_FORMAT_TYPE_JSON, EVENT_FORMAT_TYPES, WEBHOOK_EVENT_TYPE_API_FAILURE, WEBHOOK_EVENT_TYPE_FAILED } = require('../../../src/common/consts'); +const { WEBHOOK_EVENT_TYPES, EVENT_FORMAT_TYPE_JSON, EVENT_FORMAT_TYPES, WEBHOOK_EVENT_TYPE_API_FAILURE, WEBHOOK_EVENT_TYPE_FAILED, ERROR_MESSAGES: { NOT_FOUND } } = require('../../../src/common/consts'); const webhookRequestSender = require('./helpers/requestCreator'); @@ -12,6 +13,47 @@ describe('Webhooks api', function () { }); describe('Good requests', async function () { + describe('GET /v1/webhooks/:webhook_id/test', function() { + it('should return 200 and webhook_status_code=200', async function() { + const webhook = generateWebhook(); + nock(webhook.url).post('').reply(200); + + const createWebhookResponse = await webhookRequestSender.createWebhook(webhook); + expect(createWebhookResponse.status).to.be.equal(201); + + const webhookId = createWebhookResponse.body.id; + + const testWebhookResponse = await webhookRequestSender.testWebhook(webhookId); + expect(testWebhookResponse.status).to.be.equal(200); + expect(testWebhookResponse.body).to.have.a.property('webhook_status_code').and.to.be.equal(200); + }); + it('should return 400 and webhook_status_code=400', async function () { + const webhook = generateWebhook(); + nock(webhook.url).post('').reply(400); + + const createWebhookResponse = await webhookRequestSender.createWebhook(webhook); + expect(createWebhookResponse.status).to.be.equal(201); + + const webhookId = createWebhookResponse.body.id; + + const testWebhookResponse = await webhookRequestSender.testWebhook(webhookId); + expect(testWebhookResponse.status).to.be.equal(200); + expect(testWebhookResponse.body).to.have.a.property('webhook_status_code').and.to.be.equal(400); + }); + it('should return 200 and webhook_status_code=422', async function () { + const webhook = generateWebhook(); + nock(webhook.url).post('').reply(422); + + const createWebhookResponse = await webhookRequestSender.createWebhook(webhook); + expect(createWebhookResponse.status).to.be.equal(201); + + const webhookId = createWebhookResponse.body.id; + + const testWebhookResponse = await webhookRequestSender.testWebhook(webhookId); + expect(testWebhookResponse.status).to.be.equal(200); + expect(testWebhookResponse.body).to.have.a.property('webhook_status_code').and.to.be.equal(422); + }); + }); describe('GET /v1/webhooks', async function () { before('clean webhooks', async function() { const webhooksResponse = await webhookRequestSender.getWebhooks(); @@ -183,6 +225,18 @@ describe('Webhooks api', function () { }); describe('Bad requests', function () { + describe('GET /v1/webhooks/:webhook_id/test', function() { + it('should get 400 for bad webhook_id format', async function() { + const testWebhookResponse = await webhookRequestSender.testWebhook('bad_id_format'); + expect(testWebhookResponse.statusCode).to.equal(400); + expect(testWebhookResponse.body).to.be.deep.equal({ + message: 'Input validation error', + validation_errors: [ + 'path/webhook_id should match format "uuid"' + ] + }); + }); + }); describe('POST /v1/webhooks', function () { describe('name validation', function() { it('Create webhook with bad type of name', async function () { @@ -271,6 +325,13 @@ describe('Webhooks api', function () { }); describe('Sad requests', function() { + describe('GET /v1/webhooks/:webhook_id/test', function () { + it('should return 404 for unexist webhook', async function() { + const testWebhookResponse = await webhookRequestSender.testWebhook(uuid.v4()); + expect(testWebhookResponse.status).to.be.equal(404); + expect(testWebhookResponse.body).to.have.a.property('message').and.to.be.equal(NOT_FOUND); + }); + }); describe('GET /v1/webhooks/:webhook_id', function () { it('should return 404 for no existing webhook', async function() { const notExistingWebhookId = uuid.v4(); diff --git a/tests/unit-tests/webhooks/webhooksFormatter-test.js b/tests/unit-tests/webhooks/webhooksFormatter-test.js index 5eff8a3f1..d45669e20 100644 --- a/tests/unit-tests/webhooks/webhooksFormatter-test.js +++ b/tests/unit-tests/webhooks/webhooksFormatter-test.js @@ -8,6 +8,7 @@ const { EVENT_FORMAT_TYPE_JSON, EVENT_FORMAT_TYPE_SLACK, EVENT_FORMAT_TYPE_TEAMS, + WEBHOOK_TEST_MESSAGE, WEBHOOK_TEAMS_DEFAULT_THEME_COLOR, WEBHOOK_EVENT_TYPE_STARTED, WEBHOOK_EVENT_TYPE_FINISHED, @@ -17,7 +18,9 @@ const { WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, WEBHOOK_EVENT_TYPE_IN_PROGRESS, WEBHOOK_EVENT_TYPE_FAILED, - WEBHOOK_EVENT_TYPES + WEBHOOK_EVENT_TYPES, + WEBHOOK_SLACK_DEFAULT_MESSAGE_ICON, + WEBHOOK_SLACK_DEFAULT_REPORTER_NAME } = require('../../../src/common/consts'); const webhooksFormatter = rewire('../../../src/webhooks/models/webhooksFormatter'); @@ -32,406 +35,444 @@ describe('webhooksFormatter', function () { afterEach(() => { sandbox.resetHistory(); }); - after(() => { sandbox.restore(); }); - describe(EVENT_FORMAT_TYPE_SLACK, function() { - it(WEBHOOK_EVENT_TYPE_STARTED + ' - load test', function() { - const testId = uuid.v4(); - const jobId = uuid.v4(); - const report = { - ramp_to: 100, - test_name: 'some test name', - arrival_rate: 5, - duration: 120, - environment: 'test', - parallelism: 10 - }; - const expectedResult = `🤓 *Test ${report.test_name} with id: ${testId} has started*.\n + + describe('#format', function() { + describe(EVENT_FORMAT_TYPE_SLACK, function () { + it(WEBHOOK_EVENT_TYPE_STARTED + ' - load test', function () { + const testId = uuid.v4(); + const jobId = uuid.v4(); + const report = { + ramp_to: 100, + test_name: 'some test name', + arrival_rate: 5, + duration: 120, + environment: 'test', + parallelism: 10 + }; + const expectedResult = `🤓 *Test ${report.test_name} with id: ${testId} has started*.\n *test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival rate: ${report.arrival_rate} scenarios per second, ramp to: ${report.ramp_to} scenarios per second, number of runners: ${report.parallelism}`; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); - expect(payload.text).to.be.equal(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_STARTED + ' - functional test', function() { - const testId = uuid.v4(); - const jobId = uuid.v4(); - const report = { - test_name: 'some test name', - arrival_count: 5, - duration: 120, - environment: 'test', - parallelism: 10 - }; - const expectedResult = `🤓 *Test ${report.test_name} with id: ${testId} has started*.\n + expect(payload.text).to.be.equal(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_STARTED + ' - functional test', function () { + const testId = uuid.v4(); + const jobId = uuid.v4(); + const report = { + test_name: 'some test name', + arrival_count: 5, + duration: 120, + environment: 'test', + parallelism: 10 + }; + const expectedResult = `🤓 *Test ${report.test_name} with id: ${testId} has started*.\n *test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival count: ${report.arrival_count} scenarios, number of runners: ${report.parallelism}`; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); - expect(payload.text).to.be.equal(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_ABORTED, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const expectedResult = `😢 *Test ${report.test_name} with id: ${testId} was aborted.*\n`; + expect(payload.text).to.be.equal(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_ABORTED, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const expectedResult = `😢 *Test ${report.test_name} with id: ${testId} was aborted.*\n`; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_ABORTED, uuid.v4(), testId, report); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_ABORTED, uuid.v4(), testId, report); - expect(payload.text).to.be.equal(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_FINISHED, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const additionalInfo = { - aggregatedReport: { - aggregate: { - key: 'value' + expect(payload.text).to.be.equal(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_FINISHED, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const additionalInfo = { + aggregatedReport: { + aggregate: { + key: 'value' + } } - } - }; - const stats = 'some stats string'; - statsFormatterStub.returns(stats); + }; + const stats = 'some stats string'; + statsFormatterStub.returns(stats); - const expectedResult = `😎 *Test ${report.test_name} with id: ${testId} is finished.*\n ${stats}\n`; + const expectedResult = `😎 *Test ${report.test_name} with id: ${testId} is finished.*\n ${stats}\n`; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_FINISHED, uuid.v4(), testId, report, additionalInfo); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_FINISHED, uuid.v4(), testId, report, additionalInfo); - expect(payload.text).to.be.equal(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const additionalInfo = { - lastScores: [25, 60, 12], - score: 45, - benchmarkThreshold: 90, - aggregatedReport: { - aggregate: { - key: 'value' + expect(payload.text).to.be.equal(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const additionalInfo = { + lastScores: [25, 60, 12], + score: 45, + benchmarkThreshold: 90, + aggregatedReport: { + aggregate: { + key: 'value' + } } - } - }; - const statsText = 'some text'; - statsFormatterStub.returns(statsText); - const expectedResult = `:cry: *Test ${report.test_name} got a score of ${additionalInfo.score.toFixed(1)}` + - ` this is below the threshold of ${additionalInfo.benchmarkThreshold}. last 3 scores are: ${additionalInfo.lastScores.join()}` + - `.*\n${statsText}\n`; + }; + const statsText = 'some text'; + statsFormatterStub.returns(statsText); + const expectedResult = `:cry: *Test ${report.test_name} got a score of ${additionalInfo.score.toFixed(1)}` + + ` this is below the threshold of ${additionalInfo.benchmarkThreshold}. last 3 scores are: ${additionalInfo.lastScores.join()}` + + `.*\n${statsText}\n`; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, uuid.v4(), testId, report, additionalInfo); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, uuid.v4(), testId, report, additionalInfo); - expect(payload.text).to.be.equal(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const additionalInfo = { - lastScores: [90, 100, 100], - score: 97.5, - benchmarkThreshold: 90, - aggregatedReport: { - aggregate: { - key: 'value' + expect(payload.text).to.be.equal(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const additionalInfo = { + lastScores: [90, 100, 100], + score: 97.5, + benchmarkThreshold: 90, + aggregatedReport: { + aggregate: { + key: 'value' + } } - } - }; - const statsText = 'some text'; - statsFormatterStub.returns(statsText); - const expectedResult = `:rocket: *Test ${report.test_name} got a score of ${additionalInfo.score.toFixed(1)}` + - ` this is above the threshold of ${additionalInfo.benchmarkThreshold}. last 3 scores are: ${additionalInfo.lastScores.join()}` + - `.*\n${statsText}\n`; + }; + const statsText = 'some text'; + statsFormatterStub.returns(statsText); + const expectedResult = `:rocket: *Test ${report.test_name} got a score of ${additionalInfo.score.toFixed(1)}` + + ` this is above the threshold of ${additionalInfo.benchmarkThreshold}. last 3 scores are: ${additionalInfo.lastScores.join()}` + + `.*\n${statsText}\n`; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, uuid.v4(), testId, report, additionalInfo); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, uuid.v4(), testId, report, additionalInfo); - expect(payload.text).to.be.equal(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_FAILED, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name', - environment: 'test' - }; - const additionalInfo = { - stats: { - data: 'data' - } - }; - const expectedResult = `😞 *Test with id: ${testId} Failed*.\n + expect(payload.text).to.be.equal(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_FAILED, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name', + environment: 'test' + }; + const additionalInfo = { + stats: { + data: 'data' + } + }; + const expectedResult = `😞 *Test with id: ${testId} Failed*.\n test configuration:\n environment: ${report.environment}\n ${additionalInfo.stats.data}`; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_FAILED, uuid.v4(), testId, report, additionalInfo); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_FAILED, uuid.v4(), testId, report, additionalInfo); - expect(payload.text).to.be.equal(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_IN_PROGRESS, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const expectedResult = `:hammer_and_wrench: *Test ${report.test_name} with id: ${testId} is in progress!*`; + expect(payload.text).to.be.equal(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_IN_PROGRESS, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const expectedResult = `:hammer_and_wrench: *Test ${report.test_name} with id: ${testId} is in progress!*`; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_IN_PROGRESS, uuid.v4(), testId, report); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_IN_PROGRESS, uuid.v4(), testId, report); - expect(payload.text).to.be.equal(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_API_FAILURE, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const expectedResult = `:boom: *Test ${report.test_name} with id: ${testId} has encountered an API failure!* :skull:`; + expect(payload.text).to.be.equal(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_API_FAILURE, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const expectedResult = `:boom: *Test ${report.test_name} with id: ${testId} has encountered an API failure!* :skull:`; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_API_FAILURE, uuid.v4(), testId, report); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_API_FAILURE, uuid.v4(), testId, report); - expect(payload.text).to.be.equal(expectedResult); - }); - it('uknown event type -> expect error to be thrown', function () { - const unknownEventType = 'superUknownEventType'; - const expectedErrorMessage = `Unrecognized webhook event: ${unknownEventType}, must be one of the following: ${WEBHOOK_EVENT_TYPES.join(', ')}`; - expect(webhooksFormatter.format.bind(null, EVENT_FORMAT_TYPE_SLACK, unknownEventType)).to.throw(expectedErrorMessage); - }); - it('should display the grafana url if specified in report', function() { - const testId = uuid.v4(); - const jobId = uuid.v4(); - const report = { - ramp_to: 100, - test_name: 'some test name', - arrival_rate: 5, - duration: 120, - environment: 'test', - parallelism: 10, - grafana_report: 'http://local.grafana.io/predator' - }; - const expectedResult = `🤓 *Test ${report.test_name} with id: ${testId} has started*.\n + expect(payload.text).to.be.equal(expectedResult); + }); + it('uknown event type -> expect error to be thrown', function () { + const unknownEventType = 'superUknownEventType'; + const expectedErrorMessage = `Unrecognized webhook event: ${unknownEventType}, must be one of the following: ${WEBHOOK_EVENT_TYPES.join(', ')}`; + expect(webhooksFormatter.format.bind(null, EVENT_FORMAT_TYPE_SLACK, unknownEventType)).to.throw(expectedErrorMessage); + }); + it('should display the grafana url if specified in report', function () { + const testId = uuid.v4(); + const jobId = uuid.v4(); + const report = { + ramp_to: 100, + test_name: 'some test name', + arrival_rate: 5, + duration: 120, + environment: 'test', + parallelism: 10, + grafana_report: 'http://local.grafana.io/predator' + }; + const expectedResult = `🤓 *Test ${report.test_name} with id: ${testId} has started*.\n *test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival rate: ${report.arrival_rate} scenarios per second, ramp to: ${report.ramp_to} scenarios per second, number of runners: ${report.parallelism}`; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); - expect(payload.text).to.be.equal(expectedResult); - }); - }); - describe(EVENT_FORMAT_TYPE_TEAMS, function() { - it(WEBHOOK_EVENT_TYPE_STARTED + ' - load test', function() { - const testId = uuid.v4(); - const jobId = uuid.v4(); - const report = { - ramp_to: 100, - test_name: 'some test name', - arrival_rate: 5, - duration: 120, - environment: 'test', - parallelism: 10 - }; - const expectedResult = { - text: `😃 *Test ${report.test_name} with id: ${testId} has started*. \n*test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival rate: ${report.arrival_rate} scenarios per second, ramp to: ${report.ramp_to} scenarios per second, number of runners: ${report.parallelism}`, - themeColor: '957c58' - - }; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); - - expect(payload).to.eql(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_STARTED + ' - functional test', function() { - const testId = uuid.v4(); - const jobId = uuid.v4(); - const report = { - test_name: 'some test name', - arrival_count: 5, - duration: 120, - environment: 'test', - parallelism: 10 - }; - const expectedResult = { - text: `😃 *Test ${report.test_name} with id: ${testId} has started*. \n*test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival count: ${report.arrival_count} scenarios, number of runners: ${report.parallelism}`, - themeColor: '957c58' - }; - - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); - - expect(payload).to.eql(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_ABORTED, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const expectedResult = { - text: `😧 *Test ${report.test_name} with id: ${testId} was aborted.*`, - themeColor: '957c58' - }; - - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_ABORTED, uuid.v4(), testId, report); - - expect(payload).to.eql(expectedResult); + expect(payload.text).to.be.equal(expectedResult); + }); }); - it(WEBHOOK_EVENT_TYPE_FINISHED, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const additionalInfo = { - aggregatedReport: { - aggregate: { - key: 'value' + describe(EVENT_FORMAT_TYPE_TEAMS, function () { + it(WEBHOOK_EVENT_TYPE_STARTED + ' - load test', function () { + const testId = uuid.v4(); + const jobId = uuid.v4(); + const report = { + ramp_to: 100, + test_name: 'some test name', + arrival_rate: 5, + duration: 120, + environment: 'test', + parallelism: 10 + }; + const expectedResult = { + text: `😃 *Test ${report.test_name} with id: ${testId} has started*. \n*test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival rate: ${report.arrival_rate} scenarios per second, ramp to: ${report.ramp_to} scenarios per second, number of runners: ${report.parallelism}`, + themeColor: '957c58' + }; + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); + + expect(payload).to.eql(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_STARTED + ' - functional test', function () { + const testId = uuid.v4(); + const jobId = uuid.v4(); + const report = { + test_name: 'some test name', + arrival_count: 5, + duration: 120, + environment: 'test', + parallelism: 10 + }; + const expectedResult = { + text: `😃 *Test ${report.test_name} with id: ${testId} has started*. \n*test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival count: ${report.arrival_count} scenarios, number of runners: ${report.parallelism}`, + themeColor: '957c58' + }; + + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); + + expect(payload).to.eql(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_ABORTED, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const expectedResult = { + text: `😧 *Test ${report.test_name} with id: ${testId} was aborted.*`, + themeColor: '957c58' + }; + + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_ABORTED, uuid.v4(), testId, report); + + expect(payload).to.eql(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_FINISHED, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const additionalInfo = { + aggregatedReport: { + aggregate: { + key: 'value' + } } - } - }; - const stats = 'some stats string'; - statsFormatterStub.returns(stats); + }; + const stats = 'some stats string'; + statsFormatterStub.returns(stats); - const expectedResult = { - text: `😎 *Test ${report.test_name} with id: ${testId} is finished.* \nsome stats string`, - themeColor: '957c58' - }; + const expectedResult = { + text: `😎 *Test ${report.test_name} with id: ${testId} is finished.* \nsome stats string`, + themeColor: '957c58' + }; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_FINISHED, uuid.v4(), testId, report, additionalInfo); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_FINISHED, uuid.v4(), testId, report, additionalInfo); - expect(payload).to.eql(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const additionalInfo = { - lastScores: [25, 60, 12], - score: 45, - benchmarkThreshold: 90, - aggregatedReport: { - aggregate: { - key: 'value' + expect(payload).to.eql(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const additionalInfo = { + lastScores: [25, 60, 12], + score: 45, + benchmarkThreshold: 90, + aggregatedReport: { + aggregate: { + key: 'value' + } } - } - }; - const statsText = 'some text'; - statsFormatterStub.returns(statsText); - const expectedResult = { - text: `😢 *Test ${report.test_name} got a score of ${additionalInfo.score.toFixed(1)}` + - ` this is below the threshold of ${additionalInfo.benchmarkThreshold}. last 3 scores are: ${additionalInfo.lastScores.join()}` + - `.* \n${statsText} \n`, - themeColor: '957c58' - }; - - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, uuid.v4(), testId, report, additionalInfo); - - expect(payload).to.be.eql(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const additionalInfo = { - lastScores: [90, 100, 100], - score: 97.5, - benchmarkThreshold: 90, - aggregatedReport: { - aggregate: { - key: 'value' + }; + const statsText = 'some text'; + statsFormatterStub.returns(statsText); + const expectedResult = { + text: `😢 *Test ${report.test_name} got a score of ${additionalInfo.score.toFixed(1)}` + + ` this is below the threshold of ${additionalInfo.benchmarkThreshold}. last 3 scores are: ${additionalInfo.lastScores.join()}` + + `.* \n${statsText} \n`, + themeColor: '957c58' + }; + + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, uuid.v4(), testId, report, additionalInfo); + + expect(payload).to.be.eql(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const additionalInfo = { + lastScores: [90, 100, 100], + score: 97.5, + benchmarkThreshold: 90, + aggregatedReport: { + aggregate: { + key: 'value' + } } - } - }; - const statsText = 'some text'; - statsFormatterStub.returns(statsText); - const expectedResult = { - text: `🚀 *Test some name got a score of ${additionalInfo.score.toFixed(1)}` + - ` this is above the threshold of ${additionalInfo.benchmarkThreshold}. last 3 scores are: ${additionalInfo.lastScores.join()}` + - `.* \n${statsText} \n`, - themeColor: '957c58' - }; - - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, uuid.v4(), testId, report, additionalInfo); - - expect(payload).to.eql(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_FAILED, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name', - environment: 'test' - }; - const additionalInfo = { - stats: { - data: 'data' - } - }; - const expectedResult = { - text: `😧 *Test with id: ${testId} Failed*. \ntest configuration: \n \n environment: test \n \n ${additionalInfo.stats.data}`, - themeColor: '957c58' - }; - - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_FAILED, uuid.v4(), testId, report, additionalInfo); - - expect(payload).to.eql(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_IN_PROGRESS, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const expectedResult = { - text: `🔨 *Test some name with id: ${testId} is in progress!*`, - themeColor: '957c58' - }; - - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_IN_PROGRESS, uuid.v4(), testId, report); - - expect(payload).to.eql(expectedResult); - }); - it(WEBHOOK_EVENT_TYPE_API_FAILURE, function () { - const testId = uuid.v4(); - const report = { - test_name: 'some name' - }; - const expectedResult = { - text: `🔥 *Test ${report.test_name} with id: ${testId} has encountered an API failure!* 💀`, - themeColor: '957c58' - }; - - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_API_FAILURE, uuid.v4(), testId, report); - - expect(payload).to.eql(expectedResult); - }); - it('uknown event type -> expect error to be thrown', function () { - const unknownEventType = 'superUknownEventType'; - const expectedErrorMessage = `Unrecognized webhook event: ${unknownEventType}, must be one of the following: ${WEBHOOK_EVENT_TYPES.join(', ')}`; - expect(webhooksFormatter.format.bind(null, EVENT_FORMAT_TYPE_TEAMS, unknownEventType)).to.throw(expectedErrorMessage); - }); - it('should display the grafana url if specified in report', function() { - const testId = uuid.v4(); - const jobId = uuid.v4(); - const report = { - ramp_to: 100, - test_name: 'some test name', - arrival_rate: 5, - duration: 120, - environment: 'test', - parallelism: 10, - grafana_report: 'http://local.grafana.io/predator' - }; - const expectedResult = { - text: `😃 *Test ${report.test_name} with id: ${testId} has started*. \n*test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival rate: ${report.arrival_rate} scenarios per second, ramp to: ${report.ramp_to} scenarios per second, number of runners: ${report.parallelism}`, - themeColor: '957c58' - }; - - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); - - expect(payload).to.eql(expectedResult); + }; + const statsText = 'some text'; + statsFormatterStub.returns(statsText); + const expectedResult = { + text: `🚀 *Test some name got a score of ${additionalInfo.score.toFixed(1)}` + + ` this is above the threshold of ${additionalInfo.benchmarkThreshold}. last 3 scores are: ${additionalInfo.lastScores.join()}` + + `.* \n${statsText} \n`, + themeColor: '957c58' + }; + + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, uuid.v4(), testId, report, additionalInfo); + + expect(payload).to.eql(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_FAILED, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name', + environment: 'test' + }; + const additionalInfo = { + stats: { + data: 'data' + } + }; + const expectedResult = { + text: `😧 *Test with id: ${testId} Failed*. \ntest configuration: \n \n environment: test \n \n ${additionalInfo.stats.data}`, + themeColor: '957c58' + }; + + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_FAILED, uuid.v4(), testId, report, additionalInfo); + + expect(payload).to.eql(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_IN_PROGRESS, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const expectedResult = { + text: `🔨 *Test some name with id: ${testId} is in progress!*`, + themeColor: '957c58' + }; + + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_IN_PROGRESS, uuid.v4(), testId, report); + + expect(payload).to.eql(expectedResult); + }); + it(WEBHOOK_EVENT_TYPE_API_FAILURE, function () { + const testId = uuid.v4(); + const report = { + test_name: 'some name' + }; + const expectedResult = { + text: `🔥 *Test ${report.test_name} with id: ${testId} has encountered an API failure!* 💀`, + themeColor: '957c58' + }; + + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_API_FAILURE, uuid.v4(), testId, report); + + expect(payload).to.eql(expectedResult); + }); + it('uknown event type -> expect error to be thrown', function () { + const unknownEventType = 'superUknownEventType'; + const expectedErrorMessage = `Unrecognized webhook event: ${unknownEventType}, must be one of the following: ${WEBHOOK_EVENT_TYPES.join(', ')}`; + expect(webhooksFormatter.format.bind(null, EVENT_FORMAT_TYPE_TEAMS, unknownEventType)).to.throw(expectedErrorMessage); + }); + it('should display the grafana url if specified in report', function () { + const testId = uuid.v4(); + const jobId = uuid.v4(); + const report = { + ramp_to: 100, + test_name: 'some test name', + arrival_rate: 5, + duration: 120, + environment: 'test', + parallelism: 10, + grafana_report: 'http://local.grafana.io/predator' + }; + const expectedResult = { + text: `😃 *Test ${report.test_name} with id: ${testId} has started*. \n*test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival rate: ${report.arrival_rate} scenarios per second, ramp to: ${report.ramp_to} scenarios per second, number of runners: ${report.parallelism}`, + themeColor: '957c58' + }; + + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_TEAMS, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report); + + expect(payload).to.eql(expectedResult); + }); }); - }); - describe(EVENT_FORMAT_TYPE_JSON, function () { - WEBHOOK_EVENT_TYPES.forEach(webhookEventType => { - it(webhookEventType, function () { + describe(EVENT_FORMAT_TYPE_JSON, function () { + WEBHOOK_EVENT_TYPES.forEach(webhookEventType => { + it(webhookEventType, function () { + const testId = uuid.v4(); + const jobId = uuid.v4(); + const report = { + ramp_to: 100, + test_name: 'some test name', + arrival_rate: 5, + duration: 120, + environment: 'test', + parallelism: 10 + }; + const additionalInfo = { + some: { + nested: { + value: ['Look', 'more', 'values'] + } + } + }; + const expectedResult = { + test_id: testId, + job_id: jobId, + event: webhookEventType, + additional_details: { + report, + ...additionalInfo + } + }; + + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_JSON, webhookEventType, jobId, testId, report, additionalInfo); + + expect(payload).to.be.deep.equal(expectedResult); + }); + }); + it('uknown event type -> expect error to be thrown', function () { + const unknownEventType = 'superUknownEventType'; + const expectedErrorMessage = `Unrecognized webhook event: ${unknownEventType}, must be one of the following: ${WEBHOOK_EVENT_TYPES.join(', ')}`; + expect(webhooksFormatter.format.bind(null, EVENT_FORMAT_TYPE_JSON, unknownEventType)).to.throw(expectedErrorMessage); + }); + it('should display the grafana url if specified in report', function () { const testId = uuid.v4(); const jobId = uuid.v4(); const report = { @@ -440,7 +481,8 @@ describe('webhooksFormatter', function () { arrival_rate: 5, duration: 120, environment: 'test', - parallelism: 10 + parallelism: 10, + grafana_report: 'https://local.predator.io/grafana' }; const additionalInfo = { some: { @@ -452,61 +494,42 @@ describe('webhooksFormatter', function () { const expectedResult = { test_id: testId, job_id: jobId, - event: webhookEventType, + event: WEBHOOK_EVENT_TYPE_STARTED, additional_details: { report, ...additionalInfo } }; - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_JSON, webhookEventType, jobId, testId, report, additionalInfo); + const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_JSON, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report, additionalInfo); expect(payload).to.be.deep.equal(expectedResult); }); }); - it('uknown event type -> expect error to be thrown', function() { - const unknownEventType = 'superUknownEventType'; - const expectedErrorMessage = `Unrecognized webhook event: ${unknownEventType}, must be one of the following: ${WEBHOOK_EVENT_TYPES.join(', ')}`; - expect(webhooksFormatter.format.bind(null, EVENT_FORMAT_TYPE_JSON, unknownEventType)).to.throw(expectedErrorMessage); - }); - it('should display the grafana url if specified in report', function () { - const testId = uuid.v4(); - const jobId = uuid.v4(); - const report = { - ramp_to: 100, - test_name: 'some test name', - arrival_rate: 5, - duration: 120, - environment: 'test', - parallelism: 10, - grafana_report: 'https://local.predator.io/grafana' - }; - const additionalInfo = { - some: { - nested: { - value: ['Look', 'more', 'values'] - } - } - }; - const expectedResult = { - test_id: testId, - job_id: jobId, - event: WEBHOOK_EVENT_TYPE_STARTED, - additional_details: { - report, - ...additionalInfo - } - }; - - const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_JSON, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report, additionalInfo); - - expect(payload).to.be.deep.equal(expectedResult); + describe('Unknown format', function () { + it('should throw an error', function () { + const unknownFormat = 'some_random_format'; + expect(webhooksFormatter.format.bind(null, unknownFormat, WEBHOOK_EVENT_TYPE_STARTED)).to.throw(`Unrecognized webhook format: ${unknownFormat}, available options: ${EVENT_FORMAT_TYPES.join()}`); + }); }); }); - describe('Unknown format', function() { - it('should throw an error', function() { - const unknownFormat = 'some_random_format'; - expect(webhooksFormatter.format.bind(null, unknownFormat, WEBHOOK_EVENT_TYPE_STARTED)).to.throw(`Unrecognized webhook format: ${unknownFormat}, available options: ${EVENT_FORMAT_TYPES.join()}`); + describe('#formatSimpleMessage', function() { + it(EVENT_FORMAT_TYPE_JSON, function() { + const payload = webhooksFormatter.formatSimpleMessage(EVENT_FORMAT_TYPE_JSON); + expect(payload).to.have.a.property('greeting').and.to.be.equal(WEBHOOK_TEST_MESSAGE); + }); + it(EVENT_FORMAT_TYPE_SLACK, function () { + const payload = webhooksFormatter.formatSimpleMessage(EVENT_FORMAT_TYPE_SLACK); + expect(payload).to.be.deep.equal({ + text: WEBHOOK_TEST_MESSAGE, + icon_emoji: WEBHOOK_SLACK_DEFAULT_MESSAGE_ICON, + username: WEBHOOK_SLACK_DEFAULT_REPORTER_NAME + }); + }); + it(EVENT_FORMAT_TYPE_TEAMS, function () { + const payload = webhooksFormatter.formatSimpleMessage(EVENT_FORMAT_TYPE_TEAMS); + expect(payload).to.have.a.property('text').and.to.be.equal(WEBHOOK_TEST_MESSAGE); + expect(payload).to.have.a.property('themeColor').and.to.be.equal(WEBHOOK_TEAMS_DEFAULT_THEME_COLOR); }); }); }); diff --git a/tests/unit-tests/webhooks/webhooksManager.js b/tests/unit-tests/webhooks/webhooksManager.js index e3085001e..c2fda2902 100644 --- a/tests/unit-tests/webhooks/webhooksManager.js +++ b/tests/unit-tests/webhooks/webhooksManager.js @@ -216,12 +216,14 @@ describe('webhooksManager', () => { expect(requestSenderSendStub.args[0][0]).to.be.deep.equal({ method: 'POST', url: webhooks[0].url, - body: format + body: format, + resolveWithFullResponse: true }); expect(requestSenderSendStub.args[1][0]).to.be.deep.equal({ method: 'POST', url: webhooks[1].url, - body: format + body: format, + resolveWithFullResponse: true }); expect(webhooksFormatterFormatStub.callCount).to.be.equal(2); @@ -263,7 +265,8 @@ describe('webhooksManager', () => { expect(requestSenderSendStub.args[0][0]).to.be.deep.equal({ method: 'POST', url: webhooks[0].url, - body: format + body: format, + resolveWithFullResponse: true }); expect(webhooksFormatterFormatStub.callCount).to.be.equal(1); @@ -312,12 +315,14 @@ describe('webhooksManager', () => { expect(requestSenderSendStub.args[0][0]).to.be.deep.equal({ method: 'POST', url: webhooks[0].url, - body: format + body: format, + resolveWithFullResponse: true }); expect(requestSenderSendStub.args[1][0]).to.be.deep.equal({ method: 'POST', url: globalWebhook.url, - body: format + body: format, + resolveWithFullResponse: true }); expect(webhooksFormatterFormatStub.callCount).to.be.equal(2); @@ -349,7 +354,8 @@ describe('webhooksManager', () => { expect(requestSenderSendStub.args[0][0]).to.be.deep.equal({ method: 'POST', url: globalWebhook.url, - body: format + body: format, + resolveWithFullResponse: true }); expect(webhooksFormatterFormatStub.callCount).to.be.equal(1); }); @@ -390,7 +396,8 @@ describe('webhooksManager', () => { requestSenderSendStub.withArgs({ method: 'POST', url: globalWebhook.url, - body: format + body: format, + resolveWithFullResponse: true }).rejects(); await webhooksManager.fireWebhookByEvent(job, WEBHOOK_EVENT_TYPE_STARTED, report); @@ -402,17 +409,20 @@ describe('webhooksManager', () => { expect(requestSenderSendStub.args[0][0]).to.be.deep.equal({ method: 'POST', url: webhooks[0].url, - body: format + body: format, + resolveWithFullResponse: true }); expect(requestSenderSendStub.args[1][0]).to.be.deep.equal({ method: 'POST', url: webhooks[1].url, - body: format + body: format, + resolveWithFullResponse: true }); expect(requestSenderSendStub.args[2][0]).to.be.deep.equal({ method: 'POST', url: globalWebhook.url, - body: format + body: format, + resolveWithFullResponse: true }); expect(webhooksFormatterFormatStub.callCount).to.be.equal(3);