diff --git a/apps/customer-portal/webapp/src/features/security/api/__tests__/useGetProductVulnerabilities.test.tsx b/apps/customer-portal/webapp/src/features/security/api/__tests__/useGetProductVulnerabilities.test.tsx new file mode 100644 index 0000000000..1af0926cdd --- /dev/null +++ b/apps/customer-portal/webapp/src/features/security/api/__tests__/useGetProductVulnerabilities.test.tsx @@ -0,0 +1,84 @@ +// Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { renderHook, waitFor } from "@testing-library/react"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; +import type { ReactNode } from "react"; +import { useGetProductVulnerabilities } from "@features/security/api/useGetProductVulnerabilities"; + +const mockLogger = { debug: vi.fn(), error: vi.fn() }; +vi.mock("@hooks/useLogger", () => ({ useLogger: () => mockLogger })); + +let mockIsSignedIn = true; +let mockIsAuthLoading = false; +vi.mock("@asgardeo/react", () => ({ + useAsgardeo: () => ({ isSignedIn: mockIsSignedIn, isLoading: mockIsAuthLoading }), +})); + +const mockAuthFetch = vi.fn(); +vi.mock("@/hooks/useAuthApiClient", () => ({ useAuthApiClient: () => mockAuthFetch })); + +describe("useGetProductVulnerabilities", () => { + let queryClient: QueryClient; + const originalConfig = window.config; + + const wrapper = ({ children }: { children: ReactNode }) => ( + {children} + ); + + beforeEach(() => { + queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + mockIsSignedIn = true; + mockIsAuthLoading = false; + vi.clearAllMocks(); + vi.stubGlobal("config", { CUSTOMER_PORTAL_BACKEND_BASE_URL: "https://api.test" }); + }); + + afterEach(() => { + window.config = originalConfig; + vi.unstubAllGlobals(); + }); + + it("encodes vulnerability id in request URL", async () => { + const payload = { id: "v-1", cveId: "CVE-1" }; + mockAuthFetch.mockResolvedValueOnce({ + ok: true, + status: 200, + json: () => Promise.resolve(payload), + } as Response); + + const { result } = renderHook( + () => useGetProductVulnerabilities("XRAY-999 003"), + { wrapper }, + ); + + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + expect(mockAuthFetch).toHaveBeenCalledWith( + "https://api.test/products/vulnerabilities/XRAY-999%20003", + { method: "GET" }, + ); + }); + + it("throws when base URL is missing", async () => { + vi.stubGlobal("config", {}); + const { result } = renderHook(() => useGetProductVulnerabilities("v-1"), { wrapper }); + await waitFor(() => expect(result.current.isError).toBe(true)); + expect(result.current.error?.message).toBe( + "CUSTOMER_PORTAL_BACKEND_BASE_URL is not configured", + ); + }); +}); diff --git a/apps/customer-portal/webapp/src/features/security/api/__tests__/useGetProductVulnerability.test.tsx b/apps/customer-portal/webapp/src/features/security/api/__tests__/useGetProductVulnerability.test.tsx new file mode 100644 index 0000000000..f87e40179b --- /dev/null +++ b/apps/customer-portal/webapp/src/features/security/api/__tests__/useGetProductVulnerability.test.tsx @@ -0,0 +1,95 @@ +// Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { renderHook, waitFor } from "@testing-library/react"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; +import type { ReactNode } from "react"; +import { useGetProductVulnerability } from "@features/security/api/useGetProductVulnerability"; + +const mockLogger = { debug: vi.fn(), error: vi.fn() }; +vi.mock("@hooks/useLogger", () => ({ useLogger: () => mockLogger })); + +let mockIsSignedIn = true; +let mockIsAuthLoading = false; +vi.mock("@asgardeo/react", () => ({ + useAsgardeo: () => ({ isSignedIn: mockIsSignedIn, isLoading: mockIsAuthLoading }), +})); + +const mockAuthFetch = vi.fn(); +vi.mock("@/hooks/useAuthApiClient", () => ({ useAuthApiClient: () => mockAuthFetch })); + +describe("useGetProductVulnerability", () => { + let queryClient: QueryClient; + const originalConfig = window.config; + + const wrapper = ({ children }: { children: ReactNode }) => ( + {children} + ); + + beforeEach(() => { + queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + mockIsSignedIn = true; + mockIsAuthLoading = false; + vi.clearAllMocks(); + vi.stubGlobal("config", { CUSTOMER_PORTAL_BACKEND_BASE_URL: "https://api.test" }); + }); + + afterEach(() => { + window.config = originalConfig; + vi.unstubAllGlobals(); + }); + + it("fetches product vulnerability details", async () => { + const payload = { id: "v-1", cveId: "CVE-1" }; + mockAuthFetch.mockResolvedValueOnce({ + ok: true, + status: 200, + json: () => Promise.resolve(payload), + } as Response); + + const { result } = renderHook(() => useGetProductVulnerability("v-1"), { wrapper }); + + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + expect(result.current.data).toEqual(payload); + expect(mockAuthFetch).toHaveBeenCalledWith( + "https://api.test/products/vulnerabilities/v-1", + { method: "GET" }, + ); + }); + + it("returns disabled query when signed out", () => { + mockIsSignedIn = false; + const { result } = renderHook(() => useGetProductVulnerability("v-1"), { wrapper }); + expect(result.current.fetchStatus).toBe("idle"); + expect(mockAuthFetch).not.toHaveBeenCalled(); + }); + + it("surfaces backend failures", async () => { + mockAuthFetch.mockResolvedValueOnce({ + ok: false, + status: 500, + statusText: "Internal Server Error", + } as Response); + + const { result } = renderHook(() => useGetProductVulnerability("v-1"), { wrapper }); + + await waitFor(() => expect(result.current.isError).toBe(true)); + expect(result.current.error?.message).toContain( + "Error fetching product vulnerability: 500 Internal Server Error", + ); + }); +}); diff --git a/apps/customer-portal/webapp/src/features/security/api/__tests__/useGetVulnerabilitiesMetaData.test.tsx b/apps/customer-portal/webapp/src/features/security/api/__tests__/useGetVulnerabilitiesMetaData.test.tsx new file mode 100644 index 0000000000..9822e3e2ca --- /dev/null +++ b/apps/customer-portal/webapp/src/features/security/api/__tests__/useGetVulnerabilitiesMetaData.test.tsx @@ -0,0 +1,81 @@ +// Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { renderHook, waitFor } from "@testing-library/react"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; +import type { ReactNode } from "react"; +import { useGetVulnerabilitiesMetaData } from "@features/security/api/useGetVulnerabilitiesMetaData"; + +const mockLogger = { debug: vi.fn(), error: vi.fn() }; +vi.mock("@hooks/useLogger", () => ({ useLogger: () => mockLogger })); + +let mockIsSignedIn = true; +let mockIsAuthLoading = false; +vi.mock("@asgardeo/react", () => ({ + useAsgardeo: () => ({ isSignedIn: mockIsSignedIn, isLoading: mockIsAuthLoading }), +})); + +const mockAuthFetch = vi.fn(); +vi.mock("@/hooks/useAuthApiClient", () => ({ useAuthApiClient: () => mockAuthFetch })); + +describe("useGetVulnerabilitiesMetaData", () => { + let queryClient: QueryClient; + const originalConfig = window.config; + + const wrapper = ({ children }: { children: ReactNode }) => ( + {children} + ); + + beforeEach(() => { + queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + mockIsSignedIn = true; + mockIsAuthLoading = false; + vi.clearAllMocks(); + vi.stubGlobal("config", { CUSTOMER_PORTAL_BACKEND_BASE_URL: "https://api.test" }); + }); + + afterEach(() => { + window.config = originalConfig; + vi.unstubAllGlobals(); + }); + + it("fetches metadata and exposes severities", async () => { + const payload = { severities: [{ id: "1", label: "Critical" }] }; + mockAuthFetch.mockResolvedValueOnce({ + ok: true, + status: 200, + json: () => Promise.resolve(payload), + } as Response); + + const { result } = renderHook(() => useGetVulnerabilitiesMetaData(), { wrapper }); + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + expect(result.current.data).toEqual(payload); + }); + + it("returns query error for non-ok responses", async () => { + mockAuthFetch.mockResolvedValueOnce({ + ok: false, + statusText: "Bad Request", + } as Response); + + const { result } = renderHook(() => useGetVulnerabilitiesMetaData(), { wrapper }); + await waitFor(() => expect(result.current.isError).toBe(true)); + expect(result.current.error?.message).toBe( + "Error fetching vulnerabilities metadata: Bad Request", + ); + }); +}); diff --git a/apps/customer-portal/webapp/src/features/security/api/__tests__/usePostProductVulnerabilitiesSearch.test.tsx b/apps/customer-portal/webapp/src/features/security/api/__tests__/usePostProductVulnerabilitiesSearch.test.tsx new file mode 100644 index 0000000000..1077bc1c1a --- /dev/null +++ b/apps/customer-portal/webapp/src/features/security/api/__tests__/usePostProductVulnerabilitiesSearch.test.tsx @@ -0,0 +1,95 @@ +// Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { renderHook, waitFor } from "@testing-library/react"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; +import type { ReactNode } from "react"; +import { usePostProductVulnerabilitiesSearch } from "@features/security/api/usePostProductVulnerabilitiesSearch"; + +const mockLogger = { debug: vi.fn(), error: vi.fn() }; +vi.mock("@hooks/useLogger", () => ({ useLogger: () => mockLogger })); + +let mockIsSignedIn = true; +let mockIsAuthLoading = false; +vi.mock("@asgardeo/react", () => ({ + useAsgardeo: () => ({ isSignedIn: mockIsSignedIn, isLoading: mockIsAuthLoading }), +})); + +const mockAuthFetch = vi.fn(); +vi.mock("@/hooks/useAuthApiClient", () => ({ useAuthApiClient: () => mockAuthFetch })); + +describe("usePostProductVulnerabilitiesSearch", () => { + let queryClient: QueryClient; + const originalConfig = window.config; + const request = { filters: { severityId: 1 }, pagination: { offset: 0, limit: 10 } }; + + const wrapper = ({ children }: { children: ReactNode }) => ( + {children} + ); + + beforeEach(() => { + queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + mockIsSignedIn = true; + mockIsAuthLoading = false; + vi.clearAllMocks(); + vi.stubGlobal("config", { CUSTOMER_PORTAL_BACKEND_BASE_URL: "https://api.test" }); + }); + + afterEach(() => { + window.config = originalConfig; + vi.unstubAllGlobals(); + }); + + it("posts search request and returns response payload", async () => { + const payload = { productVulnerabilities: [], totalRecords: 0, offset: 0, limit: 10 }; + mockAuthFetch.mockResolvedValueOnce({ + ok: true, + status: 200, + json: () => Promise.resolve(payload), + } as Response); + + const { result } = renderHook( + () => usePostProductVulnerabilitiesSearch(request), + { wrapper }, + ); + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + expect(result.current.data).toEqual(payload); + expect(mockAuthFetch).toHaveBeenCalledWith( + "https://api.test/products/vulnerabilities/search", + expect.objectContaining({ + method: "POST", + body: JSON.stringify(request), + }), + ); + }); + + it("parses api error response text", async () => { + mockAuthFetch.mockResolvedValueOnce({ + ok: false, + status: 400, + statusText: "Bad Request", + text: () => Promise.resolve('{"message":"Invalid filter payload"}'), + } as Response); + + const { result } = renderHook( + () => usePostProductVulnerabilitiesSearch(request), + { wrapper }, + ); + await waitFor(() => expect(result.current.isError).toBe(true)); + expect(result.current.error?.message).toBe("Invalid filter payload"); + }); +}); diff --git a/apps/customer-portal/webapp/src/features/security/components/__tests__/ProductVulnerabilitiesTableHeader.test.tsx b/apps/customer-portal/webapp/src/features/security/components/__tests__/ProductVulnerabilitiesTableHeader.test.tsx new file mode 100644 index 0000000000..3148eae91d --- /dev/null +++ b/apps/customer-portal/webapp/src/features/security/components/__tests__/ProductVulnerabilitiesTableHeader.test.tsx @@ -0,0 +1,61 @@ +// Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { fireEvent, render, screen } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; +import ProductVulnerabilitiesTableHeader from "@features/security/components/ProductVulnerabilitiesTableHeader"; +import { PRODUCT_VULNERABILITIES_SEARCH_PLACEHOLDER } from "@features/security/constants/securityConstants"; + +describe("ProductVulnerabilitiesTableHeader", () => { + it("invokes search callbacks and clear icon action", () => { + const onSearchChange = vi.fn(); + render( + , + ); + + fireEvent.change( + screen.getByPlaceholderText(PRODUCT_VULNERABILITIES_SEARCH_PLACEHOLDER), + { target: { value: "cve-123" } }, + ); + const clearButton = screen + .getAllByRole("button") + .find((button) => button.className.includes("MuiIconButton")); + expect(clearButton).toBeDefined(); + fireEvent.click(clearButton!); + expect(onSearchChange).toHaveBeenCalledWith("cve-123"); + expect(onSearchChange).toHaveBeenCalledWith(""); + }); + + it("shows clear-filters label when active filters exist", () => { + render( + , + ); + + expect(screen.getByRole("button", { name: /clear filters \(2\)/i })).toBeInTheDocument(); + }); +}); diff --git a/apps/customer-portal/webapp/src/features/security/components/__tests__/SecurityStats.test.tsx b/apps/customer-portal/webapp/src/features/security/components/__tests__/SecurityStats.test.tsx index 4431576133..4f70b628c2 100644 --- a/apps/customer-portal/webapp/src/features/security/components/__tests__/SecurityStats.test.tsx +++ b/apps/customer-portal/webapp/src/features/security/components/__tests__/SecurityStats.test.tsx @@ -22,14 +22,6 @@ import { ThemeProvider, createTheme } from "@wso2/oxygen-ui"; import { type ReactNode } from "react"; import SecurityStats from "@features/security/components/SecurityStats"; -vi.mock("@features/security/api/usePostProductVulnerabilitiesSearch", () => ({ - usePostProductVulnerabilitiesSearch: vi.fn(() => ({ - data: { totalRecords: 1265, offset: 0, limit: 10 }, - isLoading: false, - isError: false, - })), -})); - vi.mock("@features/dashboard/api/useGetProjectCasesStats", () => ({ useGetProjectCasesStats: vi.fn(() => ({ data: { @@ -71,21 +63,15 @@ function renderSecurityStats() { } describe("SecurityStats", () => { - it("should render three stat cards with correct labels", () => { + it("should render stat cards with correct labels", () => { renderSecurityStats(); - expect(screen.getByText("Total Vulnerabilities")).toBeInTheDocument(); - expect(screen.getByText("Active Security Reports")).toBeInTheDocument(); + expect(screen.getByText("Outstanding Security Reports")).toBeInTheDocument(); expect( screen.getByText("Resolved Security Reports (Last 30d)"), ).toBeInTheDocument(); }); - it("should display vulnerability count from API", () => { - renderSecurityStats(); - expect(screen.getByText("1265")).toBeInTheDocument(); - }); - it("should display active security reports count", () => { renderSecurityStats(); expect(screen.getByText("25")).toBeInTheDocument(); @@ -97,14 +83,14 @@ describe("SecurityStats", () => { }); it("should render with loading state", async () => { - const { usePostProductVulnerabilitiesSearch } = await import( - "@features/security/api/usePostProductVulnerabilitiesSearch" + const { useGetProjectCasesStats } = await import( + "@features/dashboard/api/useGetProjectCasesStats" ); - vi.mocked(usePostProductVulnerabilitiesSearch).mockReturnValue({ + vi.mocked(useGetProjectCasesStats).mockReturnValue({ data: undefined, isLoading: true, isError: false, - } as any); + } as ReturnType); renderSecurityStats(); diff --git a/apps/customer-portal/webapp/src/features/security/components/__tests__/VulnerabilityDetailsContent.test.tsx b/apps/customer-portal/webapp/src/features/security/components/__tests__/VulnerabilityDetailsContent.test.tsx new file mode 100644 index 0000000000..1fe27c769c --- /dev/null +++ b/apps/customer-portal/webapp/src/features/security/components/__tests__/VulnerabilityDetailsContent.test.tsx @@ -0,0 +1,83 @@ +// Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { fireEvent, render, screen } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; +import VulnerabilityDetailsContent from "@features/security/components/VulnerabilityDetailsContent"; + +vi.mock("@features/security/components/VulnerabilityDetailsSkeleton", () => ({ + default: () =>
loading
, +})); + +describe("VulnerabilityDetailsContent", () => { + const baseData = { + id: "1", + cveId: "CVE-2026-1111", + vulnerabilityId: "XRAY-100", + severity: { id: "1", label: "Critical" }, + productName: "API Manager", + productVersion: "4.2.0", + componentName: "component-a", + version: "1.0.0", + type: "library", + useCase: null, + justification: null, + resolution: null, + status: { id: "2", label: "Resolved" }, + }; + + it("renders loading skeleton", () => { + render( + , + ); + expect(screen.getByTestId("details-skeleton")).toBeInTheDocument(); + }); + + it("renders content details and NVD link", () => { + render( + , + ); + expect(screen.getAllByText("CVE-2026-1111").length).toBeGreaterThan(0); + expect(screen.getByRole("link", { name: "NVD" })).toHaveAttribute( + "href", + expect.stringContaining("/CVE-2026-1111"), + ); + }); + + it("triggers back callback", () => { + const onBack = vi.fn(); + render( + , + ); + fireEvent.click(screen.getByLabelText("Back")); + expect(onBack).toHaveBeenCalled(); + }); +}); diff --git a/apps/customer-portal/webapp/src/features/security/pages/__tests__/SecurityPage.test.tsx b/apps/customer-portal/webapp/src/features/security/pages/__tests__/SecurityPage.test.tsx new file mode 100644 index 0000000000..c7f233037c --- /dev/null +++ b/apps/customer-portal/webapp/src/features/security/pages/__tests__/SecurityPage.test.tsx @@ -0,0 +1,92 @@ +// Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { fireEvent, render, screen } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; +import SecurityPage from "@features/security/pages/SecurityPage"; +import { SecurityStatKey } from "@features/security/types/security"; +import { CaseStatus } from "@features/support/constants/supportConstants"; + +const mockSetSearchParams = vi.fn(); +const mockNavigate = vi.fn(); + +vi.mock("react-router", () => ({ + useParams: () => ({ projectId: "p-1" }), + useSearchParams: () => [new URLSearchParams(""), mockSetSearchParams], +})); + +vi.mock("@hooks/useModifierAwareNavigate", () => ({ + useModifierAwareNavigate: () => mockNavigate, +})); + +vi.mock("@api/useGetProjectDetails", () => ({ + default: () => ({ data: { type: { label: "Enterprise" } } }), +})); +vi.mock("@api/useGetProjectFeatures", () => ({ + default: () => ({ data: {}, isLoading: false }), +})); +vi.mock("@api/useGetProjectFilters", () => ({ + default: () => ({ + data: { + caseStates: [ + { id: "1", label: CaseStatus.OPEN }, + { id: "2", label: CaseStatus.CLOSED }, + ], + }, + }), +})); + +vi.mock("@utils/permission", () => ({ + getProjectPermissions: () => ({ + hasSecurityReportAnalysis: true, + hasComponentAnalysis: true, + }), +})); + +vi.mock("@features/security/components/SecurityStats", () => ({ + default: ({ onStatClick }: { onStatClick: (k: SecurityStatKey) => void }) => ( + + ), +})); +vi.mock("@components/tab-bar/TabBar", () => ({ + default: () =>
tab-bar
, +})); +vi.mock("@features/security/components/ProductVulnerabilitiesTable", () => ({ + default: () =>
product-table
, +})); +vi.mock("@features/security/components/SecurityReportAnalysis", () => ({ + default: () =>
report-analysis
, +})); + +describe("SecurityPage", () => { + it("shows stats and default tab content", () => { + render(); + expect(screen.getByText("trigger-stat")).toBeInTheDocument(); + expect(screen.getByText("tab-bar")).toBeInTheDocument(); + expect(screen.getByText("report-analysis")).toBeInTheDocument(); + }); + + it("applies stat filter mode and supports back action", () => { + render(); + fireEvent.click(screen.getByText("trigger-stat")); + expect(screen.getByRole("button", { name: "Back" })).toBeInTheDocument(); + expect(screen.queryByText("tab-bar")).not.toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: "Back" })); + expect(screen.getByText("tab-bar")).toBeInTheDocument(); + }); +}); diff --git a/apps/customer-portal/webapp/src/features/security/pages/__tests__/VulnerabilityDetailsPage.test.tsx b/apps/customer-portal/webapp/src/features/security/pages/__tests__/VulnerabilityDetailsPage.test.tsx new file mode 100644 index 0000000000..5fda06f8dc --- /dev/null +++ b/apps/customer-portal/webapp/src/features/security/pages/__tests__/VulnerabilityDetailsPage.test.tsx @@ -0,0 +1,75 @@ +// Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { fireEvent, render, screen } from "@testing-library/react"; +import { describe, expect, it, vi, beforeEach } from "vitest"; +import VulnerabilityDetailsPage from "@features/security/pages/VulnerabilityDetailsPage"; + +const mockNavigate = vi.fn(); +const mockShowLoader = vi.fn(); +const mockHideLoader = vi.fn(); +const mockShowError = vi.fn(); +const mockHook = vi.fn(); + +vi.mock("react-router", () => ({ + useNavigate: () => mockNavigate, + useParams: () => ({ projectId: "p-1", vulnerabilityId: "v-1" }), +})); + +vi.mock("@context/linear-loader/LoaderContext", () => ({ + useLoader: () => ({ showLoader: mockShowLoader, hideLoader: mockHideLoader }), +})); + +vi.mock("@context/error-banner/ErrorBannerContext", () => ({ + useErrorBanner: () => ({ showError: mockShowError }), +})); + +vi.mock("@features/security/api/useGetProductVulnerability", () => ({ + useGetProductVulnerability: (...args: unknown[]) => mockHook(...args), +})); + +vi.mock("@features/security/components/VulnerabilityDetailsContent", () => ({ + default: ({ onBack }: { onBack: () => void }) => ( + + ), +})); + +describe("VulnerabilityDetailsPage", () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it("shows loader while vulnerability details are loading", () => { + mockHook.mockReturnValue({ data: undefined, isLoading: true, isError: false }); + render(); + expect(mockShowLoader).toHaveBeenCalled(); + }); + + it("shows error banner when request fails", () => { + mockHook.mockReturnValue({ data: undefined, isLoading: false, isError: true }); + render(); + expect(mockShowError).toHaveBeenCalled(); + }); + + it("navigates back to security center", () => { + mockHook.mockReturnValue({ data: { id: "1" }, isLoading: false, isError: false }); + render(); + fireEvent.click(screen.getByText("go-back")); + expect(mockNavigate).toHaveBeenCalledWith("/projects/p-1/security-center", { + state: { fromBack: true }, + }); + }); +}); diff --git a/apps/customer-portal/webapp/src/features/security/utils/__tests__/productVulnerabilitiesTable.test.ts b/apps/customer-portal/webapp/src/features/security/utils/__tests__/productVulnerabilitiesTable.test.ts new file mode 100644 index 0000000000..30e174904d --- /dev/null +++ b/apps/customer-portal/webapp/src/features/security/utils/__tests__/productVulnerabilitiesTable.test.ts @@ -0,0 +1,40 @@ +// Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { describe, expect, it } from "vitest"; +import { + countProductVulnerabilityTableActiveFilters, + formatProductVulnerabilitiesClearFiltersLabel, +} from "@features/security/utils/productVulnerabilitiesTable"; + +describe("productVulnerabilitiesTable utils", () => { + it("counts active filters and trimmed search input", () => { + expect( + countProductVulnerabilityTableActiveFilters(" CVE ", { + severityId: 1, + productName: "APIM", + productVersion: "4.2.0", + }), + ).toBe(4); + expect(countProductVulnerabilityTableActiveFilters(" ", {})).toBe(0); + }); + + it("formats clear-filter label with count", () => { + expect(formatProductVulnerabilitiesClearFiltersLabel(3)).toBe( + "Clear Filters (3)", + ); + }); +}); diff --git a/apps/customer-portal/webapp/src/features/security/utils/__tests__/securityPage.test.ts b/apps/customer-portal/webapp/src/features/security/utils/__tests__/securityPage.test.ts new file mode 100644 index 0000000000..7e2b6c0bff --- /dev/null +++ b/apps/customer-portal/webapp/src/features/security/utils/__tests__/securityPage.test.ts @@ -0,0 +1,54 @@ +// Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { describe, expect, it } from "vitest"; +import { + parseSecurityTabQueryParam, + parseSecurityReportCaseSortField, + parseSecurityReportViewMode, +} from "@features/security/utils/securityPage"; +import { + SecurityReportCaseSortField, + SecurityReportViewMode, + SecurityTabId, +} from "@features/security/types/security"; + +describe("securityPage utils", () => { + it("parses tab query param with fallback", () => { + expect(parseSecurityTabQueryParam(SecurityTabId.COMPONENTS)).toBe( + SecurityTabId.COMPONENTS, + ); + expect(parseSecurityTabQueryParam("invalid")).toBe( + SecurityTabId.VULNERABILITIES, + ); + }); + + it("parses report view mode with fallback to all", () => { + expect(parseSecurityReportViewMode(SecurityReportViewMode.MY)).toBe( + SecurityReportViewMode.MY, + ); + expect(parseSecurityReportViewMode("unknown")).toBe(SecurityReportViewMode.ALL); + }); + + it("parses case sort field with fallback to createdOn", () => { + expect(parseSecurityReportCaseSortField(SecurityReportCaseSortField.state)).toBe( + SecurityReportCaseSortField.state, + ); + expect(parseSecurityReportCaseSortField("bad")).toBe( + SecurityReportCaseSortField.createdOn, + ); + }); +}); diff --git a/apps/customer-portal/webapp/src/features/security/utils/__tests__/vulnerabilities.test.ts b/apps/customer-portal/webapp/src/features/security/utils/__tests__/vulnerabilities.test.ts new file mode 100644 index 0000000000..1140ab3864 --- /dev/null +++ b/apps/customer-portal/webapp/src/features/security/utils/__tests__/vulnerabilities.test.ts @@ -0,0 +1,35 @@ +// Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { describe, expect, it } from "vitest"; +import { + getVulnerabilitySeverityColor, + getVulnerabilityStatusColor, +} from "@features/security/utils/vulnerabilities"; + +describe("vulnerabilities utils", () => { + it("maps severity labels to theme colors", () => { + expect(getVulnerabilitySeverityColor(" Critical ")).toBe("error.main"); + expect(getVulnerabilitySeverityColor("HIGH")).toBe("warning.main"); + expect(getVulnerabilitySeverityColor("unknown")).toBe("text.secondary"); + }); + + it("maps status labels to theme colors with strict matching", () => { + expect(getVulnerabilityStatusColor("in progress")).toBe("warning.main"); + expect(getVulnerabilityStatusColor("resolved")).toBe("success.main"); + expect(getVulnerabilityStatusColor("unresolved")).toBe("text.secondary"); + }); +});