From f6240d0108630e3c0b04920c2d71cc7b2ee008bd Mon Sep 17 00:00:00 2001 From: jose-carlos-sousa <2409jmsousa@gmail.com> Date: Thu, 15 Feb 2024 20:56:57 +0000 Subject: [PATCH] Making tests --- .env | 7 +++--- src/api/middleware/validators/offer.js | 5 ++--- src/models/Offer.js | 13 ++++++++++- test/end-to-end/offer.js | 9 ++++---- test/offer_schema.js | 31 ++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/.env b/.env index 281a1752..c886003a 100644 --- a/.env +++ b/.env @@ -33,10 +33,9 @@ TEST_LOG_REQUESTS=false ADMIN_EMAIL=ni@aefeup.pt ADMIN_PASSWORD=n1j0bs_ftw.12345 - # List of regexes or url's specifying allowed origins. Example: # ACCESS_CONTROL_ALLOW_ORIGINS=["https:\\/\\/deploy-preview-\\d+--nijobs\\.netlify\\.app", "https://nijobs.netlify.app"] -ACCESS_CONTROL_ALLOW_ORIGINS=["https:\\/\\/deploy-preview-\\d+--nijobs\\.netlify\\.app", "https://nijobs.netlify.app","https://localhost"] +ACCESS_CONTROL_ALLOW_ORIGINS= # Mail service information. If you don't provide a MAIL_FROM, no emails will be sent. The app will execute no-ops and won't crash # However, if you want to send emails, you need to fill all of the following 2 fields @@ -46,7 +45,7 @@ ACCESS_CONTROL_ALLOW_ORIGINS=["https:\\/\\/deploy-preview-\\d+--nijobs\\.netlify MAIL_FROM= # Password for email above -MAIL_FROM_PASSWORD= +MAIL_FROM_PASSWORD= # Cloudinary API URL to save images CLOUDINARY_URL= @@ -55,4 +54,4 @@ CLOUDINARY_URL= WEBSERVER_HOST=https://localhost:8087 # Path to save file uploads, the path must be relative to the root of the project - Defaults to static -UPLOAD_FOLDER= +UPLOAD_FOLDER= \ No newline at end of file diff --git a/src/api/middleware/validators/offer.js b/src/api/middleware/validators/offer.js index 2e6ee670..1f0b8141 100644 --- a/src/api/middleware/validators/offer.js +++ b/src/api/middleware/validators/offer.js @@ -85,14 +85,13 @@ export const create = useExpressValidators([ .if((value, { req }) => req.body.jobType !== "FREELANCE") .exists().withMessage(ValidationReasons.REQUIRED).bail() .isInt().withMessage(ValidationReasons.INT), - + body("jobMaxDuration", ValidationReasons.DEFAULT) - .if((value, { req }) => req.body.jobType !== "FREELANCE") .exists().withMessage(ValidationReasons.REQUIRED).bail() .isInt().withMessage(ValidationReasons.INT).bail() .custom(jobMaxDurationGreaterOrEqualThanJobMinDuration), - + body("jobStartDate", ValidationReasons.DEFAULT) .optional() diff --git a/src/models/Offer.js b/src/models/Offer.js index 054ee8c5..fa0cee66 100644 --- a/src/models/Offer.js +++ b/src/models/Offer.js @@ -32,15 +32,20 @@ const OfferSchema = new Schema({ }, jobMinDuration: { - type: Number + type: Number, + required: validateMinDuration, }, + jobMaxDuration: { + required: true, type: Number, validate: [ validateJobMaxDuration, "`jobMaxDuration` must be larger than `jobMinDuration`", + ], }, + jobStartDate: { type: Date }, description: { type: String, @@ -133,9 +138,15 @@ export function validatePublishEndDateLimit(publishDate, publishEndDate) { // jobMaxDuration must be larger than jobMinDuration function validateJobMaxDuration(value) { + if (this.jobType === "FREELANCE" && this.jobMinDuration === undefined) return true; return value >= this.jobMinDuration; } +function validateMinDuration() { + if (this.jobType === "FREELANCE") return false; + return true; +} + function validateOwnerConcurrentOffers(value) { return concurrentOffersNotExceeded(this.constructor)(value, this.publishDate, this.publishEndDate); } diff --git a/test/end-to-end/offer.js b/test/end-to-end/offer.js index a50d130a..373e102d 100644 --- a/test/end-to-end/offer.js +++ b/test/end-to-end/offer.js @@ -232,18 +232,19 @@ describe("Offer endpoint tests", () => { FieldValidatorTester.mustBeFuture(); FieldValidatorTester.mustBeAfter("publishDate"); }); - describe("jobMinDuration", () => { const FieldValidatorTester = BodyValidatorTester("jobMinDuration"); - FieldValidatorTester.mustBeNumber(); + if (BodyValidatorTester("jobType") !== "freelance") { + FieldValidatorTester.isRequired(); + FieldValidatorTester.mustBeNumber(); + } }); - describe("jobMaxDuration", () => { const FieldValidatorTester = BodyValidatorTester("jobMaxDuration"); + FieldValidatorTester.isRequired(); FieldValidatorTester.mustBeNumber(); FieldValidatorTester.mustBeGreaterThanOrEqualToField("jobMinDuration"); }); - describe("jobStartDate", () => { const FieldValidatorTester = BodyValidatorTester("jobStartDate"); FieldValidatorTester.mustBeDate(); diff --git a/test/offer_schema.js b/test/offer_schema.js index dbc1949b..1694974b 100644 --- a/test/offer_schema.js +++ b/test/offer_schema.js @@ -129,6 +129,37 @@ describe("# Offer Schema tests", () => { }); }); + test("'jobMinDuration' is required if type offer different than freelance", async () => { + const offer = new Offer({}); + try { + await offer.validate(); + } catch (err) { + expect(err.errors.jobMinDuration).toBeDefined(); + expect(err.errors.jobMinDuration).toHaveProperty("kind", "required"); + expect(err.errors.jobMinDuration).toHaveProperty("message", "Path `jobMinDuration` is required."); + } + }); + + test("'jobMinDuration' is not required for 'FREELANCE' job type", async () => { + const offer = new Offer({ jobType: "FREELANCE" }); + try { + await offer.validate(); + } catch (err) { + expect(err.errors.jobMinDuration).toBeFalsy(); + } + }); + + test("'jobMaxDuration' is required", async () => { + const offer = new Offer({}); + try { + await offer.validate(); + } catch (err) { + expect(err.errors.jobMaxDuration).toBeDefined(); + expect(err.errors.jobMaxDuration).toHaveProperty("kind", "required"); + expect(err.errors.jobMaxDuration).toHaveProperty("message", "Path `jobMaxDuration` is required."); + } + }); + describe("required using custom validators (checking for array lengths, etc)", () => { describe(`'fields' must have between ${MIN_FIELDS} and ${MAX_FIELDS} values`, () => { test("Below minimum should throw error", async () => {