From f2c0c5085cea5cd017de8fc9dd41bc657f05072b Mon Sep 17 00:00:00 2001 From: Jared Lucas Amistad Schulz Date: Sun, 17 Nov 2024 23:57:50 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AAwrote=20more=20tests=20for=20the=20?= =?UTF-8?q?login=20function=20and=20added=20test.env=20variables=20to=20th?= =?UTF-8?q?e=20readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 + backend/tests/login.test.ts | 73 ++++++++++++++++++++++++---------- backend/tests/register.test.ts | 18 +++++++++ 3 files changed, 72 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 3c3e7a3..612ab37 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ To run tests locally, follow the steps below. DATABASE_URL="postgres://postgres:postgres@localhost:5432" DIRECT_URL="postgres://postgres:postgres@localhost:5432" NODE_ENV=test +REDIS_PORT=6380 +SESSION_SECRET=notsecret ``` 2. Ensure you have docker installed and make sure you have the docker engine running. diff --git a/backend/tests/login.test.ts b/backend/tests/login.test.ts index e2ef45b..133c5a5 100644 --- a/backend/tests/login.test.ts +++ b/backend/tests/login.test.ts @@ -45,27 +45,58 @@ describe("Tests", () => { test("Unauthorized User", async () => { const { status, body } = await request(app).post("/auth/register").send({ - username: "shinjisatoo", - password: "testpassword", - email: "longseason1997@gmail.com", - userType: "ATTENDEE", - }); - const users = await prisma.user.findMany({}) - console.log(users) - const newUser = await prisma.user.findFirst({ - where: { - id: body.newUser.id, - }, + username: "shinjisatoo", + password: "testpassword", + email: "longseason1997@gmail.com", + userType: "ATTENDEE", + }); + + const newUser = await prisma.user.findFirst({ + where: { + id: body.newUser.id, + }, + }); + if (newUser == null) return; + expect(status).toBe(201); + expect(newUser).not.toBeNull(); + + const response2 = await request(app) + .get("/user") + .send({ + userId: newUser.id, }); - if (newUser == null) return; - expect(status).toBe(201); - expect(newUser).not.toBeNull(); - - const response2 = await request(app) - .get("/user") - .send({ - userId: newUser.id, - }); - expect(response2.status).toBe(401); + expect(response2.status).toBe(401); + }) + + test("Wrong password", async () => { + const { status, body } = await request(app).post("/auth/register").send({ + username: "shinjisatoo", + password: "testpassword", + email: "longseason1997@gmail.com", + userType: "ATTENDEE", + }); + + const response = await request(app).post("/auth/login").send({ + username: body.newUser.username, + password: "wrongpassword", + }); + + expect(response.status).toBe(400); + }) + + test("Nonexistent Username", async () => { + const { status, body } = await request(app).post("/auth/register").send({ + username: "shinjisatoo", + password: "testpassword", + email: "longseason1997@gmail.com", + userType: "ATTENDEE", + }); + + const response = await request(app).post("/auth/login").send({ + username: "Idon'texistlol", + password: "testpassword", + }); + + expect(response.status).toBe(400); }) }); diff --git a/backend/tests/register.test.ts b/backend/tests/register.test.ts index 12e81a8..11543fa 100644 --- a/backend/tests/register.test.ts +++ b/backend/tests/register.test.ts @@ -53,4 +53,22 @@ describe("Tests", () => { expect(status).toBe(400); }) + + test("Username already exists", async () => { + await request(app).post("/auth/register").send({ + username: "shinjisatoo", + password: "testpassword", + email: "longseason1996@gmail.com", + userType: "ATTENDEE", + }); + + const { status, body } = await request(app).post("/auth/register").send({ + username: "shinjisatoo", + password: "testpassword2", + email: "longseason1997@gmail.com", + userType: "ATTENDEE", + }); + + expect(status).toBe(400); + }) });