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
|
||||
|
|
|
|||
Loading…
Reference in a new issue