using Microsoft.AspNetCore.Mvc; using PlotLine.Controllers; using PlotLine.Models; using PlotLine.Services; internal static class MapTests { static void Check(bool value, string message) { if (!value) throw new InvalidOperationException(message); } public static void ValidationAndAccess() { var a = new MapFeature { Name = "First town", LocationID = 42 }; var b = new MapFeature { Name = "Other town", X = 30, Y = -20 }; var d = new MapDocument { Map = new() { ProjectID = 7 }, Features = [a, b], Relationships = [new() { SourceID = a.FeatureID, TargetID = b.FeatureID, Direction = "N", RelativeDistance = "Far", MeasuredDistance = 126, Unit = "kilometres", TypicalTravelTime = "two hours" }] }; Check(MapValidation.Validate(d) is null, "Rough geography with conflicting visual scale must be valid."); d.Relationships.Add(new() { SourceID = b.FeatureID, TargetID = a.FeatureID, Direction = "N", RelativeDistance = "Close" }); Check(MapValidation.Validate(d) is null, "Contradictory intentions must remain saveable."); d.Relationships[0].TargetID = Guid.NewGuid(); Check(MapValidation.Validate(d) is not null, "Cross-map relationships must fail."); d.Relationships[0].TargetID = b.FeatureID; b.GeometryClass = "Path"; b.StartFeatureID = Guid.NewGuid(); Check(MapValidation.Validate(d) is not null, "Unknown route endpoints must fail."); b.StartFeatureID = a.FeatureID; Check(MapValidation.Validate(d) is null, "A route may attach to a marker."); b.GeometryJson = "{\"points\":[[true,0],[0,1]]}"; Check(MapValidation.Validate(d) is not null, "Non-numeric shape points must fail gracefully."); b.GeometryJson = "{}"; b.Width = double.NaN; Check(MapValidation.Validate(d) is not null, "Non-finite geometry must fail."); b.Width = 20; b.CreateLocation = true; Check(MapValidation.Validate(d) is not null, "Provisional geography must not silently become canonical Locations."); b.IsProvisional = false; Check(MapValidation.Validate(d) is null, "Explicit Location creation is supported."); b.FeatureID = a.FeatureID; Check(MapValidation.Validate(d) is not null, "Duplicate feature identifiers must fail."); // A denied project must short-circuit every route before any repository can be touched. var controller = new MapsController(null!, null!, null!, null!, new DeniedAccess(), null!); Check(controller.Index(7, 1).Result is NotFoundResult, "Index must enforce ownership/collaboration access."); Check(controller.Index(7, 1, detached: true).Result is NotFoundResult, "Detached Maps must use the same project-access gate."); Check(controller.State(7, 1).Result is NotFoundResult, "State must enforce access."); Check(controller.Create(new() { ProjectID = 7 }).Result is NotFoundResult, "Create must enforce access."); Check(controller.Save(d).Result is NotFoundResult, "Save must enforce access."); Check(controller.Delete(7, 1, 0).Result is NotFoundResult, "Delete must enforce access."); var posts = typeof(MapsController).GetMethods().Where(m => m.GetCustomAttributes(typeof(HttpPostAttribute), true).Length > 0); Check(posts.Count() == 6 && posts.All(m => m.GetCustomAttributes(typeof(ValidateAntiForgeryTokenAttribute), true).Length > 0), "Every mutation needs antiforgery and the shared subscription enforcement filter."); } private sealed class DeniedAccess : IProjectAccessService { public Task CanAccessProjectAsync(int id) => Task.FromResult(false); public Task CanOwnProjectAsync(int id) => Task.FromResult(false); public Task CanAccessEntityAsync(string type, int id) => Task.FromResult(false); } }