feat(portal): Emails list and detail, with the body in a sandboxed frame
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
0961a10bb2
commit
0904808e81
12 files changed
+531
-1
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() }));
|
||||
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
|
||||
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
|
||||
|
||||
import { GET } from "./route";
|
||||
|
||||
const req = (url: string) => new Request(url) as unknown as Parameters<typeof GET>[0];
|
||||
const ctx = (path?: string[]) => ({ params: Promise.resolve({ path }) });
|
||||
|
||||
beforeEach(() => {
|
||||
mocks.callMobileApi.mockReset();
|
||||
mocks.getToken.mockReset().mockResolvedValue("jwt");
|
||||
});
|
||||
|
||||
describe("GET /api/emails/[[...path]]", () => {
|
||||
it("returns 401 without a session", async () => {
|
||||
mocks.getToken.mockResolvedValue(null);
|
||||
expect((await GET(req("http://localhost/api/emails"), ctx())).status).toBe(401);
|
||||
});
|
||||
|
||||
it("forwards the list query", async () => {
|
||||
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { content: [] } });
|
||||
await GET(req("http://localhost/api/emails?recipient=s&page=0&size=20"), ctx());
|
||||
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/emails?recipient=s&page=0&size=20", { token: "jwt" });
|
||||
});
|
||||
|
||||
it("forwards the detail id", async () => {
|
||||
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { id: 7 } });
|
||||
await GET(req("http://localhost/api/emails/7"), ctx(["7"]));
|
||||
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/emails/7", { token: "jwt" });
|
||||
});
|
||||
|
||||
it("refuses dot segments", async () => {
|
||||
expect((await GET(req("http://localhost/api/emails/.."), ctx([".."]))).status).toBe(400);
|
||||
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("passes a refusal through with its code", async () => {
|
||||
mocks.callMobileApi.mockResolvedValue({ ok: false, status: 403, error: "Only a super admin can read this email", code: "SUPER_ADMIN_REQUIRED" });
|
||||
const res = await GET(req("http://localhost/api/emails/3"), ctx(["3"]));
|
||||
expect(res.status).toBe(403);
|
||||
expect(await res.json()).toMatchObject({ code: "SUPER_ADMIN_REQUIRED" });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
import { callMobileApi } from "@/app/lib/server/mobileApi";
|
||||
import { getToken } from "@/app/lib/server/session";
|
||||
import { badRequest, errorResponse, unauthorized } from "@/app/lib/server/apiRoute";
|
||||
|
||||
// Read-only proxy for /api/admin/emails; mobile-api owns the BO_EMAILS and super-admin guards.
|
||||
type Ctx = { params: Promise<{ path?: string[] }> };
|
||||
|
||||
export async function GET(req: NextRequest, { params }: Ctx) {
|
||||
const token = await getToken();
|
||||
if (!token) return unauthorized();
|
||||
const { path = [] } = await params; // [[...path]]: the list call has no segment
|
||||
if (path.some((s) => s === "." || s === "..")) return badRequest("Invalid path.");
|
||||
const target = `/api/admin/emails${path.map(encodeURIComponent).map((s) => `/${s}`).join("")}${new URL(req.url).search}`;
|
||||
const result = await callMobileApi<unknown>(target, { token });
|
||||
if (!result.ok) return errorResponse(result);
|
||||
return NextResponse.json(result.data);
|
||||
}
|
||||
Reference in new issue
Block a user