Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix company edition image upload #319

Merged
merged 3 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/api/middleware/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { MAX_FILE_SIZE_MB } from "./utils.js";

const parseError = (message) => message.toLowerCase().replace(/ /g, "-");

export const parseSingleFile = (field_name) => (req, res, next) => {
export const parseSingleFile = (field_name, required = true) => (req, res, next) => {
const upload = multerConfig.single(field_name);
upload(req, res, (error) => {
if (error || !req.file) {
if (error || (!req.file && required)) {
let message = "required";
let param = field_name;
if (error) {
Expand Down Expand Up @@ -44,6 +44,7 @@ export const parseSingleFile = (field_name) => (req, res, next) => {
};

export const localSave = async (req, res, next) => {
if (!req.file) return next();
const buffer = req.file.buffer;
const extension = req.file.mimetype.substr(req.file.mimetype.indexOf("/") + 1);
const filename = `${req.user.company}.${extension}`;
Expand All @@ -69,6 +70,7 @@ export const localSave = async (req, res, next) => {
const upload = util.promisify(cloudinary.uploader.upload);

export const cloudSave = async (req, res, next) => {
if (!req.file) return next();
const filename = req.file.filename;
const file_path = path.join(config.upload_folder, filename);

Expand Down
4 changes: 3 additions & 1 deletion src/api/routes/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,16 @@ export default (app) => {
* Edit company details.
* Company or admin can edit.
*/

router.put("/:companyId/edit",
or([
authMiddleware.isCompany,
authMiddleware.isAdmin,
authMiddleware.isGod
], { status_code: HTTPStatus.UNAUTHORIZED, error_code: ErrorTypes.FORBIDDEN, msg: ValidationReasons.INSUFFICIENT_PERMISSIONS }),
validators.edit,
fileMiddleware.parseSingleFile("logo", false),
fileMiddleware.localSave,
fileMiddleware.cloudSave,
(req, res, next) => companyMiddleware.canManageAccountSettings(req.params.companyId)(req, res, next),
(req, res, next) => companyMiddleware.isNotBlocked(req.params.companyId)(req, res, next),
(req, res, next) => companyMiddleware.isNotDisabled(req.params.companyId)(req, res, next),
Expand Down
1 change: 1 addition & 0 deletions test/data/not-a-logo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is not a logo!
64 changes: 55 additions & 9 deletions test/end-to-end/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -2515,7 +2515,7 @@ describe("Company endpoint", () => {
const changing_values = {
name: "Changed name",
bio: "Changed bio",
logo: "http://awebsite.com/changedlogo.jpg",
logo: "test/data/logo-niaefeup.png",
contacts: ["123", "456"],
};

Expand Down Expand Up @@ -2665,9 +2665,7 @@ describe("Company endpoint", () => {

const res = await test_agent
.put(`/company/${test_company._id}/edit`)
.send({
name: changing_values.name
})
.field("name", changing_values.name)
.expect(HTTPStatus.OK);

expect(res.body).toHaveProperty("name", changing_values.name);
Expand All @@ -2681,14 +2679,10 @@ describe("Company endpoint", () => {

const res = await test_agent
.put(`/company/${test_company._id}/edit`)
.send({
name: changing_values.name,
})
.field("name", changing_values.name)
.expect(HTTPStatus.OK);

expect(res.body).toHaveProperty("name", changing_values.name);
const changed_company = await Company.findById(test_company._id);
expect(changed_company.name).toBe(changing_values.name);
});
});

Expand All @@ -2715,6 +2709,58 @@ describe("Company endpoint", () => {
expect(test_offer.contacts).toEqual(changing_values.contacts);
});

describe("Updating company logo", () => {
test("Should fail if not an image", async () => {
await test_agent
.post("/auth/login")
.send(test_user_company)
.expect(HTTPStatus.OK);

const res = await test_agent
.put(`/company/${test_company._id}/edit`)
.attach("logo", "test/data/not-a-logo.txt")
.expect(HTTPStatus.UNPROCESSABLE_ENTITY);

expect(res.body.errors).toContainEqual({
"location": "body",
"msg": ValidationReasons.IMAGE_FORMAT,
"param": "logo"
});
});

test("Should fail if image is too big", async () => {
await test_agent
.post("/auth/login")
.send(test_user_company)
.expect(HTTPStatus.OK);

const res = await test_agent
.put(`/company/${test_company._id}/edit`)
.attach("logo", "test/data/logo-niaefeup-10mb.png")
.expect(HTTPStatus.UNPROCESSABLE_ENTITY);

expect(res.body.errors).toContainEqual({
"location": "body",
"msg": ValidationReasons.FILE_TOO_LARGE(MAX_FILE_SIZE_MB),
"param": "logo"
});
});

test("Should succeed if image is valid", async () => {
await test_agent
.post("/auth/login")
.send(test_user_company)
.expect(HTTPStatus.OK);

const res = await test_agent
.put(`/company/${test_company._id}/edit`)
.attach("logo", changing_values.logo)
.expect(HTTPStatus.OK);

expect(res.body).toHaveProperty("logo");
});
});

describe("Using disabled/blocked company (god)", () => {
test("Should fail if company is blocked (god)", async () => {
const res = await test_agent
Expand Down