From 3106e46f85536db7a2615c845a24bbfbd35afcbb Mon Sep 17 00:00:00 2001 From: zkldi Date: Thu, 27 May 2021 21:04:18 +0100 Subject: [PATCH] finish tests for login --- .../server/router/api/v1/auth/router.test.ts | 82 ++++++++++++++++++- .../src/server/router/api/v1/auth/router.ts | 4 +- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/server/src/server/router/api/v1/auth/router.test.ts b/server/src/server/router/api/v1/auth/router.test.ts index 37eab18a9..30ad1a0a6 100644 --- a/server/src/server/router/api/v1/auth/router.test.ts +++ b/server/src/server/router/api/v1/auth/router.test.ts @@ -60,13 +60,93 @@ t.test("POST /api/v1/auth/login", (t) => { const cookie = res.headers["set-cookie"]; - const stat = await mockApi.post("/api/v1/auth/stat").set("Cookie", cookie); + const stat = await mockApi.post("/api/v1/auth/status").set("Cookie", cookie); t.equal(stat.status, 200); t.end(); }); + t.test("Should return 409 if user already logged in", async (t) => { + const res = await mockApi.post("/api/v1/auth/login").send({ + username: "test_zkldi", + password: "password", + captcha: "foo", + }); + + const cookie = res.headers["set-cookie"]; + + const res2 = await mockApi + .post("/api/v1/auth/login") + .send({ + username: "test_zkldi", + password: "password", + captcha: "foo", + }) + .set("Cookie", cookie); + + t.equal(res2.status, 409); + + t.end(); + }); + + t.test("Should return 401 if password invalid", async (t) => { + const res = await mockApi.post("/api/v1/auth/login").send({ + username: "test_zkldi", + password: "invalid_password", + captcha: "foo", + }); + + t.equal(res.status, 401); + + t.end(); + }); + + t.test("Should return 404 if user invalid", async (t) => { + const res = await mockApi.post("/api/v1/auth/login").send({ + username: "invalid_user", + password: "password", + captcha: "foo", + }); + + t.equal(res.status, 404); + + t.end(); + }); + + t.test("Should return 400 if no password", async (t) => { + const res = await mockApi.post("/api/v1/auth/login").send({ + username: "invalid_user", + captcha: "foo", + }); + + t.equal(res.status, 400); + + t.end(); + }); + + t.test("Should return 400 if no username", async (t) => { + const res = await mockApi.post("/api/v1/auth/login").send({ + password: "password", + captcha: "foo", + }); + + t.equal(res.status, 400); + + t.end(); + }); + + t.test("Should return 400 if no captcha", async (t) => { + const res = await mockApi.post("/api/v1/auth/login").send({ + password: "password", + username: "test_zkldi", + }); + + t.equal(res.status, 400); + + t.end(); + }); + t.end(); }); diff --git a/server/src/server/router/api/v1/auth/router.ts b/server/src/server/router/api/v1/auth/router.ts index 18aa62873..827f5d18c 100644 --- a/server/src/server/router/api/v1/auth/router.ts +++ b/server/src/server/router/api/v1/auth/router.ts @@ -91,7 +91,7 @@ router.post( if (!requestedUser) { logger.verbose(`Invalid username for login ${req.body.username}.`); - return res.status(400).json({ + return res.status(404).json({ success: false, description: `This user does not exist.`, }); @@ -101,7 +101,7 @@ router.post( if (!passwordMatch) { logger.verbose("Invalid password provided."); - return res.status(400).json({ + return res.status(401).json({ success: false, description: `Invalid password.`, });