refactor: improve account validation in transaction updates and enhance test setup with mock API context

This commit is contained in:
GitHub Copilot
2025-04-24 11:23:14 -04:00
parent 96093200f5
commit c424691658
4 changed files with 133 additions and 104 deletions

View File

@@ -40,13 +40,14 @@ export const PUT: APIRoute = async ({ request, params }) => {
// If account is changing, validate new account exists // If account is changing, validate new account exists
let newAccount = oldAccount; let newAccount = oldAccount;
if (updates.accountId && updates.accountId !== oldTransaction.accountId) { if (updates.accountId && updates.accountId !== oldTransaction.accountId) {
newAccount = accounts.find((a) => a.id === updates.accountId); const foundAccount = accounts.find((a) => a.id === updates.accountId);
if (!newAccount) { if (!foundAccount) {
return new Response(JSON.stringify({ error: "Account not found" }), { return new Response(JSON.stringify({ error: "Account not found" }), {
status: 404, status: 404,
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
}); });
} }
newAccount = foundAccount;
} }
// First, remove the old transaction's effect on the old account // First, remove the old transaction's effect on the old account

View File

@@ -2,11 +2,12 @@ import { describe, it, expect } from "vitest";
import { GET as listAccounts } from "../pages/api/accounts/index"; import { GET as listAccounts } from "../pages/api/accounts/index";
import { GET as getAccount } from "../pages/api/accounts/[id]/index"; import { GET as getAccount } from "../pages/api/accounts/[id]/index";
import { GET as listTransactions } from "../pages/api/accounts/[id]/transactions/index"; import { GET as listTransactions } from "../pages/api/accounts/[id]/transactions/index";
import { createMockAPIContext } from "./setup";
describe("Accounts API", () => { describe("Accounts API", () => {
describe("GET /api/accounts", () => { describe("GET /api/accounts", () => {
it("should return all accounts", async () => { it("should return all accounts", async () => {
const response = await listAccounts(); const response = await listAccounts(createMockAPIContext() as any);
const accounts = await response.json(); const accounts = await response.json();
expect(response.status).toBe(200); expect(response.status).toBe(200);
@@ -18,7 +19,9 @@ describe("Accounts API", () => {
describe("GET /api/accounts/:id", () => { describe("GET /api/accounts/:id", () => {
it("should return a specific account", async () => { it("should return a specific account", async () => {
const response = await getAccount({ params: { id: "1" } }); const response = await getAccount(
createMockAPIContext({ params: { id: "1" } }) as any
);
const account = await response.json(); const account = await response.json();
expect(response.status).toBe(200); expect(response.status).toBe(200);
@@ -27,7 +30,9 @@ describe("Accounts API", () => {
}); });
it("should return 404 for non-existent account", async () => { it("should return 404 for non-existent account", async () => {
const response = await getAccount({ params: { id: "999" } }); const response = await getAccount(
createMockAPIContext({ params: { id: "999" } }) as any
);
const error = await response.json(); const error = await response.json();
expect(response.status).toBe(404); expect(response.status).toBe(404);
@@ -36,8 +41,10 @@ describe("Accounts API", () => {
}); });
describe("GET /api/accounts/:id/transactions", () => { describe("GET /api/accounts/:id/transactions", () => {
it("should return transactions for a specific account", async () => { it("should return transactions for an account", async () => {
const response = await listTransactions({ params: { id: "1" } }); const response = await listTransactions(
createMockAPIContext({ params: { id: "1" } }) as any
);
const transactions = await response.json(); const transactions = await response.json();
expect(response.status).toBe(200); expect(response.status).toBe(200);
@@ -46,7 +53,9 @@ describe("Accounts API", () => {
}); });
it("should return empty array for account with no transactions", async () => { it("should return empty array for account with no transactions", async () => {
const response = await listTransactions({ params: { id: "999" } }); const response = await listTransactions(
createMockAPIContext({ params: { id: "999" } }) as any
);
const transactions = await response.json(); const transactions = await response.json();
expect(response.status).toBe(200); expect(response.status).toBe(200);

View File

@@ -1,5 +1,31 @@
import { beforeEach } from "vitest"; import { beforeEach } from "vitest";
import { accounts, transactions } from "../data/store"; import { accounts, transactions } from "../data/store";
import type { APIContext } from "astro";
// Create a mock APIContext factory
export function createMockAPIContext<
T extends Record<string, string> = Record<string, string>
>({ params = {} as T } = {}): Partial<APIContext> {
return {
params,
props: {},
request: new Request("http://localhost:4321"),
site: new URL("http://localhost:4321"),
generator: "test",
url: new URL("http://localhost:4321"),
clientAddress: "127.0.0.1",
cookies: new Headers() as any, // Cast Headers to cookies as we don't need cookie functionality in tests
redirect: () => new Response(),
locals: {},
preferredLocale: undefined,
preferredLocaleList: [],
currentLocale: undefined,
routePattern: "/api/[...path]",
originPathname: "/api",
getActionResult: () => undefined,
isPrerendered: false,
};
}
// Reset test data before each test // Reset test data before each test
beforeEach(() => { beforeEach(() => {

View File

@@ -6,6 +6,7 @@ import {
} from "../pages/api/transactions/[id]/index"; } from "../pages/api/transactions/[id]/index";
import { accounts, transactions } from "../data/store"; import { accounts, transactions } from "../data/store";
import type { Transaction } from "../types"; import type { Transaction } from "../types";
import { createMockAPIContext } from "./setup";
describe("Transactions API", () => { describe("Transactions API", () => {
describe("POST /api/transactions", () => { describe("POST /api/transactions", () => {
@@ -18,14 +19,14 @@ describe("Transactions API", () => {
amount: -25.0, amount: -25.0,
}; };
const response = await createTransaction({ const ctx = createMockAPIContext() as any;
request: new Request("http://localhost:4321/api/transactions", { ctx.request = new Request("http://localhost:4321/api/transactions", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(newTransaction), body: JSON.stringify(newTransaction),
}),
}); });
const response = await createTransaction(ctx);
const result = await response.json(); const result = await response.json();
expect(response.status).toBe(201); expect(response.status).toBe(201);
@@ -40,14 +41,14 @@ describe("Transactions API", () => {
// Missing required fields // Missing required fields
}; };
const response = await createTransaction({ const ctx = createMockAPIContext() as any;
request: new Request("http://localhost:4321/api/transactions", { ctx.request = new Request("http://localhost:4321/api/transactions", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(invalidTransaction), body: JSON.stringify(invalidTransaction),
}),
}); });
const response = await createTransaction(ctx);
const error = await response.json(); const error = await response.json();
expect(response.status).toBe(400); expect(response.status).toBe(400);
@@ -56,20 +57,20 @@ describe("Transactions API", () => {
it("should reject transaction with invalid account", async () => { it("should reject transaction with invalid account", async () => {
const invalidTransaction = { const invalidTransaction = {
accountId: "999", // Non-existent account accountId: "999",
date: "2025-04-24", date: "2025-04-24",
description: "Invalid Account Test", description: "Invalid Account Test",
amount: 100, amount: 100,
}; };
const response = await createTransaction({ const ctx = createMockAPIContext() as any;
request: new Request("http://localhost:4321/api/transactions", { ctx.request = new Request("http://localhost:4321/api/transactions", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(invalidTransaction), body: JSON.stringify(invalidTransaction),
}),
}); });
const response = await createTransaction(ctx);
const error = await response.json(); const error = await response.json();
expect(response.status).toBe(404); expect(response.status).toBe(404);
@@ -77,14 +78,14 @@ describe("Transactions API", () => {
}); });
it("should reject invalid request body", async () => { it("should reject invalid request body", async () => {
const response = await createTransaction({ const ctx = createMockAPIContext() as any;
request: new Request("http://localhost:4321/api/transactions", { ctx.request = new Request("http://localhost:4321/api/transactions", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: "invalid json", body: "invalid json",
}),
}); });
const response = await createTransaction(ctx);
const error = await response.json(); const error = await response.json();
expect(response.status).toBe(400); expect(response.status).toBe(400);
@@ -101,15 +102,14 @@ describe("Transactions API", () => {
amount: -75.0, amount: -75.0,
}; };
const response = await updateTransaction({ const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
params: { id: "1" }, ctx.request = new Request("http://localhost:4321/api/transactions/1", {
request: new Request("http://localhost:4321/api/transactions/1", { method: "PUT",
method: "PUT", headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json" }, body: JSON.stringify(updates),
body: JSON.stringify(updates),
}),
}); });
const response = await updateTransaction(ctx);
const result = await response.json(); const result = await response.json();
expect(response.status).toBe(200); expect(response.status).toBe(200);
@@ -121,15 +121,14 @@ describe("Transactions API", () => {
}); });
it("should reject update with invalid request body", async () => { it("should reject update with invalid request body", async () => {
const response = await updateTransaction({ const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
params: { id: "1" }, ctx.request = new Request("http://localhost:4321/api/transactions/1", {
request: new Request("http://localhost:4321/api/transactions/1", { method: "PUT",
method: "PUT", headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json" }, body: "invalid json",
body: "invalid json",
}),
}); });
const response = await updateTransaction(ctx);
const error = await response.json(); const error = await response.json();
expect(response.status).toBe(400); expect(response.status).toBe(400);
@@ -137,15 +136,14 @@ describe("Transactions API", () => {
}); });
it("should reject update for non-existent transaction", async () => { it("should reject update for non-existent transaction", async () => {
const response = await updateTransaction({ const ctx = createMockAPIContext({ params: { id: "999" } }) as any;
params: { id: "999" }, ctx.request = new Request("http://localhost:4321/api/transactions/999", {
request: new Request("http://localhost:4321/api/transactions/999", { method: "PUT",
method: "PUT", headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json" }, body: JSON.stringify({ description: "Test" }),
body: JSON.stringify({ description: "Test" }),
}),
}); });
const response = await updateTransaction(ctx);
const error = await response.json(); const error = await response.json();
expect(response.status).toBe(404); expect(response.status).toBe(404);
@@ -156,15 +154,14 @@ describe("Transactions API", () => {
// First update the transaction to point to a non-existent account // First update the transaction to point to a non-existent account
transactions[0].accountId = "999"; transactions[0].accountId = "999";
const response = await updateTransaction({ const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
params: { id: "1" }, ctx.request = new Request("http://localhost:4321/api/transactions/1", {
request: new Request("http://localhost:4321/api/transactions/1", { method: "PUT",
method: "PUT", headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json" }, body: JSON.stringify({ amount: -100 }),
body: JSON.stringify({ amount: -100 }),
}),
}); });
const response = await updateTransaction(ctx);
const error = await response.json(); const error = await response.json();
expect(response.status).toBe(404); expect(response.status).toBe(404);
@@ -183,19 +180,17 @@ describe("Transactions API", () => {
const oldTransaction = transactions.find((t) => t.id === "1"); const oldTransaction = transactions.find((t) => t.id === "1");
if (!oldTransaction) throw new Error("Test transaction not found"); if (!oldTransaction) throw new Error("Test transaction not found");
// Update transaction to move it to a different account const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
const response = await updateTransaction({ ctx.request = new Request("http://localhost:4321/api/transactions/1", {
params: { id: "1" }, method: "PUT",
request: new Request("http://localhost:4321/api/transactions/1", { headers: { "Content-Type": "application/json" },
method: "PUT", body: JSON.stringify({
headers: { "Content-Type": "application/json" }, accountId: newAccount.id,
body: JSON.stringify({ amount: -100,
accountId: newAccount.id,
amount: -100,
}),
}), }),
}); });
const response = await updateTransaction(ctx);
const result = await response.json(); const result = await response.json();
expect(response.status).toBe(200); expect(response.status).toBe(200);
@@ -211,15 +206,17 @@ describe("Transactions API", () => {
}); });
it("should reject update without transaction ID", async () => { it("should reject update without transaction ID", async () => {
const response = await updateTransaction({ const ctx = createMockAPIContext() as any;
params: {}, ctx.request = new Request(
request: new Request("http://localhost:4321/api/transactions/undefined", { "http://localhost:4321/api/transactions/undefined",
{
method: "PUT", method: "PUT",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ description: "Test" }), body: JSON.stringify({ description: "Test" }),
}), }
}); );
const response = await updateTransaction(ctx);
const error = await response.json(); const error = await response.json();
expect(response.status).toBe(400); expect(response.status).toBe(400);
@@ -231,15 +228,14 @@ describe("Transactions API", () => {
const savedAccounts = [...accounts]; const savedAccounts = [...accounts];
accounts.length = 0; accounts.length = 0;
const response = await updateTransaction({ const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
params: { id: "1" }, ctx.request = new Request("http://localhost:4321/api/transactions/1", {
request: new Request("http://localhost:4321/api/transactions/1", { method: "PUT",
method: "PUT", headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json" }, body: JSON.stringify({ amount: -100 }),
body: JSON.stringify({ amount: -100 }),
}),
}); });
const response = await updateTransaction(ctx);
const error = await response.json(); const error = await response.json();
expect(response.status).toBe(404); expect(response.status).toBe(404);
@@ -250,18 +246,17 @@ describe("Transactions API", () => {
}); });
it("should reject update when new account doesn't exist", async () => { it("should reject update when new account doesn't exist", async () => {
const response = await updateTransaction({ const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
params: { id: "1" }, ctx.request = new Request("http://localhost:4321/api/transactions/1", {
request: new Request("http://localhost:4321/api/transactions/1", { method: "PUT",
method: "PUT", headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json" }, body: JSON.stringify({
body: JSON.stringify({ accountId: "999", // Non-existent account
accountId: "999", // Non-existent account amount: -100,
amount: -100,
}),
}), }),
}); });
const response = await updateTransaction(ctx);
const error = await response.json(); const error = await response.json();
expect(response.status).toBe(404); expect(response.status).toBe(404);
@@ -275,9 +270,9 @@ describe("Transactions API", () => {
const transactionAmount = transactions[0].amount; const transactionAmount = transactions[0].amount;
const initialCount = transactions.length; const initialCount = transactions.length;
const response = await deleteTransaction({ const response = await deleteTransaction(
params: { id: "1" }, createMockAPIContext({ params: { id: "1" } }) as any
}); );
expect(response.status).toBe(204); expect(response.status).toBe(204);
expect(transactions).toHaveLength(initialCount - 1); expect(transactions).toHaveLength(initialCount - 1);
@@ -285,9 +280,7 @@ describe("Transactions API", () => {
}); });
it("should reject delete without transaction ID", async () => { it("should reject delete without transaction ID", async () => {
const response = await deleteTransaction({ const response = await deleteTransaction(createMockAPIContext() as any);
params: {},
});
const error = await response.json(); const error = await response.json();
@@ -296,9 +289,9 @@ describe("Transactions API", () => {
}); });
it("should return 404 for non-existent transaction", async () => { it("should return 404 for non-existent transaction", async () => {
const response = await deleteTransaction({ const response = await deleteTransaction(
params: { id: "999" }, createMockAPIContext({ params: { id: "999" } }) as any
}); );
const error = await response.json(); const error = await response.json();
@@ -317,9 +310,9 @@ describe("Transactions API", () => {
}; };
transactions.push(testTransaction); transactions.push(testTransaction);
const response = await deleteTransaction({ const response = await deleteTransaction(
params: { id: "test-delete" }, createMockAPIContext({ params: { id: "test-delete" } }) as any
}); );
const error = await response.json(); const error = await response.json();