feat(CustomersControllerTests.cs): add integration tests for GET /api/customer/ endpoint

feat(CustomersControllerTests.cs): add integration tests for GET /api/customer/{id} endpoint
This commit is contained in:
Jean Gatto
2023-08-27 16:41:13 -03:00
parent 304e816a16
commit 7f6128e69d
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
@@ -41,6 +42,8 @@ public class CustomersControllerTests : IAsyncLifetime
private readonly SqliteConnection _eventStoreDbContextSqlite = new(ConnectionString);
private readonly SqliteConnection _writeDbContextSqlite = new(ConnectionString);
#region POST /api/customer/
[Fact]
public async Task Should_ReturnsHttpStatus200Ok_When_Post_ValidRequest()
{
@@ -160,6 +163,69 @@ public class CustomersControllerTests : IAsyncLifetime
.And.AllSatisfy(error => error.Message.Should().Be("The provided email address is already in use."));
}
#endregion
#region GET /api/customer/
[Fact]
public async Task Should_ReturnsHttpStatus200Ok_When_GetAll()
{
// Arrange
var queryModels = new Faker<CustomerQueryModel>()
.UsePrivateConstructor()
.RuleFor(queryModel => queryModel.Id, faker => faker.Random.Guid())
.RuleFor(queryModel => queryModel.FirstName, faker => faker.Person.FirstName)
.RuleFor(queryModel => queryModel.LastName, faker => faker.Person.LastName)
.RuleFor(queryModel => queryModel.Email, faker => faker.Person.Email)
.RuleFor(queryModel => queryModel.Gender, faker => faker.PickRandom<EGender>().ToString())
.RuleFor(queryModel => queryModel.DateOfBirth, faker => faker.Person.DateOfBirth)
.Generate(10);
var readOnlyRepository = Substitute.For<ICustomerReadOnlyRepository>();
readOnlyRepository.GetAllAsync().Returns(queryModels);
await using var webApplicationFactory = InitializeWebAppFactory(configureServices: (services) =>
{
services.RemoveAll<ICustomerReadOnlyRepository>();
services.AddScoped(_ => readOnlyRepository);
});
using var httpClient = webApplicationFactory.CreateClient(CreateClientOptions());
// Act
using var act = await httpClient.GetAsync(Endpoint);
// Assert (HTTP)
act.Should().NotBeNull();
act.IsSuccessStatusCode.Should().BeTrue();
act.StatusCode.Should().Be(HttpStatusCode.OK);
// Assert (HTTP Content Response)
var response = (await act.Content.ReadAsStringAsync()).FromJson<ApiResponse<IEnumerable<CustomerQueryModel>>>();
response.Should().NotBeNull();
response.Success.Should().BeTrue();
response.StatusCode.Should().Be(StatusCodes.Status200OK);
response.Errors.Should().BeEmpty();
response.Result.Should().NotBeNullOrEmpty()
.And.OnlyHaveUniqueItems()
.And.HaveCount(queryModels.Count)
.And.AllSatisfy(model =>
{
model.Id.Should().NotBeEmpty();
model.FirstName.Should().NotBeNullOrWhiteSpace();
model.LastName.Should().NotBeNullOrWhiteSpace();
model.Email.Should().NotBeNullOrWhiteSpace();
model.Gender.Should().NotBeNullOrWhiteSpace();
model.FullName.Should().NotBeNullOrWhiteSpace();
});
await readOnlyRepository.Received(1).GetAllAsync();
}
#endregion
#region GET /api/customer/{id}
[Fact]
public async Task Should_ReturnsHttpStatus200Ok_When_GetById_ValidRequest()
{
@@ -285,6 +351,8 @@ public class CustomersControllerTests : IAsyncLifetime
await readOnlyRepository.Received(1).GetByIdAsync(Arg.Is<Guid>(id => id == customerId));
}
#endregion
#region IAsyncLifetime
public async Task InitializeAsync()
@@ -301,6 +369,8 @@ public class CustomersControllerTests : IAsyncLifetime
#endregion
#region Helpers
private WebApplicationFactory<Program> InitializeWebAppFactory(
Action<IServiceCollection> configureServices = null,
Action<IServiceScope> configureServiceScope = null)
@@ -353,4 +423,6 @@ public class CustomersControllerTests : IAsyncLifetime
private static WebApplicationFactoryClientOptions CreateClientOptions() =>
new() { BaseAddress = new Uri("https://127.0.0.1"), AllowAutoRedirect = false };
#endregion
}