Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/jobs/models/jobManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -298,11 +299,21 @@ function addCron(jobId, job, cronExpression, configData) {
async function globalWebhookAssignmentGuard(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;
}
}
36 changes: 32 additions & 4 deletions tests/integration-tests/jobs/createJobGlobal-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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'
})
Expand All @@ -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'
})
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down