From 8d51c82f3551ebd8885e03b324a90a7a059a30d2 Mon Sep 17 00:00:00 2001 From: Vanderlan Gomes Date: Mon, 25 Sep 2023 21:02:33 -0300 Subject: [PATCH] Fix test naming and add AAA pattern (#17) * Fix test naming and add AAA pattern * Fix API Startup and Docker Compose --------- Co-authored-by: 83585-Vanderlan Silva --- .github/workflows/dotnet.yml | 4 +- .github/workflows/sonar.yml | 4 +- Dockerfile | 2 - Orion.Api/Controllers/CustomersController.cs | 2 +- Orion.Api/Controllers/UsersController.cs | 3 + Orion.Api/Program.cs | 1 + Orion.Api/appsettings.Development.json | 6 +- Orion.Api/appsettings.Production.json | 6 +- Orion.Api/appsettings.Test.json | 4 +- .../Generic/BaseEntityRepository.cs | 2 +- .../Generic/IBaseEntityRepository.cs | 2 +- .../Implementations/UserRepository.cs | 2 +- Orion.Domain/Extensions/StrignExtensions.cs | 4 +- .../Implementation/CustomerService.cs | 2 +- Orion.Domain/Implementation/UserService.cs | 12 +- Orion.Domain/Interfaces/ICustomerService.cs | 2 +- .../Base/IBaseEntityRepository.cs | 2 +- Orion.Domain/Repositories/IUserRepository.cs | 2 +- Orion.Entities/Enuns/UserProfile.cs | 9 +- Orion.Test/API/HealthCheckApiTest.cs | 5 +- Orion.Test/Controllers/AuthControllerTest.cs | 10 +- .../Controllers/CustomersControllerTest.cs | 19 +- Orion.Test/Controllers/UsersControllerTest.cs | 21 +- Orion.Test/Services/CustomerServiceTest.cs | 73 +++-- Orion.Test/Services/UserServiceTest.cs | 88 +++--- README.md | 2 +- VBaseProject.Test/Services/UserServiceTest.cs | 265 ------------------ 27 files changed, 173 insertions(+), 381 deletions(-) delete mode 100644 VBaseProject.Test/Services/UserServiceTest.cs diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index e2a6151..dc2a354 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -24,12 +24,12 @@ jobs: run: dotnet test --no-build --verbosity normal - name: Code Coverage run: | - cd ./VBaseProject.Test/ + cd ./Orion.Test/ dotnet add package coverlet.msbuild dotnet test /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov - name: Publish coverage report to coveralls.io uses: coverallsapp/github-action@v1.0.1 with: github-token: ${{ secrets.GITHUB_TOKEN }} - path-to-lcov: ./VBaseProject.Test/TestResults/coverage.info + path-to-lcov: ./Orion.Test/TestResults/coverage.info \ No newline at end of file diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml index 7ef9327..80d8aa2 100644 --- a/.github/workflows/sonar.yml +++ b/.github/workflows/sonar.yml @@ -42,6 +42,6 @@ jobs: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} shell: powershell run: | - .\.sonar\scanner\dotnet-sonarscanner begin /k:"vanderlan_VBaseProject-NetCoreAPI" /o:"vanderlan-gomes" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" - dotnet build VBaseProject.sln + .\.sonar\scanner\dotnet-sonarscanner begin /k:"vanderlan_Orion-Api" /o:"vanderlan-gomes" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" + dotnet build Orion.sln .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}" diff --git a/Dockerfile b/Dockerfile index 2afb590..bc6cd5c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,5 @@ FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build -EXPOSE 5000 - WORKDIR /app COPY . . diff --git a/Orion.Api/Controllers/CustomersController.cs b/Orion.Api/Controllers/CustomersController.cs index 614348f..f62a62b 100644 --- a/Orion.Api/Controllers/CustomersController.cs +++ b/Orion.Api/Controllers/CustomersController.cs @@ -30,7 +30,7 @@ public CustomersController(ICustomerService customerService, IMapper mapper) : b [ProducesResponseType((int)HttpStatusCode.OK)] public async Task Get([FromQuery] CustomerFilter filter) { - var customer = await _customerService.ListPaginate(filter); + var customer = await _customerService.ListPaginateAsync(filter); var customerOutputList = Mapper.Map>(customer); diff --git a/Orion.Api/Controllers/UsersController.cs b/Orion.Api/Controllers/UsersController.cs index 64c3736..d3ad246 100644 --- a/Orion.Api/Controllers/UsersController.cs +++ b/Orion.Api/Controllers/UsersController.cs @@ -45,6 +45,9 @@ public async Task Get(string id) var user = await _userService.FindByIdAsync(id); var userOutput = Mapper.Map(user); + if (userOutput is null) + return NotFound(); + return Ok(userOutput); } diff --git a/Orion.Api/Program.cs b/Orion.Api/Program.cs index 4d59f9e..761db5e 100644 --- a/Orion.Api/Program.cs +++ b/Orion.Api/Program.cs @@ -30,6 +30,7 @@ public static void Main(string[] args) private static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) + .UseUrls("http://*:5000") .UseStartup() .UseIISIntegration() .ConfigureLogging((context, logging) => diff --git a/Orion.Api/appsettings.Development.json b/Orion.Api/appsettings.Development.json index c695026..e1e8bb8 100644 --- a/Orion.Api/appsettings.Development.json +++ b/Orion.Api/appsettings.Development.json @@ -8,13 +8,13 @@ }, "DatabaseOptions": { - "ConnectionString": "Data Source=.\\SQLEXPRESS;Initial Catalog=Orion;Integrated Security=True;Trust Server Certificate=true;MultipleActiveResultSets=True;" + "ConnectionString": "Data Source=localhost,1433;Initial Catalog=Orion;User ID=sa;Password=123;TrustServerCertificate=True" }, "JwtConfiguration": { "SymmetricSecurityKey": "5cCI6IkpXVCJ9.eyJlbWFpbCI6InZhbmRlcmxhbi5nc0BnbWFpbC5jb20iLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJhZG1p", - "Issuer": "http://www.myapplication.com", - "Audience": "http://www.myapplication.com", + "Issuer": "Orion API", + "Audience": "http://www.orion-api.com", "TokenExpirationMinutes": 15 } } diff --git a/Orion.Api/appsettings.Production.json b/Orion.Api/appsettings.Production.json index 34d3119..c3046f1 100644 --- a/Orion.Api/appsettings.Production.json +++ b/Orion.Api/appsettings.Production.json @@ -8,13 +8,13 @@ }, "DatabaseOptions": { - "ConnectionString": "Data Source=.\\SQLEXPRESS;Initial Catalog=Orion;Integrated Security=True;Trust Server Certificate=true;MultipleActiveResultSets=True;" + "ConnectionString": "Data Source=10.0.0.90,1433;Initial Catalog=Orion;User ID=sa;Password=123;TrustServerCertificate=True" }, "JwtConfiguration": { "SymmetricSecurityKey": "5cCI6IkpXVCJ9.eyJlbWFpbCI6InZhbmRlcmxhbi5nc0BnbWFpbC5jb20iLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJhZG1p", - "Issuer": "http://www.myapplication.com", - "Audience": "http://www.myapplication.com", + "Issuer": "Orion API", + "Audience": "http://www.orion-api.com", "TokenExpirationMinutes": 15 } } diff --git a/Orion.Api/appsettings.Test.json b/Orion.Api/appsettings.Test.json index 34d3119..b5be8be 100644 --- a/Orion.Api/appsettings.Test.json +++ b/Orion.Api/appsettings.Test.json @@ -13,8 +13,8 @@ "JwtConfiguration": { "SymmetricSecurityKey": "5cCI6IkpXVCJ9.eyJlbWFpbCI6InZhbmRlcmxhbi5nc0BnbWFpbC5jb20iLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJhZG1p", - "Issuer": "http://www.myapplication.com", - "Audience": "http://www.myapplication.com", + "Issuer": "Orion API", + "Audience": "http://www.orion-api.com", "TokenExpirationMinutes": 15 } } diff --git a/Orion.Data/Repository/Generic/BaseEntityRepository.cs b/Orion.Data/Repository/Generic/BaseEntityRepository.cs index 519fa5b..0e97040 100644 --- a/Orion.Data/Repository/Generic/BaseEntityRepository.cs +++ b/Orion.Data/Repository/Generic/BaseEntityRepository.cs @@ -39,7 +39,7 @@ public virtual async Task FindByIdAsync(string publicId) return await DataContext.Set().FirstOrDefaultAsync(x => x.PublicId == publicId); } - public async Task> GetBy(Expression> predicate) + public async Task> GetByAsync(Expression> predicate) { return await DataContext.Set().Where(predicate).ToListAsync(); } diff --git a/Orion.Data/Repository/Generic/IBaseEntityRepository.cs b/Orion.Data/Repository/Generic/IBaseEntityRepository.cs index b583e8c..afe32a5 100644 --- a/Orion.Data/Repository/Generic/IBaseEntityRepository.cs +++ b/Orion.Data/Repository/Generic/IBaseEntityRepository.cs @@ -9,7 +9,7 @@ public interface IBaseEntityRepository where T : class { Task AddAsync(T entity); Task DeleteAsync(string publicId); - Task> GetBy(Expression> predicate); + Task> GetByAsync(Expression> predicate); Task FindByIdAsync(string publicId); void Update(T entity); } diff --git a/Orion.Data/Repository/Implementations/UserRepository.cs b/Orion.Data/Repository/Implementations/UserRepository.cs index 124e755..bb4c829 100644 --- a/Orion.Data/Repository/Implementations/UserRepository.cs +++ b/Orion.Data/Repository/Implementations/UserRepository.cs @@ -23,7 +23,7 @@ public async Task LoginAsync(string email, string password) return user ?? null; } - public async Task> ListPaginate(UserFilter filter) + public async Task> ListPaginateAsync(UserFilter filter) { var pagination = (filter.Page * filter.Quantity) - filter.Quantity; diff --git a/Orion.Domain/Extensions/StrignExtensions.cs b/Orion.Domain/Extensions/StrignExtensions.cs index 20153fa..08e7a75 100644 --- a/Orion.Domain/Extensions/StrignExtensions.cs +++ b/Orion.Domain/Extensions/StrignExtensions.cs @@ -7,10 +7,8 @@ public static class StrignExtensions { public static string ToSha512(this string text) { - using SHA512 hashAlgorithm = SHA512.Create(); - var byteValue = Encoding.UTF8.GetBytes(text); - var byteHash = hashAlgorithm.ComputeHash(byteValue); + var byteHash = SHA512.HashData(byteValue); StringBuilder hex = new(); diff --git a/Orion.Domain/Implementation/CustomerService.cs b/Orion.Domain/Implementation/CustomerService.cs index a55dc82..f9c1cdb 100644 --- a/Orion.Domain/Implementation/CustomerService.cs +++ b/Orion.Domain/Implementation/CustomerService.cs @@ -43,7 +43,7 @@ public async Task FindByIdAsync(string publicId) return await _unitOfWork.CustomerRepository.FindByIdAsync(publicId); } - public async Task> ListPaginate(CustomerFilter filter) + public async Task> ListPaginateAsync(CustomerFilter filter) { return await _unitOfWork.CustomerRepository.ListPaginate(filter); } diff --git a/Orion.Domain/Implementation/UserService.cs b/Orion.Domain/Implementation/UserService.cs index 1d9afa3..e3a7a61 100644 --- a/Orion.Domain/Implementation/UserService.cs +++ b/Orion.Domain/Implementation/UserService.cs @@ -81,10 +81,10 @@ public async Task UpdateAsync(User user) public async Task AddRefreshTokenAsync(RefreshToken refreshToken) { - var existantRt = await _unitOfWork.RefreshTokenRepository.GetBy(x => x.Email == refreshToken.Email); + var existantRefresToken = await _unitOfWork.RefreshTokenRepository.GetByAsync(x => x.Email == refreshToken.Email); - if (existantRt.Any()) - return existantRt.First(); + if (existantRefresToken.Any()) + return existantRefresToken.First(); var added = await _unitOfWork.RefreshTokenRepository.AddAsync(refreshToken); await _unitOfWork.CommitAsync(); @@ -102,11 +102,11 @@ public async Task GetUserByRefreshTokenAsync(string refreshToken) ); } - var token = await _unitOfWork.RefreshTokenRepository.GetBy(x => x.Refreshtoken.Equals(refreshToken)); + var token = await _unitOfWork.RefreshTokenRepository.GetByAsync(x => x.Refreshtoken.Equals(refreshToken)); if (token != null && token.Any()) { - var user = await _unitOfWork.UserRepository.GetBy(x => x.Email == token.First().Email); + var user = await _unitOfWork.UserRepository.GetByAsync(x => x.Email == token.First().Email); if (user.Any()) { @@ -122,7 +122,7 @@ public async Task GetUserByRefreshTokenAsync(string refreshToken) public async Task> ListPaginateAsync(UserFilter filter) { - return await _unitOfWork.UserRepository.ListPaginate(filter); + return await _unitOfWork.UserRepository.ListPaginateAsync(filter); } } } diff --git a/Orion.Domain/Interfaces/ICustomerService.cs b/Orion.Domain/Interfaces/ICustomerService.cs index 363b00a..a370403 100644 --- a/Orion.Domain/Interfaces/ICustomerService.cs +++ b/Orion.Domain/Interfaces/ICustomerService.cs @@ -8,6 +8,6 @@ namespace Orion.Domain.Interfaces { public interface ICustomerService : IBaseService { - Task> ListPaginate(CustomerFilter filter); + Task> ListPaginateAsync(CustomerFilter filter); } } diff --git a/Orion.Domain/Repositories/Base/IBaseEntityRepository.cs b/Orion.Domain/Repositories/Base/IBaseEntityRepository.cs index 5947781..fb5f061 100644 --- a/Orion.Domain/Repositories/Base/IBaseEntityRepository.cs +++ b/Orion.Domain/Repositories/Base/IBaseEntityRepository.cs @@ -9,7 +9,7 @@ public interface IBaseEntityRepository where T : class { Task AddAsync(T entity); Task DeleteAsync(string publicId); - Task> GetBy(Expression> predicate); + Task> GetByAsync(Expression> predicate); Task FindByIdAsync(string publicId); void Update(T entity); } diff --git a/Orion.Domain/Repositories/IUserRepository.cs b/Orion.Domain/Repositories/IUserRepository.cs index 82657b4..93ac7dc 100644 --- a/Orion.Domain/Repositories/IUserRepository.cs +++ b/Orion.Domain/Repositories/IUserRepository.cs @@ -9,7 +9,7 @@ namespace Orion.Domain.Repositories public interface IUserRepository : IBaseEntityRepository { Task LoginAsync(string email, string password); - Task> ListPaginate(UserFilter filter); + Task> ListPaginateAsync(UserFilter filter); Task FindByEmailAsync(string email); } } diff --git a/Orion.Entities/Enuns/UserProfile.cs b/Orion.Entities/Enuns/UserProfile.cs index b4b47c4..3a86d20 100644 --- a/Orion.Entities/Enuns/UserProfile.cs +++ b/Orion.Entities/Enuns/UserProfile.cs @@ -1,8 +1,13 @@ +using System.ComponentModel; + namespace Orion.Entities.Enuns { public enum UserProfile { - Admin = 0, - Customer = 1 + [Description("Admin")] + Admin = 1, + + [Description("Customer")] + Customer = 2 } } diff --git a/Orion.Test/API/HealthCheckApiTest.cs b/Orion.Test/API/HealthCheckApiTest.cs index 6636b43..ff938a0 100644 --- a/Orion.Test/API/HealthCheckApiTest.cs +++ b/Orion.Test/API/HealthCheckApiTest.cs @@ -10,11 +10,14 @@ public class HealthCheckApiTest: ApiTestInitializer [Fact] public async Task GetAsync_HealthCheck_ReturnsHealthy() { + //arrange Setup(); - var successMessageService = "Healthy"; + + //act var result = await Client.GetAsync("/health-check"); + //assert var content = await result.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.OK, result.StatusCode); diff --git a/Orion.Test/Controllers/AuthControllerTest.cs b/Orion.Test/Controllers/AuthControllerTest.cs index 5e8ca2f..0397655 100644 --- a/Orion.Test/Controllers/AuthControllerTest.cs +++ b/Orion.Test/Controllers/AuthControllerTest.cs @@ -26,6 +26,7 @@ public AuthControllerTest() [Fact] public async Task Login_WithValidCredentials_ReturnsOk() { + //arrange & act var result = await _authController.Login( new UserLoginModel { @@ -37,6 +38,7 @@ public async Task Login_WithValidCredentials_ReturnsOk() var contentResult = (OkObjectResult) result; var userApiToken = (UserApiTokenModel) contentResult.Value; + //assert Assert.IsType(contentResult); Assert.Equal(200, contentResult.StatusCode); @@ -48,6 +50,7 @@ public async Task Login_WithValidCredentials_ReturnsOk() [Fact] public async Task Login_WithInvalidCredentials_RetunsUnauthorized() { + //arrange & act var result = await _authController.Login( new UserLoginModel { @@ -58,6 +61,7 @@ public async Task Login_WithInvalidCredentials_RetunsUnauthorized() var contentResult = (UnauthorizedResult)result; + //assert Assert.IsType(contentResult); Assert.Equal(401, contentResult.StatusCode); } @@ -65,6 +69,8 @@ public async Task Login_WithInvalidCredentials_RetunsUnauthorized() [Fact] public async Task RefreshToken_WithValidRefreshToken_ReturnsNewToken() { + //arrange & act + var result = await _authController.RefreshToken( RefreshTokenMotherObject.ValidRefreshTokenModel() ); @@ -72,6 +78,7 @@ public async Task RefreshToken_WithValidRefreshToken_ReturnsNewToken() var contentResult = (OkObjectResult)result; var userApiToken = (UserApiTokenModel)contentResult.Value; + //asert Assert.IsType(contentResult); Assert.Equal(200, contentResult.StatusCode); @@ -82,12 +89,14 @@ public async Task RefreshToken_WithValidRefreshToken_ReturnsNewToken() [Fact] public async Task RefreshToken_WithInvalidRefreshToken_ReturnsUnauthorized() { + //arrange & act var result = await _authController.RefreshToken( new RefreshTokenModel { RefreshToken = null} ); var contentResult = (UnauthorizedResult)result; + //assert Assert.IsType(contentResult); Assert.Equal(401, contentResult.StatusCode); } @@ -102,7 +111,6 @@ private void SetupServiceMock() userServiceMock.Setup(x => x.AddRefreshTokenAsync(It.IsAny())).ReturnsAsync(RefreshTokenMotherObject.ValidRefreshToken()); userServiceMock.Setup(x => x.GetUserByRefreshTokenAsync(RefreshTokenMotherObject.ValidRefreshToken().Refreshtoken)).ReturnsAsync(UserMotherObject.ValidAdminUser()); - //Arrange var inMemorySettings = new Dictionary { {"JwtConfiguration:SymmetricSecurityKey", "5cCI6IkpXVCJ9.eyJlbWFpbCI6InZhbmRlcmxhbi5nc0BnbWFpbC5jb20iLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJhZG1p"}, {"JwtConfiguration:Issuer", "http://www.myapplication.com"}, diff --git a/Orion.Test/Controllers/CustomersControllerTest.cs b/Orion.Test/Controllers/CustomersControllerTest.cs index 9df9186..e81ec13 100644 --- a/Orion.Test/Controllers/CustomersControllerTest.cs +++ b/Orion.Test/Controllers/CustomersControllerTest.cs @@ -26,11 +26,13 @@ public CustomersControllerTestTest() [Fact] public async Task GetCustomerById_WithValidId_ReturnsValidCustomer() { + //arrange & act var result = await _customersController.Get(CustomerMotherObject.ValidCustomer().PublicId); var contentResult = (OkObjectResult) result; var customer = (CustomerOutput) contentResult.Value; + //assert Assert.IsType(contentResult); Assert.Equal(200, contentResult.StatusCode); @@ -41,11 +43,13 @@ public async Task GetCustomerById_WithValidId_ReturnsValidCustomer() [Fact] public async Task PostCustomer_WithValidData_CreateNewCustomer() { + //arrange & act var result = await _customersController.Post(CustomerMotherObject.ValidCustomerInput()); var contentResult = (CreatedResult) result; var contentResultObject = (CustomerOutput)contentResult.Value; - + + //assert Assert.IsType(contentResult); Assert.Equal(201, contentResult.StatusCode); Assert.NotNull(contentResult.Value); @@ -55,10 +59,12 @@ public async Task PostCustomer_WithValidData_CreateNewCustomer() [Fact] public async Task PutCustomer_WithValidData_EditCustomer() { + //arrange & act var result = await _customersController.Put(CustomerMotherObject.ValidCustomer().PublicId, CustomerMotherObject.ValidCustomerInput()); var contentResult = (AcceptedResult)result; + //assert Assert.IsType(contentResult); Assert.Equal(202, contentResult.StatusCode); } @@ -66,10 +72,12 @@ public async Task PutCustomer_WithValidData_EditCustomer() [Fact] public async Task DeleteCustomer_WithValidId_DeleteCustomer() { + //arrange & act var result = await _customersController.Delete(CustomerMotherObject.ValidCustomer().PublicId); var contentResult = (NoContentResult)result; + //assert Assert.IsType(contentResult); Assert.Equal(204, contentResult.StatusCode); } @@ -77,12 +85,17 @@ public async Task DeleteCustomer_WithValidId_DeleteCustomer() [Fact] public async Task GetCustomers_WithPagination_ReturnsAllCustomers() { + //arrange + var expectedCount = 4; + + //act var result = await _customersController.Get(new CustomerFilter()); var contentResult = (OkObjectResult)result; var customersPagedList = (PagedList)contentResult.Value; - Assert.Equal(4, customersPagedList.Count); + //assert + Assert.Equal(expectedCount, customersPagedList.Count); Assert.Equal(200, contentResult.StatusCode); } @@ -103,7 +116,7 @@ private void SetupServiceMock() customerServiceMock.Setup(x => x.AddAsync(It.IsAny())).ReturnsAsync(CustomerMotherObject.ValidCustomer()); customerServiceMock.Setup(x => x.UpdateAsync(It.IsAny())).Verifiable(); customerServiceMock.Setup(x => x.DeleteAsync(CustomerMotherObject.ValidCustomer().PublicId)).Verifiable(); - customerServiceMock.Setup(x => x.ListPaginate(It.IsAny())). + customerServiceMock.Setup(x => x.ListPaginateAsync(It.IsAny())). ReturnsAsync(customerListPaginated); _customersController = new CustomersController(customerServiceMock.Object, Mapper); diff --git a/Orion.Test/Controllers/UsersControllerTest.cs b/Orion.Test/Controllers/UsersControllerTest.cs index 7463a56..f066fa7 100644 --- a/Orion.Test/Controllers/UsersControllerTest.cs +++ b/Orion.Test/Controllers/UsersControllerTest.cs @@ -24,13 +24,15 @@ public UsersControllerTestTest() } [Fact] - public async Task GetUserValidTest() + public async Task GetUsertById_WithValidId_ReturnsValidCustomer() { + //arrange & act var result = await _usersController.Get(UserMotherObject.ValidAdminUser().PublicId); var contentResult = (OkObjectResult) result; var user = (UserOutput) contentResult.Value; + //assert Assert.IsType(contentResult); Assert.Equal(200, contentResult.StatusCode); @@ -40,52 +42,59 @@ public async Task GetUserValidTest() } [Fact] - public async Task CreateUserValidTest() + public async Task PostUser_WithValidData_CreateAUser() { + //arrange & act var result = await _usersController.Post(UserMotherObject.ValidAdminUserInput()); var contentResult = (CreatedResult) result; + //assert Assert.IsType(contentResult); Assert.Equal(201, contentResult.StatusCode); } [Fact] - public async Task EditUserValidTest() + public async Task PutUser_WithValidData_UpdateUser() { + //arrange & act var result = await _usersController.Put(UserMotherObject.ValidAdminUser().PublicId, UserMotherObject.ValidAdminUserInput()); var contentResult = (AcceptedResult) result; + //assert Assert.IsType(contentResult); Assert.Equal(202, contentResult.StatusCode); } [Fact] - public async Task DeleteUserValidTest() + public async Task DeleteUser_WithExistantId_DeleteUser() { + //arrange & act var result = await _usersController.Delete(UserMotherObject.ValidAdminUser().PublicId); var contentResult = (NoContentResult)result; + //assert Assert.IsType(contentResult); Assert.Equal(204, contentResult.StatusCode); } [Fact] - public async Task ListUsersTest() + public async Task GetUsers_WithValidFilter_ReturnsAListOfUsers() { + //arrange & act var result = await _usersController.Get(new UserFilter()); var contentResult = (OkObjectResult) result; var userPagedList = (PagedList) contentResult.Value; + //assert Assert.Equal(4, userPagedList.Count); Assert.Equal(200, contentResult.StatusCode); } - private void SetupServiceMock() { var userServiceMock = new Mock(); diff --git a/Orion.Test/Services/CustomerServiceTest.cs b/Orion.Test/Services/CustomerServiceTest.cs index 645ac87..4f75d63 100644 --- a/Orion.Test/Services/CustomerServiceTest.cs +++ b/Orion.Test/Services/CustomerServiceTest.cs @@ -19,26 +19,31 @@ public CustomerServiceTest(DependencyInjectionSetupFixture fixture) : base(fixtu } [Fact] - public async Task AddValidCustomerTest() + public async Task AddAsync_WithValidData_AddCustomerAsSuccess() { + //arrange using var scope = ServiceProvider.CreateScope(); var customerService = scope.ServiceProvider.GetService(); + //act var customerSaved = await customerService.AddAsync(CustomerMotherObject.ValidCustomer()); var customerFound = await customerService.FindByIdAsync(customerSaved.PublicId); + //assert Assert.NotNull(customerFound); Assert.Equal(CustomerMotherObject.ValidCustomer().Name, customerFound.Name); Assert.True(customerFound.CustomerId > 0); } [Fact] - public async Task RemoveCustomerTest() + public async Task DeleteAsync_WithExistantId_RemoveCustomerAsSuccess() { - using var scope = ServiceProvider.CreateScope(); + //arrange + using var scope = ServiceProvider.CreateScope(); var customerService = scope.ServiceProvider.GetService(); - var customerSaved = await customerService.AddAsync(CustomerMotherObject.ValidCustomer()); + //act + var customerSaved = await customerService.AddAsync(CustomerMotherObject.ValidCustomer()); var customerFound = await customerService.FindByIdAsync(customerSaved.PublicId); Assert.NotNull(customerFound); @@ -49,82 +54,92 @@ public async Task RemoveCustomerTest() Assert.Null(customerDeleted); } + [Fact] - public async Task RemoveCustomerNotFoundTest() + public async Task DeleteAsync_NotFound_ThrowsNotFoundException() { - using var scope = ServiceProvider.CreateScope(); + //arrange + using var scope = ServiceProvider.CreateScope(); var customerService = scope.ServiceProvider.GetService(); - await Assert.ThrowsAsync(() => customerService.DeleteAsync("invalid_id")); + //act & assert + await Assert.ThrowsAsync(() => customerService.DeleteAsync("invalid_id")); } [Fact] - public async Task EditCustomerTest() + public async Task UpdateAsync_WithValidData_UpdateCustomerAsSuccess() { - using var scope = ServiceProvider.CreateScope(); + //arrange + using var scope = ServiceProvider.CreateScope(); var customerService = scope.ServiceProvider.GetService(); var customerSaved = await customerService.AddAsync(CustomerMotherObject.ValidCustomer()); var customerFound = await customerService.FindByIdAsync(customerSaved.PublicId); - Assert.NotNull(customerFound); - + //act customerFound.Name = "Jane"; - await customerService.UpdateAsync(customerFound); await customerService.FindByIdAsync(customerSaved.PublicId); var customerEdited = await customerService.FindByIdAsync(customerSaved.PublicId); + //assert Assert.Equal(customerFound.Name, customerEdited.Name); } [Fact] - public async Task ListCustomerPaginatedTest() + public async Task ListPaginateAsync_WithEmptyFilter_GetAllfCustomers() { - using var scope = ServiceProvider.CreateScope(); + //arrange + using var scope = ServiceProvider.CreateScope(); var customerService = scope.ServiceProvider.GetService(); - var customerSaved = await customerService.AddAsync(CustomerMotherObject.ValidCustomer()); + //act + var customerSaved = await customerService.AddAsync(CustomerMotherObject.ValidCustomer()); var customerSaved2 = await customerService.AddAsync(CustomerMotherObject.ValidCustomer2()); - var customerList = await customerService.ListPaginate(new CustomerFilter { }); + var customerList = await customerService.ListPaginateAsync(new CustomerFilter { }); + //aseert Assert.NotNull(customerList); - Assert.True(customerList.Items.Where(x => x.Name.Equals(CustomerMotherObject.ValidCustomer().Name)).Any()); Assert.True(customerList.Items.Where(x => x.Name.Equals(CustomerMotherObject.ValidCustomer2().Name)).Any()); } [Fact] - public async Task ListCustomerPaginatedFilterByNameTest() + public async Task ListPaginateAsync_WithFilterByName_GetAllMatchedCustomers() { - using var scope = ServiceProvider.CreateScope(); + //arrange + using var scope = ServiceProvider.CreateScope(); var customerService = scope.ServiceProvider.GetService(); - var customerSaved = await customerService.AddAsync(CustomerMotherObject.ValidCustomer()); + //act + var customerSaved = await customerService.AddAsync(CustomerMotherObject.ValidCustomer()); var customerSaved2 = await customerService.AddAsync(CustomerMotherObject.ValidCustomer2()); - var customerList = await customerService.ListPaginate(new CustomerFilter { Query = CustomerMotherObject.ValidCustomer().Name }); + var customerList = await customerService.ListPaginateAsync(new CustomerFilter { Query = CustomerMotherObject.ValidCustomer().Name }); + //assert Assert.NotNull(customerList); - Assert.True(customerList.Items.Where(x => x.Name.Equals(CustomerMotherObject.ValidCustomer().Name)).Any()); Assert.False(customerList.Items.Where(x => x.Name.Equals(CustomerMotherObject.ValidCustomer2().Name)).Any()); } - [Fact] - public async Task ListCustomerPaginatedQuantityTest() + [Theory] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + public async Task ListPaginateAsync_WithConfiguredQuantiry_ReturnsListWithTheExpctedQuantity(int expectedQuantity) { - const int expectedQuantity = 1; - + //arrange using var scope = ServiceProvider.CreateScope(); var customerService = scope.ServiceProvider.GetService(); - await customerService.AddAsync(CustomerMotherObject.ValidCustomer()); - - var customerList = await customerService.ListPaginate(new CustomerFilter { Quantity = expectedQuantity }); + //act + await customerService.AddAsync(CustomerMotherObject.ValidCustomer()); + var customerList = await customerService.ListPaginateAsync(new CustomerFilter { Quantity = expectedQuantity }); + //assert Assert.NotNull(customerList); Assert.Equal(expectedQuantity, customerList.Items.Count); } diff --git a/Orion.Test/Services/UserServiceTest.cs b/Orion.Test/Services/UserServiceTest.cs index abbbaca..9ca82f1 100644 --- a/Orion.Test/Services/UserServiceTest.cs +++ b/Orion.Test/Services/UserServiceTest.cs @@ -25,14 +25,18 @@ public UserServiceTest(DependencyInjectionSetupFixture fixture) : base(fixture) #region User CRUD tests [Fact] - public async Task AddValidUserTest() + public async Task AddAsync_WithValidData_AddUserAsSuccess() { + //arrange using var scope = ServiceProvider.CreateScope(); var userService = scope.ServiceProvider.GetService(); + //act var userSaved = await userService.AddAsync(UserMotherObject.ValidAdminUser()); + var userFound = await userService.FindByIdAsync(userSaved.PublicId); + //assert Assert.NotNull(userFound); Assert.Equal(UserMotherObject.ValidAdminUser().Password.ToSha512(), userFound.Password); Assert.Equal(userFound.Name, UserMotherObject.ValidAdminUser().Name); @@ -42,8 +46,9 @@ public async Task AddValidUserTest() } [Fact] - public async Task AddDuplicatedUserTest() + public async Task AddAsync_DuplicatedUser_ThrowsConflictException() { + //arrange using var scope = ServiceProvider.CreateScope(); var userService = scope.ServiceProvider.GetService(); @@ -54,13 +59,15 @@ public async Task AddDuplicatedUserTest() Assert.Equal(UserMotherObject.ValidAdminUser().Password.ToSha512(), userFound.Password); Assert.Equal(userFound.Name, UserMotherObject.ValidAdminUser().Name); + //act && assert await Assert.ThrowsAsync(() => userService.AddAsync(UserMotherObject.ValidAdminUser())); await userService.DeleteAsync(userFound.PublicId); } [Fact] - public async Task ListUserPaginatedFilterTest() + public async Task ListPaginateAsync_WithFilterByName_GetAllMatchedUsers() { + //arrange var userCount = 1; using var scope = ServiceProvider.CreateScope(); @@ -69,6 +76,7 @@ public async Task ListUserPaginatedFilterTest() var userSaved = await userService.AddAsync(UserMotherObject.ValidAdminUser()); var userFound = await userService.FindByIdAsync(userSaved.PublicId); + //act var userPaginated = await userService.ListPaginateAsync( new UserFilter { Query = UserMotherObject.ValidAdminUser().Name, @@ -78,6 +86,7 @@ public async Task ListUserPaginatedFilterTest() } }); + //assert Assert.Equal(userCount, userPaginated.Count); Assert.Contains(userPaginated.Items, x => x.Name == UserMotherObject.ValidAdminUser().Name); @@ -85,17 +94,20 @@ public async Task ListUserPaginatedFilterTest() } [Fact] - public async Task AddInvalidUserTest() + public async Task AddAsync_WithInvalidData_ThrowsBusinessException() { + //arrange using var scope = ServiceProvider.CreateScope(); var userService = scope.ServiceProvider.GetService(); + //act & assert await Assert.ThrowsAsync(() => userService.AddAsync(UserMotherObject.InvalidAdminUserWihoutPassword())); } [Fact] - public async Task RemoveUserTest() + public async Task DeleteAsync_WithExistantId_RemoveUserAsSuccess() { + //arrange using var scope = ServiceProvider.CreateScope(); var userService = scope.ServiceProvider.GetService(); @@ -104,16 +116,19 @@ public async Task RemoveUserTest() Assert.NotNull(userFound); + //act await userService.DeleteAsync(userFound.PublicId); var userDeleted = await userService.FindByIdAsync(userSaved.PublicId); + //assert Assert.Null(userDeleted); } [Fact] - public async Task EditUserTest() + public async Task UpdateAsync_WithValidData_UpdateUserAsSuccess() { + //arrange using var scope = ServiceProvider.CreateScope(); var userService = scope.ServiceProvider.GetService(); @@ -129,8 +144,10 @@ public async Task EditUserTest() await userService.UpdateAsync(userFound); await userService.FindByIdAsync(userSaved.PublicId); + //act var userEdited = await userService.FindByIdAsync(userSaved.PublicId); + //assert Assert.Equal(userFound.Email, userEdited.Email); Assert.Equal(userFound.Password, userEdited.Password); Assert.Equal(userFound.Name, userEdited.Name); @@ -142,8 +159,9 @@ public async Task EditUserTest() #region User Authentication tests [Fact] - public async Task LoginValidTest() + public async Task LoginAsync_WithValidCredentials_LoginAsSuccess() { + //arrange using var scope = ServiceProvider.CreateScope(); var userService = scope.ServiceProvider.GetService(); @@ -152,10 +170,11 @@ public async Task LoginValidTest() Assert.NotNull(userFound); + //act var userLoged = await userService.LoginAsync(userFound.Email, UserMotherObject.ValidAdminUser().Password); + //assert Assert.NotNull(userLoged); - Assert.Equal(userLoged.Email, userAdded.Email); Assert.Equal(userLoged.Password, userAdded.Password); Assert.Equal(userLoged.Name, userAdded.Name); @@ -164,24 +183,26 @@ public async Task LoginValidTest() } [Fact] - public async Task LoginInvalidPassTest() + public async Task LoginAsync_WithInvalidCredentials_ThrowsUnauthorizedUserException() { + //arrange using var scope = ServiceProvider.CreateScope(); var userService = scope.ServiceProvider.GetService(); var userAdded = await userService.AddAsync(UserMotherObject.ValidAdminUser()); var userFound = await userService.FindByIdAsync(userAdded.PublicId); - Assert.NotNull(userFound); - + //act & assert await Assert.ThrowsAsync(() => userService.LoginAsync(userFound.Email, "wrong pass")); + Assert.NotNull(userFound); await userService.DeleteAsync(userFound.PublicId); } [Fact] - public async Task AddRefreshTokenValidTest() + public async Task RefreshTokenAddAync_WithValidEmail_AddARefreskToken() { + //arrange using var scope = ServiceProvider.CreateScope(); var userService = scope.ServiceProvider.GetService(); @@ -194,10 +215,11 @@ public async Task AddRefreshTokenValidTest() var refreshTokenAdded = await userService.AddRefreshTokenAsync(new RefreshToken { Email = UserMotherObject.ValidAdminUser().Email, Refreshtoken = refreshToken }); + //act var userByRefreshToken = await userService.GetUserByRefreshTokenAsync(refreshTokenAdded.Refreshtoken); + //assert Assert.NotNull(userByRefreshToken); - Assert.Equal(userByRefreshToken.Email, userAdded.Email); Assert.Equal(userByRefreshToken.Password, userAdded.Password); Assert.Equal(userByRefreshToken.Name, userAdded.Name); @@ -205,32 +227,12 @@ public async Task AddRefreshTokenValidTest() await userService.DeleteAsync(userFound.PublicId); } - [Fact] - public async Task RefreshTokenNullTest() - { - using var scope = ServiceProvider.CreateScope(); - var userService = scope.ServiceProvider.GetService(); - var messages = scope.ServiceProvider.GetService>(); - - var userAdded = await userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await userService.FindByIdAsync(userAdded.PublicId); - - Assert.NotNull(userFound); - - var refreshToken = Guid.NewGuid().ToString(); - - await userService.AddRefreshTokenAsync(new RefreshToken { Email = UserMotherObject.ValidAdminUser().Email, Refreshtoken = refreshToken }); - - var exeption = await Assert.ThrowsAsync(() => userService.GetUserByRefreshTokenAsync(null)); - - Assert.Equal(exeption.Message, messages[UserMessages.InvalidRefreshToken]); - - await userService.DeleteAsync(userFound.PublicId); - } - - [Fact] - public async Task RefreshTokenInvalidTest() + [Theory] + [InlineData(null)] + [InlineData("Invalid refresh token")] + public async Task GetUserByRefreshTokenAsync_WithInvalidId_ThrowsUnauthorizedUserException(string refreshTokenId) { + //arrange using var scope = ServiceProvider.CreateScope(); var userService = scope.ServiceProvider.GetService(); var messages = scope.ServiceProvider.GetService>(); @@ -244,8 +246,10 @@ public async Task RefreshTokenInvalidTest() await userService.AddRefreshTokenAsync(new RefreshToken { Email = UserMotherObject.ValidAdminUser().Email, Refreshtoken = refreshToken }); - var exeption = await Assert.ThrowsAsync(() => userService.GetUserByRefreshTokenAsync("wrong refresh token")); + //act + var exeption = await Assert.ThrowsAsync(() => userService.GetUserByRefreshTokenAsync(refreshTokenId)); + //assert Assert.Equal(exeption.Message, messages[UserMessages.InvalidRefreshToken]); await userService.DeleteAsync(userFound.PublicId); @@ -254,10 +258,10 @@ public async Task RefreshTokenInvalidTest() [Fact] public void CryptoSha512Test() { - const string stringValidTest = "userPawssTest1234A%@&!"; - const string expectedResult = "8c890b40034e242c05f27eec302a1f552be2a0a879b25b546c38d73c096d04aa8dfbf013a6c7e63a06ef42a346035c0e2256726d5aecb628df7bf6b42804802a"; + const string passwordTest = "userPawssTest1234A%@&!"; + const string expectedHash = "8c890b40034e242c05f27eec302a1f552be2a0a879b25b546c38d73c096d04aa8dfbf013a6c7e63a06ef42a346035c0e2256726d5aecb628df7bf6b42804802a"; - Assert.Equal(expectedResult, stringValidTest.ToSha512()); + Assert.Equal(expectedHash, passwordTest.ToSha512()); } #endregion diff --git a/README.md b/README.md index 31e531e..4c510be 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # **Orion** -[![Build and test](https://github.com/vanderlan/Orion-NetCoreAPI/actions/workflows/dotnet.yml/badge.svg)](https://github.com/vanderlan/Orion-NetCoreAPI/actions/workflows/dotnet.yml) [![Coverage Status](https://coveralls.io/repos/github/vanderlan/Orion-NetCoreAPI/badge.svg)](https://coveralls.io/github/vanderlan/Orion-NetCoreAPI) [![GitHub release](https://img.shields.io/github/release/vanderlan/Orion-NetCoreAPI.svg)](https://GitHub.com/vanderlan/Orion-NetCoreAPI/) [![GitHub repo size](https://img.shields.io/github/repo-size/vanderlan/Orion-NetCoreAPI)](https://github.com/vanderlan/Orion-NetCoreAPI) +[![Build and test](https://github.com/vanderlan/Orion-Api/actions/workflows/dotnet.yml/badge.svg)](https://github.com/vanderlan/Orion-Api/actions/workflows/dotnet.yml) [![Coverage Status](https://coveralls.io/repos/github/vanderlan/Orion-Api/badge.svg)](https://coveralls.io/github/vanderlan/Orion-Api) [![GitHub release](https://img.shields.io/github/release/vanderlan/Orion-Api.svg)](https://GitHub.com/vanderlan/Orion-Api/) [![GitHub repo size](https://img.shields.io/github/repo-size/vanderlan/Orion-Api)](https://github.com/vanderlan/Orion-Api) **About this Project** diff --git a/VBaseProject.Test/Services/UserServiceTest.cs b/VBaseProject.Test/Services/UserServiceTest.cs deleted file mode 100644 index 41c9a39..0000000 --- a/VBaseProject.Test/Services/UserServiceTest.cs +++ /dev/null @@ -1,265 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Localization; -using System; -using System.Threading.Tasks; -using VBaseProject.Entities.Domain; -using VBaseProject.Entities.Filter; -using VBaseProject.Resources; -using VBaseProject.Domain.Exceptions; -using VBaseProject.Domain.Extensions; -using VBaseProject.Domain.Interfaces; -using VBaseProject.Test.Configuration; -using VBaseProject.Test.MotherObjects; -using VBaseProject.Test.Services.BaseService; -using Xunit; -using static VBaseProject.Resources.Messages.MessagesKeys; - -namespace VBaseProject.Test.Services -{ - public class UserServiceTest : BaseServiceTest - { - public UserServiceTest(DependencyInjectionSetupFixture fixture) : base(fixture) - { - - } - - #region User CRUD tests - [Fact] - public async Task AddValidUserTest() - { - using var scope = _serviceProvider.CreateScope(); - var _userService = scope.ServiceProvider.GetService(); - - var userSaved = await _userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await _userService.FindByIdAsync(userSaved.PublicId); - - Assert.NotNull(userFound); - Assert.Equal(UserMotherObject.ValidAdminUser().Password.ToSha512(), userFound.Password); - Assert.Equal(userFound.Name, UserMotherObject.ValidAdminUser().Name); - Assert.True(userFound.UserId > 0); - - await _userService.DeleteAsync(userFound.PublicId); - } - - [Fact] - public async Task AddDuplicatedUserTest() - { - using var scope = _serviceProvider.CreateScope(); - var _userService = scope.ServiceProvider.GetService(); - - var userSaved = await _userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await _userService.FindByIdAsync(userSaved.PublicId); - - Assert.NotNull(userFound); - Assert.Equal(UserMotherObject.ValidAdminUser().Password.ToSha512(), userFound.Password); - Assert.Equal(userFound.Name, UserMotherObject.ValidAdminUser().Name); - - await Assert.ThrowsAsync(() => _userService.AddAsync(UserMotherObject.ValidAdminUser())); - - await _userService.DeleteAsync(userFound.PublicId); - } - [Fact] - public async Task ListUserPaginatedFilterTest() - { - var userCount = 1; - - using var scope = _serviceProvider.CreateScope(); - var _userService = scope.ServiceProvider.GetService(); - - var userSaved = await _userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await _userService.FindByIdAsync(userSaved.PublicId); - - var userPaginated = await _userService.ListPaginateAsync( - new UserFilter { - Query = UserMotherObject.ValidAdminUser().Name, - Entity = new User - { - Name = UserMotherObject.ValidAdminUser().Name - } - }); - - Assert.Equal(userCount, userPaginated.Count); - Assert.Contains(userPaginated.Items, x => x.Name == UserMotherObject.ValidAdminUser().Name); - - await _userService.DeleteAsync(userFound.PublicId); - } - - [Fact] - public async Task AddInvalidUserTest() - { - using var scope = _serviceProvider.CreateScope(); - var _userService = scope.ServiceProvider.GetService(); - - await Assert.ThrowsAsync(() => _userService.AddAsync(UserMotherObject.InvalidAdminUserWihoutPassword())); - } - - [Fact] - public async Task RemoveUserTest() - { - using var scope = _serviceProvider.CreateScope(); - var _userService = scope.ServiceProvider.GetService(); - - var userSaved = await _userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await _userService.FindByIdAsync(userSaved.PublicId); - - Assert.NotNull(userFound); - - await _userService.DeleteAsync(userFound.PublicId); - - var userDeleted = await _userService.FindByIdAsync(userSaved.PublicId); - - Assert.Null(userDeleted); - } - - [Fact] - public async Task EditUserTest() - { - using var scope = _serviceProvider.CreateScope(); - var _userService = scope.ServiceProvider.GetService(); - - var userSaved = await _userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await _userService.FindByIdAsync(userSaved.PublicId); - - Assert.NotNull(userFound); - - userFound.Name = "Jane"; - userFound.Email = "newemail@gmail.com"; - userFound.Password = "123"; - - await _userService.UpdateAsync(userFound); - await _userService.FindByIdAsync(userSaved.PublicId); - - var userEdited = await _userService.FindByIdAsync(userSaved.PublicId); - - Assert.Equal(userFound.Email, userEdited.Email); - Assert.Equal(userFound.Password, userEdited.Password); - Assert.Equal(userFound.Name, userEdited.Name); - - await _userService.DeleteAsync(userFound.PublicId); - } - - #endregion - - #region User Authentication tests - [Fact] - public async Task LoginValidTest() - { - using var scope = _serviceProvider.CreateScope(); - var _userService = scope.ServiceProvider.GetService(); - - var userAdded = await _userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await _userService.FindByIdAsync(userAdded.PublicId); - - Assert.NotNull(userFound); - - var userLoged = await _userService.LoginAsync(userFound.Email, UserMotherObject.ValidAdminUser().Password); - - Assert.NotNull(userLoged); - - Assert.Equal(userLoged.Email, userAdded.Email); - Assert.Equal(userLoged.Password, userAdded.Password); - Assert.Equal(userLoged.Name, userAdded.Name); - - await _userService.DeleteAsync(userFound.PublicId); - } - - [Fact] - public async Task LoginInvalidPassTest() - { - using var scope = _serviceProvider.CreateScope(); - var _userService = scope.ServiceProvider.GetService(); - - var userAdded = await _userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await _userService.FindByIdAsync(userAdded.PublicId); - - Assert.NotNull(userFound); - - await Assert.ThrowsAsync(() => _userService.LoginAsync(userFound.Email, "wrong pass")); - - await _userService.DeleteAsync(userFound.PublicId); - } - - [Fact] - public async Task AddRefreshTokenValidTest() - { - using var scope = _serviceProvider.CreateScope(); - var _userService = scope.ServiceProvider.GetService(); - - var userAdded = await _userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await _userService.FindByIdAsync(userAdded.PublicId); - - Assert.NotNull(userFound); - - var refreshToken = Guid.NewGuid().ToString(); - - var refreshTokenAdded = await _userService.AddRefreshTokenAsync(new RefreshToken { Email = UserMotherObject.ValidAdminUser().Email, Refreshtoken = refreshToken }); - - var userByRefreshToken = await _userService.GetUserByRefreshTokenAsync(refreshTokenAdded.Refreshtoken); - - Assert.NotNull(userByRefreshToken); - - Assert.Equal(userByRefreshToken.Email, userAdded.Email); - Assert.Equal(userByRefreshToken.Password, userAdded.Password); - Assert.Equal(userByRefreshToken.Name, userAdded.Name); - - await _userService.DeleteAsync(userFound.PublicId); - } - - [Fact] - public async Task RefreshTokenNullTest() - { - using var scope = _serviceProvider.CreateScope(); - var _userService = scope.ServiceProvider.GetService(); - var messages = scope.ServiceProvider.GetService>(); - - var userAdded = await _userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await _userService.FindByIdAsync(userAdded.PublicId); - - Assert.NotNull(userFound); - - var refreshToken = Guid.NewGuid().ToString(); - - await _userService.AddRefreshTokenAsync(new RefreshToken { Email = UserMotherObject.ValidAdminUser().Email, Refreshtoken = refreshToken }); - - var exeption = await Assert.ThrowsAsync(() => _userService.GetUserByRefreshTokenAsync(null)); - - Assert.Equal(exeption.Message, messages[UserMessages.InvalidRefreshToken]); - - await _userService.DeleteAsync(userFound.PublicId); - } - - [Fact] - public async Task RefreshTokenInvalidTest() - { - using var scope = _serviceProvider.CreateScope(); - var _userService = scope.ServiceProvider.GetService(); - var messages = scope.ServiceProvider.GetService>(); - - var userAdded = await _userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await _userService.FindByIdAsync(userAdded.PublicId); - - Assert.NotNull(userFound); - - var refreshToken = Guid.NewGuid().ToString(); - - await _userService.AddRefreshTokenAsync(new RefreshToken { Email = UserMotherObject.ValidAdminUser().Email, Refreshtoken = refreshToken }); - - var exeption = await Assert.ThrowsAsync(() => _userService.GetUserByRefreshTokenAsync("wrong refresh token")); - - Assert.Equal(exeption.Message, messages[UserMessages.InvalidRefreshToken]); - - await _userService.DeleteAsync(userFound.PublicId); - } - - [Fact] - public void CryptoSHA512Test() - { - var stringValidTest = "userPawssTest1234A%@&!"; - var expectedResult = "8c890b40034e242c05f27eec302a1f552be2a0a879b25b546c38d73c096d04aa8dfbf013a6c7e63a06ef42a346035c0e2256726d5aecb628df7bf6b42804802a"; - - Assert.Equal(expectedResult, stringValidTest.ToSha512()); - } - - #endregion - } -}