@unito/integration-debugger
Version:
The Unito Integration Debugger
58 lines (57 loc) • 2.59 kB
JavaScript
// Check: An integration answers a "400 Bad Request" when passed missing credentials.
//
// Each request receives a X-Unito-Credentials header which contains a
// Base64-encoded JSON of the credentials required to contact the provider.
//
// The integration must return a 400 Bad Request when there are missing information in the credentials.
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const headers_1 = __importDefault(require("../../resources/headers"));
function extractCredentials(step) {
if (!step.headersIn || !step.headersIn[headers_1.default.CREDENTIALS]) {
return;
}
return JSON.parse(Buffer.from(step.headersIn[headers_1.default.CREDENTIALS], 'base64').toString('utf8'));
}
const check = {
label: 'Error - Missing credentials',
prepareOnPreparedSteps: false,
validateOnError: true,
activatedByDefault: false,
prepare: async (stepResult, _crawlerDriver) => {
const step = stepResult.step;
const credentials = extractCredentials(step);
// This check is only performed when credentials were meant to sent to the integration.
if (!credentials || Object.keys(credentials).length === 0) {
return [];
}
step.headersIn ??= {};
// We inject a key in the credentials to know later on that those credentials are supposed to be invalid.
step.headersIn[headers_1.default.CREDENTIALS] = Buffer.from(JSON.stringify({ integrationDebuggerCheck: true })).toString('base64');
return [step];
},
validate: (step, _crawlerDriver) => {
const credentials = extractCredentials(step);
if (!credentials || !credentials.integrationDebuggerCheck) {
return;
}
const payload = step.payloadOut;
const isError = payload?.code && payload?.message;
if (isError && payload?.code !== '400') {
step.errors.push({
keyword: 'unito',
message: `Return a 400 Bad Request response when the credentials are missing or cannot be parsed.`,
detailedMessage: `Your integration was sent missing credentials but did not respond with a 400 Bad Request`,
instancePath: step.path,
schemaPath: step.schemaPath ?? '',
params: {
code: 'MISSING_CREDENTIALS_UNHANDLED',
},
});
}
},
};
exports.default = check;
;