2026-11-11 16:30:42 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
|
using Avalonia.Threading;
|
|
|
|
|
|
using PostIt.ViewModels;
|
|
|
|
|
|
using PostIt.Views;
|
|
|
|
|
|
using Yavsc.Abstract.Resources;
|
|
|
|
|
|
using Yavsc.Api.Client;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PostIt.Tests;
|
|
|
|
|
|
|
2026-11-12 14:32:19 +00:00
|
|
|
|
[Trait("Category", "non-reg")]
|
2026-11-16 15:55:32 +00:00
|
|
|
|
[Collection(AvaloniaUiCollection.Name)]
|
|
|
|
|
|
public sealed class ResourceUsageReliefPageTests(AvaloniaUiFixture ui)
|
2026-11-11 16:30:42 +00:00
|
|
|
|
{
|
|
|
|
|
|
private static ResourceUsageSummary Usage(string id, decimal consumption, decimal credits = 0m) => new()
|
|
|
|
|
|
{
|
|
|
|
|
|
UserId = id, UserName = id + "-name", Consumption = consumption, GrossConsumption = consumption + credits,
|
|
|
|
|
|
AppliedCredits = credits, ConsumptionLimit = 1000m, IsQuotaReached = consumption >= 1000m,
|
|
|
|
|
|
RemainingConsumption = Math.Max(0m, 1000m - consumption)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-11-16 15:55:32 +00:00
|
|
|
|
[Theory]
|
2026-11-11 16:30:42 +00:00
|
|
|
|
[InlineData("Moderator")]
|
|
|
|
|
|
[InlineData("administrator")]
|
|
|
|
|
|
[InlineData("Performer")]
|
2026-11-16 15:55:32 +00:00
|
|
|
|
public Task Nonadministrators_cannot_load_or_erase_or_open_the_page(string role)
|
|
|
|
|
|
=> ui.RunAsync(async () =>
|
2026-11-11 16:30:42 +00:00
|
|
|
|
{
|
|
|
|
|
|
await using var profile = new AdministrationAccessTests.ProfileTransport(role);
|
|
|
|
|
|
var session = new SessionStatusViewModel { Api = profile };
|
|
|
|
|
|
await session.RefreshAsync();
|
|
|
|
|
|
await using var transport = new Transport();
|
|
|
|
|
|
using var vm = new ResourceUsageReliefPageViewModel(new(transport, "https://example.invalid/api/v1/"), session);
|
|
|
|
|
|
await vm.InitializeAsync();
|
|
|
|
|
|
Assert.False(vm.IsAdministrator);
|
|
|
|
|
|
Assert.Empty(transport.Requests);
|
|
|
|
|
|
Assert.Empty(vm.Users);
|
|
|
|
|
|
Assert.False(vm.RefreshCommand.CanExecute(null));
|
|
|
|
|
|
Assert.False(vm.EraseCommand.CanExecute(null));
|
|
|
|
|
|
var administration = new AdministrationPageViewModel(null, session: session);
|
|
|
|
|
|
await administration.OpenResourceUsageReliefCommand.ExecuteAsync(null);
|
|
|
|
|
|
Assert.Contains("Administrator", administration.StatusMessage);
|
2026-11-16 15:55:32 +00:00
|
|
|
|
}, TestContext.Current.CancellationToken);
|
2026-11-11 16:30:42 +00:00
|
|
|
|
|
2026-11-16 15:55:32 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
|
public Task Page_loads_only_exhausted_users_and_confirmed_remission_updates_the_result()
|
|
|
|
|
|
=> ui.RunAsync(async () =>
|
2026-11-11 16:30:42 +00:00
|
|
|
|
{
|
|
|
|
|
|
await using var profile = new AdministrationAccessTests.ProfileTransport("Administrator");
|
|
|
|
|
|
var session = new SessionStatusViewModel { Api = profile };
|
|
|
|
|
|
await session.RefreshAsync();
|
|
|
|
|
|
await using var transport = new Transport();
|
|
|
|
|
|
using var vm = new ResourceUsageReliefPageViewModel(new(transport, "https://example.invalid/api/v1/"), session);
|
|
|
|
|
|
await vm.InitializeAsync("alice");
|
|
|
|
|
|
Assert.Equal(2, vm.Users.Count);
|
|
|
|
|
|
Assert.Equal("alice", vm.SelectedUsage?.UserId);
|
|
|
|
|
|
vm.SearchText = "BOB";
|
|
|
|
|
|
Assert.Equal("bob", Assert.Single(vm.FilteredUsers).UserId);
|
|
|
|
|
|
vm.SearchText = "";
|
|
|
|
|
|
vm.PointsText = "125,50";
|
|
|
|
|
|
Assert.Equal(974.5m, vm.ConsumptionAfter);
|
|
|
|
|
|
Assert.False(vm.EraseCommand.CanExecute(null));
|
|
|
|
|
|
await vm.EraseCommand.ExecuteAsync(null);
|
|
|
|
|
|
Assert.Null(transport.Erasure);
|
|
|
|
|
|
vm.ConfirmErasure = true;
|
|
|
|
|
|
Assert.True(vm.EraseCommand.CanExecute(null));
|
|
|
|
|
|
ResourceUsageSummary? updated = null;
|
|
|
|
|
|
vm.UsageUpdated += result => updated = result;
|
|
|
|
|
|
await vm.EraseCommand.ExecuteAsync(null);
|
|
|
|
|
|
Assert.Equal(125.5m, transport.Erasure?.Points);
|
|
|
|
|
|
Assert.Equal(20m, transport.Erasure?.ExpectedCredits);
|
|
|
|
|
|
Assert.Same(updated, vm.SelectedUsage);
|
|
|
|
|
|
Assert.Equal(974.5m, vm.SelectedUsage!.Consumption);
|
|
|
|
|
|
Assert.False(vm.SelectedUsage.IsQuotaReached);
|
|
|
|
|
|
Assert.False(vm.ConfirmErasure);
|
|
|
|
|
|
Assert.False(vm.EraseCommand.CanExecute(null));
|
|
|
|
|
|
Assert.Contains("n’est plus atteinte", vm.StatusMessage);
|
|
|
|
|
|
Assert.Contains("alice", vm.StatusMessage);
|
|
|
|
|
|
Assert.Contains("GET https://example.invalid/api/v1/resource-usage/admin", transport.Requests);
|
|
|
|
|
|
Assert.Contains("POST https://example.invalid/api/v1/resource-usage/admin/alice/erase", transport.Requests);
|
2026-11-16 15:55:32 +00:00
|
|
|
|
}, TestContext.Current.CancellationToken);
|
2026-11-11 16:30:42 +00:00
|
|
|
|
|
2026-11-16 15:55:32 +00:00
|
|
|
|
[Theory]
|
2026-11-11 16:30:42 +00:00
|
|
|
|
[InlineData("")]
|
|
|
|
|
|
[InlineData("0")]
|
|
|
|
|
|
[InlineData("-1")]
|
|
|
|
|
|
[InlineData("1100.00000001")]
|
|
|
|
|
|
[InlineData("0.000000001")]
|
|
|
|
|
|
[InlineData("1,000.00")]
|
|
|
|
|
|
[InlineData("NaN")]
|
|
|
|
|
|
[InlineData("1e2")]
|
2026-11-16 15:55:32 +00:00
|
|
|
|
public Task Invalid_amounts_never_reach_the_API(string amount)
|
|
|
|
|
|
=> ui.RunAsync(async () =>
|
2026-11-11 16:30:42 +00:00
|
|
|
|
{
|
|
|
|
|
|
await using var profile = new AdministrationAccessTests.ProfileTransport("Administrator");
|
|
|
|
|
|
var session = new SessionStatusViewModel { Api = profile };
|
|
|
|
|
|
await session.RefreshAsync();
|
|
|
|
|
|
await using var transport = new Transport();
|
|
|
|
|
|
using var vm = new ResourceUsageReliefPageViewModel(new(transport, "https://example.invalid/api/v1/"), session);
|
|
|
|
|
|
await vm.InitializeAsync("alice");
|
|
|
|
|
|
vm.PointsText = amount;
|
|
|
|
|
|
vm.ConfirmErasure = true;
|
|
|
|
|
|
Assert.False(vm.EraseCommand.CanExecute(null));
|
|
|
|
|
|
Assert.NotEmpty(vm.AmountError);
|
|
|
|
|
|
await vm.EraseCommand.ExecuteAsync(null);
|
|
|
|
|
|
Assert.Null(transport.Erasure);
|
2026-11-16 15:55:32 +00:00
|
|
|
|
}, TestContext.Current.CancellationToken);
|
2026-11-11 16:30:42 +00:00
|
|
|
|
|
2026-11-16 15:55:32 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
|
public Task Changing_target_or_amount_or_refreshing_invalidates_confirmation()
|
|
|
|
|
|
=> ui.RunAsync(async () =>
|
2026-11-11 16:30:42 +00:00
|
|
|
|
{
|
|
|
|
|
|
await using var profile = new AdministrationAccessTests.ProfileTransport("Administrator");
|
|
|
|
|
|
var session = new SessionStatusViewModel { Api = profile };
|
|
|
|
|
|
await session.RefreshAsync();
|
|
|
|
|
|
await using var transport = new Transport();
|
|
|
|
|
|
using var vm = new ResourceUsageReliefPageViewModel(new(transport, "https://example.invalid/api/v1/"), session);
|
|
|
|
|
|
await vm.InitializeAsync("alice");
|
|
|
|
|
|
vm.PointsText = "10";
|
|
|
|
|
|
vm.ConfirmErasure = true;
|
|
|
|
|
|
vm.PointsText = "20";
|
|
|
|
|
|
Assert.False(vm.ConfirmErasure);
|
|
|
|
|
|
vm.ConfirmErasure = true;
|
|
|
|
|
|
vm.SelectedUsage = vm.Users.Single(u => u.UserId == "bob");
|
|
|
|
|
|
Assert.False(vm.ConfirmErasure);
|
|
|
|
|
|
Assert.Empty(vm.PointsText);
|
|
|
|
|
|
vm.PointsText = "20";
|
|
|
|
|
|
vm.ConfirmErasure = true;
|
|
|
|
|
|
await vm.RefreshCommand.ExecuteAsync(null);
|
|
|
|
|
|
Assert.False(vm.ConfirmErasure);
|
|
|
|
|
|
Assert.Empty(vm.PointsText);
|
|
|
|
|
|
Assert.Equal("bob", vm.SelectedUsage?.UserId);
|
2026-11-16 15:55:32 +00:00
|
|
|
|
}, TestContext.Current.CancellationToken);
|
2026-11-11 16:30:42 +00:00
|
|
|
|
|
2026-11-16 15:55:32 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
|
public Task Failure_requires_a_fresh_snapshot_before_retry()
|
|
|
|
|
|
=> ui.RunAsync(async () =>
|
2026-11-11 16:30:42 +00:00
|
|
|
|
{
|
|
|
|
|
|
await using var profile = new AdministrationAccessTests.ProfileTransport("Administrator");
|
|
|
|
|
|
var session = new SessionStatusViewModel { Api = profile };
|
|
|
|
|
|
await session.RefreshAsync();
|
|
|
|
|
|
await using var transport = new Transport { FailErase = true };
|
|
|
|
|
|
using var vm = new ResourceUsageReliefPageViewModel(new(transport, "https://example.invalid/api/v1/"), session);
|
|
|
|
|
|
await vm.InitializeAsync("alice");
|
|
|
|
|
|
vm.PointsText = "100";
|
|
|
|
|
|
vm.ConfirmErasure = true;
|
|
|
|
|
|
await vm.EraseCommand.ExecuteAsync(null);
|
|
|
|
|
|
Assert.Contains("Actualisez", vm.StatusMessage);
|
|
|
|
|
|
Assert.False(vm.ConfirmErasure);
|
|
|
|
|
|
vm.ConfirmErasure = true;
|
|
|
|
|
|
Assert.False(vm.EraseCommand.CanExecute(null));
|
|
|
|
|
|
transport.FailErase = false;
|
|
|
|
|
|
await vm.EraseCommand.ExecuteAsync(null);
|
|
|
|
|
|
Assert.Single(transport.Requests, r => r.StartsWith("POST"));
|
|
|
|
|
|
await vm.RefreshCommand.ExecuteAsync(null);
|
|
|
|
|
|
vm.PointsText = "100";
|
|
|
|
|
|
vm.ConfirmErasure = true;
|
|
|
|
|
|
Assert.True(vm.EraseCommand.CanExecute(null));
|
2026-11-16 15:55:32 +00:00
|
|
|
|
}, TestContext.Current.CancellationToken);
|
2026-11-11 16:30:42 +00:00
|
|
|
|
|
2026-11-16 15:55:32 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
|
public Task Logout_clears_private_data_and_a_late_response_cannot_restore_it()
|
|
|
|
|
|
=> ui.RunAsync(async () =>
|
2026-11-11 16:30:42 +00:00
|
|
|
|
{
|
|
|
|
|
|
await using var profile = new AdministrationAccessTests.ProfileTransport("Administrator");
|
|
|
|
|
|
var session = new SessionStatusViewModel { Api = profile };
|
|
|
|
|
|
await session.RefreshAsync();
|
|
|
|
|
|
await using var transport = new Transport { Pending = new(TaskCreationOptions.RunContinuationsAsynchronously) };
|
|
|
|
|
|
using var vm = new ResourceUsageReliefPageViewModel(new(transport, "https://example.invalid/api/v1/"), session);
|
|
|
|
|
|
var loading = vm.InitializeAsync("alice");
|
|
|
|
|
|
session.Api = null;
|
|
|
|
|
|
session.Refresh();
|
|
|
|
|
|
transport.Pending.SetResult([Usage("alice", 1100m)]);
|
|
|
|
|
|
await loading;
|
|
|
|
|
|
Assert.Empty(vm.Users);
|
|
|
|
|
|
Assert.Null(vm.SelectedUsage);
|
|
|
|
|
|
Assert.False(vm.IsAdministrator);
|
|
|
|
|
|
Assert.False(vm.EraseCommand.CanExecute(null));
|
2026-11-16 15:55:32 +00:00
|
|
|
|
}, TestContext.Current.CancellationToken);
|
2026-11-11 16:30:42 +00:00
|
|
|
|
|
2026-11-16 15:55:32 +00:00
|
|
|
|
[Theory]
|
2026-11-11 16:30:42 +00:00
|
|
|
|
[InlineData(400, 800)]
|
|
|
|
|
|
[InlineData(800, 400)]
|
2026-11-16 15:55:32 +00:00
|
|
|
|
public Task Page_displays_target_preview_and_explicit_confirmation_and_disposes_on_exit(int width, int height)
|
|
|
|
|
|
=> ui.RunAsync(async () =>
|
2026-11-11 16:30:42 +00:00
|
|
|
|
{
|
|
|
|
|
|
await using var profile = new AdministrationAccessTests.ProfileTransport("Administrator");
|
|
|
|
|
|
var session = new SessionStatusViewModel { Api = profile };
|
|
|
|
|
|
await session.RefreshAsync();
|
|
|
|
|
|
await using var transport = new Transport();
|
|
|
|
|
|
using var vm = new ResourceUsageReliefPageViewModel(new(transport, "https://example.invalid/api/v1/"), session);
|
|
|
|
|
|
await vm.InitializeAsync("alice");
|
|
|
|
|
|
vm.PointsText = "100";
|
|
|
|
|
|
var page = new ResourceUsageReliefPage { DataContext = vm };
|
|
|
|
|
|
var window = new Window { Content = page, Width = width, Height = height };
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
window.Show();
|
|
|
|
|
|
Dispatcher.UIThread.RunJobs();
|
|
|
|
|
|
Assert.Equal("Compte cible : alice", page.FindControl<TextBlock>("ReliefTarget")!.Text);
|
|
|
|
|
|
Assert.Contains("1000", page.FindControl<TextBlock>("ReliefPreview")!.Text);
|
|
|
|
|
|
Assert.Same(vm.EraseCommand, page.FindControl<Button>("ErasePoints")!.Command);
|
|
|
|
|
|
Assert.False(page.FindControl<Button>("ErasePoints")!.IsEffectivelyEnabled);
|
|
|
|
|
|
page.FindControl<CheckBox>("ReliefConfirmation")!.IsChecked = true;
|
|
|
|
|
|
Dispatcher.UIThread.RunJobs();
|
|
|
|
|
|
Assert.True(page.FindControl<Button>("ErasePoints")!.IsEffectivelyEnabled);
|
|
|
|
|
|
var admin = new AdministrationPage { DataContext = new AdministrationPageViewModel() };
|
|
|
|
|
|
Assert.NotNull(admin.FindControl<Button>("OpenResourceUsageRelief")!.Command);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally { window.Close(); }
|
|
|
|
|
|
Assert.False(vm.IsAdministrator);
|
|
|
|
|
|
Assert.Empty(vm.Users);
|
|
|
|
|
|
Assert.False(vm.ConfirmErasure);
|
2026-11-16 15:55:32 +00:00
|
|
|
|
}, TestContext.Current.CancellationToken);
|
2026-11-11 16:30:42 +00:00
|
|
|
|
|
|
|
|
|
|
private sealed class Transport : IYavscApiClient
|
|
|
|
|
|
{
|
|
|
|
|
|
public HttpClient Http { get; } = new();
|
|
|
|
|
|
public List<string> Requests { get; } = [];
|
|
|
|
|
|
public EraseResourceUsageRequest? Erasure { get; private set; }
|
|
|
|
|
|
public bool FailErase { get; set; }
|
|
|
|
|
|
public TaskCompletionSource<List<ResourceUsageSummary>>? Pending { get; init; }
|
|
|
|
|
|
public async Task<T> CallAsync<T>(HttpMethod method, string path, object? body = null, CancellationToken ct = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
Requests.Add(method + " " + path);
|
|
|
|
|
|
object result;
|
|
|
|
|
|
if (method == HttpMethod.Get)
|
|
|
|
|
|
result = Pending is null ? new List<ResourceUsageSummary>
|
|
|
|
|
|
{ Usage("alice", 1100m, 20m), Usage("bob", 2000m), Usage("carol", 999m) } : await Pending.Task;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Erasure = Assert.IsType<EraseResourceUsageRequest>(body);
|
|
|
|
|
|
if (FailErase) throw new HttpRequestException("Les crédits ont changé.", null, HttpStatusCode.Conflict);
|
|
|
|
|
|
result = Usage("alice", 1100m - Erasure.Points, 20m + Erasure.Points);
|
|
|
|
|
|
}
|
|
|
|
|
|
return (T)result;
|
|
|
|
|
|
}
|
|
|
|
|
|
public Task<T> CallAsync<T>(HttpMethod method, string path, Func<HttpContent> contentFactory, CancellationToken ct = default)
|
|
|
|
|
|
=> throw new NotSupportedException();
|
|
|
|
|
|
public Task CallAsync(HttpMethod method, string path, object? body = null, CancellationToken ct = default)
|
|
|
|
|
|
=> throw new NotSupportedException();
|
|
|
|
|
|
public Task CallAsync(HttpMethod method, string path, Func<HttpContent> contentFactory, CancellationToken ct = default)
|
|
|
|
|
|
=> throw new NotSupportedException();
|
|
|
|
|
|
public Task<byte[]> DownloadAsync(HttpMethod method, string path, CancellationToken ct = default)
|
|
|
|
|
|
=> throw new NotSupportedException();
|
|
|
|
|
|
public ValueTask DisposeAsync() { Http.Dispose(); return ValueTask.CompletedTask; }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|