using FluentAssertions; using Microsoft.Extensions.Configuration; namespace SlpModularCms.Api.Tests; /// /// What every site (client project) must look like, checked from its files alone: the modules it references and /// the defaults it commits. The sites carry no logic of their own (all behaviour lives in Core and the modules and /// is tested against the master SlpModularCms.Api host), so a site can only go wrong in its composition: /// a module reference that is missing or too much, or an unsafe default. These tests need no database. /// public class SiteContractTests { public static IEnumerable Sites() => [ [SiteFiles.SlpSoftware], [SiteFiles.AndreaSlopFotografie], ]; [Fact] public void TheMasterCms_ReferencesEveryModule() { SiteFiles.Modules("SlpModularCms.Api").Should().BeEquivalentTo( ["Availability", "Contact", "Hero", "Identity", "Master", "Offerings"], "the master Api is the all-modules composition the CI builds and tests"); } [Fact] public void SlpSoftware_IsTheFullComposition() { SiteFiles.Modules(SiteFiles.SlpSoftware).Should().BeEquivalentTo( ["Availability", "Contact", "Hero", "Identity", "Master", "Offerings"]); } [Fact] public void AndreaSlopFotografie_HasEverythingExceptTheMasterModule() { // Not a master/slave setup: no Master module, so /System/capabilities does not list it. SiteFiles.Modules(SiteFiles.AndreaSlopFotografie).Should().BeEquivalentTo( ["Availability", "Contact", "Hero", "Identity", "Offerings"]); } [Theory] [MemberData(nameof(Sites))] public void NoSiteReferencesAModuleTheMasterDoesNot(string site) { SiteFiles.Modules(site).Should().BeSubsetOf(SiteFiles.Modules("SlpModularCms.Api"), "a module that the CI does not build and test must not ship in a site"); } [Theory] [InlineData(SiteFiles.SlpSoftware, "SpaFallback")] [InlineData(SiteFiles.AndreaSlopFotografie, "PrerenderedPages")] public void EachSite_ServesItsWebsiteTheRightWay(string site, string mode) { // Missing means SpaFallback (the host's default); Andrea's prerendered site must say so explicitly, // otherwise it would start as an SPA and answer 200 for every unknown path. var configured = SiteFiles.Committed(site)["Website:Mode"]; (configured ?? "SpaFallback").Should().Be(mode); } [Theory] [InlineData(SiteFiles.SlpSoftware, true)] [InlineData(SiteFiles.AndreaSlopFotografie, false)] public void TheHeroIsWrittenAsTextOnlyForTheSiteWithoutHeroImages(string site, bool textOnly) { // SLP Software's hero has no images, so the host writes it as text; ASF's baked hero is a picture and stays when the CMS hero has no slide. SiteFiles.Committed(site).GetValue("Hero:TextOnlyRegion").Should().Be(textOnly); } [Theory] [InlineData(SiteFiles.SlpSoftware, "SLP Software: ")] [InlineData(SiteFiles.AndreaSlopFotografie, "")] public void TheHeroTitlePrefix_IsSetForSlpSoftware_AndEmptyForAsf(string site, string prefix) { (SiteFiles.Committed(site)["Hero:TitlePrefix"] ?? string.Empty).Should().Be(prefix); } [Theory] [MemberData(nameof(Sites))] public void WebsiteIsNoIndex_ByDefault(string site) { SiteFiles.Committed(site).GetValue("Website:NoIndex").Should().BeTrue( "indexing is switched on per environment, never by the committed defaults"); } [Theory] [MemberData(nameof(Sites))] public void NoSecretsAreCommitted(string site) { var config = SiteFiles.Committed(site); config["JwtSettings:Secret"].Should().StartWith("<"); config["ConnectionStrings:DefaultConnection"].Should().Contain(""); config["Identity:Setup:Token"].Should().BeNullOrEmpty(); config["Observability:SentryDsn"].Should().BeNullOrEmpty(); config.GetSection("ForwardedHeaders:KnownProxies").Get().Should().BeNullOrEmpty( "the proxy address is environment configuration"); } [Theory] [InlineData(SiteFiles.SlpSoftware, "slpsoftware")] [InlineData(SiteFiles.AndreaSlopFotografie, "andreaslop")] public void Contact_IsOffByDefault_ForTheRightSite_WithNoRecipientOrCaptchaSecretCommitted(string site, string siteId) { var config = SiteFiles.Committed(site); config.GetValue("Contact:Enabled").Should().BeFalse(); config["Contact:SiteId"].Should().Be(siteId); config.GetValue("Contact:Captcha:Enabled").Should().BeFalse(); config["Contact:RecipientEmail"].Should().BeNullOrEmpty("the address comes from the environment only"); config["Contact:Captcha:Secret"].Should().BeNullOrEmpty(); config["Contact:Captcha:SiteKey"].Should().BeNullOrEmpty(); } [Theory] [MemberData(nameof(Sites))] public void Mail_IsOffByDefault_AndNoCredentialIsCommitted(string site) { var config = SiteFiles.Committed(site); config["Email:Smtp:Host"].Should().BeNullOrEmpty("mail is switched on per environment"); config["Email:Smtp:Username"].Should().BeNullOrEmpty(); config["Email:Smtp:Password"].Should().BeNullOrEmpty(); config["Email:FromAddress"].Should().NotBeNullOrEmpty(); } [Fact] public void AndreaSlopFotografie_DefaultPolicyIsStrictSite_AndAdminApiHealthStayStrict() { var config = SiteFiles.Committed(SiteFiles.AndreaSlopFotografie); config["SecurityHeaders:DefaultPolicy"].Should().Be("StrictSite"); var rules = config.GetSection("SecurityHeaders:PathPolicies").GetChildren() .ToDictionary(c => c["PathPrefix"]!, c => c["Policy"]!); rules.Should().Contain(new KeyValuePair("/admin", "Strict")) .And.Contain(new KeyValuePair("/api/v1", "Strict")) .And.Contain(new KeyValuePair("/health", "Strict")); } [Fact] public void AndreaSlopFotografie_SiteOrigins_AreHttpsOnly_AndNotAddedToTheAdminLists() { var config = SiteFiles.Committed(SiteFiles.AndreaSlopFotografie); var site = config.GetSection("SecurityHeaders:AllowedSiteScriptOrigins").Get()! .Concat(config.GetSection("SecurityHeaders:AllowedSiteConnectOrigins").Get()!); site.Should().NotBeEmpty().And.OnlyContain(o => o.StartsWith("https://")); config.GetSection("SecurityHeaders:AllowedScriptOrigins").Get().Should().BeEmpty(); config.GetSection("SecurityHeaders:AllowedConnectOrigins").Get().Should().BeEmpty(); } [Fact] public void AndreaSlopFotografie_WritesItsProductionAddressInTheBuild_AndHasNoPublicAddressByDefault() { // The build is the same on test and production: the host swaps the authored address for the instance's own. var config = SiteFiles.Committed(SiteFiles.AndreaSlopFotografie); config["Website:AuthoredBaseUrl"].Should().Be("https://andreaslop.nl"); config["Website:PublicBaseUrl"].Should().BeNullOrEmpty("the instance's own address is environment configuration"); } [Fact] public void AndreaSlopFotografie_MailBrandingPointsAtTheAdminAssets() { var config = SiteFiles.Committed(SiteFiles.AndreaSlopFotografie); config["Email:LogoUrl"].Should().Be("/admin/brand-logo-mail.png"); config["Email:LogoDarkUrl"].Should().Be("/admin/brand-logo-on-dark.png"); } [Theory] [MemberData(nameof(Sites))] public void EachSiteProgram_IsAThinComposition(string site) { // The site's Program.cs may only compose: no mode, module or option decisions in code. var program = File.ReadAllText(Path.Combine(SiteFiles.SourceRoot(), site, "Program.cs")); program.Should().Contain("CmsHost.ConfigureServices").And.Contain("CmsHost.ConfigurePipeline"); program.Should().NotContain("WebsiteMode.", "the mode is chosen with Website:Mode in appsettings.json"); } }