Skip to content

Commit

Permalink
Finished input validation tests for /company/:id/edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Naapperas committed Apr 25, 2023
1 parent b05cb13 commit 08a3ecc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
6 changes: 6 additions & 0 deletions codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage:
status:
project:
default:
target: 80%
threshold: 5%
5 changes: 3 additions & 2 deletions src/api/middleware/validators/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ export const edit = useExpressValidators([
.withMessage(ValidationReasons.TOO_LONG(CompanyConstants.bio.max_length)),
body("contacts", ValidationReasons.DEFAULT)
.optional()
.customSanitizer(ensureArray)
.isArray().withMessage(ValidationReasons.ARRAY).bail()
.isArray({ min: CompanyConstants.contacts.min_length, max: CompanyConstants.contacts.max_length })
.withMessage(ValidationReasons.ARRAY_SIZE(CompanyConstants.contacts.min_length, CompanyConstants.contacts.max_length)),
.withMessage(ValidationReasons.ARRAY_SIZE(CompanyConstants.contacts.min_length, CompanyConstants.contacts.max_length))
.customSanitizer(ensureArray),
body("logo", ValidationReasons.DEFAULT)
.optional()
.isString().withMessage(ValidationReasons.STRING).bail()
Expand Down
72 changes: 63 additions & 9 deletions test/end-to-end/company/:id/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import ValidationReasons from "../../../../src/api/middleware/validators/validat
import hash from "../../../../src/lib/passwordHashing";
import Account from "../../../../src/models/Account";
import Company from "../../../../src/models/Company";
import CompanyConstants from "../../../../src/models/constants/Company";
import Offer from "../../../../src/models/Offer";
import withGodToken from "../../../utils/GodToken";
import ValidatorTester from "../../../utils/ValidatorTester";
import { DAY_TO_MS } from "../../../utils/TimeConstants";

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

const test_user_admin = {
email: "[email protected]",
password: "password123",
};

beforeAll(async () => {
await Account.deleteMany({});
await Company.deleteMany({});
await Offer.deleteMany({});

await Account.create({
email: test_user_admin.email,
password: await hash(test_user_admin.password),
isAdmin: true,
});
});

afterAll(async () => {
Expand Down Expand Up @@ -76,6 +89,56 @@ describe("PUT /company/edit", () => {
});
});

describe("Field Validation", () => {

const company_data = {
name: "Test Company",
logo: "http://awebsite.com/alogo.jpg",
};

let company;

beforeAll(async () => {
company = await Company.create(company_data);
});

afterAll(async () => {
await Company.deleteMany({ name: company.name });
});

const EndpointValidatorTester = ValidatorTester(
(params) => request().put(`/company/${company._id}/edit`).send(withGodToken(params))
);
const BodyValidatorTester = EndpointValidatorTester("body");

describe("name", () => {
const FieldValidatorTester = BodyValidatorTester("name");

FieldValidatorTester.mustBeString();
FieldValidatorTester.hasMaxLength(CompanyConstants.companyName.max_length);
FieldValidatorTester.hasMinLength(CompanyConstants.companyName.min_length);
});

describe("bio", () => {
const FieldValidatorTester = BodyValidatorTester("bio");

FieldValidatorTester.mustBeString();
FieldValidatorTester.hasMaxLength(CompanyConstants.bio.max_length);
});

describe("contacts", () => {
const FieldValidatorTester = BodyValidatorTester("contacts");

FieldValidatorTester.mustBeArray();
// FieldValidatorTester.mustHaveAtLeast(CompanyConstants.contacts.min_length);
FieldValidatorTester.mustBeArrayBetween(CompanyConstants.contacts.min_length, CompanyConstants.contacts.max_length);
});

describe("logo", () => {
// TODO: Add tests for logo when the route has multer middleware to handle file uploads
});
});

describe("Without auth", () => {

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

describe("With auth", () => {
const test_user_admin = {
email: "[email protected]",
password: "password123",
};

const test_user_company_1 = {
email: "[email protected]",
Expand Down Expand Up @@ -148,11 +207,6 @@ describe("PUT /company/edit", () => {
]);

await Account.create([
{
email: test_user_admin.email,
password: await hash(test_user_admin.password),
isAdmin: true,
},
{
email: test_user_company_1.email,
password: await hash(test_user_company_1.password),
Expand Down

0 comments on commit 08a3ecc

Please sign in to comment.