2024-08-17 16:38:40 +02:00
|
|
|
|
using FluentAssertions;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
|
using Vegasco.WebApi.Cars;
|
|
|
|
|
|
using Vegasco.WebApi.Persistence;
|
|
|
|
|
|
|
|
|
|
|
|
namespace WebApi.Tests.Integration.Cars;
|
|
|
|
|
|
|
|
|
|
|
|
[Collection(SharedTestCollection.Name)]
|
|
|
|
|
|
public class DeleteCarTests : IAsyncLifetime
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly WebAppFactory _factory;
|
|
|
|
|
|
private readonly IServiceScope _scope;
|
|
|
|
|
|
private readonly ApplicationDbContext _dbContext;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly CarFaker _carFaker = new();
|
|
|
|
|
|
|
|
|
|
|
|
public DeleteCarTests(WebAppFactory factory)
|
|
|
|
|
|
{
|
|
|
|
|
|
_factory = factory;
|
|
|
|
|
|
_scope = _factory.Services.CreateScope();
|
|
|
|
|
|
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task DeleteCar_ShouldReturnNotFound_WhenCarDoesNotExist()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
var randomCarId = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
2025-06-12 17:43:22 +02:00
|
|
|
|
HttpResponseMessage response = await _factory.HttpClient.DeleteAsync($"v1/cars/{randomCarId}");
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task DeleteCar_ShouldDeleteCar_WhenCarExists()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
2025-06-12 17:43:22 +02:00
|
|
|
|
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
|
|
|
|
|
|
HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
createCarResponse.EnsureSuccessStatusCode();
|
|
|
|
|
|
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
2025-06-12 17:43:22 +02:00
|
|
|
|
HttpResponseMessage response = await _factory.HttpClient.DeleteAsync($"v1/cars/{createdCar!.Id}");
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
|
|
|
|
|
|
_dbContext.Cars.Should().BeEmpty();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task InitializeAsync() => Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DisposeAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
_scope.Dispose();
|
|
|
|
|
|
await _dbContext.DisposeAsync();
|
|
|
|
|
|
await _factory.ResetDatabaseAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|