Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanderlan Gomes da Silva committed Nov 14, 2023
1 parent 4bdc15a commit dd50299
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 28 deletions.
62 changes: 57 additions & 5 deletions tests/Orion.Test/Api/UsersApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using Orion.Test.Integration.Setup;
using System;
using System.Net;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Orion.Application.Core.Commands.UserChangePassword;
using Xunit;

namespace Orion.Test.Api
Expand All @@ -12,7 +14,7 @@ public class UsersApiTest : IntegrationTestsBootstrapper
{
public UsersApiTest(IntegrationTestsFixture fixture) : base(fixture)
{
AuthUser();
AuthDefaulthUser();
}

[Fact]
Expand All @@ -39,7 +41,7 @@ public async Task PostUser_WithValidData_CreateAUser()
}

[Fact]
public async Task PutUser_WithValidData_CreateAUser()
public async Task PutUser_WithValidData_UpdateUser()
{
//arrange
var userCreated = await CreateUserAsync();
Expand Down Expand Up @@ -92,11 +94,61 @@ public async Task DeleteUser_WithInvalidId_ReturnsNotFound()
Assert.Equal(HttpStatusCode.NotFound, httpResponseDelete.StatusCode);
}

private async Task<UserCreateResponse> CreateUserAsync()
[Theory]
[InlineData("123", "12345", "30298302")]
[InlineData("", "12345", "30298302")]
[InlineData("", "12345", null)]
[InlineData("", "", null)]
[InlineData("123", null, null)]
public async Task UpdateUserPassword_WithInvalidPasswordsAndConfirmation_ReturnsBadRequest(string currentPass, string newPass, string newPassConfirm)
{
var createUserRequest = UserFaker.GetUserCreateRequest();
//arrange
var changePasswordRequest = new UserChangePasswordRequest
{
CurrentPassword = currentPass,
NewPassword = newPass,
NewPasswordConfirm = newPassConfirm
};

//act
var httpResponsePatch = await AuthenticatedHttpClient.PatchAsync("/api/Users/Me/PasswordChange", GetStringContent(changePasswordRequest));

Assert.Equal(HttpStatusCode.BadRequest, httpResponsePatch.StatusCode);
}

[Fact]
public async Task UpdateUserPassword_WithValidPasswordsAndConfirmation_ReturnsAcepted()
{
//arrange
var user = UserFaker.GetUserCreateRequest();

var userCreated = await CreateUserAsync(user);

var changePasswordRequest = new UserChangePasswordRequest
{
CurrentPassword = user.Password,
NewPassword = "Ab647477382",
NewPasswordConfirm = "Ab647477382"
};

var httpClient = IntegrationTestsFixture.GetNewHttpClient();

var tokenResult = AuthUser(userCreated.Email, user.Password);

httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenResult.Token);

//act
var httpResponsePatch = await httpClient.PatchAsync("/api/Users/Me/PasswordChange", GetStringContent(changePasswordRequest));

//assert
Assert.Equal(HttpStatusCode.Accepted, httpResponsePatch.StatusCode);
}

private async Task<UserCreateResponse> CreateUserAsync(UserCreateRequest userCreateRequest = null)
{
userCreateRequest ??= UserFaker.GetUserCreateRequest();

var httpResponsePost = await AuthenticatedHttpClient.PostAsync("/api/Users", GetStringContent(createUserRequest));
var httpResponsePost = await AuthenticatedHttpClient.PostAsync("/api/Users", GetStringContent(userCreateRequest));

Assert.Equal(HttpStatusCode.Created, httpResponsePost.StatusCode);

Expand Down
43 changes: 26 additions & 17 deletions tests/Orion.Test/Integration/Setup/IntegrationTestsBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Orion.Domain.Core.Entities;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -11,44 +12,52 @@ namespace Orion.Test.Integration.Setup;

public abstract class IntegrationTestsBootstrapper : IClassFixture<IntegrationTestsFixture>, IDisposable
{
protected readonly HttpClient HttpClient;
private readonly HttpClient _httpClient;
protected readonly HttpClient AuthenticatedHttpClient;
protected readonly User DefaultSystemUser;
private readonly User _defaultSystemUser;
protected readonly IntegrationTestsFixture IntegrationTestsFixture;

protected IServiceProvider ServiceProvider { get; private set; }

public IntegrationTestsBootstrapper(IntegrationTestsFixture fixture)
protected IntegrationTestsBootstrapper(IntegrationTestsFixture fixture)
{
HttpClient = fixture.HttpClient;
_httpClient = fixture.HttpClient;
AuthenticatedHttpClient = fixture.AuthenticatedHttpClient;
DefaultSystemUser = fixture.DefaultSystemUser;
_defaultSystemUser = fixture.DefaultSystemUser;
ServiceProvider = fixture.ServiceProvider;
IntegrationTestsFixture = fixture;
}

protected void AuthUser()
protected void AuthDefaulthUser()
{
var result = HttpClient.PostAsync("/api/Auth/Login", GetStringContent(
new UserLoginModel
{
Email = DefaultSystemUser.Email,
Password = "123"
}))
var tokenResult = AuthUser(_defaultSystemUser.Email, "123");

AuthenticatedHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenResult.Token);
}

protected UserApiTokenModel AuthUser(string email, string password)
{
var result = _httpClient.PostAsync("/api/Auth/Login", GetStringContent(
new UserLoginModel
{
Email = email,
Password = password
}))
.GetAwaiter().GetResult();

var content = result.Content.ReadAsStringAsync().GetAwaiter().GetResult();

var tokenResult = JsonConvert.DeserializeObject<UserApiTokenModel>(content);

AuthenticatedHttpClient.DefaultRequestHeaders.Authorization = new("Bearer", tokenResult.Token);
return JsonConvert.DeserializeObject<UserApiTokenModel>(content);
}

protected static StringContent GetStringContent(object obj)
{
return new StringContent(JsonConvert.SerializeObject(obj), Encoding.Default, "application/json");
}

private bool _disposedValue = false;
private bool _disposedValue;

protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (!_disposedValue)
{
Expand Down
20 changes: 14 additions & 6 deletions tests/Orion.Test/Integration/Setup/IntegrationTestsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public class IntegrationTestsFixture
public readonly User DefaultSystemUser;
public readonly IServiceProvider ServiceProvider;
private readonly IUnitOfWork _unitOfWork;
private readonly SqlConnection connection;
private readonly SqlConnection _sqlConnection;
private IConfiguration _configuration;
private WebApplicationFactory<Program> AppFactory;

public IntegrationTestsFixture()
{
Expand All @@ -44,24 +45,31 @@ public IntegrationTestsFixture()

DefaultSystemUser = UserFaker.GetDefaultSystemUser();

connection = new SqlConnection(_configuration["ConnectionStrings:OrionDatabase"]);
_sqlConnection = new SqlConnection(_configuration["ConnectionStrings:OrionDatabase"]);

AppFactory = appFactory;

BeforeEachTest();
}

public HttpClient GetNewHttpClient()
{
return AppFactory.CreateClient();
}

private void BeforeEachTest()
{
var tablesToTruncate = new[] { "User", "RefreshToken" };

lock (connection)
lock (_sqlConnection)
{
using (connection)
using (_sqlConnection)
{
connection.Open();
_sqlConnection.Open();

foreach (var table in tablesToTruncate)
{
var command = new SqlCommand($"TRUNCATE TABLE dbo.[{table}]", connection);
var command = new SqlCommand($"TRUNCATE TABLE dbo.[{table}]", _sqlConnection);

command.ExecuteNonQuery();
}
Expand Down

0 comments on commit dd50299

Please sign in to comment.