-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from vicheanath/git-repo
Add Authentication UnitTest
- Loading branch information
Showing
5 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
SearchBugs.Application.UnitTests/AuthenicationsTest/LoginCommandHandlerTest.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,87 @@ | ||
using FluentAssertions; | ||
using Moq; | ||
using SearchBugs.Application.Authentications.Login; | ||
using SearchBugs.Domain.Services; | ||
using SearchBugs.Domain.Users; | ||
using Shared.Results; | ||
|
||
namespace SearchBugs.Application.UnitTests.AuthenicationsTest; | ||
|
||
public class LoginCommandHandlerTest | ||
{ | ||
private readonly Mock<IUserRepository> _userRepository; | ||
private readonly Mock<IJwtProvider> _jwtProvider; | ||
private readonly Mock<IPasswordHashingService> _hasher; | ||
private readonly LoginCommandHandler _sut; | ||
|
||
public LoginCommandHandlerTest() | ||
{ | ||
_userRepository = new(); | ||
_jwtProvider = new(); | ||
_hasher = new(); | ||
_sut = new LoginCommandHandler(_userRepository.Object, _jwtProvider.Object, _hasher.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_WhenUserNotFound_ShouldReturnFailure_WithNotFoundByEmailError() | ||
{ | ||
// Arrange | ||
var email = "[email protected]"; | ||
var password = "password"; | ||
var command = new LoginCommand(email, password); | ||
_userRepository.Setup(x => x.GetUserByEmailAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(Result.Failure<User>(UserErrors.NotFoundByEmail(email))); | ||
|
||
// Act | ||
var result = await _sut.Handle(command, CancellationToken.None); | ||
|
||
// Assert | ||
result.IsSuccess.Should().BeFalse(); | ||
result.Error.Should().Be(UserErrors.NotFoundByEmail(email)); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_WhenPasswordIsInvalid_ShouldReturnFailure_WithInvalidPasswordError() | ||
{ | ||
// Arrange | ||
var email = "[email protected]"; | ||
var password = "password"; | ||
var command = new LoginCommand(email, password); | ||
_userRepository.Setup(x => x.GetUserByEmailAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(Result.Success(User.Create(Name.Create("First", "Last"), Email.Create(email), "hashedPassword").Value)); | ||
|
||
_hasher.Setup(x => x.VerifyPassword(It.IsAny<string>(), It.IsAny<string>())) | ||
.Returns(false); | ||
|
||
// Act | ||
var result = await _sut.Handle(command, CancellationToken.None); | ||
result.IsSuccess.Should().BeFalse(); | ||
result.Error.Should().Be(UserErrors.InvalidPassword); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_WhenUserFound_ShouldReturnSuccess_WithJwtToken() | ||
{ | ||
// Arrange | ||
var email = "[email protected]"; | ||
var password = "password"; | ||
var command = new LoginCommand(email, password); | ||
var user = User.Create(Name.Create("First", "Last"), Email.Create(email), "hashedPassword").Value; | ||
_userRepository.Setup(x => x.GetUserByEmailAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(Result.Success(user)); | ||
|
||
_hasher.Setup(x => x.VerifyPassword(It.IsAny<string>(), It.IsAny<string>())) | ||
.Returns(true); | ||
|
||
_jwtProvider.Setup(x => x.GenerateJwtToken(It.IsAny<User>())) | ||
.Returns("jwtToken"); | ||
|
||
// Act | ||
var result = await _sut.Handle(command, CancellationToken.None); | ||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Token.Should().Be("jwtToken"); | ||
|
||
} | ||
|
||
} | ||
|
73 changes: 73 additions & 0 deletions
73
SearchBugs.Application.UnitTests/AuthenicationsTest/RegisterCommandHandlerTest.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,73 @@ | ||
using FluentAssertions; | ||
using Moq; | ||
using SearchBugs.Application.Authentications; | ||
using SearchBugs.Application.Authentications.Register; | ||
using SearchBugs.Domain; | ||
using SearchBugs.Domain.Roles; | ||
using SearchBugs.Domain.Services; | ||
using SearchBugs.Domain.Users; | ||
using Shared.Results; | ||
|
||
namespace SearchBugs.Application.UnitTests.AuthenicationsTest; | ||
|
||
public class RegisterCommandHandlerTest | ||
{ | ||
private readonly Mock<IUserRepository> _userRepository; | ||
private readonly Mock<IPasswordHashingService> _hasher; | ||
private readonly Mock<IUnitOfWork> _unitOfWork; | ||
private readonly RegisterCommandHandler _sut; | ||
|
||
public RegisterCommandHandlerTest() | ||
{ | ||
_userRepository = new(); | ||
_hasher = new(); | ||
_unitOfWork = new(); | ||
_sut = new RegisterCommandHandler(_userRepository.Object, _hasher.Object, _unitOfWork.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_WhenEmailIsNotUnique_ShouldReturnFailure_WithDuplicateEmailError() | ||
{ | ||
// Arrange | ||
var email = "[email protected]"; | ||
var password = "password"; | ||
|
||
var command = new RegisterCommand(email, password, "First", "Last"); | ||
|
||
_userRepository.Setup(x => x.IsEmailUniqueAsync(Email.Create(email), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(Result.Failure<User>(AuthValidationErrors.EmailAlreadyExists)); | ||
|
||
// Act | ||
var result = await _sut.Handle(command, CancellationToken.None); | ||
|
||
// Assert | ||
result.IsSuccess.Should().BeFalse(); | ||
result.Error.Should().Be(AuthValidationErrors.EmailAlreadyExists); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_WhenEmailIsUnique_ShouldReturnSuccess() | ||
{ | ||
// Arrange | ||
var email = "[email protected]"; | ||
var password = "password"; | ||
|
||
var command = new RegisterCommand(email, password, "First", "Last"); | ||
|
||
_userRepository.Setup(x => x.IsEmailUniqueAsync(Email.Create(email), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(Result.Success<User>(User.Create(Name.Create("First", "Last"), Email.Create(email), "hashedPassword").Value)); | ||
|
||
|
||
_userRepository.Setup(x => x.GetRoleByIdAsync(Role.Guest.Id, It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(Result.Success<Role>(Role.Guest)); | ||
|
||
// Act | ||
var result = await _sut.Handle(command, CancellationToken.None); | ||
|
||
// Assert | ||
result.IsSuccess.Should().BeTrue(); | ||
_userRepository.Verify(x => x.Add(It.IsAny<User>()), Times.Once); | ||
_unitOfWork.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once); | ||
_userRepository.Verify(x => x.GetRoleByIdAsync(Role.Guest.Id, It.IsAny<CancellationToken>()), Times.Once); | ||
} | ||
} |
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