UNPKG

@unito/integration-debugger

Version:

The Unito Integration Debugger

96 lines (95 loc) 3.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const crawler_1 = require("../crawler"); const helpers_1 = require("./helpers"); /** * Check: Update Item - Invalid * * The Specification defines specific error codes that can be returned in case of failure for a given action * This check will generate invalid update 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 `PATCH /foos/${existingId}` with the following invalid payload: * * ```json * { * "value": "<random string>" * } * ``` * ... the integration should return a 400 or 422 error code. */ const check = { label: 'UpdateItem - Invalid', prepareOnPreparedSteps: false, validateOnError: true, activatedByDefault: false, prepare: async (stepResult, crawlerDriver) => { const step = stepResult.step; if (step.operation !== crawler_1.Operation.GetCollection || !step.schemaPath || !step.payloadOut) { return []; } const schema = crawlerDriver.getRelationSchema(step.schemaPath); if (schema && schema.canUpdateItem) { // 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)(schema.fields); const itemSummaries = step.payloadOut.data; return [ { path: itemSummaries[0]?.path, requestSchema: undefined, schemaPath: step.schemaPath, parentOperation: step.operation, parentPath: step.path, operation: crawler_1.Operation.UpdateItem, payloadIn: invalidItem, headersIn: step.headersIn, warnings: [], errors: [], }, ]; } return []; }, validate: (step, _crawlerDriver) => { if (!step.context || step.context.name !== 'UpdateItemInvalid') { 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;