PlotDirector/PlotLine.Tests/BookRecoveryTests.cs

40 lines
4.7 KiB
C#

using System.Text.Json;
using System.Text.Json.Nodes;
using PlotLine.Models;
using PlotLine.Services;
internal static class BookRecoveryTests
{
public static void ReadOnlyBackupAccess()=>ReadOnlyBackupAccessAsync().GetAwaiter().GetResult();
private static async Task ReadOnlyBackupAccessAsync(){
var user=NativeWorkflowTests.Proxy<ICurrentUserService>((n,a)=>n=="get_UserId"?(int?)1:throw new Exception(n));
var subscription=NativeWorkflowTests.Proxy<ISubscriptionService>((n,a)=>throw new Exception("Read-only backup/validation must not use the edit-content gate."));
var filter=new SubscriptionEnforcementFilter(user,subscription);
foreach(var action in new[]{"ReviewBackup","Download","Upload","Preview","Confirm"}){
var http=new Microsoft.AspNetCore.Http.DefaultHttpContext();http.Request.Method="POST";
var descriptor=new Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor{ActionName=action};
var context=new Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext(new Microsoft.AspNetCore.Mvc.ActionContext(http,new Microsoft.AspNetCore.Routing.RouteData(),descriptor),[],new Dictionary<string,object?>(),new PlotLine.Controllers.BookRecoveryController(null!,null!,null!,user));var called=false;
await filter.OnActionExecutionAsync(context,()=>{called=true;return Task.FromResult(new Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext(context,[],context.Controller));});if(!called)throw new Exception("Backup read-only access blocked.");
}
}
public static void Contracts()
{
var snapshot = new ProjectBackupExport { Project = new() { ProjectID = 1, ProjectName = "Fixture" }, Data = new() { Books = [new() { BookID = 1, ProjectID = 1, BookTitle = "First" }, new() { BookID = 2, ProjectID = 1, BookTitle = "Second", IsArchived = true }] } };
var package = new BookBackupPackage { SourceProjectKey = Guid.NewGuid(), Snapshot = BookBackupGraph.Select(snapshot, [2], Guid.NewGuid()), Identities = new() { ["Books:2"] = Guid.NewGuid().ToString("N") + "/Books/2" } };
package.Integrity = BookBackupGraph.Hash(new { package.SourceProjectKey, package.Snapshot, package.Identities, package.Warnings }); var json = JsonSerializer.Serialize(package);
if (BookRecoveryService.Read(json).Snapshot.Data.Books.Single().BookID != 2) throw new Exception("Selection included an unselected book.");
var store = new BookRecoveryUploads(); var old = store.Add(1, json); var latest = store.Add(1, json); try { store.Get(2, latest); throw new Exception("Foreign upload leaked."); } catch (UnauthorizedAccessException) { }
try { store.Get(1, old); throw new Exception("Superseded generation survived."); } catch (UnauthorizedAccessException) { }
store.Get(1, latest);
var firstGeneration=store.Begin(1);var newerGeneration=store.Begin(1);store.Add(1,json,"newer.plotdirector",newerGeneration);
try{store.Add(1,json,"earlier.plotdirector",firstGeneration);throw new Exception("Slow earlier server upload replaced the latest generation.");}catch(ArgumentException){}
if(store.Name(1,newerGeneration)!="newer.plotdirector")throw new Exception("Filename and upload generation diverged.");
var node = JsonNode.Parse(json)!; node["Snapshot"]!["Data"]!["Books"]![0]!["BookTitle"] = "Tampered"; try { BookRecoveryService.Read(node.ToJsonString()); throw new Exception("Invalid integrity accepted."); } catch (ArgumentException) { }
node = JsonNode.Parse(json)!; node["FormatVersion"] = "999"; try { BookRecoveryService.Read(node.ToJsonString()); throw new Exception("Future format accepted."); } catch (ArgumentException) { }
node = JsonNode.Parse(json)!; node["Snapshot"]!["Data"]!.AsObject().Remove("SceneChecklistItems"); try { BookRecoveryService.Read(node.ToJsonString()); throw new Exception("Incomplete current backup accepted."); } catch (ArgumentException) { }
try { BookRecoveryService.Read("{\"Format\":\"PlotDirector.BookBackup\",\"Format\":\"other\"}"); throw new Exception("Duplicate JSON fields accepted."); } catch (ArgumentException) { }
node=JsonNode.Parse(json)!;node["Snapshot"]!["Recovery"]!["Protected"]=true;try{BookRecoveryService.Read(node.ToJsonString());throw new Exception("Server-only protected state accepted as a portable backup.");}catch(ArgumentException){}
foreach(var malformed in new[]{"null","[]","{\"Format\":\"PlotDirector.BookBackup\",\"Snapshot\":null}","{\"Format\":42}"})try{BookRecoveryService.Read(malformed);throw new Exception("Malformed root accepted.");}catch(Exception e)when(e is ArgumentException or JsonException){}
try { BookBackupGraph.Select(snapshot, [404]); throw new Exception("Foreign selection accepted."); } catch (ArgumentException) { }
}
}