Skip to content

Commit 08a3ecc

Browse files
committed
Finished input validation tests for /company/:id/edit
1 parent b05cb13 commit 08a3ecc

File tree

3 files changed

+72
-11
lines changed

3 files changed

+72
-11
lines changed

codecov.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
target: 80%
6+
threshold: 5%

src/api/middleware/validators/company.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ export const edit = useExpressValidators([
130130
.withMessage(ValidationReasons.TOO_LONG(CompanyConstants.bio.max_length)),
131131
body("contacts", ValidationReasons.DEFAULT)
132132
.optional()
133-
.customSanitizer(ensureArray)
133+
.isArray().withMessage(ValidationReasons.ARRAY).bail()
134134
.isArray({ min: CompanyConstants.contacts.min_length, max: CompanyConstants.contacts.max_length })
135-
.withMessage(ValidationReasons.ARRAY_SIZE(CompanyConstants.contacts.min_length, CompanyConstants.contacts.max_length)),
135+
.withMessage(ValidationReasons.ARRAY_SIZE(CompanyConstants.contacts.min_length, CompanyConstants.contacts.max_length))
136+
.customSanitizer(ensureArray),
136137
body("logo", ValidationReasons.DEFAULT)
137138
.optional()
138139
.isString().withMessage(ValidationReasons.STRING).bail()

test/end-to-end/company/:id/edit.js

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import ValidationReasons from "../../../../src/api/middleware/validators/validat
33
import hash from "../../../../src/lib/passwordHashing";
44
import Account from "../../../../src/models/Account";
55
import Company from "../../../../src/models/Company";
6+
import CompanyConstants from "../../../../src/models/constants/Company";
67
import Offer from "../../../../src/models/Offer";
78
import withGodToken from "../../../utils/GodToken";
9+
import ValidatorTester from "../../../utils/ValidatorTester";
810
import { DAY_TO_MS } from "../../../utils/TimeConstants";
911

1012
describe("PUT /company/edit", () => {
@@ -27,10 +29,21 @@ describe("PUT /company/edit", () => {
2729
contacts: ["123", "456"],
2830
};
2931

32+
const test_user_admin = {
33+
email: "admin@email.com",
34+
password: "password123",
35+
};
36+
3037
beforeAll(async () => {
3138
await Account.deleteMany({});
3239
await Company.deleteMany({});
3340
await Offer.deleteMany({});
41+
42+
await Account.create({
43+
email: test_user_admin.email,
44+
password: await hash(test_user_admin.password),
45+
isAdmin: true,
46+
});
3447
});
3548

3649
afterAll(async () => {
@@ -76,6 +89,56 @@ describe("PUT /company/edit", () => {
7689
});
7790
});
7891

92+
describe("Field Validation", () => {
93+
94+
const company_data = {
95+
name: "Test Company",
96+
logo: "http://awebsite.com/alogo.jpg",
97+
};
98+
99+
let company;
100+
101+
beforeAll(async () => {
102+
company = await Company.create(company_data);
103+
});
104+
105+
afterAll(async () => {
106+
await Company.deleteMany({ name: company.name });
107+
});
108+
109+
const EndpointValidatorTester = ValidatorTester(
110+
(params) => request().put(`/company/${company._id}/edit`).send(withGodToken(params))
111+
);
112+
const BodyValidatorTester = EndpointValidatorTester("body");
113+
114+
describe("name", () => {
115+
const FieldValidatorTester = BodyValidatorTester("name");
116+
117+
FieldValidatorTester.mustBeString();
118+
FieldValidatorTester.hasMaxLength(CompanyConstants.companyName.max_length);
119+
FieldValidatorTester.hasMinLength(CompanyConstants.companyName.min_length);
120+
});
121+
122+
describe("bio", () => {
123+
const FieldValidatorTester = BodyValidatorTester("bio");
124+
125+
FieldValidatorTester.mustBeString();
126+
FieldValidatorTester.hasMaxLength(CompanyConstants.bio.max_length);
127+
});
128+
129+
describe("contacts", () => {
130+
const FieldValidatorTester = BodyValidatorTester("contacts");
131+
132+
FieldValidatorTester.mustBeArray();
133+
// FieldValidatorTester.mustHaveAtLeast(CompanyConstants.contacts.min_length);
134+
FieldValidatorTester.mustBeArrayBetween(CompanyConstants.contacts.min_length, CompanyConstants.contacts.max_length);
135+
});
136+
137+
describe("logo", () => {
138+
// TODO: Add tests for logo when the route has multer middleware to handle file uploads
139+
});
140+
});
141+
79142
describe("Without auth", () => {
80143

81144
const company_data = generateTestCompany({
@@ -109,10 +172,6 @@ describe("PUT /company/edit", () => {
109172
});
110173

111174
describe("With auth", () => {
112-
const test_user_admin = {
113-
email: "admin@email.com",
114-
password: "password123",
115-
};
116175

117176
const test_user_company_1 = {
118177
email: "company1@email.com",
@@ -148,11 +207,6 @@ describe("PUT /company/edit", () => {
148207
]);
149208

150209
await Account.create([
151-
{
152-
email: test_user_admin.email,
153-
password: await hash(test_user_admin.password),
154-
isAdmin: true,
155-
},
156210
{
157211
email: test_user_company_1.email,
158212
password: await hash(test_user_company_1.password),

0 commit comments

Comments
 (0)