feat: add resource quotas, PayPal subscriptions and paid disk allowances
Calculate and enforce configurable resource consumption, expose balances in PostIt and document the rule in CGU. Add confirmed monthly PayPal credits and disk quotas of twice the free default for Discovery and five times for Premium, with payment reconciliation, migrations and regression tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
0728d2a3f7
commit
650e5f5e30
69 changed files with 19111 additions and 203 deletions
19
src/PostIt/PostIt.Tests/AboutTermsTests.cs
Normal file
19
src/PostIt/PostIt.Tests/AboutTermsTests.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
using PostIt.ViewModels;
|
||||
|
||||
namespace PostIt.Tests;
|
||||
|
||||
public sealed class AboutTermsTests
|
||||
{
|
||||
[Fact]
|
||||
public void Terms_follow_the_configured_instance_and_changes_to_it()
|
||||
{
|
||||
var settings = new Settings();
|
||||
settings.Authentication.Authority = "https://first.example/";
|
||||
var model = new AboutPageViewModel(settings);
|
||||
Assert.Equal("https://first.example/Home/CGU", model.TermsUri.AbsoluteUri);
|
||||
Assert.Equal("https://first.example/ResourceSubscription", model.SubscriptionsUri.AbsoluteUri);
|
||||
settings.Authentication.Authority = "https://second.example";
|
||||
Assert.Equal("https://second.example/Home/CGU", model.TermsUri.AbsoluteUri);
|
||||
Assert.Equal("https://second.example/ResourceSubscription", model.SubscriptionsUri.AbsoluteUri);
|
||||
}
|
||||
}
|
||||
|
|
@ -15,6 +15,32 @@ namespace PostIt.Tests;
|
|||
|
||||
public class AdministrationAccessTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Profile_quota_failure_keeps_the_consumption_visible_without_enabling_profile_changes()
|
||||
{
|
||||
await using var api = new ProfileTransport("Performer") { ExhaustedQuota = true };
|
||||
var model = new UserProfilePageViewModel(api, usageClient: api.UsageClient);
|
||||
await model.RefreshAsync();
|
||||
Assert.True(model.ResourceUsage?.IsQuotaReached);
|
||||
Assert.False(model.IsProfileLoaded);
|
||||
Assert.False(model.SaveProfileCommand.CanExecute(null));
|
||||
Assert.Contains("resource_quota_exceeded", model.StatusMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Administration_quota_failure_keeps_the_authorized_usage_overview()
|
||||
{
|
||||
await using var api = new ProfileTransport("Administrator");
|
||||
var session = new SessionStatusViewModel { Api = api };
|
||||
await session.RefreshAsync();
|
||||
api.ExhaustedQuota = true;
|
||||
var model = new AdministrationPageViewModel(api, api.UsageClient, session);
|
||||
await model.RefreshAsync();
|
||||
Assert.True(Assert.Single(model.UsageOverview).IsQuotaReached);
|
||||
Assert.Empty(model.Users);
|
||||
Assert.Contains("les usages restent consultables", model.StatusMessage);
|
||||
}
|
||||
|
||||
[AvaloniaTheory]
|
||||
[InlineData("Moderator", false)]
|
||||
[InlineData("Administrator", true)]
|
||||
|
|
@ -242,6 +268,7 @@ public class AdministrationAccessTests
|
|||
public Task<Me>? PendingProfile { get; set; }
|
||||
public bool FailProfile { get; set; }
|
||||
public bool FailUsage { get; set; }
|
||||
public bool ExhaustedQuota { get; set; }
|
||||
public bool HideIpAddressInCalls { get; set; }
|
||||
public ResourceUsageApiClient UsageClient => new(this, Settings.ApiUrl);
|
||||
|
||||
|
|
@ -268,6 +295,8 @@ public class AdministrationAccessTests
|
|||
object result;
|
||||
if (path.EndsWith("/account/me"))
|
||||
{
|
||||
if (ExhaustedQuota)
|
||||
throw new HttpRequestException("resource_quota_exceeded", null, System.Net.HttpStatusCode.TooManyRequests);
|
||||
if (FailProfile)
|
||||
throw new HttpRequestException("Profile unavailable");
|
||||
result = PendingProfile is not null ? await PendingProfile
|
||||
|
|
@ -277,11 +306,22 @@ public class AdministrationAccessTests
|
|||
{
|
||||
if (FailUsage)
|
||||
throw new HttpRequestException("Usage unavailable");
|
||||
var usage = new ResourceUsageSummary { UserId = "alice-id", UserName = "alice", ApiCalls = 12m };
|
||||
var usage = new ResourceUsageSummary
|
||||
{
|
||||
UserId = "alice-id", UserName = "alice", ApiCalls = 12m,
|
||||
Consumption = ExhaustedQuota ? 10000m : 0.24m,
|
||||
ConsumptionLimit = 10000m,
|
||||
RemainingConsumption = ExhaustedQuota ? 0m : 9999.76m,
|
||||
IsQuotaReached = ExhaustedQuota
|
||||
};
|
||||
result = path.EndsWith("/admin") ? new List<ResourceUsageSummary> { usage } : usage;
|
||||
}
|
||||
else if (path.EndsWith("/users"))
|
||||
{
|
||||
if (ExhaustedQuota)
|
||||
throw new HttpRequestException("resource_quota_exceeded", null, System.Net.HttpStatusCode.TooManyRequests);
|
||||
result = new List<UserInfo> { new() { UserId = "alice-id", UserName = "alice" } };
|
||||
}
|
||||
else
|
||||
throw new InvalidOperationException($"Unexpected endpoint: {path}");
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,18 @@ namespace PostIt.Tests;
|
|||
|
||||
public class ResourceUsageApiClientTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Usage_remains_visible_when_the_rest_of_the_profile_is_unavailable()
|
||||
{
|
||||
var usage = new ResourceUsageApiClient(new FakeYavscApiClient(), "https://example.invalid/api/v1/");
|
||||
var model = new PostIt.ViewModels.UserProfilePageViewModel(null, usageClient: usage);
|
||||
await model.RefreshAsync();
|
||||
Assert.NotNull(model.ResourceUsage);
|
||||
Assert.Equal(20.65m, model.ResourceUsage.Consumption);
|
||||
Assert.Null(model.UsageError);
|
||||
Assert.False(model.IsProfileLoaded);
|
||||
Assert.False(model.IsBusy);
|
||||
}
|
||||
[Fact]
|
||||
public async Task GetCurrentAsync_calls_user_usage_endpoint()
|
||||
{
|
||||
|
|
@ -19,6 +31,10 @@ public class ResourceUsageApiClientTests
|
|||
Assert.Equal("GET", fake.LastMethod);
|
||||
Assert.Equal(12.5m, usage.ApiCalls);
|
||||
Assert.Equal(3.2m, usage.CpuSeconds);
|
||||
Assert.Equal(20.65m, usage.Consumption);
|
||||
Assert.Equal(10000m, usage.ConsumptionLimit);
|
||||
Assert.Equal(9979.35m, usage.RemainingConsumption);
|
||||
Assert.False(usage.IsQuotaReached);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -56,6 +72,9 @@ public class ResourceUsageApiClientTests
|
|||
CpuSeconds = 3.2m,
|
||||
BandwidthMb = 18.4m,
|
||||
StorageMb = 51.2m,
|
||||
Consumption = 20.65m,
|
||||
ConsumptionLimit = 10000m,
|
||||
RemainingConsumption = 9979.35m,
|
||||
EstimatedCompensation = 4.9m,
|
||||
Currency = "EUR",
|
||||
RecordedAtUtc = DateTimeOffset.UtcNow
|
||||
|
|
|
|||
|
|
@ -96,6 +96,17 @@ public partial class AdministrationPageViewModel : ViewModelBase
|
|||
IsBusy = true;
|
||||
try
|
||||
{
|
||||
var usage = await _usageClient.GetAdminOverviewAsync().ConfigureAwait(true)
|
||||
?? throw new InvalidOperationException("Le serveur a renvoyé une liste d’usages vide.");
|
||||
if (!_session.IsAdministrator)
|
||||
{
|
||||
ClearAdministrationData();
|
||||
StatusMessage = "Le rôle Administrator est requis.";
|
||||
return;
|
||||
}
|
||||
foreach (var item in usage)
|
||||
UsageOverview.Add(item);
|
||||
|
||||
var query = SearchText.Trim();
|
||||
var usersEndpoint = string.IsNullOrWhiteSpace(query)
|
||||
? new Uri(new Uri(_api.Settings.ApiUrl.TrimEnd('/') + "/", UriKind.Absolute), "users").ToString()
|
||||
|
|
@ -103,11 +114,10 @@ public partial class AdministrationPageViewModel : ViewModelBase
|
|||
|
||||
var users = await _api.CallAsync<List<UserInfo>>(HttpMethod.Get, usersEndpoint).ConfigureAwait(true)
|
||||
?? throw new InvalidOperationException("Le serveur a renvoyé une liste d’utilisateurs vide.");
|
||||
var usage = await _usageClient.GetAdminOverviewAsync().ConfigureAwait(true)
|
||||
?? throw new InvalidOperationException("Le serveur a renvoyé une liste d’usages vide.");
|
||||
|
||||
if (!_session.IsAdministrator)
|
||||
{
|
||||
ClearAdministrationData();
|
||||
StatusMessage = "Le rôle Administrator est requis.";
|
||||
return;
|
||||
}
|
||||
|
|
@ -116,8 +126,6 @@ public partial class AdministrationPageViewModel : ViewModelBase
|
|||
{
|
||||
Users.Add(user);
|
||||
}
|
||||
foreach (var item in usage)
|
||||
UsageOverview.Add(item);
|
||||
|
||||
OnPropertyChanged(nameof(FilteredUsers));
|
||||
OnPropertyChanged(nameof(SelectedUsage));
|
||||
|
|
@ -125,6 +133,16 @@ public partial class AdministrationPageViewModel : ViewModelBase
|
|||
await LoadToolsAsync();
|
||||
StatusMessage = Users.Count > 0 ? $"{Users.Count} utilisateur(s) chargé(s)." : "Aucun utilisateur trouvé.";
|
||||
}
|
||||
catch (HttpRequestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
if (!_session.IsAdministrator)
|
||||
{
|
||||
ClearAdministrationData();
|
||||
StatusMessage = "Le rôle Administrator est requis.";
|
||||
}
|
||||
else
|
||||
StatusMessage = $"Quota insuffisant : les usages restent consultables. {ex.Message}";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ClearAdministrationData();
|
||||
|
|
|
|||
|
|
@ -17,13 +17,24 @@ public partial class AboutPageViewModel : ViewModelBase
|
|||
public const string RepositoryUrl = "https://forgejo.pschneider.fr/notazof/yavsc";
|
||||
public const string IssuesUrl = RepositoryUrl + "/issues";
|
||||
|
||||
public AboutPageViewModel()
|
||||
private readonly Settings? _settings;
|
||||
|
||||
public AboutPageViewModel(Settings? settings = null)
|
||||
{
|
||||
_settings = settings;
|
||||
Version = ReadVersion();
|
||||
}
|
||||
|
||||
public string Version { get; }
|
||||
|
||||
public Uri TermsUri => new(new Uri(
|
||||
(_settings?.Authentication.Authority ?? AuthenticationSettings.DefaultAuthority).TrimEnd('/') + "/"), "Home/CGU");
|
||||
public Uri SubscriptionsUri => new(new Uri(
|
||||
(_settings?.Authentication.Authority ?? AuthenticationSettings.DefaultAuthority).TrimEnd('/') + "/"), "ResourceSubscription");
|
||||
|
||||
[ObservableProperty]
|
||||
public partial string? StatusMessage { get; set; }
|
||||
|
||||
public string Description { get; } =
|
||||
"PostIt est le client de la plateforme Yavsc : blog, devis, " +
|
||||
"demandes de prestations et fichiers personnels.";
|
||||
|
|
@ -49,13 +60,20 @@ public partial class AboutPageViewModel : ViewModelBase
|
|||
[RelayCommand]
|
||||
private Task OpenIssues() => OpenUrlAsync(IssuesUrl);
|
||||
|
||||
private static async Task OpenUrlAsync(string url)
|
||||
[RelayCommand]
|
||||
private Task OpenTerms() => OpenUrlAsync(TermsUri.AbsoluteUri);
|
||||
|
||||
[RelayCommand]
|
||||
private Task OpenSubscriptions() => OpenUrlAsync(SubscriptionsUri.AbsoluteUri);
|
||||
|
||||
private async Task OpenUrlAsync(string url)
|
||||
{
|
||||
StatusMessage = null;
|
||||
var app = (App)App.Current!;
|
||||
var topLevel = TopLevel.GetTopLevel(app.View);
|
||||
if (topLevel is not null)
|
||||
if (topLevel is null || !await topLevel.Launcher.LaunchUriAsync(new Uri(url)).ConfigureAwait(true))
|
||||
{
|
||||
await topLevel.Launcher.LaunchUriAsync(new Uri(url)).ConfigureAwait(true);
|
||||
StatusMessage = "Impossible d'ouvrir le navigateur : " + url;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,9 @@ public partial class UserProfilePageViewModel : ViewModelBase
|
|||
private readonly YavscApiClient? _api;
|
||||
private readonly SessionStatusViewModel? _sessionStatus;
|
||||
private readonly ResourceUsageApiClient? _usageClient;
|
||||
private readonly AboutPageViewModel _subscriptionLinks;
|
||||
public IAsyncRelayCommand OpenSubscriptionsCommand => _subscriptionLinks.OpenSubscriptionsCommand;
|
||||
public string? SubscriptionLinkError => _subscriptionLinks.StatusMessage;
|
||||
|
||||
[ObservableProperty]
|
||||
public partial ResourceUsageSummary? ResourceUsage { get; set; }
|
||||
|
|
@ -158,6 +161,8 @@ public partial class UserProfilePageViewModel : ViewModelBase
|
|||
_api = api;
|
||||
_sessionStatus = sessionStatus;
|
||||
_usageClient = usageClient;
|
||||
_subscriptionLinks = new AboutPageViewModel(api?.Settings);
|
||||
_subscriptionLinks.PropertyChanged += (_, _) => OnPropertyChanged(nameof(SubscriptionLinkError));
|
||||
}
|
||||
|
||||
public UserProfilePageViewModel() : this(null, null) { }
|
||||
|
|
@ -167,19 +172,18 @@ public partial class UserProfilePageViewModel : ViewModelBase
|
|||
[RelayCommand]
|
||||
public async Task RefreshAsync()
|
||||
{
|
||||
if (_api is null)
|
||||
{
|
||||
IsProfileLoaded = false;
|
||||
StatusMessage = "Client API de profil indisponible.";
|
||||
return;
|
||||
}
|
||||
|
||||
IsBusy = true;
|
||||
IsProfileLoaded = false;
|
||||
ResourceUsage = null;
|
||||
UsageError = null;
|
||||
try
|
||||
{
|
||||
await LoadUsageAsync();
|
||||
if (_api is null)
|
||||
{
|
||||
StatusMessage = "Client API de profil indisponible.";
|
||||
return;
|
||||
}
|
||||
var me = await _api.CallAsync<Me>(HttpMethod.Get, new Uri(new Uri(_api.Settings.ApiUrl.TrimEnd('/') + "/", UriKind.Absolute), "account/me").ToString()).ConfigureAwait(true);
|
||||
if (me is null)
|
||||
{
|
||||
|
|
@ -218,18 +222,6 @@ public partial class UserProfilePageViewModel : ViewModelBase
|
|||
}
|
||||
IsProfileLoaded = true;
|
||||
StatusMessage = "Profil chargé.";
|
||||
if (_usageClient is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
ResourceUsage = await _usageClient.GetCurrentAsync()
|
||||
?? throw new InvalidOperationException("Le serveur a renvoyé un usage vide.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
UsageError = $"Impossible de charger mon usage : {ex.Message}";
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -264,6 +256,24 @@ public partial class UserProfilePageViewModel : ViewModelBase
|
|||
}
|
||||
}
|
||||
|
||||
private async Task LoadUsageAsync()
|
||||
{
|
||||
if (_usageClient is null)
|
||||
{
|
||||
UsageError = "Client API de consommation indisponible.";
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
ResourceUsage = await _usageClient.GetCurrentAsync()
|
||||
?? throw new InvalidOperationException("Le serveur a renvoyé un usage vide.");
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or JsonException or InvalidOperationException)
|
||||
{
|
||||
UsageError = $"Impossible de charger mon usage : {ex.Message}";
|
||||
}
|
||||
}
|
||||
|
||||
public ProfileUpdateRequest BuildProfileUpdateRequest()
|
||||
{
|
||||
static string NormalizeField(string? value, string placeholder)
|
||||
|
|
|
|||
|
|
@ -34,5 +34,12 @@
|
|||
<Button Content="Signaler un problème"
|
||||
Command="{Binding OpenIssuesCommand}"
|
||||
HorizontalAlignment="Center"/>
|
||||
<Button Content="CGU de PostIt et de Yavsc"
|
||||
Command="{Binding OpenTermsCommand}"
|
||||
HorizontalAlignment="Center"/>
|
||||
<Button Content="Abonnements Découverte et Premium"
|
||||
Command="{Binding OpenSubscriptionsCommand}"
|
||||
HorizontalAlignment="Center"/>
|
||||
<TextBlock Text="{Binding StatusMessage}" TextWrapping="Wrap" MaxWidth="420"/>
|
||||
</StackPanel>
|
||||
</ContentPage>
|
||||
|
|
|
|||
|
|
@ -119,8 +119,13 @@
|
|||
<TextBlock Text="Usage des ressources" FontWeight="Bold"/>
|
||||
<TextBlock Text="{Binding ApiCalls, StringFormat='Appels API : {0:F0}'}"/>
|
||||
<TextBlock Text="{Binding CpuSeconds, StringFormat='CPU : {0:F3} s'}"/>
|
||||
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mo'}"/>
|
||||
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage : {0:F3} Mo'}"/>
|
||||
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mio'}"/>
|
||||
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage ajouté : {0:F3} Mio'}"/>
|
||||
<TextBlock Text="{Binding Consumption, StringFormat='Consommation : {0:F3} points'}"/>
|
||||
<TextBlock Text="{Binding CreditBalance, StringFormat='Crédit reporté : {0:F3} points'}"/>
|
||||
<TextBlock Text="{Binding ConsumptionLimit, StringFormat='Seuil : {0:F3} points'}"/>
|
||||
<TextBlock Text="{Binding RemainingConsumption, StringFormat='Solde : {0:F3} points'}"/>
|
||||
<TextBlock Text="Quota atteint" IsVisible="{Binding IsQuotaReached}"/>
|
||||
<TextBlock Text="{Binding EstimatedCompensation, StringFormat='Estimation : {0:F2}'}"/>
|
||||
<TextBlock Text="{Binding Currency}"/>
|
||||
</StackPanel>
|
||||
|
|
@ -141,8 +146,13 @@
|
|||
<TextBlock Text="{Binding UserId}"/>
|
||||
<TextBlock Text="{Binding ApiCalls, StringFormat='Appels API : {0:F0}'}"/>
|
||||
<TextBlock Text="{Binding CpuSeconds, StringFormat='CPU : {0:F3} s'}"/>
|
||||
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mo'}"/>
|
||||
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage : {0:F3} Mo'}"/>
|
||||
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mio'}"/>
|
||||
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage ajouté : {0:F3} Mio'}"/>
|
||||
<TextBlock Text="{Binding Consumption, StringFormat='Consommation : {0:F3} points'}"/>
|
||||
<TextBlock Text="{Binding CreditBalance, StringFormat='Crédit reporté : {0:F3} points'}"/>
|
||||
<TextBlock Text="{Binding ConsumptionLimit, StringFormat='Seuil : {0:F3} points'}"/>
|
||||
<TextBlock Text="{Binding RemainingConsumption, StringFormat='Solde : {0:F3} points'}"/>
|
||||
<TextBlock Text="Quota atteint" IsVisible="{Binding IsQuotaReached}"/>
|
||||
<TextBlock Text="{Binding EstimatedCompensation, StringFormat='Estimation : {0:F2}'}"/>
|
||||
<TextBlock Text="{Binding Currency}"/>
|
||||
</StackPanel>
|
||||
|
|
|
|||
|
|
@ -31,11 +31,22 @@
|
|||
<StackPanel Spacing="4">
|
||||
<TextBlock Text="Mon usage des ressources" FontSize="18" FontWeight="SemiBold"/>
|
||||
<TextBlock Text="{Binding UsageError}" TextWrapping="Wrap"/>
|
||||
<Button Content="Abonnements Découverte et Premium"
|
||||
Command="{Binding OpenSubscriptionsCommand}"/>
|
||||
<TextBlock Text="{Binding SubscriptionLinkError}" TextWrapping="Wrap"/>
|
||||
<StackPanel DataContext="{Binding ResourceUsage}" Spacing="4">
|
||||
<TextBlock Text="{Binding ApiCalls, StringFormat='Appels API : {0:F0}'}"/>
|
||||
<TextBlock Text="{Binding CpuSeconds, StringFormat='CPU : {0:F3} s'}"/>
|
||||
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mo'}"/>
|
||||
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage : {0:F3} Mo'}"/>
|
||||
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mio'}"/>
|
||||
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage ajouté : {0:F3} Mio'}"/>
|
||||
<TextBlock Text="{Binding Consumption, StringFormat='Consommation : {0:F3} points'}"/>
|
||||
<TextBlock Text="{Binding GrossConsumption, StringFormat='Consommation brute : {0:F3} points'}"/>
|
||||
<TextBlock Text="{Binding AppliedCredits, StringFormat='Usage effacé : {0:F3} points'}"/>
|
||||
<TextBlock Text="{Binding CreditBalance, StringFormat='Crédit reporté disponible : {0:F3} points'}"/>
|
||||
<TextBlock Text="{Binding ConsumptionLimit, StringFormat='Seuil : {0:F3} points'}"/>
|
||||
<TextBlock Text="{Binding RemainingConsumption, StringFormat='Solde : {0:F3} points'}"/>
|
||||
<TextBlock Text="Quota atteint : les opérations consommatrices sont bloquées."
|
||||
IsVisible="{Binding IsQuotaReached}" TextWrapping="Wrap"/>
|
||||
<TextBlock Text="{Binding EstimatedCompensation, StringFormat='Estimation : {0:F2}'}"/>
|
||||
<TextBlock Text="{Binding Currency}"/>
|
||||
</StackPanel>
|
||||
|
|
|
|||
Loading…
Reference in a new issue