Skip to content

Commit

Permalink
Fix test naming and add AAA pattern (#17)
Browse files Browse the repository at this point in the history
* Fix test naming and add AAA pattern

* Fix API Startup and Docker Compose

---------

Co-authored-by: 83585-Vanderlan Silva <[email protected]>
  • Loading branch information
vanderlan and 83585-Vanderlan authored Sep 26, 2023
1 parent 36ede95 commit 8d51c82
Show file tree
Hide file tree
Showing 27 changed files with 173 additions and 381 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: ./VBaseProject.Test/TestResults/coverage.info
path-to-lcov: ./Orion.Test/TestResults/coverage.info

4 changes: 2 additions & 2 deletions .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build

EXPOSE 5000

WORKDIR /app

COPY . .
Expand Down
2 changes: 1 addition & 1 deletion Orion.Api/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public CustomersController(ICustomerService customerService, IMapper mapper) : b
[ProducesResponseType((int)HttpStatusCode.OK)]
public async Task<IActionResult> Get([FromQuery] CustomerFilter filter)
{
var customer = await _customerService.ListPaginate(filter);
var customer = await _customerService.ListPaginateAsync(filter);

var customerOutputList = Mapper.Map<PagedList<CustomerOutput>>(customer);

Expand Down
3 changes: 3 additions & 0 deletions Orion.Api/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public async Task<IActionResult> Get(string id)
var user = await _userService.FindByIdAsync(id);
var userOutput = Mapper.Map<UserOutput>(user);

if (userOutput is null)
return NotFound();

return Ok(userOutput);
}

Expand Down
1 change: 1 addition & 0 deletions Orion.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static void Main(string[] args)

private static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://*:5000")
.UseStartup<Startup>()
.UseIISIntegration()
.ConfigureLogging((context, logging) =>
Expand Down
6 changes: 3 additions & 3 deletions Orion.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 3 additions & 3 deletions Orion.Api/appsettings.Production.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions Orion.Api/appsettings.Test.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion Orion.Data/Repository/Generic/BaseEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public virtual async Task<T> FindByIdAsync(string publicId)
return await DataContext.Set<T>().FirstOrDefaultAsync(x => x.PublicId == publicId);
}

public async Task<IEnumerable<T>> GetBy(Expression<Func<T, bool>> predicate)
public async Task<IEnumerable<T>> GetByAsync(Expression<Func<T, bool>> predicate)
{
return await DataContext.Set<T>().Where(predicate).ToListAsync();
}
Expand Down
2 changes: 1 addition & 1 deletion Orion.Data/Repository/Generic/IBaseEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IBaseEntityRepository<T> where T : class
{
Task<T> AddAsync(T entity);
Task DeleteAsync(string publicId);
Task<IEnumerable<T>> GetBy(Expression<Func<T, bool>> predicate);
Task<IEnumerable<T>> GetByAsync(Expression<Func<T, bool>> predicate);
Task<T> FindByIdAsync(string publicId);
void Update(T entity);
}
Expand Down
2 changes: 1 addition & 1 deletion Orion.Data/Repository/Implementations/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task<User> LoginAsync(string email, string password)
return user ?? null;
}

public async Task<PagedList<User>> ListPaginate(UserFilter filter)
public async Task<PagedList<User>> ListPaginateAsync(UserFilter filter)
{
var pagination = (filter.Page * filter.Quantity) - filter.Quantity;

Expand Down
4 changes: 1 addition & 3 deletions Orion.Domain/Extensions/StrignExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion Orion.Domain/Implementation/CustomerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task<Customer> FindByIdAsync(string publicId)
return await _unitOfWork.CustomerRepository.FindByIdAsync(publicId);
}

public async Task<PagedList<Customer>> ListPaginate(CustomerFilter filter)
public async Task<PagedList<Customer>> ListPaginateAsync(CustomerFilter filter)
{
return await _unitOfWork.CustomerRepository.ListPaginate(filter);
}
Expand Down
12 changes: 6 additions & 6 deletions Orion.Domain/Implementation/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public async Task UpdateAsync(User user)

public async Task<RefreshToken> 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();
Expand All @@ -102,11 +102,11 @@ public async Task<User> 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())
{
Expand All @@ -122,7 +122,7 @@ public async Task<User> GetUserByRefreshTokenAsync(string refreshToken)

public async Task<PagedList<User>> ListPaginateAsync(UserFilter filter)
{
return await _unitOfWork.UserRepository.ListPaginate(filter);
return await _unitOfWork.UserRepository.ListPaginateAsync(filter);
}
}
}
2 changes: 1 addition & 1 deletion Orion.Domain/Interfaces/ICustomerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace Orion.Domain.Interfaces
{
public interface ICustomerService : IBaseService<Customer>
{
Task<PagedList<Customer>> ListPaginate(CustomerFilter filter);
Task<PagedList<Customer>> ListPaginateAsync(CustomerFilter filter);
}
}
2 changes: 1 addition & 1 deletion Orion.Domain/Repositories/Base/IBaseEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IBaseEntityRepository<T> where T : class
{
Task<T> AddAsync(T entity);
Task DeleteAsync(string publicId);
Task<IEnumerable<T>> GetBy(Expression<Func<T, bool>> predicate);
Task<IEnumerable<T>> GetByAsync(Expression<Func<T, bool>> predicate);
Task<T> FindByIdAsync(string publicId);
void Update(T entity);
}
Expand Down
2 changes: 1 addition & 1 deletion Orion.Domain/Repositories/IUserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Orion.Domain.Repositories
public interface IUserRepository : IBaseEntityRepository<User>
{
Task<User> LoginAsync(string email, string password);
Task<PagedList<User>> ListPaginate(UserFilter filter);
Task<PagedList<User>> ListPaginateAsync(UserFilter filter);
Task<User> FindByEmailAsync(string email);
}
}
9 changes: 7 additions & 2 deletions Orion.Entities/Enuns/UserProfile.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
5 changes: 4 additions & 1 deletion Orion.Test/API/HealthCheckApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion Orion.Test/Controllers/AuthControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public AuthControllerTest()
[Fact]
public async Task Login_WithValidCredentials_ReturnsOk()
{
//arrange & act
var result = await _authController.Login(
new UserLoginModel
{
Expand All @@ -37,6 +38,7 @@ public async Task Login_WithValidCredentials_ReturnsOk()
var contentResult = (OkObjectResult) result;
var userApiToken = (UserApiTokenModel) contentResult.Value;

//assert
Assert.IsType<OkObjectResult>(contentResult);
Assert.Equal(200, contentResult.StatusCode);

Expand All @@ -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
{
Expand All @@ -58,20 +61,24 @@ public async Task Login_WithInvalidCredentials_RetunsUnauthorized()

var contentResult = (UnauthorizedResult)result;

//assert
Assert.IsType<UnauthorizedResult>(contentResult);
Assert.Equal(401, contentResult.StatusCode);
}

[Fact]
public async Task RefreshToken_WithValidRefreshToken_ReturnsNewToken()
{
//arrange & act

var result = await _authController.RefreshToken(
RefreshTokenMotherObject.ValidRefreshTokenModel()
);

var contentResult = (OkObjectResult)result;
var userApiToken = (UserApiTokenModel)contentResult.Value;

//asert
Assert.IsType<OkObjectResult>(contentResult);
Assert.Equal(200, contentResult.StatusCode);

Expand All @@ -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<UnauthorizedResult>(contentResult);
Assert.Equal(401, contentResult.StatusCode);
}
Expand All @@ -102,7 +111,6 @@ private void SetupServiceMock()
userServiceMock.Setup(x => x.AddRefreshTokenAsync(It.IsAny<RefreshToken>())).ReturnsAsync(RefreshTokenMotherObject.ValidRefreshToken());
userServiceMock.Setup(x => x.GetUserByRefreshTokenAsync(RefreshTokenMotherObject.ValidRefreshToken().Refreshtoken)).ReturnsAsync(UserMotherObject.ValidAdminUser());

//Arrange
var inMemorySettings = new Dictionary<string, string> {
{"JwtConfiguration:SymmetricSecurityKey", "5cCI6IkpXVCJ9.eyJlbWFpbCI6InZhbmRlcmxhbi5nc0BnbWFpbC5jb20iLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJhZG1p"},
{"JwtConfiguration:Issuer", "http://www.myapplication.com"},
Expand Down
Loading

0 comments on commit 8d51c82

Please sign in to comment.