Skip to content

Commit

Permalink
Started reworking company enablement tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Naapperas committed Jun 20, 2023
1 parent 6c3bfb4 commit 623d81a
Show file tree
Hide file tree
Showing 2 changed files with 313 additions and 271 deletions.
270 changes: 0 additions & 270 deletions test/end-to-end/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ErrorTypes } from "../../src/api/middleware/errorHandler";
import ValidationReasons from "../../src/api/middleware/validators/validationReasons";
import {
COMPANY_BLOCKED_NOTIFICATION,
COMPANY_ENABLED_NOTIFICATION,
} from "../../src/email-templates/companyManagement";
import EmailService from "../../src/lib/emailService";
import hash from "../../src/lib/passwordHashing";
Expand Down Expand Up @@ -292,273 +291,4 @@ describe("Company endpoint", () => {
});
});
});

describe("PUT /company/enable", () => {

let disabled_test_company_1, disabled_test_company_2, disabled_test_company_3, disabled_test_company_4, disabled_test_company_mail;
const test_user_admin = {
email: "[email protected]",
password: "password123",
};
const test_user_company_1 = {
email: "[email protected]",
password: "password123",
};
const test_user_company_2 = {
email: "[email protected]",
password: "password123",
};
const test_user_company_3 = {
email: "[email protected]",
password: "password123",
};
const test_user_company_4 = {
email: "[email protected]",
password: "password123",
};
const test_user_mail = {
email: "[email protected]",
password: "password123",
};

const test_agent = agent();

beforeAll(async () => {
await test_agent
.delete("/auth/login")
.expect(HTTPStatus.OK);

await Company.deleteMany({});

[
disabled_test_company_1,
disabled_test_company_2,
disabled_test_company_3,
disabled_test_company_4,
disabled_test_company_mail
] = await Company.create([
{
name: "disabled-test-company-1",
isDisabled: true,
hasFinishedRegistration: true
}, {
name: "disabled-test-company-2",
isDisabled: true,
hasFinishedRegistration: true
}, {
name: "disabled-test-company-3",
isDisabled: true,
hasFinishedRegistration: true
}, {
name: "disabled-test-company-4",
logo: "http://awebsite.com/alogo.jpg",
isDisabled: true,
hasFinishedRegistration: true
}, {
name: "disabled-test-company-mail",
isDisabled: true,
hasFinishedRegistration: true
}
]);

await Account.deleteMany({});
await Account.create({
email: test_user_admin.email,
password: await hash(test_user_admin.password),
isAdmin: true
});
await Account.create({
email: test_user_company_1.email,
password: await hash(test_user_company_1.password),
company: disabled_test_company_1._id
});
await Account.create({
email: test_user_company_2.email,
password: await hash(test_user_company_2.password),
company: disabled_test_company_2._id
});
await Account.create({
email: test_user_company_3.email,
password: await hash(test_user_company_3.password),
company: disabled_test_company_3._id
});
await Account.create({
email: test_user_company_4.email,
password: await hash(test_user_company_4.password),
company: disabled_test_company_4._id
});
await Account.create({
email: test_user_mail.email,
password: await hash(test_user_mail.password),
company: disabled_test_company_mail._id
});

const offer = {
title: "Test Offer",
publishDate: new Date(Date.now()),
publishEndDate: new Date(Date.now() + (DAY_TO_MS)),
description: "For Testing Purposes",
contacts: ["[email protected]", "229417766"],
jobType: "SUMMER INTERNSHIP",
jobMinDuration: 1,
jobMaxDuration: 6,
fields: ["DEVOPS", "BACKEND", "OTHER"],
technologies: ["React", "CSS"],
location: "Testing Street, Test City, 123",
requirements: ["The candidate must be tested", "Fluent in testJS"],
owner: disabled_test_company_4._id,
ownerName: disabled_test_company_4.name,
ownerLogo: disabled_test_company_4.logo,
isHidden: true,
hiddenReason: OfferConstants.HiddenOfferReasons.COMPANY_DISABLED,
};

await Offer.create([offer, offer]);

});

afterEach(async () => {
await test_agent
.delete("/auth/login")
.expect(HTTPStatus.OK);
});

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

describe("Id validation", () => {
test("Should fail if using invalid id", async () => {

const res = await test_agent
.put("/company/123/enable")
.send(withGodToken())
.expect(HTTPStatus.UNPROCESSABLE_ENTITY);
expect(res.body).toHaveProperty("error_code", ErrorTypes.VALIDATION_ERROR);
expect(res.body).toHaveProperty("errors");
expect(res.body.errors[0]).toHaveProperty("param", "companyId");
expect(res.body.errors[0]).toHaveProperty("msg", ValidationReasons.OBJECT_ID);
});

test("Should fail if company does not exist", async () => {

const id = "111111111111111111111111";
const res = await test_agent
.put(`/company/${id}/enable`)
.send(withGodToken())
.expect(HTTPStatus.UNPROCESSABLE_ENTITY);

expect(res.body).toHaveProperty("error_code", ErrorTypes.VALIDATION_ERROR);
expect(res.body).toHaveProperty("errors");
expect(res.body.errors[0]).toHaveProperty("param", "companyId");
expect(res.body.errors[0]).toHaveProperty("msg", ValidationReasons.COMPANY_NOT_FOUND(id));
});
});

test("should fail to enable if logged as different company", async () => {

await test_agent
.post("/auth/login")
.send(test_user_company_2)
.expect(HTTPStatus.OK);

const res = await test_agent
.put(`/company/${disabled_test_company_1._id}/enable`);

expect(res.status).toBe(HTTPStatus.FORBIDDEN);
expect(res.body.error_code).toBe(ErrorTypes.FORBIDDEN);
expect(res.body.errors).toContainEqual({ msg: ValidationReasons.INSUFFICIENT_PERMISSIONS_COMPANY_SETTINGS });

});

test("should fail to enable if not logged", async () => {

const res = await test_agent
.put(`/company/${disabled_test_company_1._id}/enable`);

expect(res.status).toBe(HTTPStatus.UNAUTHORIZED);
expect(res.body.error_code).toBe(ErrorTypes.FORBIDDEN);
expect(res.body.errors).toContainEqual({ msg: ValidationReasons.INSUFFICIENT_PERMISSIONS });

});

test("Should enable company if god token is sent", async () => {

const res = await test_agent
.put(`/company/${disabled_test_company_3._id}/enable`)
.send(withGodToken());

expect(res.status).toBe(HTTPStatus.OK);
expect(res.body.isDisabled).toBe(false);
});

test("Should enable company if logged as admin", async () => {

await test_agent
.post("/auth/login")
.send(test_user_admin)
.expect(HTTPStatus.OK);

const res = await test_agent
.put(`/company/${disabled_test_company_2._id}/enable`);

expect(res.status).toBe(HTTPStatus.OK);
expect(res.body.isDisabled).toBe(false);
});

test("Should enable company if logged as same company", async () => {

await test_agent
.post("/auth/login")
.send(test_user_company_1)
.expect(HTTPStatus.OK);

const res = await test_agent
.put(`/company/${disabled_test_company_1._id}/enable`);

expect(res.status).toBe(HTTPStatus.OK);
expect(res.body.isDisabled).toBe(false);
});

test("should change offers' 'isHidden' on company enable", async () => {

const offersBefore = await Offer.find({ owner: disabled_test_company_4._id });

expect(offersBefore.every(({ isHidden }) => isHidden === true)).toBe(true);
expect(offersBefore.every(
({ hiddenReason }) => hiddenReason === OfferConstants.HiddenOfferReasons.COMPANY_DISABLED)
).toBe(true);

const res = await test_agent
.put(`/company/${disabled_test_company_4._id}/enable`)
.send(withGodToken());

expect(res.status).toBe(HTTPStatus.OK);
expect(res.body.isDisabled).toBe(false);

const offersAfter = await Offer.find({ owner: disabled_test_company_4._id });

expect(offersAfter.every(({ isHidden }) => isHidden === false)).toBe(true);
expect(offersAfter.every(({ hiddenReason }) => hiddenReason === undefined)).toBe(true);
});

test("should send an email to the company user when it is enabled", async () => {
await test_agent
.put(`/company/${disabled_test_company_mail._id}/enable`)
.send(withGodToken())
.expect(HTTPStatus.OK);

const emailOptions = COMPANY_ENABLED_NOTIFICATION(
disabled_test_company_mail.name
);

expect(EmailService.sendMail).toHaveBeenCalledWith(expect.objectContaining({
subject: emailOptions.subject,
to: test_user_mail.email,
template: emailOptions.template,
context: emailOptions.context,
}));
});
});
});
Loading

0 comments on commit 623d81a

Please sign in to comment.