Skip to content

Commit

Permalink
Merge pull request #232 from Arquisoft/username-spaces
Browse files Browse the repository at this point in the history
Added validations for username
  • Loading branch information
Raulms29 committed May 4, 2024
2 parents 6eed655 + ecbd906 commit 8afb373
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions users/userservice/src/controllers/user-controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from 'express';
import bcrypt from 'bcrypt';
import { validateProfileBody } from '../utils/profile-body-validation';
import { validateNoSpaces, validateMaxLength } from '../utils/username-validation';

const {
validateRequiredFields,
Expand All @@ -17,6 +18,8 @@ const addUser = async (req: Request, res: Response) => {
validateRequiredFields(req, ['username', 'password']);
validateNotEmpty(req, ['username']);
validateRequiredLength(req, ['password'], 8);
validateNoSpaces(req, ['username']);
validateMaxLength(req, ['username'], 20);

const username = req.body.username.toString();
const password = req.body.password.toString();
Expand Down
19 changes: 19 additions & 0 deletions users/userservice/src/utils/username-validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Request } from 'express';

function validateNoSpaces(req: Request, fieldsThatCannotHaveSpace: string[]) {
for (const field of fieldsThatCannotHaveSpace) {
if (req.body[field]!.indexOf(' ') >= 0) {
throw new Error(`The field "${field}" cannot have spaces`);
}
}
}

function validateMaxLength(req: Request, fields: string[], maxLength: number) {
for (const field of fields) {
if (req.body[field]!.length > maxLength) {
throw new Error(`The field "${field}" cannot have more than ${maxLength} characters`);
}
}
}

export { validateNoSpaces, validateMaxLength };
20 changes: 20 additions & 0 deletions users/userservice/test/user-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ describe('User Service', () => {
expect(response.status).toBe(400);
});

it('should get an error when registering a user with a blank space on POST /adduser', async () => {
const newUser = {
username: 'invaliduser01 ',
password: 'testpassword2',
};

const response = await request(app).post('/adduser').send(newUser);
expect(response.status).toBe(400);
});

it('should get an error when registering a user with more than 20 characters POST /adduser', async () => {
const newUser = {
username: 'thisusernamehasmorethan20characters',
password: 'testpassword2',
};

const response = await request(app).post('/adduser').send(newUser);
expect(response.status).toBe(400);
});

// GET /history error (no user)
it('should get an error in GET /history when not passing a user', async () => {
const response = await request(app)
Expand Down

0 comments on commit 8afb373

Please sign in to comment.