Skip to content

Commit f6240d0

Browse files
Making tests
1 parent 049f332 commit f6240d0

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

.env

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ TEST_LOG_REQUESTS=false
3333
ADMIN_EMAIL=ni@aefeup.pt
3434
ADMIN_PASSWORD=n1j0bs_ftw.12345
3535

36-
3736
# List of regexes or url's specifying allowed origins. Example:
3837
# ACCESS_CONTROL_ALLOW_ORIGINS=["https:\\/\\/deploy-preview-\\d+--nijobs\\.netlify\\.app", "https://nijobs.netlify.app"]
39-
ACCESS_CONTROL_ALLOW_ORIGINS=["https:\\/\\/deploy-preview-\\d+--nijobs\\.netlify\\.app", "https://nijobs.netlify.app","https://localhost"]
38+
ACCESS_CONTROL_ALLOW_ORIGINS=
4039

4140
# 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
4241
# 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
4645
MAIL_FROM=
4746

4847
# Password for email above
49-
MAIL_FROM_PASSWORD=
48+
MAIL_FROM_PASSWORD=
5049

5150
# Cloudinary API URL to save images
5251
CLOUDINARY_URL=
@@ -55,4 +54,4 @@ CLOUDINARY_URL=
5554
WEBSERVER_HOST=https://localhost:8087
5655

5756
# Path to save file uploads, the path must be relative to the root of the project - Defaults to static
58-
UPLOAD_FOLDER=
57+
UPLOAD_FOLDER=

src/api/middleware/validators/offer.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,13 @@ export const create = useExpressValidators([
8585
.if((value, { req }) => req.body.jobType !== "FREELANCE")
8686
.exists().withMessage(ValidationReasons.REQUIRED).bail()
8787
.isInt().withMessage(ValidationReasons.INT),
88-
88+
8989

9090
body("jobMaxDuration", ValidationReasons.DEFAULT)
91-
.if((value, { req }) => req.body.jobType !== "FREELANCE")
9291
.exists().withMessage(ValidationReasons.REQUIRED).bail()
9392
.isInt().withMessage(ValidationReasons.INT).bail()
9493
.custom(jobMaxDurationGreaterOrEqualThanJobMinDuration),
95-
94+
9695

9796
body("jobStartDate", ValidationReasons.DEFAULT)
9897
.optional()

src/models/Offer.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,20 @@ const OfferSchema = new Schema({
3232
},
3333

3434
jobMinDuration: {
35-
type: Number
35+
type: Number,
36+
required: validateMinDuration,
3637
},
38+
3739
jobMaxDuration: {
40+
required: true,
3841
type: Number,
3942
validate: [
4043
validateJobMaxDuration,
4144
"`jobMaxDuration` must be larger than `jobMinDuration`",
45+
4246
],
4347
},
48+
4449
jobStartDate: { type: Date },
4550
description: {
4651
type: String,
@@ -133,9 +138,15 @@ export function validatePublishEndDateLimit(publishDate, publishEndDate) {
133138

134139
// jobMaxDuration must be larger than jobMinDuration
135140
function validateJobMaxDuration(value) {
141+
if (this.jobType === "FREELANCE" && this.jobMinDuration === undefined) return true;
136142
return value >= this.jobMinDuration;
137143
}
138144

145+
function validateMinDuration() {
146+
if (this.jobType === "FREELANCE") return false;
147+
return true;
148+
}
149+
139150
function validateOwnerConcurrentOffers(value) {
140151
return concurrentOffersNotExceeded(this.constructor)(value, this.publishDate, this.publishEndDate);
141152
}

test/end-to-end/offer.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,19 @@ describe("Offer endpoint tests", () => {
232232
FieldValidatorTester.mustBeFuture();
233233
FieldValidatorTester.mustBeAfter("publishDate");
234234
});
235-
236235
describe("jobMinDuration", () => {
237236
const FieldValidatorTester = BodyValidatorTester("jobMinDuration");
238-
FieldValidatorTester.mustBeNumber();
237+
if (BodyValidatorTester("jobType") !== "freelance") {
238+
FieldValidatorTester.isRequired();
239+
FieldValidatorTester.mustBeNumber();
240+
}
239241
});
240-
241242
describe("jobMaxDuration", () => {
242243
const FieldValidatorTester = BodyValidatorTester("jobMaxDuration");
244+
FieldValidatorTester.isRequired();
243245
FieldValidatorTester.mustBeNumber();
244246
FieldValidatorTester.mustBeGreaterThanOrEqualToField("jobMinDuration");
245247
});
246-
247248
describe("jobStartDate", () => {
248249
const FieldValidatorTester = BodyValidatorTester("jobStartDate");
249250
FieldValidatorTester.mustBeDate();

test/offer_schema.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,37 @@ describe("# Offer Schema tests", () => {
129129
});
130130
});
131131

132+
test("'jobMinDuration' is required if type offer different than freelance", async () => {
133+
const offer = new Offer({});
134+
try {
135+
await offer.validate();
136+
} catch (err) {
137+
expect(err.errors.jobMinDuration).toBeDefined();
138+
expect(err.errors.jobMinDuration).toHaveProperty("kind", "required");
139+
expect(err.errors.jobMinDuration).toHaveProperty("message", "Path `jobMinDuration` is required.");
140+
}
141+
});
142+
143+
test("'jobMinDuration' is not required for 'FREELANCE' job type", async () => {
144+
const offer = new Offer({ jobType: "FREELANCE" });
145+
try {
146+
await offer.validate();
147+
} catch (err) {
148+
expect(err.errors.jobMinDuration).toBeFalsy();
149+
}
150+
});
151+
152+
test("'jobMaxDuration' is required", async () => {
153+
const offer = new Offer({});
154+
try {
155+
await offer.validate();
156+
} catch (err) {
157+
expect(err.errors.jobMaxDuration).toBeDefined();
158+
expect(err.errors.jobMaxDuration).toHaveProperty("kind", "required");
159+
expect(err.errors.jobMaxDuration).toHaveProperty("message", "Path `jobMaxDuration` is required.");
160+
}
161+
});
162+
132163
describe("required using custom validators (checking for array lengths, etc)", () => {
133164
describe(`'fields' must have between ${MIN_FIELDS} and ${MAX_FIELDS} values`, () => {
134165
test("Below minimum should throw error", async () => {

0 commit comments

Comments
 (0)