diff --git a/src/api/middleware/validators/offer.js b/src/api/middleware/validators/offer.js index df8843d66..0593b9aae 100644 --- a/src/api/middleware/validators/offer.js +++ b/src/api/middleware/validators/offer.js @@ -2,7 +2,7 @@ const { body, query, param } = require("express-validator"); const { useExpressValidators, APIError } = require("../errorHandler"); const ValidationReasons = require("./validationReasons"); -const { valuesInSet, ensureArray, isObjectId, maxHTMLContentLength } = require("./validatorUtils"); +const { valuesInSet, ensureArray, isObjectId, maxHTMLContentLength, normalizeDate } = require("./validatorUtils"); const JobTypes = require("../../../models/constants/JobTypes"); const { FieldTypes, MIN_FIELDS, MAX_FIELDS } = require("../../../models/constants/FieldTypes"); const { TechnologyTypes, MIN_TECHNOLOGIES, MAX_TECHNOLOGIES } = require("../../../models/constants/TechnologyTypes"); @@ -69,8 +69,6 @@ const publishEndDateLimit = (publishEndDateCandidate, { req }) => { return true; }; -const normalizeDate = (date, _context) => (new Date(Date.parse(date))).toISOString(); - const create = useExpressValidators([ body("title", ValidationReasons.DEFAULT) .exists().withMessage(ValidationReasons.REQUIRED).bail() diff --git a/src/api/middleware/validators/validatorUtils.js b/src/api/middleware/validators/validatorUtils.js index 7571fad0a..04ab3215b 100644 --- a/src/api/middleware/validators/validatorUtils.js +++ b/src/api/middleware/validators/validatorUtils.js @@ -106,11 +106,14 @@ const maxHTMLContentLength = (max) => (text) => { return true; }; +const normalizeDate = (date) => (new Date(Date.parse(date))).toISOString(); + module.exports = { valuesInSet, checkDuplicatedEmail, ensureArray, isObjectId, concurrentOffersNotExceeded, - maxHTMLContentLength + maxHTMLContentLength, + normalizeDate, }; diff --git a/test/end-to-end/offer.js b/test/end-to-end/offer.js index bf8754d17..0a6ef417e 100644 --- a/test/end-to-end/offer.js +++ b/test/end-to-end/offer.js @@ -698,6 +698,41 @@ describe("Offer endpoint tests", () => { }); }); + describe("Same 'publishDate' and 'publishEndDate'", () => { + + const date = (new Date(Date.now() + (DAY_TO_MS))).toISOString(); + let offer; + + beforeAll(async () => { + // await Offer.deleteMany({}); + + offer = generateTestOffer({ + publishDate: date, + publishEndDate: date, + owner: test_company._id, + ownerName: test_company.name, + ownerLogo: test_company.logo, + }); + }); + + afterAll(async () => { + await Offer.deleteMany({ offer }); + }); + + test("should fail if 'publishDate' and 'publishEndDate' have the same value", async () => { + + const res = await request() + .post("/offers/new") + .send(withGodToken(offer)); + + expect(res.status).toBe(HTTPStatus.UNPROCESSABLE_ENTITY); + expect(res.body).toHaveProperty("error_code", ErrorTypes.VALIDATION_ERROR); + expect(res.body.errors[0]).toHaveProperty("msg", ValidationReasons.MUST_BE_AFTER("publishDate")); + expect(res.body.errors[0]).toHaveProperty("param", "publishEndDate"); + expect(res.body.errors[0]).toHaveProperty("location", "body"); + }); + }); + describe("Incomplete registration of the offer's company", () => { let incomplete_test_company; beforeAll(async () => { @@ -2004,6 +2039,48 @@ describe("Offer endpoint tests", () => { }); }); + describe("Same 'publishDate' and 'publishEndDate'", () => { + + const dateNow = (new Date(Date.now() + (DAY_TO_MS * 5))).toISOString(); + const dateAfter = (new Date(Date.now() + (DAY_TO_MS * 10))).toISOString(); + let offer; + + beforeAll(async () => { + + // await Offer.deleteMany({}); + + offer = await Offer.create(generateTestOffer({ + publishDate: dateNow, + publishEndDate: dateNow, + owner: test_company._id, + ownerName: test_company.name, + ownerLogo: test_company.logo, + })); + }); + + afterAll(async () => { + await Offer.deleteMany({ offer }); + }); + + test("should fail if 'publishDate' and 'publishEndDate' have the same value", async () => { + + const edits = { + publishDate: dateAfter, + publishEndDate: dateAfter, + }; + + const res = await request() + .post(`/offers/edit/${offer._id}`) + .send(withGodToken(edits)); + + expect(res.status).toBe(HTTPStatus.UNPROCESSABLE_ENTITY); + expect(res.body).toHaveProperty("error_code", ErrorTypes.VALIDATION_ERROR); + expect(res.body.errors[0]).toHaveProperty("msg", ValidationReasons.MUST_BE_AFTER("publishDate")); + expect(res.body.errors[0]).toHaveProperty("param", "publishEndDate"); + expect(res.body.errors[0]).toHaveProperty("location", "body"); + + }); + }); describe("testing as a company", () => {