|
| 1 | +/* |
| 2 | +Copyright The CryptOS Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; |
| 18 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 19 | + |
| 20 | +import { ApprovalDecideDialog } from "@/components/approval-decide-dialog"; |
| 21 | + |
| 22 | +const decideApproval = vi.fn(); |
| 23 | +vi.mock("@/lib/approvals", async (importOriginal) => ({ |
| 24 | + ...(await importOriginal<typeof import("@/lib/approvals")>()), |
| 25 | + decideApproval: (...a: unknown[]) => decideApproval(...a), |
| 26 | +})); |
| 27 | + |
| 28 | +const digest = "0123456789abcdef".repeat(4); |
| 29 | +const target = { |
| 30 | + id: "ap-1", |
| 31 | + requestDigest: digest, |
| 32 | + requestedByCn: "operator@example.org", |
| 33 | + requiredLevel: "operator", |
| 34 | + summary: "Revoke certificate 0a1b on issuing-1", |
| 35 | + tool: "cert_revoke", |
| 36 | +}; |
| 37 | + |
| 38 | +describe("ApprovalDecideDialog", () => { |
| 39 | + beforeEach(() => decideApproval.mockReset()); |
| 40 | + |
| 41 | + it("repeats the approval ID and summary to check, and the digest as an audit detail", async () => { |
| 42 | + decideApproval.mockResolvedValue({ ...target, status: "approved" }); |
| 43 | + const onDecided = vi.fn(); |
| 44 | + const onClose = vi.fn(); |
| 45 | + render( |
| 46 | + <ApprovalDecideDialog approve onClose={onClose} onDecided={onDecided} target={target} />, |
| 47 | + ); |
| 48 | + |
| 49 | + const dialog = screen.getByRole("dialog"); |
| 50 | + expect(dialog).toHaveTextContent("ap-1"); |
| 51 | + expect(dialog).toHaveTextContent("Revoke certificate 0a1b on issuing-1"); |
| 52 | + expect(dialog).toHaveTextContent( |
| 53 | + "Check that the approval ID and summary match what the agent showed you.", |
| 54 | + ); |
| 55 | + expect(dialog).toHaveTextContent(digest); |
| 56 | + expect(dialog).toHaveTextContent("Recorded in the audit log"); |
| 57 | + expect(dialog).not.toHaveTextContent(/digest match/i); |
| 58 | + expect(decideApproval).not.toHaveBeenCalled(); |
| 59 | + |
| 60 | + fireEvent.click(screen.getByRole("button", { name: /^approve$/i })); |
| 61 | + |
| 62 | + await waitFor(() => expect(onDecided).toHaveBeenCalled()); |
| 63 | + expect(decideApproval).toHaveBeenCalledWith({ approve: true, id: "ap-1" }); |
| 64 | + expect(onClose).toHaveBeenCalled(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("denies through the same confirmation", async () => { |
| 68 | + decideApproval.mockResolvedValue({ ...target, status: "denied" }); |
| 69 | + const onDecided = vi.fn(); |
| 70 | + render( |
| 71 | + <ApprovalDecideDialog |
| 72 | + approve={false} |
| 73 | + onClose={vi.fn()} |
| 74 | + onDecided={onDecided} |
| 75 | + target={target} |
| 76 | + />, |
| 77 | + ); |
| 78 | + |
| 79 | + expect(screen.getByRole("dialog")).toHaveTextContent(digest); |
| 80 | + fireEvent.click(screen.getByRole("button", { name: /^deny$/i })); |
| 81 | + |
| 82 | + await waitFor(() => expect(onDecided).toHaveBeenCalled()); |
| 83 | + expect(decideApproval).toHaveBeenCalledWith({ approve: false, id: "ap-1" }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("cancels without deciding", () => { |
| 87 | + const onClose = vi.fn(); |
| 88 | + render(<ApprovalDecideDialog approve onClose={onClose} onDecided={vi.fn()} target={target} />); |
| 89 | + |
| 90 | + fireEvent.click(screen.getByRole("button", { name: /cancel/i })); |
| 91 | + |
| 92 | + expect(onClose).toHaveBeenCalled(); |
| 93 | + expect(decideApproval).not.toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("shows a refusal inline and stays open", async () => { |
| 97 | + decideApproval.mockRejectedValueOnce(new Error("approval expired")); |
| 98 | + const onClose = vi.fn(); |
| 99 | + render(<ApprovalDecideDialog approve onClose={onClose} onDecided={vi.fn()} target={target} />); |
| 100 | + |
| 101 | + fireEvent.click(screen.getByRole("button", { name: /^approve$/i })); |
| 102 | + |
| 103 | + expect(await screen.findByRole("alert")).toHaveTextContent("approval expired"); |
| 104 | + expect(onClose).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | +}); |
0 commit comments