Skip to content

Commit f3639f7

Browse files
authored
Merge pull request #197 from codeableorg/feature/g1/e2e-sign-in
test: add end-to-end login test for visitor flow
2 parents 8fe8949 + 745e3ec commit f3639f7

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

src/db/migrations/initial.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ CREATE TABLE IF NOT EXISTS products (
3636
CREATE TABLE IF NOT EXISTS carts (
3737
id SERIAL PRIMARY KEY,
3838
session_cart_id UUID UNIQUE DEFAULT gen_random_uuid(),
39-
user_id INTEGER REFERENCES users(id),
39+
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
4040
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
4141
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
4242
);

src/e2e/demo.signin.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { test, expect } from "@playwright/test";
2+
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";
10+
11+
test.describe("Visitante inicio sesion", () => {
12+
let testUserId: number;
13+
14+
test.beforeAll(async () => {
15+
const testUser: CreateUserDTO = {
16+
17+
name: null,
18+
password: await hashPassword("letmein"),
19+
isGuest: false,
20+
};
21+
22+
const existingUser = await getUserByEmail(testUser.email);
23+
24+
if (existingUser) {
25+
await deleteUser(existingUser.id);
26+
}
27+
28+
const user = await createUser(testUser);
29+
testUserId = user.id;
30+
});
31+
32+
test.afterAll(async () => {
33+
await deleteUser(testUserId);
34+
});
35+
36+
test("test", async ({ page }) => {
37+
await page.goto("http://localhost:5173/");
38+
await page.getByTestId("login").click();
39+
await page.getByRole("textbox", { name: "Correo electrónico" }).click();
40+
await page
41+
.getByRole("textbox", { name: "Correo electrónico" })
42+
43+
await page
44+
.getByRole("textbox", { name: "Correo electrónico" })
45+
.press("Tab");
46+
await page.getByRole("textbox", { name: "Contraseña" }).fill("letmein");
47+
await page.getByRole("button", { name: "Iniciar sesión" }).click();
48+
49+
await expect(page.getByText("Bienvenido [email protected]")).toBeVisible();
50+
});
51+
});

src/routes/root/components/auth-nav/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default function AuthNav({ user }: { user?: Omit<User, "password"> }) {
2626
<Link
2727
to="/login"
2828
className="hover:underline hover:decoration-white hover:underline-offset-2"
29+
data-testid="login"
2930
>
3031
Iniciar sesión
3132
</Link>

src/session.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const { getSession, commitSession, destroySession } =
2727
path: "/",
2828
sameSite: "lax",
2929
secrets: ["s3cret1"],
30-
secure: true,
30+
secure: process.env.NODE_ENV === "production", // true en producción, false en desarrollo
3131
},
3232
});
3333

0 commit comments

Comments
 (0)