using System.Security.Claims; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using PlotLine.Controllers; using PlotLine.Services; internal static class ApplicationDomainBoundaryTests { public static void Run() { using var services=new ServiceCollection().AddSingleton(new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["Site:WebsiteBaseUrl"]="https://plotdirector.com" }).Build()).BuildServiceProvider(); var builder=new ApplicationBuilder(services);builder.UseApplicationDomainBoundary();builder.Run(c=>{c.Response.StatusCode=204;return Task.CompletedTask;});var pipeline=builder.Build(); foreach(var path in new[]{"/features","/features/timeline","/problem-solver","/privacy","/pricing"}) { var c=new DefaultHttpContext{RequestServices=services};c.Request.Path=path;c.Request.QueryString=new("?from=test");pipeline(c).GetAwaiter().GetResult(); Check(c.Response.StatusCode==308 && c.Response.Headers.Location=="https://plotdirector.com"+path+"?from=test","Public route redirect"); Check(c.Response.Headers["X-Robots-Tag"]=="noindex, nofollow","Application noindex"); } foreach(var path in new[]{"/Projects","/Pricing/Checkout","/help/getting-started/first-book","/word-companion","/api/native-companion","/hubs/word-companion-follow","/problem-solver/workspace/characters"}) {var c=new DefaultHttpContext{RequestServices=services};c.Request.Path=path;pipeline(c).GetAwaiter().GetResult();Check(c.Response.StatusCode==204,"Preserve application route "+path);} Check(new HomeController().Index() is RedirectToActionResult {ControllerName:"Projects",ActionName:"Index"},"Root lands on Projects"); var http=new DefaultHttpContext();http.User=new ClaimsPrincipal(new ClaimsIdentity(new[]{new Claim(ClaimTypes.NameIdentifier,"1")},"Test")); var controller=new AccountController(null!){ControllerContext=new(new ActionContext(http,new Microsoft.AspNetCore.Routing.RouteData(),new Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor()))};controller.Url=new UrlHelper(controller.ControllerContext); Check(controller.Login() is RedirectToActionResult {ControllerName:"Projects"},"Authenticated sign in opens Projects"); Check(controller.Login("/Books/Details/123") is LocalRedirectResult {Url:"/Books/Details/123"},"Safe deep link survives"); foreach(var url in new[]{"https://example.com/","//example.com/","/\\example.com/"})Check(controller.Login(url) is RedirectToActionResult {ControllerName:"Projects"},"Reject open redirect"); var publication = Path.Combine(Path.GetTempPath(), "pd-help-publication-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(publication); try { File.WriteAllText(Path.Combine(publication, "manifest.json"), "{\"format\":2,\"audience\":\"application\",\"version\":\"first\"}"); var article = new PlotLine.Models.HelpArticle { Visibility="public", Title="Approved help",Category="Projects",CategorySlug="projects",Slug="overview",ContextKey="projects.overview",MarkdownContent="# Approved" }; File.WriteAllText(Path.Combine(publication,"articles.json"),System.Text.Json.JsonSerializer.Serialize(new[]{article})); var contentConfig=new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["HelpContent:Path"]=publication }).Build(); var help=new HelpService(null!,contentConfig); Check(help.GetByContextKey("projects.overview")?.Title=="Approved help", "Application consumes approved help artifact"); article.Title="Updated approved help"; File.WriteAllText(Path.Combine(publication,"articles.json"),System.Text.Json.JsonSerializer.Serialize(new[]{article})); File.WriteAllText(Path.Combine(publication,"manifest.json"),"{\"format\":2,\"audience\":\"application\",\"version\":\"second\"}"); Check(help.GetBySlugs("projects","overview")?.Title=="Updated approved help", "Application help publication hot reload preserves identifiers"); } finally { Directory.Delete(publication,true); } var queue = new EmailCapture(); http.Request.Host = new HostString("app.plotdirector.com"); var email = new EmailService(Microsoft.Extensions.Options.Options.Create(new PlotLine.Models.EmailSettings()), services.GetRequiredService(), queue, new HttpContextAccessor { HttpContext = http }, Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance); email.SendVerificationEmailAsync("domain-fixture@example.invalid", "Synthetic", Guid.NewGuid()).GetAwaiter().GetResult(); email.SendPasswordResetEmailAsync("domain-fixture@example.invalid", "Synthetic", Guid.NewGuid()).GetAwaiter().GetResult(); Check(queue.Items.Count == 2 && queue.Items[0].PlainTextBody!.Contains("https://app.plotdirector.com/Account/VerifyEmail?") && queue.Items[1].PlainTextBody!.Contains("https://app.plotdirector.com/Account/ResetPassword?"), "Verification/reset emails use app origin"); Console.WriteLine("PASS Application domain boundary, Projects landing, authenticated sign in and safe deep links"); } private sealed class EmailCapture : PlotLine.Data.IEmailQueueRepository { public List Items {get;}=[]; public Task CreateAsync(PlotLine.Models.EmailQueueItem item){Items.Add(item);return Task.FromResult((long)Items.Count);} public Task> DequeueBatchAsync(int size)=>throw new NotSupportedException(); public Task MarkSentAsync(long id)=>throw new NotSupportedException(); public Task MarkFailedAsync(long id,string error)=>throw new NotSupportedException(); public Task ResetStuckSendingAsync()=>throw new NotSupportedException(); } private static void Check(bool ok,string message){if(!ok)throw new InvalidOperationException(message);} }