diff --git a/CommonTestUtilities/Entities/ClientBuilder.cs b/CommonTestUtilities/Entities/ClientBuilder.cs index ba54dd8..96df670 100644 --- a/CommonTestUtilities/Entities/ClientBuilder.cs +++ b/CommonTestUtilities/Entities/ClientBuilder.cs @@ -13,7 +13,7 @@ public static (Client client, string password) Build() var password = new Faker().Internet.Password(); var client = new Faker() - .RuleFor(client => client.Id, () => new Guid()) + .RuleFor(client => client.Id, () => Guid.NewGuid()) .RuleFor(client => client.Name, (f) => f.Person.FirstName) .RuleFor(client => client.Email, (f, user) => f.Internet.Email(user.Name)) .RuleFor(client => client.Password, (f) => passwordEncripter.Encrypt(password)); diff --git a/UseCase.Test/Client/Update/UpdateClientUseCaseTest.cs b/UseCase.Test/Client/Update/UpdateClientUseCaseTest.cs index b362745..1aa8e57 100644 --- a/UseCase.Test/Client/Update/UpdateClientUseCaseTest.cs +++ b/UseCase.Test/Client/Update/UpdateClientUseCaseTest.cs @@ -72,7 +72,12 @@ private static UpdateClientUseCase CreateUseCase(ProductClientHub.Domain.Entitie clientReadOnlyRepository.GetById(client); if(emailExistsTest.IsTrue()) - clientReadOnlyRepository.EmailAlreadyExists(client); + { + var (clientWithSameEmail, _) = ClientBuilder.Build(); + clientWithSameEmail.Email = client!.Email; + + clientReadOnlyRepository.EmailAlreadyExists(clientWithSameEmail); + } return new UpdateClientUseCase(clientWriteOnlyRepository, clientReadOnlyRepository.Build(), unitOfWork, loggedUser); } diff --git a/WebApi.Test/Client/ChangePassword/ChangePasswordClientIntegrationTest.cs b/WebApi.Test/Client/ChangePassword/ChangePasswordClientIntegrationTest.cs new file mode 100644 index 0000000..5ed2732 --- /dev/null +++ b/WebApi.Test/Client/ChangePassword/ChangePasswordClientIntegrationTest.cs @@ -0,0 +1,91 @@ +using Microsoft.AspNetCore.Mvc.Testing; +using CommonTestUtilities.Cryptografhy; +using ProductClientHub.Communication.Requests; +using Shouldly; +using System.Net; +using System.Net.Http.Headers; +using System.Text; +using Newtonsoft.Json; +using ClientEntity = ProductClientHub.Domain.Entities.Client; + +namespace WebApi.Test.Client.ChangePassword; + +public class ChangePasswordClientIntegrationTest : IClassFixture +{ + private readonly CustomWebApplicationFactory _factory; + private readonly HttpClient _httpClient; + + public ChangePasswordClientIntegrationTest(CustomWebApplicationFactory factory) + { + _factory = factory; + _httpClient = factory.CreateClient(new WebApplicationFactoryClientOptions + { + BaseAddress = new Uri("https://localhost") + }); + + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "fake-token"); + } + + [Fact] + public async Task ChangePassword_ShouldReturnNoContent_WhenRequestIsValid() + { + var clientId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"); + var passwordEncripter = PasswordEncripterBuilder.Build(); + + _factory.ClientsToReturn = + [ + new ClientEntity + { + Id = clientId, + Name = "Client 1", + Email = "client1@email.com", + Password = passwordEncripter.Encrypt("password123") + } + ]; + + var request = new RequestChangePassword + { + CurrentPassword = "password123", + NewPassword = "newPassword123" + }; + + var response = await _httpClient.PostAsync( + $"/api/clients/changePassword/{clientId}", + new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json")); + + response.StatusCode.ShouldBe(HttpStatusCode.NoContent); + } + + [Fact] + public async Task ChangePassword_ShouldReturnBadRequest_WhenCurrentPasswordIsInvalid() + { + var clientId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"); + var passwordEncripter = PasswordEncripterBuilder.Build(); + + _factory.ClientsToReturn = + [ + new ClientEntity + { + Id = clientId, + Name = "Client 1", + Email = "client1@email.com", + Password = passwordEncripter.Encrypt("password123") + } + ]; + + var request = new RequestChangePassword + { + CurrentPassword = "wrong-password", + NewPassword = "newPassword123" + }; + + var response = await _httpClient.PostAsync( + $"/api/clients/changePassword/{clientId}", + new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json")); + + response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + + var body = await response.Content.ReadAsStringAsync(); + body.ShouldNotBeNullOrWhiteSpace(); + } +} diff --git a/WebApi.Test/Client/Delete/DeleteClientIntegrationTest.cs b/WebApi.Test/Client/Delete/DeleteClientIntegrationTest.cs index 53667da..5a2c43e 100644 --- a/WebApi.Test/Client/Delete/DeleteClientIntegrationTest.cs +++ b/WebApi.Test/Client/Delete/DeleteClientIntegrationTest.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.Mvc.Testing; using Shouldly; +using System.Net; using System.Net.Http.Headers; +using ClientEntity = ProductClientHub.Domain.Entities.Client; namespace WebApi.Test.Client.Delete; @@ -22,23 +24,38 @@ public DeleteClientIntegrationTest(CustomWebApplicationFactory factory) [Fact] - public async Task Delete_ShouldReturnNoContent_WhenClientIsDeleted() + public async Task DeleteClient_ShouldReturnNoContent_WhenClientExists() { var clientId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"); _factory.ClientsToReturn = [ - new ProductClientHub.Domain.Entities.Client + new ClientEntity { Id = clientId, Name = "Client 1", Email = "oioi@gmail.com", Password = "password123", - Products = new List() + Products = [] } ]; var response = await _httpClient.DeleteAsync($"/api/clients/{clientId}"); - response.StatusCode.ShouldBe(System.Net.HttpStatusCode.NoContent); + + response.StatusCode.ShouldBe(HttpStatusCode.NoContent); + } + + [Fact] + public async Task DeleteClient_ShouldReturnNotFound_WhenClientDoesNotExist() + { + var nonExistentClientId = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"); + _factory.ClientsToReturn = []; + + var response = await _httpClient.DeleteAsync($"/api/clients/{nonExistentClientId}"); + + response.StatusCode.ShouldBe(HttpStatusCode.NotFound); + + var body = await response.Content.ReadAsStringAsync(); + body.ShouldNotBeNullOrWhiteSpace(); } } \ No newline at end of file diff --git a/WebApi.Test/Client/Register/RegisterClientIntegrationTest.cs b/WebApi.Test/Client/Register/RegisterClientIntegrationTest.cs new file mode 100644 index 0000000..96beda3 --- /dev/null +++ b/WebApi.Test/Client/Register/RegisterClientIntegrationTest.cs @@ -0,0 +1,80 @@ +using Microsoft.AspNetCore.Mvc.Testing; +using ProductClientHub.Communication.Requests; +using ProductClientHub.Communication.Responses; +using Newtonsoft.Json; +using Shouldly; +using System.Net; +using System.Net.Http.Json; +using System.Text; +using ClientEntity = ProductClientHub.Domain.Entities.Client; + +namespace WebApi.Test.Client.Register; + +public class RegisterClientIntegrationTest : IClassFixture +{ + private readonly CustomWebApplicationFactory _factory; + private readonly HttpClient _httpClient; + + public RegisterClientIntegrationTest(CustomWebApplicationFactory factory) + { + _factory = factory; + _httpClient = factory.CreateClient(new WebApplicationFactoryClientOptions + { + BaseAddress = new Uri("https://localhost") + }); + } + + [Fact] + public async Task RegisterClient_ShouldReturnCreated_WhenRequestIsValid() + { + _factory.ClientsToReturn = []; + + var request = new RequestClientJson + { + Name = "Client Register", + Email = "client.register@email.com", + Password = "password123" + }; + + var response = await _httpClient.PostAsync( + "/api/register", + new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json")); + + response.StatusCode.ShouldBe(HttpStatusCode.Created); + + var body = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + body.ShouldNotBeNull(); + body!.Name.ShouldBe(request.Name); + } + + [Fact] + public async Task RegisterClient_ShouldReturnBadRequest_WhenEmailAlreadyExists() + { + _factory.ClientsToReturn = + [ + new ClientEntity + { + Id = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), + Name = "Existing Client", + Email = "existing@email.com", + Password = "password123" + } + ]; + + var request = new RequestClientJson + { + Name = "New Client", + Email = "existing@email.com", + Password = "password123" + }; + + var response = await _httpClient.PostAsync( + "/api/register", + new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json")); + + response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); + + var body = await response.Content.ReadAsStringAsync(); + body.ShouldNotBeNullOrWhiteSpace(); + } +}