Skip to content

Commit

Permalink
Fix project to build
Browse files Browse the repository at this point in the history
  • Loading branch information
vanderlan committed Oct 29, 2023
1 parent 2c0b106 commit 6d3ff86
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 187 deletions.
4 changes: 2 additions & 2 deletions src/Orion.Api/Controllers/V1/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ namespace Orion.Api.Controllers.V1;
[AllowAnonymous]
public class AuthController : ApiController
{
private readonly IUserService _userService;
private readonly IConfiguration _configuration;
private readonly IUserService _userService;

public AuthController(IUserService userService, IMediator mediator, IConfiguration configuration) : base(mediator)
{
_userService = userService;
_configuration = configuration;
_userService = userService;
}

[HttpPost("Login")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MediatR;
using Orion.Domain.Core.Entities;
using Orion.Domain.Core.Entities.Enuns;

namespace Orion.Application.Core.Commands.UserCreate;

Expand All @@ -8,6 +9,7 @@ public class UserCreateRequest : IRequest <UserCreateResponse>
public string Name { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public UserProfile Profile { get; set; }

public static implicit operator User(UserCreateRequest request)
{
Expand All @@ -18,7 +20,8 @@ public static implicit operator User(UserCreateRequest request)
{
Name = request.Name,
Password = request.Password,
Email = request.Email
Email = request.Email,
Profile = request.Profile
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ public class UserGetByIdResponse
{
public string PublicId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public DateTime LastUpdated { get; set; }
public DateTime CreatedAt { get; set; }

public static explicit operator UserGetByIdResponse(User customer)
public static explicit operator UserGetByIdResponse(User user)
{
if (customer is null)
if (user is null)
return default;

return new UserGetByIdResponse
{
PublicId = customer.PublicId,
Name = customer.Name,
LastUpdated = customer.LastUpdated,
CreatedAt = customer.CreatedAt
PublicId = user.PublicId,
Name = user.Name,
Email = user.Email,
LastUpdated = user.LastUpdated,
CreatedAt = user.CreatedAt
};
}

}
23 changes: 13 additions & 10 deletions tests/Orion.Test/Api/Controllers/AuthControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
using Xunit;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using MediatR;
using Orion.Test.Api.Controllers.BaseController;
using Orion.Domain.Core.Entities;
using Orion.Api.Controllers.V1;
using Orion.Api.Configuration;
using Orion.Api.AutoMapper.Output;
using Orion.Application.Core.Commands.LoginWithCredentials;
using Orion.Application.Core.Commands.LoginWithRefreshToken;
using Orion.Test.Faker;

namespace Orion.Test.Api.Controllers;

public class AuthControllerTest : BaseControllerTest
{
private AuthController _authController;
private IConfiguration configuration;
private IConfiguration _configuration;
private readonly User _validUser = UserFaker.Get();
private readonly RefreshToken _validRefreshToken;

Expand All @@ -34,7 +36,7 @@ public async Task Login_WithValidCredentials_ReturnsOk()
{
//arrange & act
var result = await _authController.Login(
new UserLoginModel
new LoginWithCredentialsRequest
{
Email = _validUser.Email,
Password = _validUser.Password
Expand All @@ -58,7 +60,7 @@ public async Task Login_WithInvalidCredentials_RetunsUnauthorized()
{
//arrange & act
var result = await _authController.Login(
new UserLoginModel
new LoginWithCredentialsRequest
{
Email = "invalid-login",
Password = "invalid-pass"
Expand All @@ -76,9 +78,9 @@ public async Task Login_WithInvalidCredentials_RetunsUnauthorized()
public async Task RefreshToken_WithValidRefreshToken_ReturnsNewToken()
{
//arrange & act
var (Token, _) = AuthenticationConfiguration.CreateToken(new UserOutput { Email = _validUser.Email, Name = _validUser.Name, PublicId = _validUser.PublicId }, configuration);
var (token, _) = AuthenticationConfiguration.CreateToken(new LoginWithCredentialsResponse { Email = _validUser.Email, Name = _validUser.Name, PublicId = _validUser.PublicId }, _configuration);

var result = await _authController.RefreshToken(new RefreshTokenModel { RefreshToken = _validRefreshToken.Refreshtoken, Token = Token});
var result = await _authController.RefreshToken(new LoginWithRefreshTokenRequest { RefreshToken = _validRefreshToken.Refreshtoken, Token = token});

var contentResult = (OkObjectResult)result;
var userApiToken = (UserApiTokenModel)contentResult.Value;
Expand All @@ -97,7 +99,7 @@ public async Task RefreshToken_WithInvalidRefreshToken_ReturnsUnauthorized()
{
//arrange & act
var result = await _authController.RefreshToken(
new RefreshTokenModel { RefreshToken = null }
new LoginWithRefreshTokenRequest { RefreshToken = null }
);

var contentResult = (UnauthorizedResult)result;
Expand All @@ -109,11 +111,12 @@ public async Task RefreshToken_WithInvalidRefreshToken_ReturnsUnauthorized()

private void SetupServiceMock()
{
var mediatotMock = new Mock<IMediator>();
var userServiceMock = new Mock<IUserService>();

userServiceMock.Setup(x => x.LoginAsync(_validUser.Email, _validUser.Password))
.ReturnsAsync(_validUser);

userServiceMock.Setup(x => x.AddRefreshTokenAsync(It.IsAny<RefreshToken>())).ReturnsAsync(RefreshTokenFaker.Get());
userServiceMock.Setup(x => x.SignInWithRehreshTokenAsync(_validRefreshToken.Refreshtoken, It.IsAny<string>())).ReturnsAsync(_validUser);

Expand All @@ -124,10 +127,10 @@ private void SetupServiceMock()
{"JwtOptions:TokenExpirationMinutes", "15"}
};

configuration = new ConfigurationBuilder()
_configuration = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
.Build();

_authController = new AuthController(userServiceMock.Object, Mapper, configuration);
_authController = new AuthController(userServiceMock.Object,mediatotMock.Object, _configuration);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
using AutoMapper;
using Moq;
using Orion.Api.AutoMapper.Config;

namespace Orion.Test.Api.Controllers.BaseController;

public class BaseControllerTest
{
protected readonly IMapper Mapper;

public BaseControllerTest()
{
var mockMapper = new Mock<IMapper>();

var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new InputToDomainProfile());
mc.AddProfile(new DomainToOutputProfile());
});

Mapper = mappingConfig.CreateMapper();

}
}
122 changes: 0 additions & 122 deletions tests/Orion.Test/Api/Controllers/CustomersControllerTest.cs

This file was deleted.

Loading

0 comments on commit 6d3ff86

Please sign in to comment.