Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Mar 10, 2020
1 parent 03af835 commit d992c51
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function getAllExternalServiceSimulatorPaths(): string[] {
const allPaths = Object.values(ExternalServiceSimulator).map(service =>
getExternalServiceSimulatorPath(service)
);
allPaths.push(`/api/_${NAME}/${ExternalServiceSimulator.SERVICENOW}/api/now/v1/table/incident`);
allPaths.push(`/api/_${NAME}/${ExternalServiceSimulator.SERVICENOW}/api/now/v2/table/incident`);
return allPaths;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

import Joi from 'joi';
import Hapi from 'hapi';
import { version } from 'bluebird';

interface ServiceNowRequest extends Hapi.Request {
payload: {
comments: string;
short_description: string;
caseId: string;
title?: string;
description?: string;
comments?: Array<{ commentId: string; version: string; comment: string }>;
};
}
export function initPlugin(server: Hapi.Server, path: string) {
Expand All @@ -22,8 +25,16 @@ export function initPlugin(server: Hapi.Server, path: string) {
validate: {
options: { abortEarly: false },
payload: Joi.object().keys({
comments: Joi.string(),
short_description: Joi.string(),
caseId: Joi.string(),
title: Joi.string(),
description: Joi.string(),
comments: Joi.array().items(
Joi.object({
commentId: Joi.string(),
version: Joi.string(),
comment: Joi.string(),
})
),
}),
},
},
Expand All @@ -32,14 +43,46 @@ export function initPlugin(server: Hapi.Server, path: string) {

server.route({
method: 'POST',
path: `${path}/api/now/v1/table/incident`,
path: `${path}/api/now/v2/table/incident`,
options: {
auth: false,
validate: {
options: { abortEarly: false },
payload: Joi.object().keys({
comments: Joi.string(),
short_description: Joi.string(),
caseId: Joi.string(),
title: Joi.string(),
description: Joi.string(),
comments: Joi.array().items(
Joi.object({
commentId: Joi.string(),
version: Joi.string(),
comment: Joi.string(),
})
),
}),
},
},
handler: servicenowHandler,
});

server.route({
method: 'PATCH',
path: `${path}/api/now/v2/table/incident`,
options: {
auth: false,
validate: {
options: { abortEarly: false },
payload: Joi.object().keys({
caseId: Joi.string(),
title: Joi.string(),
description: Joi.string(),
comments: Joi.array().items(
Joi.object({
commentId: Joi.string(),
version: Joi.string(),
comment: Joi.string(),
})
),
}),
},
},
Expand All @@ -52,60 +95,9 @@ export function initPlugin(server: Hapi.Server, path: string) {

function servicenowHandler(request: ServiceNowRequest, h: any) {
const body = request.payload;
const text = body && body.short_description;
if (text == null) {
return jsonResponse(h, 400, 'bad request to servicenow simulator');
}

switch (text) {
case 'success':
return jsonResponse(h, 200, 'Success');

case 'created':
return jsonResponse(h, 201, 'Created');

case 'no_text':
return jsonResponse(h, 204, 'Success');

case 'invalid_payload':
return jsonResponse(h, 400, 'Bad Request');

case 'unauthorized':
return jsonResponse(h, 401, 'Unauthorized');

case 'forbidden':
return jsonResponse(h, 403, 'Forbidden');

case 'not_found':
return jsonResponse(h, 404, 'Not found');

case 'not_allowed':
return jsonResponse(h, 405, 'Method not allowed');

case 'not_acceptable':
return jsonResponse(h, 406, 'Not acceptable');

case 'unsupported':
return jsonResponse(h, 415, 'Unsupported media type');

case 'status_500':
return jsonResponse(h, 500, 'simulated servicenow 500 response');

case 'rate_limit':
const response = {
retry_after: 1,
ok: false,
error: 'rate_limited',
};

return h
.response(response)
.type('application/json')
.header('retry-after', '1')
.code(429);
}
const text = body && body.caseId;

return jsonResponse(h, 400, 'unknown request to servicenow simulator');
return jsonResponse(h, 200, 'Success');
}

function jsonResponse(h: any, code: number, object?: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) {
.post(`/api/action/${simulatedActionId}/_execute`)
.set('kbn-xsrf', 'foo')
.send({
params: { ...mockServiceNow.params },
params: { caseId: 'success' },
})
.expect(200);

Expand Down

0 comments on commit d992c51

Please sign in to comment.