-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RSN-40] Add user authentication (#43)
* feat: add jwt and exceptions handlers Simplify the logic on the controllers' side, custom exceptions and a library for validation were added. The implemented handlers are allow to catch those exceptions and return the corresponding statuses and detailed response. Additionally, mappers have been created to more easily convert entities into the corresponding DTOs, fixed enum conversion when it comes to UserRole and export postgres port on the development environment. * test: add unit tests for jwt, handlers and validators * refactor: disallow registering user when phone is already used * feat: add global route prefix middleware * style: format code with dotnet format
- Loading branch information
Showing
12 changed files
with
59 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,14 +23,13 @@ public void Setup() | |
_mockContext = new Mock<ReasnContext>(); | ||
_hasher = new PasswordHasher<User>(); | ||
_service = new AuthService(_mockContext.Object); | ||
|
||
var user = new User | ||
{ | ||
Email = "[email protected]", | ||
Username = "jsnow", | ||
Password = _hasher.HashPassword(null!, "password") | ||
Password = _hasher.HashPassword(null!, "password"), | ||
Phone = "+123 456789" | ||
}; | ||
|
||
_mockContext.Setup(c => c.Users) | ||
.ReturnsDbSet(new List<User> { user }); | ||
} | ||
|
@@ -96,6 +95,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() | ||
{ | ||
|
@@ -106,7 +118,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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
Server/ReasnAPI/ReasnAPI/Middlewares/GlobalRoutePrefixMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace ReasnAPI.Middlewares; | ||
|
||
public class GlobalRoutePrefixMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly string _routePrefix; | ||
|
||
public GlobalRoutePrefixMiddleware(RequestDelegate next, string routePrefix) | ||
{ | ||
_next = next; | ||
_routePrefix = routePrefix; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
context.Request.PathBase = new PathString(_routePrefix); | ||
await _next(context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters