New Rules stay off lists until the person who holds the pen turns on a share link, or the team features one. Uploaded files are readable only by those readers, stripped of location metadata, and deleted with the Rule, draft, or account.
91 lines
3.0 KiB
TypeScript
91 lines
3.0 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const isDatabaseConfiguredMock = vi.fn();
|
|
const getSessionUserMock = vi.fn();
|
|
const getOwnedRuleShareSettingsMock = vi.fn();
|
|
const patchOwnedRuleShareMock = vi.fn();
|
|
const getPublicOriginMock = vi.fn();
|
|
|
|
vi.mock("../../lib/server/env", () => ({
|
|
isDatabaseConfigured: () => isDatabaseConfiguredMock(),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/session", () => ({
|
|
getSessionUser: () => getSessionUserMock(),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/publicOrigin", () => ({
|
|
getPublicOrigin: (...args: unknown[]) => getPublicOriginMock(...args),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/ruleShare", () => ({
|
|
getOwnedRuleShareSettings: (...args: unknown[]) =>
|
|
getOwnedRuleShareSettingsMock(...args),
|
|
patchOwnedRuleShare: (...args: unknown[]) => patchOwnedRuleShareMock(...args),
|
|
}));
|
|
|
|
import { GET, PATCH } from "../../app/api/rules/[id]/share/route";
|
|
|
|
function makeContext(id: string) {
|
|
return { params: Promise.resolve({ id }) };
|
|
}
|
|
|
|
beforeEach(() => {
|
|
isDatabaseConfiguredMock.mockReset();
|
|
getSessionUserMock.mockReset();
|
|
getOwnedRuleShareSettingsMock.mockReset();
|
|
patchOwnedRuleShareMock.mockReset();
|
|
getPublicOriginMock.mockReset();
|
|
getPublicOriginMock.mockReturnValue("https://communityrule.info");
|
|
});
|
|
|
|
describe("GET /api/rules/[id]/share", () => {
|
|
it("returns 401 when not signed in", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
getSessionUserMock.mockResolvedValue(null);
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1/share"),
|
|
makeContext("rule-1"),
|
|
);
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it("returns share settings for the owner", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
getSessionUserMock.mockResolvedValue({ id: "user-1", email: "a@b.c" });
|
|
getOwnedRuleShareSettingsMock.mockResolvedValueOnce({
|
|
visibility: "private",
|
|
shareUrl: "https://communityrule.info/rules/rule-1",
|
|
title: "Juniper",
|
|
});
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1/share"),
|
|
makeContext("rule-1"),
|
|
);
|
|
expect(res.status).toBe(200);
|
|
const body = (await res.json()) as { share: { visibility: string } };
|
|
expect(body.share.visibility).toBe("private");
|
|
});
|
|
});
|
|
|
|
describe("PATCH /api/rules/[id]/share", () => {
|
|
it("returns 400 when community agreement is missing", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
getSessionUserMock.mockResolvedValue({ id: "user-1", email: "a@b.c" });
|
|
patchOwnedRuleShareMock.mockResolvedValueOnce({
|
|
ok: false,
|
|
reason: "community_agreed",
|
|
});
|
|
const res = await PATCH(
|
|
new NextRequest("https://x.test/api/rules/rule-1/share", {
|
|
method: "PATCH",
|
|
body: JSON.stringify({ visibility: "shared_by_link" }),
|
|
headers: { "content-type": "application/json" },
|
|
}),
|
|
makeContext("rule-1"),
|
|
);
|
|
expect(res.status).toBe(400);
|
|
});
|
|
});
|