Skip to content

Commit

Permalink
More additions to AuthenticationService + Unit Tests and Mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddie4k-code committed Nov 16, 2024
1 parent a155c6e commit 919636e
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 46 deletions.
17 changes: 17 additions & 0 deletions UserService/UserService.Application/Exceptions/InvalidLogin.cs
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") {

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UserService.Application.Results;
using UserService.Domain.Entities;

namespace UserService.Application.Interfaces.Persistence
{
public interface IUserService
public interface IAuthenticationService
{

void Register(string username, string password);
UserResult Register(string username, string password);

User Login(string username, string password);
UserResult Login(string username, string password);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace UserService.Application.Interfaces.Persistence
public interface IUserRepository
{

void CreateUser(string email, string password);
public User CreateUser(string email, string password);

User GetUser(string email);
public User GetUser(string email);

}
}
22 changes: 22 additions & 0 deletions UserService/UserService.Application/Results/UserResult.cs
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

View workflow job for this annotation

GitHub Actions / run-service-unit-tests (UserService)

Non-nullable property 'Username' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 16 in UserService/UserService.Application/Results/UserResult.cs

View workflow job for this annotation

GitHub Actions / run-service-unit-tests (UserService)

Non-nullable property 'Username' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
this.UserId = userId;
this.Token = token;
}

}
}
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 UserService/UserService.Application/Services/UserService.cs

This file was deleted.

1 change: 1 addition & 0 deletions UserService/UserService.UnitTests/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
Expand All @@ -23,6 +24,7 @@
<ItemGroup>
<ProjectReference Include="../../UserService.Domain/UserService.Domain.csproj" />
<ProjectReference Include="..\UserService.Domain\UserService.Domain.csproj" />
<ProjectReference Include="..\UserService.Application\UserService.Application.csproj" />
</ItemGroup>

</Project>
44 changes: 44 additions & 0 deletions UserService/UserService.UnitTests/UserServiceTests.cs
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);




}
}
}

0 comments on commit 919636e

Please sign in to comment.