|
| 1 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 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 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 17 | + |
| 18 | +// Mock the gate decision + the Ink render so the test exercises only the |
| 19 | +// mapping contract enforceIdentityGate owns. withSpan is collapsed to a |
| 20 | +// pass-through so the span wrapper doesn't pull in Sentry. |
| 21 | +const { checkIdentityGateMock, renderNoticeMock } = vi.hoisted(() => ({ |
| 22 | + checkIdentityGateMock: vi.fn(), |
| 23 | + renderNoticeMock: vi.fn().mockResolvedValue(undefined), |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock("../../utils/identity/identityGate.js", () => ({ |
| 27 | + checkIdentityGate: checkIdentityGateMock, |
| 28 | +})); |
| 29 | + |
| 30 | +vi.mock("./IdentityGateNotice.js", () => ({ |
| 31 | + renderIdentityGateNotice: renderNoticeMock, |
| 32 | +})); |
| 33 | + |
| 34 | +vi.mock("../../telemetry.js", () => ({ |
| 35 | + withSpan: (_op: string, _name: string, fn: () => unknown) => fn(), |
| 36 | +})); |
| 37 | + |
| 38 | +import { enforceIdentityGate } from "./gateOrNotice.js"; |
| 39 | + |
| 40 | +const RAW = {} as any; |
| 41 | +const H160 = "0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef" as `0x${string}`; |
| 42 | + |
| 43 | +beforeEach(() => { |
| 44 | + vi.clearAllMocks(); |
| 45 | + renderNoticeMock.mockResolvedValue(undefined); |
| 46 | +}); |
| 47 | + |
| 48 | +describe("enforceIdentityGate", () => { |
| 49 | + it("does not block a revealed builder and prints nothing", async () => { |
| 50 | + checkIdentityGateMock.mockResolvedValue({ status: "revealed", productH160: H160 }); |
| 51 | + |
| 52 | + const blocked = await enforceIdentityGate(RAW); |
| 53 | + |
| 54 | + expect(blocked).toBe(false); |
| 55 | + expect(renderNoticeMock).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it.each(["not-logged-in", "anonymous", "unverifiable"] as const)( |
| 59 | + "blocks and renders the %s notice", |
| 60 | + async (status) => { |
| 61 | + checkIdentityGateMock.mockResolvedValue( |
| 62 | + status === "unverifiable" ? { status, detail: "x" } : { status, productH160: H160 }, |
| 63 | + ); |
| 64 | + |
| 65 | + const blocked = await enforceIdentityGate(RAW); |
| 66 | + |
| 67 | + expect(blocked).toBe(true); |
| 68 | + expect(renderNoticeMock).toHaveBeenCalledTimes(1); |
| 69 | + expect(renderNoticeMock).toHaveBeenCalledWith(status); |
| 70 | + }, |
| 71 | + ); |
| 72 | + |
| 73 | + it("forwards a pre-resolved registry to the gate (so mod doesn't re-resolve)", async () => { |
| 74 | + checkIdentityGateMock.mockResolvedValue({ status: "revealed", productH160: H160 }); |
| 75 | + const registry = { getRootAccount: { query: vi.fn() } }; |
| 76 | + |
| 77 | + await enforceIdentityGate(RAW, registry as any); |
| 78 | + |
| 79 | + expect(checkIdentityGateMock).toHaveBeenCalledWith(RAW, { registry }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments