Skip to content

Commit

Permalink
refactor: disallow registering user when phone is already used
Browse files Browse the repository at this point in the history
  • Loading branch information
raczu committed Jun 5, 2024
1 parent e1a355a commit c761b8e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public void Setup()
{
Email = "[email protected]",
Username = "jsnow",
Password = _hasher.HashPassword(null!, "password")
Password = _hasher.HashPassword(null!, "password"),
Phone = "+123 456789"
};

_mockContext.Setup(c => c.Users)
Expand Down Expand Up @@ -96,6 +97,19 @@ public void Register_WhenUserWithUsernameAlreadyExists_ShouldThrowBadRequestExce
Assert.ThrowsException<BadRequestException>(() => _service.Register(request));
}

[TestMethod]
public void Register_WhenUserWithPhoneAlreadyExists_ShouldThrowBadRequestException()
{
var request = new RegisterRequest
{
Email = "[email protected]",
Username = "jstark",
Phone = "+123 456789"
};

Assert.ThrowsException<BadRequestException>(() => _service.Register(request));
}

[TestMethod]
public void Register_WhenUserDoesNotExist_ShouldReturnRegisteredUser()
{
Expand All @@ -106,7 +120,7 @@ public void Register_WhenUserDoesNotExist_ShouldReturnRegisteredUser()
Email = "[email protected]",
Username = "jstark",
Password = "S3cureP@ssword!",
Phone = "+123 456789",
Phone = "+123 456781",
Address = new AddressDto
{
Street = "The Wall",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ public User Register(RegisterRequest request)
{
var userAlreadyExists = _context.Users.Any(u =>
u.Email.ToUpper() == request.Email.ToUpper() ||
u.Username.ToUpper() == request.Username.ToUpper());
u.Username.ToUpper() == request.Username.ToUpper() ||
u.Phone == request.Phone);

if (userAlreadyExists)
{
throw new BadRequestException(
"User with provided email or username already exists");
"User with provided email, username or phone already exists");
}

var user = new User
Expand Down

0 comments on commit c761b8e

Please sign in to comment.