using System.Net; using System.Text.Json; using FluentAssertions; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace SlpModularCms.Api.AndreaSlopFotografie.Tests; /// Starts the real host in-memory (Development environment). public class AsfWebApplicationFactory : WebApplicationFactory { protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.UseEnvironment("Development"); } } /// /// Requires a reachable local MariaDB matching appsettings.Development.json of the host, /// because CmsHost.ConfigurePipeline migrates the database before any endpoint answers /// (same precondition as SlpModularCms.Api.Tests). /// public class HostPipelineTests : IClassFixture { private readonly AsfWebApplicationFactory _factory; public HostPipelineTests(AsfWebApplicationFactory factory) => _factory = factory; [Fact] public async Task Capabilities_ListIdentityAvailabilityAndOfferings_ButNotMaster() { var response = await _factory.CreateClient().GetAsync("/api/v1/System/capabilities"); response.StatusCode.Should().Be(HttpStatusCode.OK); using var doc = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); var modules = doc.RootElement.GetProperty("modules").EnumerateArray().Select(e => e.GetString()).ToList(); modules.Should().Contain("Identity").And.Contain("Availability").And.Contain("Offerings"); modules.Should().NotContain("Master"); } [Fact] public async Task OfferingBanner_Is204_WhenNoneIsActive_AndOfferingsAreListed_OnAMigratedDatabase() { var client = _factory.CreateClient(); var banner = await client.GetAsync("/api/v1/offerings/banner"); banner.StatusCode.Should().Be(HttpStatusCode.NoContent); (await client.GetAsync("/api/v1/offerings")).StatusCode.Should().Be(HttpStatusCode.OK); (await client.GetAsync("/api/v1/offerings/banners")).StatusCode.Should().Be(HttpStatusCode.Unauthorized); } [Fact] public void OfferingsMigrations_MatchTheModel() { _ = _factory.CreateClient(); // starts the host and migrates using var scope = _factory.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); db.Database.HasPendingModelChanges().Should().BeFalse("every model change needs its migration and snapshot"); } [Fact] public async Task Health_IsReachable() { (await _factory.CreateClient().GetAsync("/health")).IsSuccessStatusCode.Should().BeTrue(); } [Fact] public async Task SetupStatus_ReportsNoTokenRequired_WhenNoneConfigured() { var response = await _factory.CreateClient().GetAsync("/api/v1/Setup/status"); response.StatusCode.Should().Be(HttpStatusCode.OK); (await response.Content.ReadAsStringAsync()).Should().Contain("\"tokenRequired\":false"); } [Fact] public async Task UnknownWebsitePath_Returns404_WithTheSiteSecurityHeaders_AndNoIndex() { var response = await _factory.CreateClient().GetAsync("/some-page-that-does-not-exist"); response.StatusCode.Should().Be(HttpStatusCode.NotFound); response.Headers.GetValues("Content-Security-Policy").Single() .Should().Contain("https://analytics.slpsoftware.nl"); response.Headers.GetValues("X-Robots-Tag").Single().Should().Be("noindex, nofollow"); } [Fact] public async Task RobotsTxt_DisallowsEverything_OnTheDefaultNoIndexConfiguration() { var body = await _factory.CreateClient().GetStringAsync("/robots.txt"); body.Should().Contain("Disallow: /"); } }