Skip to content

Commit

Permalink
Merge pull request #124 from Arquisoft/develop
Browse files Browse the repository at this point in the history
Coverage a master
  • Loading branch information
iyanfdezz authored Apr 22, 2024
2 parents a85fd14 + c3d1830 commit 80f82ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ 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({
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 });
Expand Down
17 changes: 16 additions & 1 deletion users/userservice/user-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ let app;

const username = "testuser";
const friendUsername = "testfriend";
const password = "testpassword";
const password = "Testpassword1";
const badPassword = "pass";

beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 80f82ef

Please sign in to comment.