71 lines
4.7 KiB
C#
71 lines
4.7 KiB
C#
using System.Text.Json;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PlotLine.Controllers;
|
|
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
|
|
internal static class ToolbarTests
|
|
{
|
|
public static void PreferencesAndRoutes()
|
|
{
|
|
Check(ToolbarCommands.Resolve(null).SequenceEqual(ToolbarCommands.Defaults), "New users receive current defaults.");
|
|
Check(ToolbarCommands.Resolve("[]").Length == 0, "An intentionally empty toolbar stays empty.");
|
|
Check(ToolbarCommands.Resolve("[\"calendar\",\"removed\",\"calendar\",\"timeline\"]").SequenceEqual(new[] { "calendar", "timeline" }), "Unknown keys are discarded, order retained and new defaults not added.");
|
|
Check(!ToolbarCommands.IsValid(["calendar", "calendar"]) && !ToolbarCommands.IsValid(["/evil"]), "Reject duplicate or unknown commands.");
|
|
var context = new ApplicationNavigationViewModel { Project = new Project { ProjectID = 7 }, Book = new Book { BookID = 12, ProjectID = 7 } };
|
|
foreach (var command in ToolbarCommands.All)
|
|
{
|
|
Check(command.IsAvailable(context), "Registered navigation commands available in authorised context.");
|
|
if (command.RequiresProject)
|
|
{
|
|
Check(!command.IsAvailable(new()), "Project commands disabled without context.");
|
|
var values = JsonSerializer.Serialize(command.RouteValues(context));
|
|
Check(values.Contains(command.RequiresBook ? "12" : "7"), "Links preserve authorised context.");
|
|
}
|
|
}
|
|
var calendar = ToolbarCommands.All.Single(x => x.Key == "calendar");
|
|
Check(calendar.IsActive("StoryCalendar", "Index", "Calendar") && calendar.IsActive("StoryCalendar", "Settings", "Calendar"), "Calendar feature active across actions.");
|
|
var review = ToolbarCommands.All.Single(x => x.Key == "review");
|
|
Check(!review.IsAvailable(new() { Project = context.Project }), "Review never invents a book.");
|
|
Check(review.IsActive("Onboarding", "StoryIntelligenceCharacters", "Onboarding") && !review.IsActive("Onboarding", "Index", "Onboarding"), "Review pages are distinct from setup.");
|
|
Check(typeof(ToolbarController).GetCustomAttributes(typeof(AuthorizeAttribute), true).Length == 1, "Preference endpoint requires authentication.");
|
|
Check(typeof(ToolbarController).GetMethod("Save")!.GetCustomAttributes(typeof(ValidateAntiForgeryTokenAttribute), true).Length == 1, "Preference endpoint requires antiforgery.");
|
|
var store = new ToolbarMemoryPreferences();
|
|
var appearances = new AppearanceMemoryPreferences();
|
|
var first = new ToolbarController(new ToolbarTestUser(4), store, appearances);
|
|
var second = new ToolbarController(new ToolbarTestUser(5), store, appearances);
|
|
first.Save(["calendar", "timeline"]).GetAwaiter().GetResult();
|
|
second.Save([]).GetAwaiter().GetResult();
|
|
Check(ToolbarCommands.Resolve(store.GetAsync(4).Result).SequenceEqual(new[] { "calendar", "timeline" }), "Preferences survive new controller/session instances.");
|
|
Check(ToolbarCommands.Resolve(store.GetAsync(5).Result).Length == 0, "Users have independent configurations.");
|
|
first.Save(["calendar"], showProjectContextMenu: false, showBookContextMenu: true).GetAwaiter().GetResult();
|
|
Check(!appearances.GetAsync(4).Result.ShowProjectContextMenu && appearances.GetAsync(4).Result.ShowBookContextMenu, "Application bar choices save with customisation.");
|
|
Check(first.Save(["unknown"]).Result is BadRequestResult, "Invalid changes rejected.");
|
|
first.Save([], true).GetAwaiter().GetResult();
|
|
Check(store.GetAsync(4).Result is null, "Restore defaults removes custom configuration.");
|
|
Check(appearances.GetAsync(4).Result.ShowProjectContextMenu && appearances.GetAsync(4).Result.ShowBookContextMenu, "Restore defaults shows both application bar menus.");
|
|
}
|
|
private static void Check(bool value, string message) { if (!value) throw new Exception(message); }
|
|
}
|
|
|
|
internal sealed class ToolbarTestUser(int id) : ICurrentUserService
|
|
{
|
|
public int? UserId => id;
|
|
public string? Email => "fixture@example.test";
|
|
public string? DisplayName => "Fixture Author";
|
|
public bool IsAuthenticated => true;
|
|
}
|
|
internal sealed class ToolbarMemoryPreferences : IToolbarPreferenceRepository
|
|
{
|
|
private readonly System.Collections.Concurrent.ConcurrentDictionary<int, string> values = new();
|
|
public Task<string?> GetAsync(int userId) => Task.FromResult(values.GetValueOrDefault(userId));
|
|
public Task SaveAsync(int userId, string? commandKeysJson)
|
|
{
|
|
if (commandKeysJson is null) values.TryRemove(userId, out _); else values[userId] = commandKeysJson;
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|