Skip to content

Commit

Permalink
Making tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-carlos-sousa committed Feb 28, 2024
1 parent c58395e commit 6c46143
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
7 changes: 3 additions & 4 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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=
Expand All @@ -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=
5 changes: 2 additions & 3 deletions src/api/middleware/validators/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 12 additions & 1 deletion src/models/Offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 5 additions & 4 deletions test/end-to-end/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
31 changes: 31 additions & 0 deletions test/offer_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 6c46143

Please sign in to comment.