diff --git a/src/jobs/models/jobManager.js b/src/jobs/models/jobManager.js index 2bb7617b8..136c8b74e 100644 --- a/src/jobs/models/jobManager.js +++ b/src/jobs/models/jobManager.js @@ -8,7 +8,8 @@ const logger = require('../../common/logger'), dockerHubConnector = require('./dockerHubConnector'), databaseConnector = require('./database/databaseConnector'), webhooksManager = require('../../webhooks/models/webhookManager'), - { CONFIG, JOB_TYPE_FUNCTIONAL_TEST } = require('../../common/consts'); + { CONFIG, JOB_TYPE_FUNCTIONAL_TEST } = require('../../common/consts'), + generateError = require('../../common/generateError'); let jobConnector; let cronJobs = {}; @@ -49,7 +50,7 @@ module.exports.scheduleFinishedContainersCleanup = async () => { module.exports.createJob = async (job) => { let jobId = uuid.v4(); const configData = await configHandler.getConfig(); - await globalWebhookAssignmentGuard(job.webhooks); + await validateWebhooksAssignment(job.webhooks); try { await databaseConnector.insertJob(jobId, job); logger.info('Job saved successfully to database'); @@ -140,7 +141,7 @@ module.exports.getJob = async (jobId) => { module.exports.updateJob = async (jobId, jobConfig) => { const configData = await configHandler.getConfig(); - await globalWebhookAssignmentGuard(jobConfig.webhooks); + await validateWebhooksAssignment(jobConfig.webhooks); let [job] = await databaseConnector.getJob(jobId); if (!job || job.length === 0) { let error = new Error('Not found'); @@ -295,14 +296,24 @@ function addCron(jobId, job, cronExpression, configData) { cronJobs[jobId] = scheduledJob; } -async function globalWebhookAssignmentGuard(webhookIds) { +async function validateWebhooksAssignment(webhookIds) { let webhooks = []; if (webhookIds && webhookIds.length > 0) { - webhooks = await Promise.all(webhookIds.map(webhookId => webhooksManager.getWebhook(webhookId))); + try { + webhooks = await Promise.all(webhookIds.map(webhookId => webhooksManager.getWebhook(webhookId))); + } catch (err) { + let error; + if (err.statusCode === 404) { + error = generateError(400, 'At least one of the webhooks does not exist'); + throw error; + } else { + error = generateError(500); + } + throw error; + } } if (webhooks.some(webhook => webhook.global)) { - const error = new Error('Assigning a global webhook to a job is not allowed'); - error.statusCode = 422; + const error = generateError(422, 'Assigning a global webhook to a job is not allowed'); throw error; } } diff --git a/tests/integration-tests/jobs/createJobGlobal-test.js b/tests/integration-tests/jobs/createJobGlobal-test.js index aca74df02..a72d2848b 100644 --- a/tests/integration-tests/jobs/createJobGlobal-test.js +++ b/tests/integration-tests/jobs/createJobGlobal-test.js @@ -161,8 +161,8 @@ describe('Create job global tests', function () { .then(function (res) { res.statusCode.should.eql(400); res.body.should.eql({ - 'message': 'Input validation error', - 'validation_errors': [ + message: 'Input validation error', + validation_errors: [ 'body/arrival_rate should be >= 1', 'body/ramp_to should be >= 1', 'body/duration should be >= 1', @@ -209,7 +209,7 @@ describe('Create job global tests', function () { }); it('Should return error for missing arrival_rate', () => { - let bodyWithoutTestId = { test_id: uuid.v4(), duration: 1, environment: 'test', type: 'load_test'}; + let bodyWithoutTestId = { test_id: uuid.v4(), duration: 1, environment: 'test', type: 'load_test' }; return schedulerRequestCreator.createJob(bodyWithoutTestId, { 'Content-Type': 'application/json' }) @@ -223,7 +223,7 @@ describe('Create job global tests', function () { }); it('Should return error for missing duration', () => { - let illegalBody = { test_id: uuid.v4(), arrival_rate: 1, environment: 'test', type: 'load_test'}; + let illegalBody = { test_id: uuid.v4(), arrival_rate: 1, environment: 'test', type: 'load_test' }; return schedulerRequestCreator.createJob(illegalBody, { 'Content-Type': 'application/json' }) @@ -271,6 +271,33 @@ describe('Create job global tests', function () { should(response.body.message).eql('Not found'); }); + describe('Create a job with not existing webhook', () => { + it('should return 400', async () => { + const job = { + type: 'load_test', + arrival_rate: 1, + duration: 120, + environment: 'test', + run_immediately: true, + emails: [], + parallelism: 1, + max_virtual_users: 500 + }; + const headers = { 'Content-Type': 'application/json' }; + + const createTestResponse = await testsRequestSender.createTest(basicTest, headers); + expect(createTestResponse.status).to.be.equal(201); + const testId = createTestResponse.body.id; + + job.webhooks = ['eec96da0-f9b6-4282-a7cf-d8d9a444b0da']; + job.test_id = testId; + + const createJobResponse = await schedulerRequestCreator.createJob(job, headers); + expect(createJobResponse.status).to.be.equal(400); + expect(createJobResponse.body.message).to.be.equal('At least one of the webhooks does not exist'); + }); + }); + describe('Create a job with a global webhook', () => { it('should return 422', async () => { const globalWebhook = { @@ -309,6 +336,7 @@ describe('Create job global tests', function () { expect(createJobResponse.body.message).to.be.equal('Assigning a global webhook to a job is not allowed'); }); }); + describe('Update a job with a global webhook', () => { it('should return 422', async () => { const globalWebhook = {