From 6c51b066fa520a6314f9a0fe562e7c8b5dfc41f2 Mon Sep 17 00:00:00 2001 From: Giovanni Fiordeponti <38134891+flower-of-the-bridges@users.noreply.github.com> Date: Mon, 26 Aug 2024 13:01:41 +0200 Subject: [PATCH] add: compiler tests (#341) --- package-lock.json | 14 ++++---- package.json | 3 +- tests/compilers.test.js | 77 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 tests/compilers.test.js diff --git a/package-lock.json b/package-lock.json index ae0ec078..f728bf77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,20 +22,20 @@ "jsonpath-plus": "^9.0.0", "JSONStream": "^1.3.5", "lodash": "^4.17.21", - "mongodb-client-encryption": "^6.0.1", + "mongodb-client-encryption": "^6.1.0", "ndjson": "^2.0.0", "p-limit": "^6.1.0", "pino": "^9.3.2", "through2": "^4.0.2", - "xlsx-write-stream": "^1.0.0" + "xlsx-write-stream": "^1.0.3" }, "devDependencies": { - "@eslint/js": "^9.8.0", + "@eslint/js": "^9.9.0", "@faker-js/faker": "^8.4.1", "@mia-platform/eslint-config-mia": "^3.0.0", "abstract-logging": "^2.0.1", "commander": "^12.1.0", - "eslint": "^9.8.0", + "eslint": "^9.9.0", "eslint-plugin-n": "^17.10.2", "fastbench": "^1.0.1", "form-data": "^4.0.0", @@ -44,7 +44,7 @@ "mongodb": "^6.8.0", "node-xlsx": "^0.24.0", "pre-commit": "^1.2.2", - "tap": "^21.0.0", + "tap": "^21.0.1", "uuid-validate": "0.0.3" }, "engines": { @@ -52,7 +52,8 @@ }, "peerDependencies": { "ajv": "^8.17.1", - "fast-json-stringify": "^6.0.0" + "fast-json-stringify": "^6.0.0", + "fastify": "^4.28.1" } }, "node_modules/@alcalzone/ansi-tokenize": { @@ -6305,7 +6306,6 @@ "url": "https://opencollective.com/fastify" } ], - "license": "MIT", "dependencies": { "@fastify/ajv-compiler": "^3.5.0", "@fastify/error": "^3.4.0", diff --git a/package.json b/package.json index a98de576..b87fc4af 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,8 @@ }, "peerDependencies": { "ajv": "^8.17.1", - "fast-json-stringify": "^6.0.0" + "fast-json-stringify": "^6.0.0", + "fastify": "^4.28.1" }, "devDependencies": { "@eslint/js": "^9.9.0", diff --git a/tests/compilers.test.js b/tests/compilers.test.js new file mode 100644 index 00000000..42a7900d --- /dev/null +++ b/tests/compilers.test.js @@ -0,0 +1,77 @@ +/* + * Copyright 2024 Mia s.r.l. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict' + +const tap = require('tap') +const Fastify = require('fastify') +const { addValidatorCompiler } = require('../lib/compilers') +const books = require('./collectionDefinitions/books') +const loadModels = require('../lib/loadModels') +const { getMongoDatabaseName, getMongoURL } = require('./utils') +const { registerMongoInstances } = require('../lib/mongo/mongo-plugin') +const { SCHEMAS_ID } = require('../lib/schemaGetters') + + +const SCHEMA_CUSTOM_KEYWORDS = { + UNIQUE_OPERATION_ID: 'operationId', +} + +tap.test('compilers', async(t) => { + const databaseName = getMongoDatabaseName() + + t.test('validator compiler should extract property correctly without collisions', async t => { + const fastify = Fastify() + + t.teardown(async() => fastify.close()) + + fastify.decorate('config', { MONGODB_URL: getMongoURL(databaseName) }) + fastify.decorate('collections', [books]) + fastify.decorate('modelName', 'books-endpoint') + await registerMongoInstances(fastify) + + + await loadModels(fastify) + + //* here i add a fake model to test collision + // eslint-disable-next-line require-atomic-updates + fastify.models['books-endpoint-2'] = {} + + addValidatorCompiler(fastify, fastify.models, { HELPERS_PREFIX: '/helpers' }) + + const schema = { + [SCHEMA_CUSTOM_KEYWORDS.UNIQUE_OPERATION_ID]: `books__MIA__${SCHEMAS_ID.POST_ITEM}__MIA__response.200`, + type: 'object', + properties: { + id: { + type: 'string', + }, + }, + required: ['id'], + } + + const url = '/test-url' + + const validationFunc = fastify.validatorCompiler({ schema, url }) + + t.ok(validationFunc, 'Validator function should be created') + t.equal(typeof validationFunc, 'function', 'Validator function should be a function') + //* here we test that post schema from model is recognized correctly. + //* we pass an empty object for ok validation and a wrong formatted _id for ko validation + t.ok(validationFunc({}), 'Validation should return true with invalid obj') + t.notOk(validationFunc({ _id: 'a' }), 'Validation should return false with invalid obj') + }) +})