-
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.
More additions to AuthenticationService + Unit Tests and Mock
- Loading branch information
1 parent
a155c6e
commit 919636e
Showing
9 changed files
with
137 additions
and
46 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
UserService/UserService.Application/Exceptions/InvalidLogin.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,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace UserService.Application.Exceptions | ||
{ | ||
|
||
public class InvalidLogin : Exception | ||
{ | ||
|
||
public InvalidLogin() : base("Incorrect Username or Password") { | ||
|
||
} | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using UserService.Domain.ValueObjects; | ||
|
||
namespace UserService.Application.Results | ||
{ | ||
public record UserResult | ||
{ | ||
public UserId UserId { get; init; } | ||
public Username Username { get; init; } | ||
public string Token { get; init; } | ||
|
||
|
||
public UserResult(UserId userId, Username username, string token) { | ||
Check warning on line 16 in UserService/UserService.Application/Results/UserResult.cs GitHub Actions / run-service-unit-tests (UserService)
Check warning on line 16 in UserService/UserService.Application/Results/UserResult.cs GitHub Actions / run-service-unit-tests (UserService)
|
||
this.UserId = userId; | ||
this.Token = token; | ||
} | ||
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
UserService/UserService.Application/Services/AuthenticationService.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,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using UserService.Application.Exceptions; | ||
using UserService.Application.Interfaces; | ||
using UserService.Application.Interfaces.Persistence; | ||
using UserService.Application.Results; | ||
using UserService.Domain.Entities; | ||
|
||
namespace UserService.Application.Services | ||
{ | ||
public class AuthenticationService : IAuthenticationService | ||
{ | ||
|
||
private readonly IUserRepository _userRepository; | ||
|
||
public AuthenticationService(IUserRepository userRepository) | ||
{ | ||
this._userRepository = userRepository; | ||
} | ||
|
||
public UserResult Register(string username, string password) | ||
{ | ||
|
||
var user = this._userRepository.CreateUser(username, password); | ||
|
||
return new UserResult(user.Id, user.Username, "token"); | ||
|
||
} | ||
|
||
public UserResult Login(string username, string password) | ||
{ | ||
|
||
var user = this._userRepository.GetUser(username) ?? throw new InvalidLogin(); | ||
|
||
if (user.Password.Value == password) { | ||
return new UserResult(user.Id, user.Username, "token"); | ||
} | ||
|
||
throw new InvalidLogin(); | ||
|
||
} | ||
} | ||
} |
41 changes: 0 additions & 41 deletions
41
UserService/UserService.Application/Services/UserService.cs
This file was deleted.
Oops, something went wrong.
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 @@ | ||
{} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Castle.Core.Logging; | ||
using Moq; | ||
using UserService.Application.Interfaces.Persistence; | ||
using UserService.Application.Results; | ||
using UserService.Application.Services; | ||
using UserService.Domain.Entities; | ||
using UserService.Domain.ValueObjects; | ||
namespace UserService.UnitTests | ||
{ | ||
public class UserServiceTests | ||
{ | ||
private readonly IAuthenticationService _userService; | ||
private readonly Mock<IUserRepository> _userRepositoryMock = new Mock<IUserRepository>(); | ||
|
||
public UserServiceTests() { | ||
this._userService = new AuthenticationService(_userRepositoryMock.Object); | ||
} | ||
|
||
[Fact] | ||
public void Login_ShouldReturnUser_IfUserExistsAndCorrectPassword() | ||
{ | ||
|
||
var user = new User{ | ||
Username = new Username("Test1234567"), | ||
Password = new Password("Test1234567") | ||
}; | ||
|
||
|
||
this._userRepositoryMock.Setup(x => x.GetUser(user.Username.Value)).Returns(user); | ||
|
||
var loggedInUser = this._userService.Login(user.Username.Value, user.Password.Value); | ||
|
||
Assert.Equal(loggedInUser.UserId.Value, user.Id.Value); | ||
|
||
|
||
|
||
|
||
} | ||
} | ||
} |