Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, vi } from "vitest";
import { NextRequest } from "next/server";
import { CngPermissionError } from "@peektravel/app-utilities";

vi.mock("@/lib/with-app", () => ({
withAppAuthentication: (handler: (...args: unknown[]) => unknown) =>
Expand Down Expand Up @@ -29,4 +30,27 @@ describe("GET /api/activities", () => {
],
});
});

it("answers a missing-permission rejection with 403 and the named permissions", async () => {
fakeCng.getAllActivities.mockRejectedValue(
new CngPermissionError(["products:read"], { message: "Forbidden" }),
);

const response = await GET(new NextRequest("http://localhost/api/activities"));

expect(response.status).toBe(403);
await expect(response.json()).resolves.toEqual({
error:
"CNG request forbidden: the app is missing the required permission: products:read",
permissions: ["products:read"],
});
});

it("re-throws any other failure", async () => {
fakeCng.getAllActivities.mockRejectedValue(new Error("boom"));

await expect(
GET(new NextRequest("http://localhost/api/activities")),
).rejects.toThrow("boom");
});
});
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import { type NextRequest, NextResponse } from "next/server";
import { type CngAccessService } from "@peektravel/app-utilities";
import {
CngPermissionError,
type CngAccessService,
} from "@peektravel/app-utilities";
import { withAppAuthentication } from "@/lib/with-app";

// This route lives under the CNG tree, so it KNOWS its accessor is a
// CngAccessService — name it and skip any runtime narrowing.
export const GET = withAppAuthentication<CngAccessService>(
async (_request: NextRequest, cng: CngAccessService) => {
const products = await cng.getAllActivities();
const activities = products.map(({ productId, name, color }) => ({
id: productId,
name,
color,
}));
return NextResponse.json({ activities });
try {
const products = await cng.getAllActivities();
const activities = products.map(({ productId, name, color }) => ({
id: productId,
name,
color,
}));
return NextResponse.json({ activities });
} catch (err) {
// A CngPermissionError is an EXPECTED outcome for an install whose
// manifest grants fewer permissions than this route calls for, so shape it
// for the UI rather than letting Next.js report a 500 with a stack trace.
// This lives here, not in withAppAuthentication: that wrapper serves every
// platform, and only CNG throws this. The transport already logged it at
// warn — don't re-log. `permissions` lets the client name what to grant.
if (err instanceof CngPermissionError) {
return NextResponse.json(
{ error: err.message, permissions: err.permissions },
{ status: err.statusCode },
);
}
throw err;
}
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ describe("SettingsViewPage", () => {
expect(setLoadingActivities).toHaveBeenCalledWith(false);
});

it("surfaces the gateway's message verbatim when the fetch is forbidden", async () => {
const setError = vi.fn();
stateSequence = [
[true, vi.fn()],
[null, vi.fn()],
[null, vi.fn()],
[false, vi.fn()],
[null, setError],
];
const message =
"CNG request forbidden: the app is missing the required permission: products:read";
mockApiFetch.mockRejectedValueOnce(new Error(message));

const button = findByType(SettingsViewPage(), "ody-button");
await (button?.props.onClick as () => Promise<void>)();

expect(setError).toHaveBeenCalledWith(message);
});

it("shows an error alert when the activities fetch fails", () => {
stateSequence = makeStates({ ready: true, error: "Request failed" });
const rendered = JSON.stringify(SettingsViewPage());
Expand Down
2 changes: 1 addition & 1 deletion templates/nextjs-starter-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test:coverage": "vitest run --coverage"
},
"dependencies": {
"@peektravel/app-utilities": "^0.8.0",
"@peektravel/app-utilities": "^0.8.2",
"next": "16.2.9",
"react": "19.2.4",
"react-dom": "19.2.4",
Expand Down
10 changes: 5 additions & 5 deletions templates/nextjs-starter-kit/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading