UNPKG

@unito/integration-debugger

Version:

The Unito Integration Debugger

96 lines (95 loc) 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const crawler_1 = require("../crawler"); const helpers_1 = require("./helpers"); /** * Check: Create Invalid Item * * The Specification defines specific error codes that can be returned in case of failure for a given action * This check will generate invalid create item request and validate that the integration returns a valid error. * @example * For the following item... * * ```json * { * "fields": {}, * "relations": [{ * "name": "foos", * "label": "Foos", * "path": "/foos", * "schema": { * "canCreateItem": true, * "fields": [{ * "name": "value", * "type": "boolean", * "readOnly": false * }] * } * }] * } * ``` * * ... trying a `POST /foos` with the following invalid payload: * * ```json * { * "value": "<random string>" * } * ``` * ... the integration should return a 400 or 422 error code. */ const check = { label: 'CreateItem - Invalid', prepareOnPreparedSteps: false, validateOnError: true, activatedByDefault: false, prepare: async (stepResult, _crawlerDriver) => { const step = stepResult.step; const preparedSteps = []; if (step.operation !== crawler_1.Operation.GetItem || !step.payloadOut) { return preparedSteps; } const { relations } = step.payloadOut; for (const relation of relations) { if (relation.schema.canCreateItem) { // We assume passing an item with an attribute name as a function should be enough to result in a failure const invalidItem = (0, helpers_1.createInvalidItem)(relation.schema.fields); preparedSteps.push({ path: relation.path, requestSchema: undefined, schemaPath: (0, crawler_1.buildSchemaUniqueId)(step.path, relation.name), parentOperation: step.operation, parentPath: step.path, operation: crawler_1.Operation.CreateItem, payloadIn: invalidItem, headersIn: step.headersIn, warnings: [], errors: [], }); } } return preparedSteps; }, validate: (step, _crawlerDriver) => { if (!step.context || step.context.name !== 'CreateItemInvalid') { return; } const payload = step.payloadOut; const isError = payload?.code && payload?.message; // First clear original errors as they are expected, we want to validate that they had the right error code. step.errors = []; if (isError && payload?.code !== '400' && payload?.code !== '422') { step.errors.push({ keyword: 'unito', message: `Return a '400' Bad Request or '422' Unprocessable Entity response when a ${step.operation} fails`, detailedMessage: `Your integration failed to complete a ${step.operation} and responded with ${payload.code} instead of the expected '400' or '422'.`, instancePath: step.path, schemaPath: step.schemaPath ?? '', params: { code: 'UNSUPPORTED_ERROR_CODE', }, }); } }, }; exports.default = check;