diff --git a/WebApi.Test/Client/Update/UpdateClientIntegrationTest.cs b/WebApi.Test/Client/Update/UpdateClientIntegrationTest.cs new file mode 100644 index 0000000..32eef69 --- /dev/null +++ b/WebApi.Test/Client/Update/UpdateClientIntegrationTest.cs @@ -0,0 +1,69 @@ +using Microsoft.AspNetCore.Mvc.Testing; +using Newtonsoft.Json; +using Shouldly; +using System.Net.Http.Headers; +using ClientEntity = ProductClientHub.Domain.Entities.Client; + +namespace WebApi.Test.Client.Update; + +public class UpdateClientIntegrationTest : IClassFixture +{ + private readonly CustomWebApplicationFactory _factory; + private readonly HttpClient _httpClient; + + public UpdateClientIntegrationTest(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 UpdateClientTest_Sucess() + { + var client = _factory.ClientsToReturn = + [ + new ClientEntity + { + Name = "Update Client", + Email = "updateclient@gmail.com" + } + ]; + + client[0].Id = Guid.NewGuid(); + + var response = await _httpClient.PutAsync($"/api/clients/{client[0].Id}", new StringContent(JsonConvert.SerializeObject(client[0]), System.Text.Encoding.UTF8, "application/json")); + + response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); + } + + [Fact] + public async Task UpdateClientTest_Error_EmailExist() + { + var client = _factory.ClientsToReturn = + [ + new ClientEntity + { + Name = "Update Client", + Email = "updateclient@gmail.com" + }, + + new ClientEntity + { + Name = "Update Client 2", + Email = "updateclient@gmail.com" + } + ]; + + client[0].Id = Guid.NewGuid(); + client[1].Id = Guid.NewGuid(); + + var response = await _httpClient.PutAsync($"/api/clients/{client[1].Id}", new StringContent(JsonConvert.SerializeObject(client[1]), System.Text.Encoding.UTF8, "application/json")); + + response.StatusCode.ShouldBe(System.Net.HttpStatusCode.BadRequest); + } +} diff --git a/WebApi.Test/CustomWebApplicationFactory.cs b/WebApi.Test/CustomWebApplicationFactory.cs index 9266e41..dacaa7e 100644 --- a/WebApi.Test/CustomWebApplicationFactory.cs +++ b/WebApi.Test/CustomWebApplicationFactory.cs @@ -4,9 +4,10 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; -using ClientEntity = ProductClientHub.Domain.Entities.Client; using ProductClientHub.Domain.Repositories.Client; +using ProductClientHub.Domain.Repositories.UnitOfWork; using ProductClientHub.Domain.Security.Tokens; +using ClientEntity = ProductClientHub.Domain.Entities.Client; namespace WebApi.Test; @@ -40,15 +41,19 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) services.AddSingleton(_clientStore); services.RemoveAll(); + services.RemoveAll(); services.RemoveAll(); services.RemoveAll(); + services.RemoveAll(); // Remove all hosted services to prevent background services from running during tests services.RemoveAll(typeof(IHostedService)); services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); }); } @@ -68,7 +73,10 @@ public FakeClientReadOnlyRepository(TestClientStore clientStore) } public Task EmailAlreadyExists(string email) - => Task.FromResult(null); + { + var client = _clientStore.Clients.FirstOrDefault(c => c.Email == email); + return Task.FromResult(client); + } public Task> GetAll() => Task.FromResult>(_clientStore.Clients); @@ -102,6 +110,38 @@ public Task Delete(Guid clientId) } } + private sealed class FakeClientWriteOnlyRepository : IClientWriteOnlyRepository + { + private readonly TestClientStore _clientStore; + + public FakeClientWriteOnlyRepository(TestClientStore clientStore) + { + _clientStore = clientStore; + } + + public Task Add(ClientEntity client) + { + _clientStore.Clients.Add(client); + return Task.CompletedTask; + } + + public Task Update(ClientEntity client) + { + var existingClient = _clientStore.Clients.FirstOrDefault(c => c.Id == client.Id); + if (existingClient != null) + { + existingClient.Name = client.Name; + existingClient.Email = client.Email; + } + return Task.FromResult(existingClient); + } + } + + private sealed class FakeUnitOfWork : IUnitOfWork + { + public Task Commit() => Task.CompletedTask; + } + private sealed class TestClientStore { public IList Clients { get; set; } = [];