61 lines
4.6 KiB
C#
61 lines
4.6 KiB
C#
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Configuration;
|
|
using PlotLine.Controllers;
|
|
using PlotLine.Data;
|
|
using PlotLine.Models.WritingSolver;
|
|
using PlotLine.Services;
|
|
using System.Security.Claims;
|
|
using System.Text.Json;
|
|
public static class WritingSolverCleanUrlTests
|
|
{
|
|
private sealed class Repository(SolverGraph graph):IWritingSolverRepository
|
|
{
|
|
public Task<SolverGraph> GetAsync()=>Task.FromResult(graph);
|
|
public Task PublishAsync(string json,string editor)=>throw new NotSupportedException();
|
|
}
|
|
public static void TransitionAndIsolation()
|
|
{
|
|
void Check(bool ok,string text) { if(!ok)throw new Exception(text); }
|
|
var graph=WritingSolverRepository.Parse(File.ReadAllText("PlotLine/Content/WritingSolver/demo.json"));
|
|
foreach(var page in graph.Solutions)page.Index=true;
|
|
var answers=new[]{new SolverAnswer("welcome",["story"]),new SolverAnswer("plot",["threads","crossing"]),new SolverAnswer("focus",["weave"])};
|
|
var provider=new EphemeralDataProtectionProvider();
|
|
using var cache=new MemoryCache(new MemoryCacheOptions());
|
|
var contexts=new WritingSolverResultContext(cache);
|
|
var config=new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string,string?>{["WritingSolver:EnableIndexing"]="true"}).Build();
|
|
WritingProblemsController Controller(DefaultHttpContext http)=>new(new Repository(graph),provider,config,contexts){ControllerContext=new(){HttpContext=http}};
|
|
var http=new DefaultHttpContext();var controller=Controller(http);
|
|
var state=provider.CreateProtector("WritingSolver.history.v1").ToTimeLimitedDataProtector().Protect(JsonSerializer.Serialize(new{Version=graph.Version,Answers=answers.Take(2)}),TimeSpan.FromHours(2));
|
|
var redirect=(RedirectToActionResult)controller.Answer(state,"focus",["weave"]).GetAwaiter().GetResult();
|
|
var slug=WritingSolverEngine.Evaluate(graph,answers).Solutions[0].Slug;
|
|
Check(redirect.ActionName=="Solution" && (string?)redirect.RouteValues!["slug"]==slug && !redirect.RouteValues.ContainsKey("state"),"Completion must redirect to clean slug.");
|
|
var cookie=http.Response.Headers.SetCookie.ToString().Split(';')[0];
|
|
Check(cookie.StartsWith("pd-solver-result=") && http.Response.Headers.SetCookie.ToString().Contains("httponly",StringComparison.OrdinalIgnoreCase),"Opaque HttpOnly cookie missing.");
|
|
DefaultHttpContext Visitor(){var h=new DefaultHttpContext();h.Request.Headers.Cookie=cookie;return h;}
|
|
foreach(var h in new[]{Visitor(),Visitor()})
|
|
{
|
|
var c=Controller(h);var page=(SolverPageView)((ViewResult)c.Solution(slug).GetAwaiter().GetResult()).Model!;
|
|
Check(page.Journey?.Outcome.Answers.Count==3 && c.ViewData["BackState"] is string,"Refresh must preserve evidence and Back history.");
|
|
Check(!h.Response.Headers.ContainsKey("X-Robots-Tag") && (string?)c.ViewData["Robots"]=="index, follow","Permanent page should be indexable when enabled.");
|
|
Check(h.Response.Headers.CacheControl.ToString()=="private, no-store","Personal pages must never enter shared caches.");
|
|
}
|
|
var fresh=(SolverPageView)((ViewResult)Controller(new()).Solution(slug).GetAwaiter().GetResult()).Model!;
|
|
Check(fresh.Journey is null,"Shared fresh visit leaked evidence.");
|
|
var signed=Visitor();signed.User=new(new ClaimsIdentity(new[]{new Claim(ClaimTypes.NameIdentifier,"other-user")},"test"));
|
|
Check(contexts.Read(signed,graph,slug) is null,"Different identity inherited anonymous evidence.");
|
|
contexts.Save(signed,graph,WritingSolverEngine.Evaluate(graph,answers),slug);
|
|
Check(contexts.Read(signed,graph,slug) is not null,"Signed-in owner lost context.");
|
|
signed.User=new(new ClaimsIdentity(new[]{new Claim(ClaimTypes.NameIdentifier,"another-user")},"test"));
|
|
Check(contexts.Read(signed,graph,slug) is null,"Different signed-in subject inherited evidence.");
|
|
cache.Compact(1);Check(contexts.Read(Visitor(),graph,slug) is null,"Expired/evicted context must fall back safely.");
|
|
http.Request.Headers.Cookie=cookie;
|
|
contexts.Save(http,graph,WritingSolverEngine.Evaluate(graph,answers),slug);
|
|
graph.Version+="changed";Check(contexts.Read(Visitor(),graph,slug) is null,"Old graph context must expire safely.");
|
|
var transient=Controller(new());transient.Index(state).GetAwaiter().GetResult();
|
|
Check(transient.Response.Headers["X-Robots-Tag"].ToString().Contains("noindex"),"Journey must stay noindex.");
|
|
}
|
|
}
|