497faae3bf
Settings → AI tools → Tests on this server (admin, S3 only). Stored in /opt/agenthub/app/member-policy.json, which member-guard.js reads as the member at each Bash call — no restart; no file = blocked. GET/PUT /api/ai-tools/member-policy, audited, 409 ENOS3 outside S3.
58 lines
2.2 KiB
TypeScript
58 lines
2.2 KiB
TypeScript
/**
|
|
* @file Tests for MemberPolicySettings — the admin's "allow members to run
|
|
* tests" switch in Settings → AI tools: hidden in local mode, for a non-admin
|
|
* and outside S3, and a toggle saves through PUT /api/ai-tools/member-policy.
|
|
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
|
*/
|
|
|
|
import { describe, expect, it, vi, beforeEach } from "vitest";
|
|
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
import "../../../i18n";
|
|
import { MemberPolicySettings } from "../MemberPolicySettings";
|
|
import { api } from "../../../lib/api";
|
|
|
|
const perms = vi.hoisted(() => ({ isAdmin: true, local: false }));
|
|
vi.mock("../../AuthGate", () => ({ usePermissions: () => perms }));
|
|
vi.mock("../../../lib/api", () => ({
|
|
api: { aiTools: { memberPolicy: vi.fn(), setMemberPolicy: vi.fn() } },
|
|
}));
|
|
|
|
const get = vi.mocked(api.aiTools.memberPolicy);
|
|
const set = vi.mocked(api.aiTools.setMemberPolicy);
|
|
|
|
describe("MemberPolicySettings", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
perms.isAdmin = true;
|
|
perms.local = false;
|
|
});
|
|
|
|
it("lets an admin allow tests, off by default", async () => {
|
|
get.mockResolvedValue({ available: true, allowTests: false });
|
|
set.mockResolvedValue({ available: true, allowTests: true });
|
|
render(<MemberPolicySettings />);
|
|
const sw = await screen.findByRole("switch", { name: "Allow members to run tests" });
|
|
expect(sw).toHaveAttribute("aria-checked", "false");
|
|
fireEvent.click(sw);
|
|
await waitFor(() => expect(set).toHaveBeenCalledWith(true));
|
|
await waitFor(() => expect(sw).toHaveAttribute("aria-checked", "true"));
|
|
});
|
|
|
|
it("hides outside S3", async () => {
|
|
get.mockResolvedValue({ available: false, allowTests: false });
|
|
const { container } = render(<MemberPolicySettings />);
|
|
await waitFor(() => expect(get).toHaveBeenCalled());
|
|
expect(container).toBeEmptyDOMElement();
|
|
});
|
|
|
|
it("asks nothing for a non-admin or in local mode", () => {
|
|
perms.isAdmin = false;
|
|
const { container } = render(<MemberPolicySettings />);
|
|
expect(container).toBeEmptyDOMElement();
|
|
perms.isAdmin = true;
|
|
perms.local = true;
|
|
render(<MemberPolicySettings />);
|
|
expect(get).not.toHaveBeenCalled();
|
|
});
|
|
});
|