Skip to content

Commit 20ce53b

Browse files
committed
fix: add ON DELETE CASCADE to user_id reference in orders table and enhance end-to-end test for order creation
1 parent 04a6a74 commit 20ce53b

File tree

2 files changed

+78
-42
lines changed

2 files changed

+78
-42
lines changed

src/db/migrations/initial.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ CREATE TABLE IF NOT EXISTS cart_items (
5353

5454
CREATE TABLE IF NOT EXISTS orders (
5555
id SERIAL PRIMARY KEY,
56-
user_id INTEGER REFERENCES users(id),
56+
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
5757
total_amount NUMERIC(10,2) NOT NULL,
5858

5959
-- Customer and shipping details

src/e2e/user-create-order.spec.ts

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,93 @@
11
import { test, expect } from "@playwright/test";
22

3-
test("User can create an order", async ({ page }) => {
4-
await page.goto("http://localhost:5173/");
3+
import { hashPassword } from "@/lib/security";
4+
import type { CreateUserDTO } from "@/models/user.model";
5+
import {
6+
createUser,
7+
deleteUser,
8+
getUserByEmail,
9+
} from "@/repositories/user.repository";
510

6-
await page.getByRole("link", { name: "Iniciar sesión" }).click();
11+
test.describe("User", () => {
12+
let testUserId: number;
713

8-
const loginForm = {
9-
"Correo electrónico": "[email protected]",
10-
Contraseña: "supersecret",
11-
};
14+
test.beforeAll(async () => {
15+
const testUser: CreateUserDTO = {
16+
17+
name: null,
18+
password: await hashPassword("letmein"),
19+
isGuest: false,
20+
};
1221

13-
for (const [key, value] of Object.entries(loginForm)) {
14-
const input = await page.getByRole("textbox", { name: key });
15-
await input.click();
16-
await input.fill(value);
17-
}
22+
const existingUser = await getUserByEmail(testUser.email);
1823

19-
await page.getByRole("button", { name: "Iniciar sesión" }).click();
24+
if (existingUser) {
25+
await deleteUser(existingUser.id);
26+
}
2027

21-
// Wait for the user to be logged in
22-
await expect(
23-
page.getByRole("button", { name: "Cerrar sesión" })
24-
).toBeVisible();
28+
const user = await createUser(testUser);
29+
testUserId = user.id;
30+
});
2531

26-
await page.getByRole("menuitem", { name: "Polos" }).click();
27-
await page.getByTestId("product-item").first().click();
32+
test.afterAll(async () => {
33+
await deleteUser(testUserId);
34+
});
2835

29-
await page.getByRole("button", { name: "Agregar al Carrito" }).click();
30-
await page.getByRole("link", { name: "Carrito de compras" }).click();
36+
test("User can create an order", async ({ page }) => {
37+
await page.goto("http://localhost:5173/");
3138

32-
await page.getByRole("link", { name: "Continuar Compra" }).click();
39+
await page.getByRole("link", { name: "Iniciar sesión" }).click();
3340

34-
const orderForm = {
35-
Nombre: "Testino",
36-
Apellido: "Diprueba",
37-
Compañia: "",
38-
Dirección: "Calle De Prueba 123",
39-
Ciudad: "Lima",
40-
"Provincia/Estado": "Lima",
41-
"Código Postal": "51111",
42-
Teléfono: "987456321",
43-
};
41+
const loginForm = {
42+
"Correo electrónico": "[email protected]",
43+
Contraseña: "letmein",
44+
};
4445

45-
for (const [key, value] of Object.entries(orderForm)) {
46-
const input = await page.getByRole("textbox", { name: key });
47-
await input.click();
48-
await input.fill(value);
49-
}
46+
for (const [key, value] of Object.entries(loginForm)) {
47+
const input = await page.getByRole("textbox", { name: key });
48+
await input.click();
49+
await input.fill(value);
50+
}
5051

51-
await page.getByRole("combobox", { name: "País" }).selectOption("PE");
52+
await page.getByRole("button", { name: "Iniciar sesión" }).click();
5253

53-
await page.getByRole("button", { name: "Confirmar Orden" }).click();
54+
// Wait for the user to be logged in
55+
await expect(
56+
page.getByRole("button", { name: "Cerrar sesión" })
57+
).toBeVisible();
5458

55-
await expect(page.getByText("¡Muchas gracias por tu compra!")).toBeVisible();
56-
await expect(page.getByTestId("orderId")).toBeVisible();
59+
await page.getByRole("menuitem", { name: "Polos" }).click();
60+
await page.getByTestId("product-item").first().click();
61+
62+
await page.getByRole("button", { name: "Agregar al Carrito" }).click();
63+
await page.getByRole("link", { name: "Carrito de compras" }).click();
64+
65+
await page.getByRole("link", { name: "Continuar Compra" }).click();
66+
67+
const orderForm = {
68+
Nombre: "Testino",
69+
Apellido: "Diprueba",
70+
Compañia: "",
71+
Dirección: "Calle De Prueba 123",
72+
Ciudad: "Lima",
73+
"Provincia/Estado": "Lima",
74+
"Código Postal": "51111",
75+
Teléfono: "987456321",
76+
};
77+
78+
for (const [key, value] of Object.entries(orderForm)) {
79+
const input = await page.getByRole("textbox", { name: key });
80+
await input.click();
81+
await input.fill(value);
82+
}
83+
84+
await page.getByRole("combobox", { name: "País" }).selectOption("PE");
85+
86+
await page.getByRole("button", { name: "Confirmar Orden" }).click();
87+
88+
await expect(
89+
page.getByText("¡Muchas gracias por tu compra!")
90+
).toBeVisible();
91+
await expect(page.getByTestId("orderId")).toBeVisible();
92+
});
5793
});

0 commit comments

Comments
 (0)