Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions WebApi.Test/Client/Update/UpdateClientIntegrationTest.cs
Original file line number Diff line number Diff line change
@@ -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<CustomWebApplicationFactory>
{
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);
}
}
44 changes: 42 additions & 2 deletions WebApi.Test/CustomWebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -40,15 +41,19 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
services.AddSingleton(_clientStore);

services.RemoveAll<IClientReadOnlyRepository>();
services.RemoveAll<IClientWriteOnlyRepository>();
services.RemoveAll<IAccessTokenValidator>();
services.RemoveAll<IDeleteClientRepository>();
services.RemoveAll<IUnitOfWork>();

// Remove all hosted services to prevent background services from running during tests
services.RemoveAll(typeof(IHostedService));

services.AddScoped<IClientReadOnlyRepository, FakeClientReadOnlyRepository>();
services.AddScoped<IClientWriteOnlyRepository, FakeClientWriteOnlyRepository>();
services.AddScoped<IAccessTokenValidator, FakeAccessTokenValidator>();
services.AddScoped<IDeleteClientRepository, FakeDeleteClientRepository>();
services.AddScoped<IUnitOfWork, FakeUnitOfWork>();
});
}

Expand All @@ -68,7 +73,10 @@ public FakeClientReadOnlyRepository(TestClientStore clientStore)
}

public Task<ClientEntity?> EmailAlreadyExists(string email)
=> Task.FromResult<ClientEntity?>(null);
{
var client = _clientStore.Clients.FirstOrDefault(c => c.Email == email);
return Task.FromResult<ClientEntity?>(client);
}

public Task<IList<ClientEntity>> GetAll()
=> Task.FromResult<IList<ClientEntity>>(_clientStore.Clients);
Expand Down Expand Up @@ -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<ClientEntity?> 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<ClientEntity?>(existingClient);
}
}

private sealed class FakeUnitOfWork : IUnitOfWork
{
public Task Commit() => Task.CompletedTask;
}

private sealed class TestClientStore
{
public IList<ClientEntity> Clients { get; set; } = [];
Expand Down
Loading