From c761b8e4958e6b3846297ed97bf522a5c4eb801c Mon Sep 17 00:00:00 2001 From: raczu Date: Wed, 5 Jun 2024 02:51:40 +0200 Subject: [PATCH] refactor: disallow registering user when phone is already used --- .../Authentication/AuthServiceTests.cs | 18 ++++++++++++++++-- .../Services/Authentication/AuthService.cs | 5 +++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/UnitTests/Services/Authentication/AuthServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/UnitTests/Services/Authentication/AuthServiceTests.cs index 78c1a0a1..e16da4bc 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/UnitTests/Services/Authentication/AuthServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/UnitTests/Services/Authentication/AuthServiceTests.cs @@ -28,7 +28,8 @@ public void Setup() { Email = "jon.snow@castleblack.com", Username = "jsnow", - Password = _hasher.HashPassword(null!, "password") + Password = _hasher.HashPassword(null!, "password"), + Phone = "+123 456789" }; _mockContext.Setup(c => c.Users) @@ -96,6 +97,19 @@ public void Register_WhenUserWithUsernameAlreadyExists_ShouldThrowBadRequestExce Assert.ThrowsException(() => _service.Register(request)); } + [TestMethod] + public void Register_WhenUserWithPhoneAlreadyExists_ShouldThrowBadRequestException() + { + var request = new RegisterRequest + { + Email = "jon.stark@castleblack.com", + Username = "jstark", + Phone = "+123 456789" + }; + + Assert.ThrowsException(() => _service.Register(request)); + } + [TestMethod] public void Register_WhenUserDoesNotExist_ShouldReturnRegisteredUser() { @@ -106,7 +120,7 @@ public void Register_WhenUserDoesNotExist_ShouldReturnRegisteredUser() Email = "jon.stark@castleblack.com", Username = "jstark", Password = "S3cureP@ssword!", - Phone = "+123 456789", + Phone = "+123 456781", Address = new AddressDto { Street = "The Wall", diff --git a/Server/ReasnAPI/ReasnAPI/Services/Authentication/AuthService.cs b/Server/ReasnAPI/ReasnAPI/Services/Authentication/AuthService.cs index 1d66a426..6b35076c 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/Authentication/AuthService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/Authentication/AuthService.cs @@ -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