From 72090f53698de104edb132881be62691543cbe7c Mon Sep 17 00:00:00 2001 From: iyanfdezz Date: Thu, 18 Apr 2024 18:35:21 +0200 Subject: [PATCH 1/3] =?UTF-8?q?A=C3=B1adida=20validaci=C3=B3n=20de=20contr?= =?UTF-8?q?ase=C3=B1a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- users/userservice/user-service.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index 43362268..d0a74f35 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -55,6 +55,13 @@ app.post("/adduser", async (req, res) => { const username = req.body.username; + const passwordRegex = /^(?=.*[A-Z])(?=.*\d).{8,}$/; + if (!passwordRegex.test(password)) { + return res.status(401).json({ + error: "Password must be at least 8 characters long, contain at least one uppercase letter, and at least one number.", + }); + } + // Check if the username already exists const existingUser = await User.findOne({ username: username }); if (existingUser) { From a2fce22dfada50d57d104cd32e1433ff71758ae3 Mon Sep 17 00:00:00 2001 From: iyanfdezz Date: Thu, 18 Apr 2024 18:40:18 +0200 Subject: [PATCH 2/3] Fix en los test --- users/userservice/user-service.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/users/userservice/user-service.test.js b/users/userservice/user-service.test.js index 62e1125c..bdbe9cd3 100644 --- a/users/userservice/user-service.test.js +++ b/users/userservice/user-service.test.js @@ -7,7 +7,7 @@ let app; const username = "testuser"; const friendUsername = "testfriend"; -const password = "testpassword"; +const password = "Testpassword1"; beforeAll(async () => { mongoServer = await MongoMemoryServer.create(); From c3d1830debb0987ccdf816534695f61b22c49d14 Mon Sep 17 00:00:00 2001 From: iyanfdezz Date: Thu, 18 Apr 2024 18:47:52 +0200 Subject: [PATCH 3/3] =?UTF-8?q?A=C3=B1adido=20test=20de=20fallo=20por=20va?= =?UTF-8?q?lidaci=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",