73 lines
4.1 KiB
C#
73 lines
4.1 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using PlotLine.Services;
|
|
|
|
internal static class ProjectAccessRegressionTests
|
|
{
|
|
private sealed class User(int? id) : ICurrentUserService
|
|
{
|
|
public int? UserId => id;
|
|
public string? Email => null;
|
|
public string? DisplayName => null;
|
|
public bool IsAuthenticated => id.HasValue;
|
|
}
|
|
private sealed class Repository : IProjectAccessRepository
|
|
{
|
|
public List<(string Type, int Id, int User)> Calls { get; } = [];
|
|
public Task<bool> UserCanAccessProjectAsync(int projectId, int userId)
|
|
{
|
|
Calls.Add(("Project", projectId, userId));
|
|
return Task.FromResult(projectId == 7 && userId == 4);
|
|
}
|
|
public Task<bool> UserCanOwnProjectAsync(int projectId, int userId)
|
|
{
|
|
Calls.Add(("Owner", projectId, userId));
|
|
return Task.FromResult(false); // This author is a collaborator.
|
|
}
|
|
public Task<bool> UserCanAccessEntityAsync(string entityType, int entityId, int userId)
|
|
{
|
|
Calls.Add((entityType, entityId, userId));
|
|
return Task.FromResult(entityId == 23 && userId == 4);
|
|
}
|
|
}
|
|
public static void FilterBoundaries() => Run().GetAwaiter().GetResult();
|
|
private static async Task Run()
|
|
{
|
|
var repo = new Repository();
|
|
var user = new User(4);
|
|
var service = new ProjectAccessService(repo, user);
|
|
var filter = new ProjectAccessFilter(service, user);
|
|
async Task Check(string controller, string action, Dictionary<string, object?> arguments, bool allowed, string expectedType)
|
|
{
|
|
repo.Calls.Clear();
|
|
var actionContext = new ActionContext(new DefaultHttpContext(), new RouteData(), new ControllerActionDescriptor { ControllerName = controller, ActionName = action });
|
|
var filters = new List<IFilterMetadata>();
|
|
var context = new ActionExecutingContext(actionContext, filters, arguments, new object());
|
|
var called = false;
|
|
await filter.OnActionExecutionAsync(context, () => { called = true; return Task.FromResult(new ActionExecutedContext(actionContext, filters, new object())); });
|
|
if (called != allowed || (!allowed && context.Result is not NotFoundResult))
|
|
throw new Exception($"{controller}/{action} must enforce access before controller execution.");
|
|
if (!repo.Calls.Any(call => call.Type == expectedType) || repo.Calls.Any(call => call.User != 4))
|
|
throw new Exception("Access must resolve the expected entity under the current author.");
|
|
}
|
|
await Check("Projects", "Details", new() { ["id"] = 98 }, false, "Project");
|
|
await Check("Projects", "Details", new() { ["id"] = 7 }, true, "Project");
|
|
await Check("Exports", "ProjectBackup", new() { ["projectId"] = 7 }, false, "Owner");
|
|
await Check("Exports", "SceneBrief", new() { ["id"] = 98 }, false, "Scene");
|
|
await Check("Exports", "SceneBrief", new() { ["id"] = 23 }, true, "Scene");
|
|
await Check("ProjectRestorePoints", "Download", new() { ["projectId"] = 7, ["id"] = 98 }, false, "Owner");
|
|
await Check("ProjectRestorePoints", "Restore", new() { ["projectId"] = 7, ["id"] = 98 }, false, "Owner");
|
|
await Check("ProjectRestorePoints", "Delete", new() { ["projectId"] = 7, ["id"] = 98 }, false, "Owner");
|
|
await Check("BookRecovery", "Backup", new() { ["projectId"] = 7 }, false, "Owner");
|
|
repo.Calls.Clear();
|
|
var anonymous = new ProjectAccessService(repo, new User(null));
|
|
if (await anonymous.CanAccessProjectAsync(7) || await anonymous.CanAccessEntityAsync("Scene", 23)
|
|
|| await anonymous.CanOwnProjectAsync(7) || await service.CanAccessProjectAsync(0)
|
|
|| await service.CanAccessEntityAsync("Scene", -1) || repo.Calls.Count != 0)
|
|
throw new Exception("Anonymous and invalid identities must fail closed before querying data.");
|
|
}
|
|
}
|