From c3d1830debb0987ccdf816534695f61b22c49d14 Mon Sep 17 00:00:00 2001 From: iyanfdezz Date: Thu, 18 Apr 2024 18:47:52 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adido=20test=20de=20fallo=20por=20valida?= =?UTF-8?q?ci=C3=B3n=20de=20contrase=C3=B1a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- users/userservice/user-service.js | 3 ++- users/userservice/user-service.test.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index d0a74f35..35c97942 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -54,7 +54,8 @@ app.post("/adduser", async (req, res) => { validateRequiredFields(req, ["username", "password"]); const username = req.body.username; - + const password= req.body.password; + const passwordRegex = /^(?=.*[A-Z])(?=.*\d).{8,}$/; if (!passwordRegex.test(password)) { return res.status(401).json({ diff --git a/users/userservice/user-service.test.js b/users/userservice/user-service.test.js index bdbe9cd3..f9bfa7ef 100644 --- a/users/userservice/user-service.test.js +++ b/users/userservice/user-service.test.js @@ -8,6 +8,7 @@ let app; const username = "testuser"; const friendUsername = "testfriend"; const password = "Testpassword1"; +const badPassword = "pass"; beforeAll(async () => { mongoServer = await MongoMemoryServer.create(); @@ -45,6 +46,20 @@ describe("User Service", () => { }); }); + + it("should send password validation error on POST /adduser", async () => { + const newUser = { + username: "testuser", + password: badPassword, + }; + + const response = await request(app).post("/adduser").send(newUser); + expect(response.status).toBe(401); + expect(response.body).toEqual({ + error: "Password must be at least 8 characters long, contain at least one uppercase letter, and at least one number.", + }); + }); + it("should add a new user on POST /adduser", async () => { const newUser = { username: "testuser",