fix: close demo payment bypass and guard order transitions
masterplan-lock / check (pull_request) Successful in 4s
CI (SIAX Cloud) / security (push) Successful in 14s
CI (SIAX Cloud) / security (pull_request) Successful in 15s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 16s
CI (SIAX Cloud) / integration (pull_request) Successful in 26s
masterplan-lock / check (push) Successful in 35s
CI (SIAX Cloud) / quality (pull_request) Successful in 37s
Security Scan (SIAX Cloud) / security (push) Successful in 1m4s
CI (SIAX Cloud) / integration (push) Successful in 1m14s
CI (SIAX Cloud) / quality (push) Successful in 2m12s
masterplan-lock / check (pull_request) Successful in 4s
CI (SIAX Cloud) / security (push) Successful in 14s
CI (SIAX Cloud) / security (pull_request) Successful in 15s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 16s
CI (SIAX Cloud) / integration (pull_request) Successful in 26s
masterplan-lock / check (push) Successful in 35s
CI (SIAX Cloud) / quality (pull_request) Successful in 37s
Security Scan (SIAX Cloud) / security (push) Successful in 1m4s
CI (SIAX Cloud) / integration (push) Successful in 1m14s
CI (SIAX Cloud) / quality (push) Successful in 2m12s
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { beforeEach, expect, it, vi } from "vitest";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
auth: vi.fn(),
|
||||
getOrder: vi.fn(),
|
||||
setStatusIfCurrent: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/src/auth/require-route-auth", () => ({ requireRouteAuth: mocks.auth }));
|
||||
vi.mock("@/lib/orders-store", () => ({
|
||||
getOrder: mocks.getOrder,
|
||||
setStatusIfCurrent: mocks.setStatusIfCurrent,
|
||||
}));
|
||||
|
||||
import { PATCH } from "@/app/api/orders/[id]/route";
|
||||
|
||||
beforeEach(() => {
|
||||
mocks.auth.mockReset().mockResolvedValue({ ctx: { tenantId: "verified-tenant" } });
|
||||
mocks.getOrder.mockReset().mockResolvedValue({ order_id: "order-1", status: "draft" });
|
||||
mocks.setStatusIfCurrent.mockReset().mockResolvedValue({ order_id: "order-1", status: "accepted" });
|
||||
});
|
||||
|
||||
function request(status: string) {
|
||||
return new NextRequest("https://0pt.app/api/orders/order-1", {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ status }),
|
||||
});
|
||||
}
|
||||
|
||||
const ctx = { params: Promise.resolve({ id: "order-1" }) };
|
||||
|
||||
it("requires verified auth before changing an order", async () => {
|
||||
mocks.auth.mockResolvedValue({ response: NextResponse.json({ error: "unauthenticated" }, { status: 401 }) });
|
||||
expect((await PATCH(request("accepted"), ctx)).status).toBe(401);
|
||||
expect(mocks.getOrder).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects client writes to payment-confirmed states", async () => {
|
||||
expect((await PATCH(request("in_production"), ctx)).status).toBe(403);
|
||||
expect((await PATCH(request("delivered"), ctx)).status).toBe(403);
|
||||
expect(mocks.setStatusIfCurrent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("cannot jump from draft directly to accepted", async () => {
|
||||
expect((await PATCH(request("accepted"), ctx)).status).toBe(409);
|
||||
expect(mocks.setStatusIfCurrent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("uses an atomic compare-and-set transition after review", async () => {
|
||||
mocks.getOrder.mockResolvedValue({ order_id: "order-1", status: "review" });
|
||||
expect((await PATCH(request("accepted"), ctx)).status).toBe(200);
|
||||
expect(mocks.setStatusIfCurrent).toHaveBeenCalledWith("order-1", "review", "accepted");
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
import { expect, it, vi } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const mocks = vi.hoisted(() => ({ create: vi.fn(), setStatus: vi.fn() }));
|
||||
const mocks = vi.hoisted(() => ({ create: vi.fn(), setStatus: vi.fn(), auth: vi.fn() }));
|
||||
vi.mock("@/lib/stripe", () => ({
|
||||
getStripe: () => ({ checkout: { sessions: { create: mocks.create } } }),
|
||||
}));
|
||||
@@ -9,10 +9,14 @@ vi.mock("@/lib/orders-store", () => ({
|
||||
getOrder: vi.fn().mockResolvedValue({ order_id: "order-1", status: "accepted", balance_ore: 5000 }),
|
||||
setStatus: mocks.setStatus,
|
||||
}));
|
||||
vi.mock("@/src/auth/require-route-auth", () => ({
|
||||
requireRouteAuth: mocks.auth,
|
||||
}));
|
||||
|
||||
import { POST } from "@/app/api/payments/[order_id]/release/route";
|
||||
|
||||
it("does not mark delivery complete merely by creating a balance Checkout Session", async () => {
|
||||
mocks.auth.mockResolvedValue({ ctx: { tenantId: "verified-tenant" } });
|
||||
mocks.create.mockResolvedValue({ url: "https://checkout.stripe.com/test" });
|
||||
const req = new NextRequest("https://0pt.app/api/payments/order-1/release", { method: "POST" });
|
||||
|
||||
@@ -23,3 +27,12 @@ it("does not mark delivery complete merely by creating a balance Checkout Sessio
|
||||
}));
|
||||
expect(mocks.setStatus).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects an unauthenticated balance Checkout request", async () => {
|
||||
mocks.create.mockClear();
|
||||
mocks.auth.mockResolvedValue({ response: NextResponse.json({ error: "unauthenticated" }, { status: 401 }) });
|
||||
const req = new NextRequest("https://0pt.app/api/payments/order-1/release", { method: "POST" });
|
||||
|
||||
expect((await POST(req, { params: Promise.resolve({ order_id: "order-1" }) })).status).toBe(401);
|
||||
expect(mocks.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -4,7 +4,7 @@ import { NextRequest } from "next/server";
|
||||
const mocks = vi.hoisted(() => ({
|
||||
constructEvent: vi.fn(),
|
||||
getOrder: vi.fn(),
|
||||
setStatus: vi.fn(),
|
||||
setStatusIfCurrent: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/stripe", () => ({
|
||||
@@ -12,7 +12,7 @@ vi.mock("@/lib/stripe", () => ({
|
||||
}));
|
||||
vi.mock("@/lib/orders-store", () => ({
|
||||
getOrder: mocks.getOrder,
|
||||
setStatus: mocks.setStatus,
|
||||
setStatusIfCurrent: mocks.setStatusIfCurrent,
|
||||
}));
|
||||
|
||||
import { POST } from "@/app/api/webhooks/stripe/route";
|
||||
@@ -23,8 +23,8 @@ beforeEach(() => {
|
||||
process.env.STRIPE_WEBHOOK_SECRET = "whsec_fake_local_regression";
|
||||
mocks.constructEvent.mockReset();
|
||||
mocks.getOrder.mockReset();
|
||||
mocks.setStatus.mockReset();
|
||||
mocks.setStatus.mockResolvedValue({ order_id: "order-1" });
|
||||
mocks.setStatusIfCurrent.mockReset();
|
||||
mocks.setStatusIfCurrent.mockResolvedValue({ order_id: "order-1" });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -57,7 +57,7 @@ it("advances a matching paid deposit only after a verified event", async () => {
|
||||
mocks.getOrder.mockResolvedValue({ order_id: "order-1", status: "draft", deposit_ore: 5000, balance_ore: 5000 });
|
||||
|
||||
expect((await POST(delivery())).status).toBe(200);
|
||||
expect(mocks.setStatus).toHaveBeenCalledWith("order-1", "in_production");
|
||||
expect(mocks.setStatusIfCurrent).toHaveBeenCalledWith("order-1", "draft", "in_production");
|
||||
});
|
||||
|
||||
it("never advances an unpaid or amount-mismatched deposit", async () => {
|
||||
@@ -66,7 +66,7 @@ it("never advances an unpaid or amount-mismatched deposit", async () => {
|
||||
expect((await POST(delivery())).status).toBe(503);
|
||||
session("deposit", "paid", 4999);
|
||||
expect((await POST(delivery())).status).toBe(422);
|
||||
expect(mocks.setStatus).not.toHaveBeenCalled();
|
||||
expect(mocks.setStatusIfCurrent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("marks a matching paid balance delivered only after acceptance", async () => {
|
||||
@@ -74,5 +74,5 @@ it("marks a matching paid balance delivered only after acceptance", async () =>
|
||||
mocks.getOrder.mockResolvedValue({ order_id: "order-1", status: "accepted", deposit_ore: 5000, balance_ore: 5000 });
|
||||
|
||||
expect((await POST(delivery())).status).toBe(200);
|
||||
expect(mocks.setStatus).toHaveBeenCalledWith("order-1", "delivered");
|
||||
expect(mocks.setStatusIfCurrent).toHaveBeenCalledWith("order-1", "accepted", "delivered");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user