Honor the share-link miss limit before querying, and keep Escape inside the access menu.
This commit is contained in:
1 parent
75649a9a42
commit
1b3d055c81
8 files changed
+189
-19
No files matched your search
@@ -64,4 +64,79 @@ describe("SelectInput listbox keyboard", () => {
|
||||
});
|
||||
expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("closes the listbox when focus leaves the trigger", async () => {
|
||||
const user = userEvent.setup();
|
||||
renderWithProviders(
|
||||
<>
|
||||
<SelectInput
|
||||
labelText="Who can read"
|
||||
showLabel
|
||||
value="option1"
|
||||
options={[
|
||||
{ value: "option1", label: "Option 1" },
|
||||
{ value: "option2", label: "Option 2" },
|
||||
]}
|
||||
/>
|
||||
<button type="button">Next</button>
|
||||
</>,
|
||||
);
|
||||
|
||||
const trigger = screen.getByRole("button", { name: /Who can read/i });
|
||||
await user.click(trigger);
|
||||
expect(await screen.findByRole("listbox")).toBeVisible();
|
||||
await user.click(screen.getByRole("button", { name: "Next" }));
|
||||
expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("Escape closes the listbox and does not reach document listeners", async () => {
|
||||
const user = userEvent.setup();
|
||||
const onDocumentKeyDown = vi.fn();
|
||||
document.addEventListener("keydown", onDocumentKeyDown);
|
||||
renderWithProviders(
|
||||
<SelectInput
|
||||
labelText="Who can read"
|
||||
showLabel
|
||||
value="option1"
|
||||
options={[
|
||||
{ value: "option1", label: "Option 1" },
|
||||
{ value: "option2", label: "Option 2" },
|
||||
]}
|
||||
/>,
|
||||
);
|
||||
|
||||
const trigger = screen.getByRole("button", { name: /Who can read/i });
|
||||
trigger.focus();
|
||||
await user.keyboard("{ArrowDown}");
|
||||
expect(await screen.findByRole("listbox")).toBeVisible();
|
||||
onDocumentKeyDown.mockClear();
|
||||
await user.keyboard("{Escape}");
|
||||
expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
|
||||
expect(onDocumentKeyDown).not.toHaveBeenCalled();
|
||||
document.removeEventListener("keydown", onDocumentKeyDown);
|
||||
});
|
||||
|
||||
it("selects an option that is clicked", async () => {
|
||||
const onChange = vi.fn();
|
||||
const user = userEvent.setup();
|
||||
renderWithProviders(
|
||||
<SelectInput
|
||||
labelText="Who can read"
|
||||
showLabel
|
||||
value="option1"
|
||||
onChange={onChange}
|
||||
options={[
|
||||
{ value: "option1", label: "Option 1" },
|
||||
{ value: "option2", label: "Option 2" },
|
||||
]}
|
||||
/>,
|
||||
);
|
||||
|
||||
await user.click(screen.getByRole("button", { name: /Who can read/i }));
|
||||
await user.click(await screen.findByRole("option", { name: "Option 2" }));
|
||||
expect(onChange).toHaveBeenCalledWith({
|
||||
target: { value: "option2", text: "Option 2" },
|
||||
});
|
||||
expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const getReadablePublishedRuleMock = vi.fn();
|
||||
const getSessionUserMock = vi.fn();
|
||||
const rateLimitKeyMock = vi.fn();
|
||||
const notFoundMock = vi.fn(() => {
|
||||
throw new Error("NEXT_NOT_FOUND");
|
||||
});
|
||||
@@ -16,10 +17,11 @@ vi.mock("next/headers", () => ({
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/rateLimit", () => ({
|
||||
rateLimitKey: () => ({ ok: true }),
|
||||
rateLimitKey: (...args: unknown[]) => rateLimitKeyMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/publishedRules", () => ({
|
||||
SHARE_LOOKUP_MISS_INTERVAL_MS: 1_000,
|
||||
getReadablePublishedRule: (...args: unknown[]) =>
|
||||
getReadablePublishedRuleMock(...args),
|
||||
}));
|
||||
@@ -34,6 +36,8 @@ describe("shared Rule page", () => {
|
||||
beforeEach(() => {
|
||||
getReadablePublishedRuleMock.mockReset();
|
||||
getSessionUserMock.mockReset();
|
||||
rateLimitKeyMock.mockReset();
|
||||
rateLimitKeyMock.mockReturnValue({ ok: true });
|
||||
notFoundMock.mockClear();
|
||||
getSessionUserMock.mockResolvedValue(null);
|
||||
});
|
||||
@@ -47,4 +51,18 @@ describe("shared Rule page", () => {
|
||||
).rejects.toThrow("NEXT_NOT_FOUND");
|
||||
expect(notFoundMock).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not look up a malformed slug when the miss limit refuses", async () => {
|
||||
rateLimitKeyMock.mockReturnValue({ ok: false, retryAfterMs: 1_000 });
|
||||
await expect(
|
||||
generateMetadata({
|
||||
params: Promise.resolve({ shareSlug: "not-a-slug" }),
|
||||
}),
|
||||
).rejects.toThrow("NEXT_NOT_FOUND");
|
||||
expect(rateLimitKeyMock).toHaveBeenCalledWith(
|
||||
"share-lookup-miss:203.0.113.8",
|
||||
1_000,
|
||||
);
|
||||
expect(getReadablePublishedRuleMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -3,6 +3,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
const isDatabaseConfiguredMock = vi.fn();
|
||||
const findUniqueMock = vi.fn();
|
||||
const rateLimitKeyMock = vi.fn();
|
||||
const rateLimitCoolingDownMock = vi.fn();
|
||||
const findManyMock = vi.fn();
|
||||
const stakeholderFindManyMock = vi.fn();
|
||||
const browserHoldsClaimForHashMock = vi.fn();
|
||||
@@ -15,6 +16,8 @@ vi.mock("../../lib/server/env", () => ({
|
||||
|
||||
vi.mock("../../lib/server/rateLimit", () => ({
|
||||
rateLimitKey: (...args: unknown[]) => rateLimitKeyMock(...args),
|
||||
rateLimitCoolingDown: (...args: unknown[]) =>
|
||||
rateLimitCoolingDownMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/db", () => ({
|
||||
@@ -77,10 +80,12 @@ beforeEach(() => {
|
||||
isRuleAdminEmailMock.mockReset();
|
||||
logAdminRuleReadMock.mockReset();
|
||||
rateLimitKeyMock.mockReset();
|
||||
rateLimitCoolingDownMock.mockReset();
|
||||
browserHoldsClaimForHashMock.mockResolvedValue(false);
|
||||
isRuleAdminEmailMock.mockReturnValue(false);
|
||||
logAdminRuleReadMock.mockResolvedValue(undefined);
|
||||
rateLimitKeyMock.mockReturnValue({ ok: true });
|
||||
rateLimitCoolingDownMock.mockReturnValue(false);
|
||||
});
|
||||
|
||||
describe("getReadablePublishedRule", () => {
|
||||
@@ -245,6 +250,38 @@ describe("getReadablePublishedRule", () => {
|
||||
1_000,
|
||||
);
|
||||
});
|
||||
|
||||
it("does not query when a share-slug miss is still cooling down", async () => {
|
||||
isDatabaseConfiguredMock.mockReturnValue(true);
|
||||
rateLimitCoolingDownMock.mockReturnValue(true);
|
||||
expect(
|
||||
await getReadablePublishedRule({
|
||||
idOrSlug: "k7f2-9qxm",
|
||||
user: null,
|
||||
viaShareSlug: true,
|
||||
requestPath: "/r/k7f2-9qxm",
|
||||
failedLookupKey: "203.0.113.8",
|
||||
}),
|
||||
).toBeNull();
|
||||
expect(findUniqueMock).not.toHaveBeenCalled();
|
||||
expect(rateLimitKeyMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns null when the miss limit refuses after a lookup", async () => {
|
||||
isDatabaseConfiguredMock.mockReturnValue(true);
|
||||
findUniqueMock.mockResolvedValueOnce(null);
|
||||
rateLimitKeyMock.mockReturnValue({ ok: false, retryAfterMs: 400 });
|
||||
expect(
|
||||
await getReadablePublishedRule({
|
||||
idOrSlug: "k7f2-9qxm",
|
||||
user: null,
|
||||
viaShareSlug: true,
|
||||
requestPath: "/r/k7f2-9qxm",
|
||||
failedLookupKey: "203.0.113.8",
|
||||
}),
|
||||
).toBeNull();
|
||||
expect(findUniqueMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("listListedPublishedRules", () => {
|
||||
|
||||
Reference in new issue
Block a user