This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+19
View File
@@ -0,0 +1,19 @@
const schemaMatcher = require('./schema-matcher');
const statusCodeMatcher = require('./status-matcher');
const coverage = require('../../helpers/coverage');
const { messages } = require('../../helpers/common');
module.exports = function getChaiPlugin(options) {
if (!(options instanceof Object) || !options.apiDefinitionsPath) {
throw new Error(messages.REQUIRED_API_DEFINITIONS_PATH);
}
coverage.init(options);
return function apiSchemaPlugin(chai) {
const { Assertion } = chai;
schemaMatcher(Assertion, options);
statusCodeMatcher(Assertion);
};
};
@@ -0,0 +1,20 @@
const validators = require('../../validators');
module.exports = (Assertion, options) => {
Assertion.addMethod('matchApiSchema', function addApiSchemaMethod(apiDefinitionsPath) {
const {
predicate, actual, expected, matchMsg, noMatchMsg,
} = validators.schemaValidator(
this._obj,
{ ...options, apiDefinitionsPath: apiDefinitionsPath || options.apiDefinitionsPath },
);
this.assert(
predicate,
matchMsg,
noMatchMsg,
expected,
actual,
);
});
};
@@ -0,0 +1,31 @@
const validators = require('../../validators');
module.exports = (Assertion) => {
buildMatcher('successful', 200);
buildMatcher('created', 201);
buildMatcher('badRequest', 400);
buildMatcher('unauthorized', 401);
buildMatcher('forbidden', 403);
buildMatcher('notFound', 404);
buildMatcher('serverError', 500);
buildMatcher('serviceUnavailable', 503);
buildMatcher('gatewayTimeout', 504);
buildMatcher('status');
function buildMatcher(str, statusToTest) {
Assertion.addMethod(str, function statusCodeMatcher(customStatus) {
const expectedStatus = statusToTest || customStatus;
const {
predicate, actual, expected, matchMsg, noMatchMsg,
} = validators.statusValidator(expectedStatus, this._obj);
this.assert(
predicate,
matchMsg,
noMatchMsg,
expected,
actual,
);
});
}
};