Files
Khaemhat/tests/Khaemhat.Web.Tests/Shell/DashboardServiceTests.cs
T
Alby96andClaude Opus 5.5 acb7e598d1 feat: widget dashboard and company settings (#30, #31)
The home page becomes the widget dashboard of mockups 004-006. Each user
switches widgets on and off, orders them (drag and drop or buttons that
also work from the keyboard), sizes them on the 12-column grid and picks
the start page; every change is saved at once. Whoever may change the
company settings sets a default layout per role, used by people who
never customized; Reset goes back to it.

Todo, Done and Deadlines collect lines from sources each module
registers, so later milestones fill them without touching the widgets;
a failing source is logged and skipped. M3 provides licence, unread
warnings and scheduled task runs. Notes, Shortcuts and System status
work now; widgets of modules still to come say which milestone brings
their data instead of showing fake numbers.

Role defaults live in the new company_settings table, written through
CompanySettingsService with company.settings.write (ADR 0022).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 19:15:06 +01:00

116 lines
7.3 KiB
C#

using Khaemhat.Application.Dashboard;
using Khaemhat.Application.Instance;
using Khaemhat.Application.Modules;
using Khaemhat.Application.Security;
using Khaemhat.Core.Security;
using Khaemhat.Web.Tests.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace Khaemhat.Web.Tests.Shell;
/// <summary>The dashboard of each user, the default of a role and the company settings behind it (issues #30 and #31).</summary>
[TestClass]
public sealed class DashboardServiceTests
{
[TestMethod]
public async Task Layout_IsPersonal_FallsBackToTheRole_ThenToTheBuiltIn()
{
var (app, _, company) = await TestInstance.StartAsync(CancellationToken.None);
await using (app)
{
var dashboard = app.Services.GetRequiredService<DashboardService>();
var users = app.Services.GetRequiredService<UserService>();
var root = new Actor(TestInstance.SuperUser, UserLevel.SuperUser, 1, null, null);
var (anna, _) = await users.CreateAsync(root, company, new NewCompanyUser("anna", "Anna Verdi", null, UserKind.Human, [PredefinedRoles.ReadOnly], new UserFlags(false, false, false, false)));
var (luca, _) = await users.CreateAsync(root, company, new NewCompanyUser("luca", "Luca Neri", null, UserKind.Human, [PredefinedRoles.Administration], new UserFlags(false, false, false, false)));
var annaActor = new Actor("anna", UserLevel.Standard, null, anna.Id, company);
var lucaActor = new Actor("luca", UserLevel.Standard, null, luca.Id, company);
// Only instance accounts see the state of the system.
Assert.IsTrue((await dashboard.AvailableAsync(root)).Any(w => w.Id == WidgetIds.SystemStatus));
Assert.IsFalse((await dashboard.AvailableAsync(annaActor)).Any(w => w.Id == WidgetIds.SystemStatus));
// Nothing saved: the built-in layout, without what the user may not see.
var first = await dashboard.GetAsync(annaActor);
Assert.AreEqual(DashboardLayoutSource.BuiltIn, first.Source);
Assert.IsTrue(first.Layout.Contains(WidgetIds.Todo));
Assert.IsFalse(first.Layout.Contains(WidgetIds.SystemStatus));
// The default of the role applies to whoever never customized.
var roleLayout = new DashboardLayout([new(WidgetIds.Notes, 6), new(WidgetIds.Deadlines, 6)]);
await dashboard.SaveRoleDefaultAsync(lucaActor, company, PredefinedRoles.ReadOnly, roleLayout);
var fromRole = await dashboard.GetAsync(annaActor);
Assert.AreEqual(DashboardLayoutSource.Role, fromRole.Source);
Assert.AreEqual(PredefinedRoles.ReadOnly, fromRole.RoleCode);
Assert.IsTrue(roleLayout.SameAs(fromRole.Layout));
Assert.IsTrue((await dashboard.RoleDefaultsAsync(company)).Any(r => r.Role.Code == PredefinedRoles.ReadOnly && r.Layout is not null));
// A user's own layout wins and stays across services; Reset brings the role default back.
var own = new DashboardLayout([new(WidgetIds.Shortcuts, 12), new(WidgetIds.Todo, 4)]);
await dashboard.SaveAsync(annaActor, own);
var reread = await app.Services.CreateScope().ServiceProvider.GetRequiredService<DashboardService>().GetAsync(annaActor with { });
Assert.AreEqual(DashboardLayoutSource.User, reread.Source);
Assert.IsTrue(own.SameAs(reread.Layout));
Assert.AreEqual(DashboardLayoutSource.BuiltIn, (await dashboard.GetAsync(lucaActor)).Source, "Another user is not touched.");
await dashboard.ResetAsync(annaActor);
Assert.AreEqual(DashboardLayoutSource.Role, (await dashboard.GetAsync(annaActor)).Source);
// Only who may change the company settings sets the default of a role.
await Assert.ThrowsExactlyAsync<AccessDeniedException>(() => dashboard.SaveRoleDefaultAsync(annaActor, company, PredefinedRoles.ReadOnly, null));
await dashboard.SaveRoleDefaultAsync(lucaActor, company, PredefinedRoles.ReadOnly, null);
Assert.AreEqual(DashboardLayoutSource.BuiltIn, (await dashboard.GetAsync(annaActor)).Source);
await app.StopAsync();
}
}
[TestMethod]
public async Task Notes_AndShortcuts_AreKeptPerUser_AndCleaned()
{
var (app, _, company) = await TestInstance.StartAsync(CancellationToken.None);
await using (app)
{
var dashboard = app.Services.GetRequiredService<DashboardService>();
var root = new Actor(TestInstance.SuperUser, UserLevel.SuperUser, 1, null, null);
Assert.IsEmpty(await dashboard.GetNotesAsync(root));
Assert.IsNull(await dashboard.GetShortcutsAsync(root), "Never chosen: the widget shows the first areas.");
await dashboard.SaveNotesAsync(root, [new(" Chiamare il commercialista ", false), new(" ", false), new("Fatto", true)]);
CollectionAssert.AreEqual(new[] { new PersonalNote("Chiamare il commercialista", false), new PersonalNote("Fatto", true) }, (await dashboard.GetNotesAsync(root)).ToList());
await dashboard.SaveNotesAsync(root, [.. Enumerable.Range(1, 30).Select(i => new PersonalNote(new string('n', 400), false))]);
var notes = await dashboard.GetNotesAsync(root);
Assert.HasCount(DashboardService.MaxNotes, notes);
Assert.IsTrue(notes.All(n => n.Text.Length <= DashboardService.MaxNoteLength));
await dashboard.SaveShortcutsAsync(root, ["/anagrafiche/clienti", "https://example.com", "//evil", "/anagrafiche/clienti", "/vendite"]);
CollectionAssert.AreEqual(new[] { "/anagrafiche/clienti", "/vendite" }, (await dashboard.GetShortcutsAsync(root))!.ToList());
await dashboard.SaveShortcutsAsync(root, []);
Assert.IsEmpty((await dashboard.GetShortcutsAsync(root))!, "An empty choice is a choice.");
await app.StopAsync();
}
}
[TestMethod]
public async Task CompanySettings_NeedThePermission_AndStayInTheirCompany()
{
var (app, _, company) = await TestInstance.StartAsync(CancellationToken.None);
await using (app)
{
var settings = app.Services.GetRequiredService<CompanySettingsService>();
var root = new Actor(TestInstance.SuperUser, UserLevel.SuperUser, 1, null, null);
var stranger = new Actor("altro", UserLevel.Standard, null, 1, "altra");
await settings.SetAsync(root, company, "test.alpha", "uno");
await settings.SetManyAsync(root, company, new Dictionary<string, string?> { ["test.beta"] = "due", ["test.alpha"] = null });
Assert.IsNull(await settings.GetAsync(company, "test.alpha"));
Assert.AreEqual("due", await settings.GetAsync(company, "test.beta"));
CollectionAssert.AreEquivalent(new[] { "test.beta" }, (await settings.GetAllAsync(company, "test.")).Keys.ToList());
await Assert.ThrowsExactlyAsync<AccessDeniedException>(() => settings.SetAsync(stranger, company, "test.gamma", "tre"));
await Assert.ThrowsExactlyAsync<ValidationFailedException>(() => settings.SetAsync(root, company, " ", "x"));
await Assert.ThrowsExactlyAsync<ValidationFailedException>(() => settings.SetAsync(root, company, "test.big", new string('x', CompanySettingsService.MaxValueLength + 1)));
await app.StopAsync();
}
}
}