Files

103 lines
3.6 KiB
C#
Raw Permalink Normal View History

2024-08-17 16:38:40 +02:00
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System.Net;
using System.Net.Http.Json;
using Vegasco.Server.Api.Cars;
using Vegasco.Server.Api.Persistence;
2024-08-17 16:38:40 +02:00
namespace Vegasco.Server.Api.Tests.Integration.Cars;
2024-08-17 16:38:40 +02:00
[Collection(SharedTestCollection.Name)]
public class UpdateCarTests : IAsyncLifetime
{
private readonly WebAppFactory _factory;
private readonly IServiceScope _scope;
private readonly ApplicationDbContext _dbContext;
private readonly CarFaker _carFaker = new();
public UpdateCarTests(WebAppFactory factory)
{
_factory = factory;
_scope = _factory.Services.CreateScope();
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
}
[Fact]
public async Task UpdateCar_ShouldUpdateCar_WhenCarExistsAndRequestIsValid()
{
// Arrange
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
2024-08-17 16:38:40 +02:00
createCarResponse.EnsureSuccessStatusCode();
2025-06-24 19:28:55 +02:00
CreateCar.Response? createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
2024-08-17 16:38:40 +02:00
UpdateCar.Request updateCarRequest = _carFaker.UpdateCarRequest();
2024-08-17 16:38:40 +02:00
// Act
HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/cars/{createdCar!.Id}", updateCarRequest);
2024-08-17 16:38:40 +02:00
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
2025-06-24 19:28:55 +02:00
CreateCar.Response? updatedCar = await response.Content.ReadFromJsonAsync<CreateCar.Response>();
2024-08-17 16:38:40 +02:00
updatedCar!.Id.Should().Be(createdCar.Id);
updatedCar.Should().BeEquivalentTo(updateCarRequest, o => o.ExcludingMissingMembers());
_dbContext.Cars.Should().ContainEquivalentOf(updatedCar, o =>
o.ExcludingMissingMembers()
.Excluding(x => x.Id))
.Which.Id.Value.Should().Be(updatedCar.Id);
2024-08-17 16:38:40 +02:00
}
[Fact]
public async Task UpdateCar_ShouldReturnValidationProblems_WhenRequestIsNotValid()
{
// Arrange
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
2024-08-17 16:38:40 +02:00
createCarResponse.EnsureSuccessStatusCode();
2025-06-24 19:28:55 +02:00
CreateCar.Response? createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
2024-08-17 16:38:40 +02:00
2025-06-24 19:28:55 +02:00
UpdateCar.Request updateCarRequest = new UpdateCar.Request("");
2024-08-17 16:38:40 +02:00
// Act
HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/cars/{createdCar!.Id}", updateCarRequest);
2024-08-17 16:38:40 +02:00
// Assert
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
2025-06-24 19:28:55 +02:00
ValidationProblemDetails? validationProblemDetails = await response.Content.ReadFromJsonAsync<ValidationProblemDetails>();
2024-08-17 16:38:40 +02:00
validationProblemDetails!.Errors.Keys.Should().Contain(x =>
x.Equals(nameof(CreateCar.Request.Name), StringComparison.OrdinalIgnoreCase));
_dbContext.Cars.Should().ContainSingle(x => x.Id.Value == createdCar.Id)
2024-08-17 16:38:40 +02:00
.Which
.Should().NotBeEquivalentTo(updateCarRequest, o => o.ExcludingMissingMembers());
}
[Fact]
public async Task UpdateCar_ShouldReturnNotFound_WhenNoCarWithIdExists()
{
// Arrange
UpdateCar.Request updateCarRequest = _carFaker.UpdateCarRequest();
2025-06-24 19:28:55 +02:00
Guid randomCarId = Guid.NewGuid();
2024-08-17 16:38:40 +02:00
// Act
HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/cars/{randomCarId}", updateCarRequest);
2024-08-17 16:38:40 +02:00
// Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
_dbContext.Cars.Should().BeEmpty();
}
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
_scope.Dispose();
await _dbContext.DisposeAsync();
await _factory.ResetDatabaseAsync();
}
}