using System.Net; using System.Net.Http.Json; using IntegrationTests.Helpers; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using WebMinRouteGroup.Data; using WebMinRouteGroup.Services; namespace IntegrationTests; [Collection("Sequential")] public class TodoEndpointsV2Tests : IClassFixture> { private readonly TestWebApplicationFactory _factory; private readonly HttpClient _httpClient; public TodoEndpointsV2Tests(TestWebApplicationFactory factory) { _factory = factory; _httpClient = factory.CreateClient(); } public static IEnumerable InvalidTodos => new List { new object[] { new TodoDto { Title = "", Description = "Test description", IsDone = false }, "Name is empty" }, new object[] { new TodoDto { Title = "no", Description = "Test description", IsDone = false }, "Name length < 3" } }; [Theory] [MemberData(nameof(InvalidTodos))] public async Task PostTodoWithValidationProblems(TodoDto todo, string errorMessage) { var response = await _httpClient.PostAsJsonAsync("/todos/v2", todo); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); var problemResult = await response.Content.ReadFromJsonAsync(); Assert.NotNull(problemResult?.Errors); Assert.Collection(problemResult.Errors, (error) => Assert.Equal(errorMessage, error.Value.First())); } [Fact] public async Task PostTodoWithValidParameters() { using (var scope = _factory.Services.CreateScope()) { var db = scope.ServiceProvider.GetService(); if (db != null && db.Todos.Any()) { db.Todos.RemoveRange(db.Todos); await db.SaveChangesAsync(); } } var client = _factory.WithWebHostBuilder(builder => { builder.ConfigureTestServices(services => { services.AddSingleton(); }); }).CreateClient(); var response = await client.PostAsJsonAsync("/todos/v2", new TodoDto { Title = "Test title", Description = "Test description", IsDone = false }); Assert.Equal(HttpStatusCode.Created, response.StatusCode); var todos = await client.GetFromJsonAsync>("/todos/v2"); Assert.NotNull(todos); Assert.Single(todos); Assert.Collection(todos, (todo) => { Assert.Equal("Test title", todo.Title); Assert.Equal("Test description", todo.Description); Assert.False(todo.IsDone); }); } }