Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "-" in validation for generating username #1660

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion middlewares/validators/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const updateUser = async (req, res, next) => {
.optional()
.min(4)
.max(20)
.regex(/^[a-zA-Z0-9]+$/)
.regex(/^[a-zA-Z0-9-]+$/)
vinit717 marked this conversation as resolved.
Show resolved Hide resolved
.message("Username must be between 4 and 20 characters long and contain only letters or numbers."),
first_name: joi.string().optional(),
last_name: joi.string().optional(),
Expand Down
43 changes: 43 additions & 0 deletions test/unit/middlewares/user-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,49 @@ describe("Middleware | Validators | User", function () {
});
expect(nextSpy.calledOnce).to.be.equal(false);
});
it("Allows a valid username", async function () {
const req = {
body: {
username: "john-doe",
},
};

const res = {
boom: {
badRequest: (message) => {
throw new Error(message);
},
},
};

const next = sinon.spy();

await updateUser(req, res, next);

expect(next.calledOnce).to.be.equal(true);
});

it("Stops the propagation of next for an invalid username", async function () {
const req = {
body: {
username: "@john_doe",
},
};

const res = {
boom: {
badRequest: () => {},
},
};

const nextSpy = sinon.spy();

await updateUser(req, res, nextSpy).catch((err) => {
expect(err).to.be.an.instanceOf(Error);
});

expect(nextSpy.calledOnce).to.be.equal(false);
});
});

describe("Create user validator for getUsers", function () {
Expand Down
Loading