Skip to content

Commit

Permalink
Added tests to verify the wrong behavior does not occur anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Naapperas committed Aug 9, 2021
1 parent 48415ac commit 83f315e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/api/middleware/validators/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion src/api/middleware/validators/validatorUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
77 changes: 77 additions & 0 deletions test/end-to-end/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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", () => {

Expand Down

0 comments on commit 83f315e

Please sign in to comment.