This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
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.
FEAT: Updating endpoint and adding new unit tests
- Loading branch information
Showing
9 changed files
with
113 additions
and
13 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
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
4 changes: 1 addition & 3 deletions
4
...Keycloak.TokenManager/TokenManager.Application.Services/Requests/User/LoginUserRequest.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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace TokenManager.Application.Services.Requests.User | ||
namespace TokenManager.Application.Services.Requests.User | ||
{ | ||
public record LoginUserRequest(string Username, string Password); | ||
} |
4 changes: 4 additions & 0 deletions
4
...cloak.TokenManager/TokenManager.Application.Services/Requests/User/RefreshTokenRequest.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,4 @@ | ||
namespace TokenManager.Application.Services.Requests.User | ||
{ | ||
public record RefreshTokenRequest(string RefreshToken); | ||
} |
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
93 changes: 93 additions & 0 deletions
93
tests/TokenManager.UnitTests/Handlers/RefreshTokenCommandHandlerTests.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,93 @@ | ||
using AutoFixture; | ||
using FluentAssertions; | ||
using Moq; | ||
using TokenManager.Application.Services.Commands.Users; | ||
using TokenManager.Domain.Entities; | ||
using TokenManager.Domain.Interfaces; | ||
|
||
namespace TokenManager.UnitTests.Handlers | ||
{ | ||
public class RefreshTokenCommandHandlerTests | ||
{ | ||
private readonly Fixture _autoFixture = new(); | ||
private readonly Mock<IUserRepository> _userRepositoryMock = new(); | ||
private readonly RefreshTokenCommandHandler _refreshTokenCommandHandler; | ||
|
||
public RefreshTokenCommandHandlerTests() | ||
{ | ||
_refreshTokenCommandHandler = new(_userRepositoryMock.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleWhenInformAValidToken_ShouldBeRefreshedTheToken() | ||
{ | ||
// Arrange | ||
var refreshTokenCommand = _autoFixture.Create<RefreshTokenCommand>(); | ||
|
||
var tokenDetails = _autoFixture.Create<TokenDetails>(); | ||
var successResult = Result<TokenDetails>.Success(tokenDetails); | ||
|
||
_userRepositoryMock | ||
.Setup(x => x.RefreshTokenAsync(It.IsAny<string>(), It.IsAny<string>())) | ||
.ReturnsAsync(successResult); | ||
|
||
//Act | ||
var resultHandle = await _refreshTokenCommandHandler.Handle(refreshTokenCommand, CancellationToken.None); | ||
|
||
//Assert | ||
resultHandle.IsSuccess | ||
.Should() | ||
.Be(true); | ||
|
||
resultHandle.Result.AccessToken | ||
.Should() | ||
.Be(tokenDetails.Access_Token); | ||
|
||
resultHandle.Result.ExpiresIn | ||
.Should() | ||
.Be(tokenDetails.Expires_In); | ||
|
||
resultHandle.Result.RefreshExpiresIn | ||
.Should() | ||
.Be(tokenDetails.Refresh_Expires_In); | ||
|
||
resultHandle.Result.RefreshToken | ||
.Should() | ||
.Be(tokenDetails.Refresh_Token); | ||
|
||
resultHandle.Result.TokenType | ||
.Should() | ||
.Be(tokenDetails.Token_Type); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleWhenInformAInvalidUser_ShouldNotBeLoggedAndShouldReturnsAnError() | ||
{ | ||
// Arrange | ||
var refreshTokenCommand = _autoFixture.Create<RefreshTokenCommand>(); | ||
|
||
var error = _autoFixture.Create<Error>(); | ||
var failureResult = Result<TokenDetails>.Failure(error); | ||
|
||
_userRepositoryMock | ||
.Setup(x => x.RefreshTokenAsync(It.IsAny<string>(), It.IsAny<string>())) | ||
.ReturnsAsync(failureResult); | ||
|
||
//Act | ||
var resultHandle = await _refreshTokenCommandHandler.Handle(refreshTokenCommand, CancellationToken.None); | ||
|
||
//Assert | ||
resultHandle.IsSuccess | ||
.Should() | ||
.Be(false); | ||
|
||
resultHandle.Error.Description | ||
.Should() | ||
.Contain(error.Description); | ||
|
||
resultHandle.Error.Code | ||
.Should() | ||
.Contain(error.Code); | ||
} | ||
} | ||
} |