Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

Fix Add company logo image bug

Khuram Niazd8471c9ae1c5
diff --git a/__tests__/AddCompany.spec.tsx b/__tests__/AddCompany.spec.tsx
index f77266e..bfa4e0f 100644
--- a/__tests__/AddCompany.spec.tsx
+++ b/__tests__/AddCompany.spec.tsx
@@ -24,10 +24,10 @@ describe("AddCompany Component", () => {
resetEditCompany={mockResetEditCompany}
dialogOpen={false}
setDialogOpen={mockSetDialogOpen}
- />
+ />,
);
expect(
- screen.getByRole("button", { name: /add company/i })
+ screen.getByRole("button", { name: /add company/i }),
).toBeInTheDocument();
});
@@ -38,7 +38,7 @@ describe("AddCompany Component", () => {
resetEditCompany={mockResetEditCompany}
dialogOpen={false}
setDialogOpen={mockSetDialogOpen}
- />
+ />,
);
const addCompanyButton = screen.getByRole("button", {
name: /add company/i,
@@ -64,7 +64,7 @@ describe("AddCompany Component", () => {
editCompany={mockEditCompany}
dialogOpen={true}
setDialogOpen={mockSetDialogOpen}
- />
+ />,
);
const addCompanyButton = await screen.findByTestId("add-company-btn");
@@ -74,7 +74,7 @@ describe("AddCompany Component", () => {
expect(dialog).toBeInTheDocument();
expect(screen.getByLabelText(/company name/i)).toHaveValue("Test Company");
expect(screen.getByLabelText(/company logo url/i)).toHaveValue(
- "http://example.com/logo.png"
+ "http://example.com/logo.png",
);
});
@@ -85,7 +85,7 @@ describe("AddCompany Component", () => {
resetEditCompany={mockResetEditCompany}
dialogOpen={true}
setDialogOpen={mockSetDialogOpen}
- />
+ />,
);
const addCompanyButton = await screen.findByTestId("add-company-btn");
@@ -127,7 +127,7 @@ describe("AddCompany Component", () => {
editCompany={mockEditCompany}
dialogOpen={true}
setDialogOpen={mockSetDialogOpen}
- />
+ />,
);
const addCompanyButton = await screen.findByTestId("add-company-btn");
@@ -150,8 +150,10 @@ describe("AddCompany Component", () => {
await waitFor(() => {
expect(updateCompany).toHaveBeenCalledTimes(1);
expect(updateCompany).toHaveBeenCalledWith({
+ id: "company-id",
company: "Edited Test Company",
logoUrl: "http://example.com/edited-logo.png",
+ createdBy: "user-id",
});
});
});
diff --git a/__tests__/addCompanyForm.schema.spec.ts b/__tests__/addCompanyForm.schema.spec.ts
new file mode 100644
index 0000000..93e6803
--- /dev/null
+++ b/__tests__/addCompanyForm.schema.spec.ts
@@ -0,0 +1,152 @@
+import { AddCompanyFormSchema } from "@/models/addCompanyForm.schema";
+
+describe("AddCompanyFormSchema", () => {
+ describe("company field", () => {
+ it("should accept valid company name", () => {
+ const validData = {
+ company: "Tech Company Inc.",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ expect(result.company).toBe("Tech Company Inc.");
+ });
+
+ it("should reject empty company name", () => {
+ const invalidData = {
+ company: "",
+ };
+
+ expect(() => AddCompanyFormSchema.parse(invalidData)).toThrow();
+ });
+ });
+
+ describe("logoUrl field", () => {
+ it("should accept valid https URL", () => {
+ const validData = {
+ company: "Tech Company",
+ logoUrl: "https://example.com/logo.png",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ expect(result.logoUrl).toBe("https://example.com/logo.png");
+ });
+
+ it("should accept valid http URL", () => {
+ const validData = {
+ company: "Tech Company",
+ logoUrl: "http://example.com/logo.png",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ expect(result.logoUrl).toBe("http://example.com/logo.png");
+ });
+
+ it("should accept empty logoUrl", () => {
+ const validData = {
+ company: "Tech Company",
+ logoUrl: "",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ expect(result.logoUrl).toBe("");
+ });
+
+ it("should default to empty string when logoUrl is undefined", () => {
+ const validData = {
+ company: "Tech Company",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ // Due to the .default("") in the schema, undefined becomes ""
+ expect(result.logoUrl).toBe("");
+ });
+
+ it("should reject malformed URL", () => {
+ const invalidData = {
+ company: "Tech Company",
+ logoUrl: "not a valid url",
+ };
+
+ expect(() => AddCompanyFormSchema.parse(invalidData)).toThrow(
+ "Please enter a valid URL",
+ );
+ });
+
+ it("should accept URL with query parameters", () => {
+ const validData = {
+ company: "Tech Company",
+ logoUrl:
+ "https://example.com/image.png?width=200&height=200&format=png",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ expect(result.logoUrl).toBe(
+ "https://example.com/image.png?width=200&height=200&format=png",
+ );
+ });
+
+ it("should accept URL with fragments", () => {
+ const validData = {
+ company: "Tech Company",
+ logoUrl: "https://example.com/logo.png#section",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ expect(result.logoUrl).toBe("https://example.com/logo.png#section");
+ });
+
+ it("should accept URL with subdomain", () => {
+ const validData = {
+ company: "Tech Company",
+ logoUrl: "https://cdn.example.com/logo.png",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ expect(result.logoUrl).toBe("https://cdn.example.com/logo.png");
+ });
+
+ it("should accept URL with port", () => {
+ const validData = {
+ company: "Tech Company",
+ logoUrl: "https://example.com:8080/logo.png",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ expect(result.logoUrl).toBe("https://example.com:8080/logo.png");
+ });
+ });
+
+ describe("optional fields", () => {
+ it("should accept id field", () => {
+ const validData = {
+ id: "company-123",
+ company: "Tech Company",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ expect(result.id).toBe("company-123");
+ });
+
+ it("should accept createdBy field", () => {
+ const validData = {
+ createdBy: "user-123",
+ company: "Tech Company",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ expect(result.createdBy).toBe("user-123");
+ });
+
+ it("should accept all fields together", () => {
+ const validData = {
+ id: "company-123",
+ createdBy: "user-123",
+ company: "Tech Company",
+ logoUrl: "https://example.com/logo.png",
+ };
+
+ const result = AddCompanyFormSchema.parse(validData);
+ expect(result).toEqual(validData);
+ });
+ });
+});
diff --git a/__tests__/company.actions.spec.ts b/__tests__/company.actions.spec.ts
index 67be402..7f83654 100644
--- a/__tests__/company.actions.spec.ts
+++ b/__tests__/company.actions.spec.ts
@@ -127,7 +127,7 @@ describe("Company Actions", () => {
it("should handle errors", async () => {
(getCurrentUser as jest.Mock).mockRejectedValue(
- new Error("Database error")
+ new Error("Database error"),
);
await expect(getCompanyList(1, 10)).resolves.toStrictEqual({
@@ -172,7 +172,7 @@ describe("Company Actions", () => {
it("should handle unexpected errors", async () => {
(getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
(prisma.company.findMany as jest.Mock).mockRejectedValue(
- new Error("Unexpected error")
+ new Error("Unexpected error"),
);
const result = await getAllCompanies();
@@ -240,7 +240,7 @@ describe("Company Actions", () => {
createdBy: mockUser.id,
};
(prisma.company.findUnique as jest.Mock).mockResolvedValue(
- mockExistingCompany
+ mockExistingCompany,
);
const result = await addCompany(validData);
@@ -258,7 +258,7 @@ describe("Company Actions", () => {
it("should handle unexpected errors", async () => {
(getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
(prisma.company.findUnique as jest.Mock).mockRejectedValue(
- new Error("Unexpected error")
+ new Error("Unexpected error"),
);
const result = await addCompany(validData);
@@ -269,6 +269,87 @@ describe("Company Actions", () => {
});
expect(prisma.company.create).not.toHaveBeenCalled();
});
+
+ it("should return error if logo URL is invalid", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ const invalidData = {
+ company: "New Company",
+ logoUrl: "javascript:alert('xss')",
+ };
+
+ const result = await addCompany(invalidData);
+
+ expect(result).toEqual({
+ success: false,
+ message: "Invalid logo URL. Only http and https protocols are allowed.",
+ });
+
+ expect(prisma.company.findUnique).not.toHaveBeenCalled();
+ expect(prisma.company.create).not.toHaveBeenCalled();
+ });
+
+ it("should return error if logo URL has data protocol", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ const invalidData = {
+ company: "New Company",
+ logoUrl: "data:image/png;base64,iVBORw0KGgo=",
+ };
+
+ const result = await addCompany(invalidData);
+
+ expect(result).toEqual({
+ success: false,
+ message: "Invalid logo URL. Only http and https protocols are allowed.",
+ });
+
+ expect(prisma.company.create).not.toHaveBeenCalled();
+ });
+
+ it("should allow empty logo URL", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.company.findUnique as jest.Mock).mockResolvedValue(null);
+ const mockCompany = {
+ id: "company-id",
+ label: "New Company",
+ value: "new company",
+ logoUrl: "",
+ createdBy: mockUser.id,
+ };
+ (prisma.company.create as jest.Mock).mockResolvedValue(mockCompany);
+ (revalidatePath as jest.Mock).mockResolvedValue(undefined);
+
+ const result = await addCompany({
+ company: "New Company",
+ logoUrl: "",
+ });
+
+ expect(result).toEqual({ success: true, data: mockCompany });
+ expect(prisma.company.create).toHaveBeenCalled();
+ });
+
+ it("should allow https URLs", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.company.findUnique as jest.Mock).mockResolvedValue(null);
+ const mockCompany = {
+ id: "company-id",
+ label: "New Company",
+ value: "new company",
+ logoUrl: "https://example.com/logo.png",
+ createdBy: mockUser.id,
+ };
+ (prisma.company.create as jest.Mock).mockResolvedValue(mockCompany);
+ (revalidatePath as jest.Mock).mockResolvedValue(undefined);
+
+ const result = await addCompany({
+ company: "New Company",
+ logoUrl: "https://example.com/logo.png",
+ });
+
+ expect(result).toEqual({ success: true, data: mockCompany });
+ expect(prisma.company.create).toHaveBeenCalled();
+ });
});
describe("updateCompany", () => {
@@ -290,7 +371,7 @@ describe("Company Actions", () => {
};
(prisma.company.update as jest.Mock).mockResolvedValue(
- mockUpdatedCompany
+ mockUpdatedCompany,
);
const result = await updateCompany(validData);
@@ -354,6 +435,66 @@ describe("Company Actions", () => {
expect(prisma.company.findUnique).not.toHaveBeenCalled();
expect(prisma.company.update).not.toHaveBeenCalled();
});
+
+ it("should return error if logo URL is invalid", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ const invalidData = {
+ ...validData,
+ logoUrl: "javascript:alert('xss')",
+ };
+
+ const result = await updateCompany(invalidData);
+
+ expect(result).toEqual({
+ success: false,
+ message: "Invalid logo URL. Only http and https protocols are allowed.",
+ });
+
+ expect(prisma.company.findUnique).not.toHaveBeenCalled();
+ expect(prisma.company.update).not.toHaveBeenCalled();
+ });
+
+ it("should return error if logo URL has data protocol", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ const invalidData = {
+ ...validData,
+ logoUrl: "data:image/png;base64,iVBORw0KGgo=",
+ };
+
+ const result = await updateCompany(invalidData);
+
+ expect(result).toEqual({
+ success: false,
+ message: "Invalid logo URL. Only http and https protocols are allowed.",
+ });
+
+ expect(prisma.company.update).not.toHaveBeenCalled();
+ });
+
+ it("should allow empty logo URL", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ (prisma.company.findUnique as jest.Mock).mockResolvedValue(null);
+
+ const mockUpdatedCompany = {
+ id: "company-id",
+ value: "updated company",
+ };
+
+ (prisma.company.update as jest.Mock).mockResolvedValue(
+ mockUpdatedCompany,
+ );
+
+ const result = await updateCompany({
+ ...validData,
+ logoUrl: "",
+ });
+
+ expect(result).toEqual({ success: true, data: mockUpdatedCompany });
+ expect(prisma.company.update).toHaveBeenCalled();
+ });
});
describe("getCompanyById", () => {
@@ -403,7 +544,7 @@ describe("Company Actions", () => {
it("should handle unexpected errors", async () => {
(getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
(prisma.company.findUnique as jest.Mock).mockRejectedValue(
- new Error("Unexpected error")
+ new Error("Unexpected error"),
);
await expect(getCompanyById(mockCompanyId)).resolves.toStrictEqual({
diff --git a/src/actions/company.actions.ts b/src/actions/company.actions.ts
index 5a51cf1..88cc520 100644
--- a/src/actions/company.actions.ts
+++ b/src/actions/company.actions.ts
@@ -9,7 +9,7 @@ import { z } from "zod";
export const getCompanyList = async (
page = 1,
limit = 10,
- countBy?: string
+ countBy?: string,
): Promise<any | undefined> => {
try {
const user = await getCurrentUser();
@@ -84,8 +84,22 @@ export const getAllCompanies = async (): Promise<any | undefined> => {
}
};
+const isValidImageUrl = (url: string): boolean => {
+ if (!url) return true;
+ try {
+ const urlObj = new URL(url);
+ // Only allow http and https protocols
+ if (!["http:", "https:"].includes(urlObj.protocol)) {
+ return false;
+ }
+ return true;
+ } catch {
+ return false;
+ }
+};
+
export const addCompany = async (
- data: z.infer<typeof AddCompanyFormSchema>
+ data: z.infer<typeof AddCompanyFormSchema>,
): Promise<any | undefined> => {
try {
const user = await getCurrentUser();
@@ -96,6 +110,13 @@ export const addCompany = async (
const { company, logoUrl } = data;
+ // Validate image URL
+ if (logoUrl && !isValidImageUrl(logoUrl)) {
+ throw new Error(
+ "Invalid logo URL. Only http and https protocols are allowed.",
+ );
+ }
+
const value = company.trim().toLowerCase();
const companyExists = await prisma.company.findUnique({
@@ -125,7 +146,7 @@ export const addCompany = async (
};
export const updateCompany = async (
- data: z.infer<typeof AddCompanyFormSchema>
+ data: z.infer<typeof AddCompanyFormSchema>,
): Promise<any | undefined> => {
try {
const user = await getCurrentUser();
@@ -140,6 +161,13 @@ export const updateCompany = async (
throw new Error("Id is not provided or no user privilages");
}
+ // Validate image URL
+ if (logoUrl && !isValidImageUrl(logoUrl)) {
+ throw new Error(
+ "Invalid logo URL. Only http and https protocols are allowed.",
+ );
+ }
+
const value = company.trim().toLowerCase();
const companyExists = await prisma.company.findUnique({
@@ -148,7 +176,7 @@ export const updateCompany = async (
},
});
- if (companyExists) {
+ if (companyExists && companyExists.id !== id) {
throw new Error("Company already exists!");
}
@@ -171,7 +199,7 @@ export const updateCompany = async (
};
export const getCompanyById = async (
- companyId: string
+ companyId: string,
): Promise<any | undefined> => {
try {
if (!companyId) {
@@ -199,7 +227,7 @@ export const getCompanyById = async (
};
export const deleteCompanyById = async (
- companyId: string
+ companyId: string,
): Promise<any | undefined> => {
try {
const user = await getCurrentUser();
@@ -215,7 +243,7 @@ export const deleteCompanyById = async (
});
if (experiences > 0) {
throw new Error(
- `Company cannot be deleted due to its use in experience section of one of the resume! `
+ `Company cannot be deleted due to its use in experience section of one of the resume! `,
);
}
const jobs = await prisma.job.count({
@@ -226,7 +254,7 @@ export const deleteCompanyById = async (
if (jobs > 0) {
throw new Error(
- `Company cannot be deleted due to ${jobs} number of associated jobs! `
+ `Company cannot be deleted due to ${jobs} number of associated jobs! `,
);
}
diff --git a/src/components/admin/AddCompany.tsx b/src/components/admin/AddCompany.tsx
index 7fbbdb8..e01b572 100644
--- a/src/components/admin/AddCompany.tsx
+++ b/src/components/admin/AddCompany.tsx
@@ -48,6 +48,12 @@ function AddCompany({
const form = useForm<z.infer<typeof AddCompanyFormSchema>>({
resolver: zodResolver(AddCompanyFormSchema),
+ defaultValues: {
+ company: "",
+ logoUrl: "",
+ id: undefined,
+ createdBy: undefined,
+ },
});
const { reset, formState } = form;
@@ -61,14 +67,16 @@ function AddCompany({
createdBy: editCompany?.createdBy,
logoUrl: editCompany?.logoUrl ?? "",
},
- { keepDefaultValues: true }
+ { keepDefaultValues: true },
);
}
}, [editCompany, reset]);
const addCompanyForm = () => {
- reset();
- resetEditCompany();
+ if (!editCompany) {
+ reset();
+ resetEditCompany();
+ }
setDialogOpen(true);
};
diff --git a/src/components/admin/CompaniesTable.tsx b/src/components/admin/CompaniesTable.tsx
index b671466..03d2605 100644
--- a/src/components/admin/CompaniesTable.tsx
+++ b/src/components/admin/CompaniesTable.tsx
@@ -1,6 +1,5 @@
"use client";
import { Button } from "../ui/button";
-import Image from "next/image";
import {
DropdownMenu,
DropdownMenuContent,
@@ -99,12 +98,11 @@ function CompaniesTable({
return (
<TableRow key={company.id}>
<TableCell className="hidden sm:table-cell">
- <Image
+ {/* eslint-disable-next-line @next/next/no-img-element */}
+ <img
alt="Company logo"
- className="aspect-square rounded-md object-cover"
- height="32"
+ className="w-8 h-8 rounded-md object-cover"
src={company.logoUrl || "/images/jobsync-logo.svg"}
- width="32"
/>
</TableCell>
<TableCell className="font-medium">{company.label}</TableCell>
diff --git a/src/models/addCompanyForm.schema.ts b/src/models/addCompanyForm.schema.ts
index 41502b1..90efd6f 100644
--- a/src/models/addCompanyForm.schema.ts
+++ b/src/models/addCompanyForm.schema.ts
@@ -1,5 +1,14 @@
import { z } from "zod";
+const isValidUrl = (url: string) => {
+ try {
+ new URL(url);
+ return true;
+ } catch {
+ return false;
+ }
+};
+
export const AddCompanyFormSchema = z.object({
id: z.string().optional(),
createdBy: z.string().optional(),
@@ -8,5 +17,12 @@ export const AddCompanyFormSchema = z.object({
error: "Company name is required.",
})
.min(1),
- logoUrl: z.string().default("").optional(),
+ logoUrl: z
+ .string()
+ .default("")
+ .optional()
+ .refine(
+ (url) => !url || isValidUrl(url),
+ "Please enter a valid URL (e.g., https://example.com/logo.png)",
+ ),
});