Skip to content

Commit

Permalink
➡️ merge pull request #22 from devsoc-unsw/feature/login
Browse files Browse the repository at this point in the history
🧪wrote more tests for the login function and added test.env variables
  • Loading branch information
lachlanshoesmith authored Nov 19, 2024
2 parents 5a97384 + f2c0c50 commit 97295c8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
73 changes: 52 additions & 21 deletions backend/tests/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
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: "[email protected]",
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: "[email protected]",
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: "[email protected]",
userType: "ATTENDEE",
});

const response = await request(app).post("/auth/login").send({
username: "Idon'texistlol",
password: "testpassword",
});

expect(response.status).toBe(400);
})
});
18 changes: 18 additions & 0 deletions backend/tests/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
userType: "ATTENDEE",
});

const { status, body } = await request(app).post("/auth/register").send({
username: "shinjisatoo",
password: "testpassword2",
email: "[email protected]",
userType: "ATTENDEE",
});

expect(status).toBe(400);
})
});

0 comments on commit 97295c8

Please sign in to comment.