Secretaries could reach neither the services module (EntityContextResolver does not recognise a secretary as clinic owner, so they resolved to `unknown` → 403) nor had any toggle to grant it. Add `services` as a first-class secretary permission resource, enforced end-to-end. Backend - DoctorSecretary::DEFAULT_PERMISSIONS: new `services` resource (default-deny). - SecretaryAccessChecker::resolveOwnerEntity(): reusable owner (clinic/doctor) resolution from the secretary's active context, for controllers whose data is fetched by [entityType, entityId] and whose generic resolver is not secretary-aware. - ClinicServiceController: resolveEntity() is now secretary-aware; every action (sections, items, tariffs — 13 total) guards with `services` view/create/ update/delete via denyUnlessGranted, ahead of the subscription gate. Frontend - SecretaryPermissions type + MySecretariesPage + SecretariesPage: `services` section so owners can grant it. - Sidebar (secretary branch): services / inventory / tags menu items gated by can(resource, 'view'). - RoleRoute: a secretary now needs the page's `permission` to open it (direct URL entry included); clinic-services, inventory, tags-settings routes accept secretary + permission gate. Tests - SecretaryResourceEnforcementTest: services denied-by-default, allowed-when- granted, create-denied-while-view-granted. - Sidebar.test: secretary menu gating for services/inventory/tags. Docs: secretary.md + clinic-services.md updated with the `services` resource and the resolveOwnerEntity note. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
120 lines
4.9 KiB
TypeScript
120 lines
4.9 KiB
TypeScript
import { fireEvent, screen } from "@testing-library/react";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { renderWithProviders } from "../../test/utils";
|
|
|
|
vi.mock("../../hooks/useSubscription", () => ({
|
|
useSubscription: () => ({ hasFeature: () => true }),
|
|
}));
|
|
|
|
import { useAuthStore } from "../../stores/authStore";
|
|
import Sidebar from "./Sidebar";
|
|
|
|
describe("Sidebar — expandable نوبتها menu", () => {
|
|
beforeEach(() => {
|
|
useAuthStore.setState({
|
|
primaryRole: "admin",
|
|
dbUuid: "c1",
|
|
userName: "ادمین",
|
|
availableContexts: [],
|
|
context: null,
|
|
} as any);
|
|
});
|
|
|
|
it("keeps نوبتها always expanded regardless of route and click", () => {
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
|
|
// منوی والد به شکل دکمه
|
|
const parent = screen.getByRole("button", { name: /نوبتها/ });
|
|
expect(parent).toBeInTheDocument();
|
|
|
|
// حتی خارج از مسیر نوبتها، زیرمنوها همیشه دیده میشوند
|
|
expect(screen.getByText("نوبت ها")).toBeInTheDocument();
|
|
expect(screen.getByText("افزودن نوبت")).toBeInTheDocument();
|
|
|
|
// کلیک روی والد آن را جمع نمیکند
|
|
fireEvent.click(parent);
|
|
expect(screen.getByText("نوبت ها")).toBeInTheDocument();
|
|
expect(screen.getByText("افزودن نوبت")).toBeInTheDocument();
|
|
});
|
|
|
|
it("auto-expands when a child route is active", () => {
|
|
renderWithProviders(<Sidebar />, { route: "/admin/appointments/new" });
|
|
// چون «افزودن نوبت» فعال است، منو باید خودکار باز باشد
|
|
expect(screen.getByText("افزودن نوبت")).toBeInTheDocument();
|
|
expect(screen.getByText("نوبت ها")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("Sidebar — خدمات در منوی اصلی (نه تنظیمات)", () => {
|
|
it("shows خدمات in the clinic main menu linking to /admin/clinic-services", () => {
|
|
useAuthStore.setState({
|
|
primaryRole: "clinic",
|
|
dbUuid: "c1",
|
|
userName: "کلینیک",
|
|
availableContexts: [],
|
|
context: null,
|
|
} as any);
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.getByText("سرویس ها").closest("a")).toHaveAttribute(
|
|
"href",
|
|
"/admin/clinic-services",
|
|
);
|
|
});
|
|
|
|
it("shows خدمات in the doctor main menu linking to /admin/clinic-services", () => {
|
|
useAuthStore.setState({
|
|
primaryRole: "doctor",
|
|
dbUuid: null,
|
|
userName: "پزشک",
|
|
availableContexts: [],
|
|
context: null,
|
|
} as any);
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.getByText("سرویس ها").closest("a")).toHaveAttribute(
|
|
"href",
|
|
"/admin/clinic-services",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("Sidebar — گِیت منوی منشی بر اساس مجوز", () => {
|
|
const setSecretary = (resources: Record<string, Record<string, boolean>>) =>
|
|
useAuthStore.setState({
|
|
primaryRole: "secretary",
|
|
dbUuid: "c1",
|
|
userName: "منشی",
|
|
availableContexts: [],
|
|
context: { scope: "clinic", permissions: { resources } },
|
|
} as any);
|
|
|
|
it("منشیِ بدون مجوز، سرویسها/انبار/تگها را نمیبیند", () => {
|
|
setSecretary({ appointments: { view: true } });
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.queryByText("سرویس ها")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("انبارداری")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("تگها")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("با مجوز services.view، آیتم «سرویس ها» به /admin/clinic-services میرود", () => {
|
|
setSecretary({ services: { view: true } });
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.getByText("سرویس ها").closest("a")).toHaveAttribute(
|
|
"href",
|
|
"/admin/clinic-services",
|
|
);
|
|
});
|
|
|
|
it("با مجوز inventory.view و tags.view، همان آیتمها نمایش داده میشوند", () => {
|
|
setSecretary({ inventory: { view: true }, tags: { view: true } });
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.getByText("انبارداری").closest("a")).toHaveAttribute(
|
|
"href",
|
|
"/admin/inventory",
|
|
);
|
|
expect(screen.getByText("تگها").closest("a")).toHaveAttribute(
|
|
"href",
|
|
"/admin/tags-settings",
|
|
);
|
|
});
|
|
});
|