Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
199 lines
7.7 KiB
TypeScript
199 lines
7.7 KiB
TypeScript
import { render, screen, waitFor, fireEvent, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import NotificationsPage from "./page";
|
|
|
|
const auth = vi.hoisted(() => ({ hasModule: vi.fn(() => true), loading: false }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({
|
|
useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }),
|
|
}));
|
|
|
|
const notif = vi.hoisted(() => ({
|
|
listNotifications: vi.fn(),
|
|
getUnreadCount: vi.fn(),
|
|
markRead: vi.fn(),
|
|
markAllRead: vi.fn(),
|
|
broadcast: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/notifications", () => notif);
|
|
|
|
const users = vi.hoisted(() => ({ listAdminUsers: vi.fn() }));
|
|
vi.mock("@/app/lib/services/adminUsers", () => ({ listAdminUsers: users.listAdminUsers }));
|
|
|
|
import { broadcast } from "@/app/lib/services/notifications";
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
auth.hasModule.mockReturnValue(true);
|
|
notif.broadcast.mockResolvedValue({ sent: 2 });
|
|
notif.listNotifications.mockResolvedValue({ items: [], total: 0 });
|
|
notif.getUnreadCount.mockResolvedValue(0);
|
|
users.listAdminUsers.mockResolvedValue({ items: [], total: 0 });
|
|
});
|
|
|
|
async function fillMessage() {
|
|
fireEvent.change(screen.getByLabelText(/message/i), { target: { value: "Limited stock today" } });
|
|
}
|
|
|
|
describe("NotificationsPage", () => {
|
|
it("renders the not-authorised state without the BO_NOTIFICATIONS grant", async () => {
|
|
auth.hasModule.mockReturnValue(false);
|
|
|
|
render(<NotificationsPage />);
|
|
|
|
expect(await screen.findByTestId("not-authorized")).toBeInTheDocument();
|
|
expect(screen.queryByLabelText(/message/i)).not.toBeInTheDocument();
|
|
expect(notif.broadcast).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("titles the page with the shared h1", async () => {
|
|
render(<NotificationsPage />);
|
|
expect(await screen.findByRole("heading", { level: 1, name: "Notifications" })).toBeInTheDocument();
|
|
});
|
|
|
|
it("blocks an empty message client-side", async () => {
|
|
render(<NotificationsPage />);
|
|
await screen.findByLabelText(/message/i);
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: /review & send/i }));
|
|
|
|
expect(screen.getByText(/message is required/i)).toBeInTheDocument();
|
|
expect(notif.broadcast).not.toHaveBeenCalled();
|
|
});
|
|
|
|
// link-sink-allowlist (F47): the deep link must be a path on this site.
|
|
it("blocks an off-site deep link client-side", async () => {
|
|
render(<NotificationsPage />);
|
|
fireEvent.change(await screen.findByLabelText(/message/i), { target: { value: "Hello" } });
|
|
fireEvent.change(screen.getByLabelText(/deep link/i), { target: { value: "https://evil.example/login" } });
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: /review & send/i }));
|
|
|
|
expect(screen.getByText(/path on this site/i)).toBeInTheDocument();
|
|
expect(notif.broadcast).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("blocks a specific-recipient target with no recipients selected", async () => {
|
|
render(<NotificationsPage />);
|
|
await fillMessage();
|
|
await userEvent.setup().click(screen.getByRole("radio", { name: "Members" }));
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: /review & send/i }));
|
|
|
|
expect(screen.getByText(/select at least one recipient/i)).toBeInTheDocument();
|
|
expect(notif.broadcast).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("sends an All Members broadcast after confirmation and shows the result summary", async () => {
|
|
notif.broadcast.mockResolvedValue({ sent: 3 });
|
|
const user = userEvent.setup();
|
|
render(<NotificationsPage />);
|
|
await fillMessage();
|
|
await user.click(screen.getByRole("radio", { name: /all members/i }));
|
|
|
|
await user.click(screen.getByRole("button", { name: /review & send/i }));
|
|
// Confirmation summary names the cohort before sending.
|
|
expect(screen.getByText(/all members/i)).toBeInTheDocument();
|
|
expect(notif.broadcast).not.toHaveBeenCalled();
|
|
|
|
await user.click(screen.getByRole("button", { name: /^send/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(broadcast).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
message: "Limited stock today",
|
|
target: { kind: "ALL_MEMBERS" },
|
|
}),
|
|
),
|
|
);
|
|
expect(await screen.findByText(/sent to 3 recipients/i)).toBeInTheDocument();
|
|
});
|
|
|
|
it("sends to selected admins by username after searching", async () => {
|
|
notif.broadcast.mockResolvedValue({ sent: 1 });
|
|
users.listAdminUsers.mockResolvedValue({
|
|
items: [{ id: 1, name: "Alex", username: "alex@bb.my", email: "alex@bb.my", enabled: true, roles: [] }],
|
|
total: 1,
|
|
});
|
|
const user = userEvent.setup();
|
|
render(<NotificationsPage />);
|
|
await fillMessage();
|
|
await user.click(screen.getByRole("radio", { name: "Admins" }));
|
|
|
|
fireEvent.change(screen.getByLabelText(/search admins/i), { target: { value: "alex" } });
|
|
await waitFor(() => expect(users.listAdminUsers).toHaveBeenCalled());
|
|
await user.click(await screen.findByText("alex@bb.my"));
|
|
|
|
await user.click(screen.getByRole("button", { name: /review & send/i }));
|
|
await user.click(screen.getByRole("button", { name: /^send/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(broadcast).toHaveBeenCalledWith(
|
|
expect.objectContaining({ target: { kind: "ADMINS", ids: ["alex@bb.my"] } }),
|
|
),
|
|
);
|
|
expect(await screen.findByText(/sent to 1 recipient/i)).toBeInTheDocument();
|
|
});
|
|
|
|
it("sends to specific members by member code chips", async () => {
|
|
notif.broadcast.mockResolvedValue({ sent: 1 });
|
|
const user = userEvent.setup();
|
|
render(<NotificationsPage />);
|
|
await fillMessage();
|
|
await user.click(screen.getByRole("radio", { name: "Members" }));
|
|
|
|
const codeInput = screen.getByLabelText(/member codes/i);
|
|
fireEvent.change(codeInput, { target: { value: "M10001" } });
|
|
fireEvent.keyDown(codeInput, { key: "Enter", code: "Enter" });
|
|
expect(screen.getByText("M10001")).toBeInTheDocument();
|
|
|
|
await user.click(screen.getByRole("button", { name: /review & send/i }));
|
|
await user.click(screen.getByRole("button", { name: /^send/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(broadcast).toHaveBeenCalledWith(
|
|
expect.objectContaining({ target: { kind: "MEMBERS", ids: ["M10001"] } }),
|
|
),
|
|
);
|
|
expect(await screen.findByText(/sent to 1 recipient/i)).toBeInTheDocument();
|
|
});
|
|
|
|
it("offers only announcement categories, never Order (add-member-notifications)", async () => {
|
|
render(<NotificationsPage />);
|
|
const user = userEvent.setup();
|
|
|
|
await user.click(await screen.findByRole("combobox", { name: /category/i }));
|
|
|
|
const options = (await screen.findAllByRole("option")).map((o) => o.textContent);
|
|
expect([...options].sort()).toEqual(["Points", "Promotion", "System"]);
|
|
});
|
|
|
|
it("says how many members were skipped because they switched off announcements", async () => {
|
|
notif.broadcast.mockResolvedValue({ sent: 1, skipped: 2 });
|
|
const user = userEvent.setup();
|
|
render(<NotificationsPage />);
|
|
await fillMessage();
|
|
|
|
await user.click(screen.getByRole("button", { name: /review & send/i }));
|
|
await user.click(screen.getByRole("button", { name: /^send/i }));
|
|
|
|
expect(
|
|
await screen.findByText("Sent to 1 recipient. 2 skipped because they switched off announcements."),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
it("names a single skipped member in the singular", async () => {
|
|
notif.broadcast.mockResolvedValue({ sent: 0, skipped: 1 });
|
|
const user = userEvent.setup();
|
|
render(<NotificationsPage />);
|
|
await fillMessage();
|
|
|
|
await user.click(screen.getByRole("button", { name: /review & send/i }));
|
|
await user.click(screen.getByRole("button", { name: /^send/i }));
|
|
|
|
expect(
|
|
await screen.findByText("Sent to 0 recipients. 1 skipped because they switched off announcements."),
|
|
).toBeInTheDocument();
|
|
});
|
|
});
|