diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dbfdd27c..4f2cfee0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ ### Added +* [Yavsc/PostIt/PayPal] Add monthly Découverte (EUR 1 excluding VAT) and Premium (EUR 2 excluding VAT) resource subscriptions, charged at EUR 1.20 / EUR 2.40 including 20% VAT. Confirmed monthly PayPal payments erase up to one / three default quotas, carry two thirds of unused monthly credit without expiry, and preserve historical counters. Add persistent idempotent payment credits, refund reconciliation, authenticated Web subscription/cancellation pages, verified REST webhooks, PostIt links and balances, migration and updated CGU. +* [Yavsc/PostIt/Subscriptions] Align new users' free quota with the existing 500 MiB database default; grant twice that standard quota for Discovery (1,000 MiB) and five times for Premium (2,500 MiB) during confirmed paid monthly periods, including their remainder after cancellation. Restore the user's base quota after expiry or refund without deleting files, refuse uploads beyond available space before writing, and expose the effective quota in profile and usage responses. +* [PostIt/API/Blogs/Yavsc.Org] Compute cumulative resource consumption in configurable points per API call, recorded CPU second, MiB transferred and MiB added to storage, with backend-defined defaults, a default 1,000-point limit and per-user overrides. Serialize accounting and known-cost admission across PostgreSQL hosts, count streamed HTTP bodies, reject further consuming operations with HTTP 429, and check personal uploads before writing. Keep usage/session access available, display the indicator and balance in PostIt, and publish linked CGU explaining the rule and after-measurement enforcement. * [PostIt.Android] Embed managed assemblies in APKs by default, including Debug builds launched outside the Makefile, to prevent the native startup abort "No assemblies found" when fast-deployment files are absent. * [PostIt] Remove the redundant "Post detail" heading and its grid row from the blog editor, including the compact layout. * [PostIt/Android] Combine system-bar/cutout safe-area insets with actual keyboard overlap at the root, instead of replacing Avalonia's automatic safe-area padding with keyboard-only padding. Preserve the toolbar, session banner, attachments and status controls; place article details beside the editor during landscape keyboard input. Only on Android, temporarily hide the title input while editing in compact mode with the keyboard open, restoring it on keyboard closure or focus change without modifying the draft. Verify visible controls outside native system bars, at least three editor lines and the editor's bottom above IME insets. diff --git a/INSTALL.md b/INSTALL.md index 24b9567a1..19112f0aa 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -76,6 +76,172 @@ login callback, updating and uninstallation on Windows with a standard (non-administrator) account. Installer compilation alone does not validate these Windows runtime behaviours. +## Resource consumption and CGU + +Configure the **same** `ResourceUsage` section on Yavsc.Api, Yavsc.Blogs and +Yavsc.Org. API and Blogs must also use the same application database. +All three hosts and configuration templates inherit the backend defaults +from [ResourceUsageOptions](src/Yavsc.Server/Settings/ResourceUsageOptions.cs). +Deploy matching backend versions across hosts. The public CGU renders the +effective coefficients and limit. The default limit is 1,000 points. +The following is an example explicit override, not a duplicate of defaults. +Use the account's immutable user ID (`sub`), not its login, for overrides: + +```json +{ + "ResourceUsage": { + "ApiCallWeight": 0.2, + "CpuSecondWeight": 0.1, + "BandwidthMbWeight": 0.05, + "StorageMbWeight": 0.5, + "DefaultLimit": 1000, + "UserLimits": { + "example-account-id": 20000 + } + } +} +``` + +Environment variables follow ASP.NET's convention, for example +`ResourceUsage__DefaultLimit` and +`ResourceUsage__UserLimits__example-account-id`. Restart all three services +after changing the rule. Invalid weights or limits fail startup. +Existing cumulative `ResourceUsageRecords` are reused, and changing weights +recalculates their gross indicator. Apply the `ResourceSubscriptions` +migration before deploying any of the three hosts: it adds the credit +balance and subscription/payment tables without clearing existing counters. + +Consumption is in **points**, independent of `EstimatedCompensation` and +currency. Counters have no automatic reset. Storage means cumulative +instrumented writes, not current occupancy; the historical CPU counter is +instrumented elapsed processing time, not exhaustive CPU profiling. + +Authenticated HTTP operations are admitted atomically against known costs +(API call and declared request body). Personal-file uploads additionally +check their aggregate storage cost before writing. Response bytes and +unknown-length request bodies are measured through counting streams, and +remaining costs are recorded after execution, including failed requests. +PostgreSQL transaction-scoped advisory locks serialize admission and +accounting for each user across hosts. In-flight operations can exceed the +limit through costs unknown at admission; subsequent operations return +HTTP 429 with `resource_quota_exceeded` Problem Details. No permanent account +suspension or strict running-operation interruption is performed. + +`GET /api/v1/resource-usage/me` returns `Consumption`, `ConsumptionLimit`, +`RemainingConsumption` and `IsQuotaReached`, alongside existing counters. +It also returns `GrossConsumption`, `AppliedCredits` and `CreditBalance`. +`Consumption` is the gross indicator less acquired credits, floored at zero. +`RemainingConsumption` includes both unused free quota and carried credit. +Usage consultation (including the role-protected admin overview), login, +session refresh and the CGU remain accessible without consuming quota. +The quota does not measure local PostIt resources, direct peer calls or +WebSocket traffic. HTTP upload size limits and disk quotas still apply. + +The public page `/Home/CGU` describes the effective rule and default limit +(and the individual limit for a signed-in user). It is linked from the Web +footer and PostIt's **About** page, using the configured authentication +authority. PostIt displays consumption and remaining points in **My profile** +and the administration usage views. + +### Monthly resource subscriptions (PayPal REST) + +The free registration keeps the configured cumulative quota without a +monthly reset. Discovery costs EUR 1 excluding VAT (EUR 1.20 including 20% +VAT) per month; Premium costs EUR 2 excluding VAT (EUR 2.40 including VAT). +Every **confirmed payment** grants up to one / three default quotas, +respectively, with renewal on the UTC monthly anniversary of the PayPal +start date. The pack allowance is captured at subscription creation, not +multiplied by an individual user's quota override. + +At reconciliation, old carried credit is used first; the new allowance +erases remaining net usage. Two thirds of the unused allowance are added +to permanent carried credit, rounded down to eight decimal places. +Old carried credit never loses another third and survives cancellation. +Example: 400 points of usage and 1,000 new points erase 400 and carry +400; an idle Premium month carries 2,000 points. +Counters are never deleted. Pending/failed payments give no credit. +Refunds (including partial refunds) revoke the corresponding award once. + +Discovery grants **twice `Constants.DefaultFSQ`** (1,000 MiB), and Premium +**five times `Constants.DefaultFSQ`** (2,500 MiB) of disk space during each +confirmed, non-reversed paid monthly period. Free registrations use the standard quota. +The reference is the standard free quota (500 MiB), not an individual's +administratively adjusted `DiskQuota`. That base quota is never overwritten. +New user objects now use the existing 500 MiB database default rather than +the former 512 MiB CLR initializer; existing accounts retain their stored quota. +Cancellation retains the disk allowance through the UTC anniversary ending +the paid period; expiry or refund restores the base quota without deleting +existing files. Uploads exceeding available space are refused before writing. +The maximum size of one file and separate podcast limits are unchanged. +Pending/failed payments grant no disk bonus. Apply the +`ResourceSubscriptions`, `PremiumDiskQuota` and `DiscoveryDiskQuota` EF migrations to the shared +database before deploying any host. Profile and usage responses expose the +effective disk quota; usage also exposes `PremiumDiskQuotaUntil` and `DiscoveryDiskQuotaUntil`. +When paid periods overlap, Premium takes precedence until its expiry, then +any still-paid Discovery period applies. + +Configure only **Yavsc.Org** for PayPal subscriptions. API and Blogs need +the migrated shared database and matching resource weights, not PayPal +credentials. Existing Classic donation settings remain unchanged. +Use the existing `PayPal:Mode`, `PayPal:ClientId` and `PayPal:ClientSecret` +REST settings; keep credentials in user-secrets or environment variables. + +```json +{ + "ResourceSubscriptions": { + "DiscoveryPlanId": "P-REPLACEWITHDISCOVERYPLAN", + "PremiumPlanId": "P-REPLACEWITHPREMIUMPLAN", + "WebhookId": "REPLACEWITHWEBHOOKID", + "DiscoveryPriceExcludingTax": 1, + "PremiumPriceExcludingTax": 2, + "VatPercent": 20 + } +} +``` + +Before enabling checkout, create a PayPal subscription product and two +**ACTIVE** billing plans in the same REST app and environment: +one indefinite `REGULAR` cycle with `interval_unit: MONTH`, +`interval_count: 1`, `total_cycles: 0`, and EUR `fixed_price.value` +of **1.20** / **2.40** respectively. These are **already VAT-inclusive** +amounts: do not add PayPal tax, setup fees, trials, quantity support or +shipping fees. Disable billing outstanding balances in the plans +(`auto_bill_outstanding: false`) to avoid bundling missed installments +into an amount that does not match one monthly payment. +Checkout checks the configured plan against its expected TTC amount and +refuses automatic collection of outstanding balances. +Numerically invalid pricing fails startup; missing plan IDs/webhook/ +credentials returns an explicit unavailable error instead of a fake success. + +Register the HTTPS endpoint +`https:///ResourceSubscription/Notify` in that REST app. +Subscribe to `BILLING.SUBSCRIPTION.ACTIVATED`, `UPDATED`, `CANCELLED`, +`SUSPENDED`, `EXPIRED`, `PAYMENT.FAILED`, and `PAYMENT.SALE.COMPLETED`, +`REFUNDED`, `REVERSED` (the lifecycle events use the +`BILLING.SUBSCRIPTION.` prefix). Set the returned webhook ID in configuration. +The server verifies the signature through PayPal, then fetches the bound +subscription and transaction history; it never grants credit from an +unverified webhook or the browser return. Payment IDs and monthly cycles +are unique, and the same per-user PostgreSQL lock serializes usage and awards. +Delayed notifications take effect on the usage measured when reconciled. + +Users manage subscriptions at `/ResourceSubscription`, linked from the Web +footer and PostIt's About/profile pages. A browser Web login may be required +independently of PostIt's session. Create/refresh/cancel require antiforgery +tokens and ownership; the management page remains available at the quota. +**Refresh payments** reconciles missed notifications and historical refunds. +Cancellation is confirmed with PayPal, not merely recorded locally. +Only one non-terminal subscription is allowed per account. +An uncertain creation is retried with its same PayPal request ID for at +most 24 hours; an older unresolved creation requires merchant-side review +before allowing a second checkout. Do not delete that record and retry +without checking PayPal for a possible existing agreement. + +Run a real Sandbox approval, first payment, renewal, repeated webhook, +refund and cancellation before enabling production. The repository tests +use synthetic PayPal transports and do not validate merchant credentials +or PayPal account eligibility. + ## Podcast live broadcasts and replays PostIt Desktop's home page provides **Podcasts : directs et replays**. diff --git a/contrib/template.appsettings-api.json b/contrib/template.appsettings-api.json index afe693a4f..b5562aded 100644 --- a/contrib/template.appsettings-api.json +++ b/contrib/template.appsettings-api.json @@ -1,4 +1,7 @@ { + "ResourceUsage": { + "UserLimits": {} + }, "Site": { "Authority": "https://preprod-yavsc.$DOMAIN", "Audience": ["api"], diff --git a/contrib/template.appsettings-blogs.json b/contrib/template.appsettings-blogs.json index 6f8fa02bc..fa1ebc71c 100644 --- a/contrib/template.appsettings-blogs.json +++ b/contrib/template.appsettings-blogs.json @@ -1,4 +1,7 @@ { + "ResourceUsage": { + "UserLimits": {} + }, "Site": { "Title": "Yavsc Blogs", "Description": "A collection of blogs about software development, technology, and programming.", diff --git a/contrib/template.appsettings-org.json b/contrib/template.appsettings-org.json index c330f5f32..abe88244b 100644 --- a/contrib/template.appsettings-org.json +++ b/contrib/template.appsettings-org.json @@ -1,4 +1,15 @@ { + "ResourceSubscriptions": { + "DiscoveryPlanId": "", + "PremiumPlanId": "", + "WebhookId": "", + "DiscoveryPriceExcludingTax": 1, + "PremiumPriceExcludingTax": 2, + "VatPercent": 20 + }, + "ResourceUsage": { + "UserLimits": {} + }, "Logging": { "LogLevel": { "Default": "Information" diff --git a/doc/architecture/postit.md b/doc/architecture/postit.md index 4e63f1c06..85cb052be 100644 --- a/doc/architecture/postit.md +++ b/doc/architecture/postit.md @@ -99,6 +99,13 @@ de chaque téléphone. ## Administration et profil personnel +Le quota disque renvoyé par le profil est le quota effectif : Découverte donne +deux fois et Premium cinq fois le quota gratuit standard durant la mensualité confirmée payée, +y compris après résiliation jusqu'à son échéance UTC. Ensuite, ou après +remboursement, le quota habituel reprend sans suppression des fichiers. +Les téléversements dépassant l'espace disponible sont refusés avant écriture. +La taille maximale par fichier et les quotas podcast restent inchangés. + L'accueil présente un seul accès **Administration**, visible et exécutable uniquement pour le rôle exact `Administrator`. `SessionStatusViewModel.RefreshAsync` lit les rôles dans `GET account/me` après le rafraîchissement de session ; @@ -119,6 +126,33 @@ indépendamment de la visibilité du bouton dans PostIt. endpoints `account/me` pour lire et enregistrer les informations personnelles. Elle affiche aussi `resource-usage/me`, sans appeler la liste des usages globaux. Une indisponibilité de l'usage n'empêche pas la gestion du profil. +La consommation est chargée avant le profil, pour rester consultable même +si une opération de gestion reçoit un refus de quota. L'administration +charge également les usages avant ses autres outils et les conserve sur +un refus HTTP 429, tant que le rôle administrateur est toujours présent. + +L'indicateur cumulé est exprimé en points : `0,02 × appels API + secondes +CPU enregistrées + 0,1 × Mio transférés + 0,3 × Mio ajoutés au stockage`. +Le profil et les vues d'administration affichent le seuil effectif, le +solde et l'atteinte du quota, indépendamment de l'estimation financière. +La section serveur `ResourceUsage` configure les coefficients et les +seuils individuels ; voir [INSTALL.md](../../INSTALL.md#resource-consumption-and-cgu). +Le contrôle admet les coûts connus puis constate les coûts restants : +il bloque les opérations suivantes, sans interrompre strictement celles +déjà en cours. + +La page **À propos** ouvre les CGU dédiées de PostIt et de Yavsc sur +`Home/CGU` auprès de l'autorité configurée, y compris sans session. +Cette page publique présente les coefficients réellement configurés, +la période cumulée, le seuil et les limites de la mesure. +**Mon profil** et **À propos** proposent aussi un lien vers +`ResourceSubscription` sur cette même autorité. La souscription mensuelle +et la résiliation PayPal utilisent la session Web et des formulaires +antiforgery ; PostIt ne stocke ni identifiant marchand ni secret PayPal. +Le profil affiche la consommation brute, l'usage effacé et le crédit +reporté, en plus de l'indicateur net. Les crédits Découverte/Premium ne +suppriment pas les compteurs : les paiements confirmés sont rapprochés +par Yavsc.Org et leur déduction est appliquée aussi au quota API/Blogs. ### Outils d'administration du site diff --git a/src/PostIt/PostIt.Tests/AboutTermsTests.cs b/src/PostIt/PostIt.Tests/AboutTermsTests.cs new file mode 100644 index 000000000..3b4a1e7c4 --- /dev/null +++ b/src/PostIt/PostIt.Tests/AboutTermsTests.cs @@ -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); + } +} diff --git a/src/PostIt/PostIt.Tests/AdministrationAccessTests.cs b/src/PostIt/PostIt.Tests/AdministrationAccessTests.cs index fc733e3f4..30feb7b6e 100644 --- a/src/PostIt/PostIt.Tests/AdministrationAccessTests.cs +++ b/src/PostIt/PostIt.Tests/AdministrationAccessTests.cs @@ -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? 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 { usage } : usage; } else if (path.EndsWith("/users")) + { + if (ExhaustedQuota) + throw new HttpRequestException("resource_quota_exceeded", null, System.Net.HttpStatusCode.TooManyRequests); result = new List { new() { UserId = "alice-id", UserName = "alice" } }; + } else throw new InvalidOperationException($"Unexpected endpoint: {path}"); diff --git a/src/PostIt/PostIt.Tests/ResourceUsageApiClientTests.cs b/src/PostIt/PostIt.Tests/ResourceUsageApiClientTests.cs index 407183b77..963577d69 100644 --- a/src/PostIt/PostIt.Tests/ResourceUsageApiClientTests.cs +++ b/src/PostIt/PostIt.Tests/ResourceUsageApiClientTests.cs @@ -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 diff --git a/src/PostIt/PostIt/ViewModels/AdministrationPageViewModel.cs b/src/PostIt/PostIt/ViewModels/AdministrationPageViewModel.cs index bc6787229..9aef20641 100644 --- a/src/PostIt/PostIt/ViewModels/AdministrationPageViewModel.cs +++ b/src/PostIt/PostIt/ViewModels/AdministrationPageViewModel.cs @@ -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>(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(); diff --git a/src/PostIt/PostIt/ViewModels/Layout/AboutPageViewModel.cs b/src/PostIt/PostIt/ViewModels/Layout/AboutPageViewModel.cs index 1082e4828..cf6883e90 100644 --- a/src/PostIt/PostIt/ViewModels/Layout/AboutPageViewModel.cs +++ b/src/PostIt/PostIt/ViewModels/Layout/AboutPageViewModel.cs @@ -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; } } diff --git a/src/PostIt/PostIt/ViewModels/Layout/UserProfilePageViewModel.cs b/src/PostIt/PostIt/ViewModels/Layout/UserProfilePageViewModel.cs index 61f071683..9dfa45704 100644 --- a/src/PostIt/PostIt/ViewModels/Layout/UserProfilePageViewModel.cs +++ b/src/PostIt/PostIt/ViewModels/Layout/UserProfilePageViewModel.cs @@ -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(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) diff --git a/src/PostIt/PostIt/Views/Layout/AboutPage.axaml b/src/PostIt/PostIt/Views/Layout/AboutPage.axaml index 5ad594e38..aa8ec5664 100644 --- a/src/PostIt/PostIt/Views/Layout/AboutPage.axaml +++ b/src/PostIt/PostIt/Views/Layout/AboutPage.axaml @@ -34,5 +34,12 @@ + + + } + +

Le premier crédit est accordé après le premier paiement confirmé, puis à chaque mensualité payée à la date anniversaire UTC du début PayPal.

+

Un seul abonnement non résilié par compte. Pour changer de pack, résiliez d'abord le précédent. Un remboursement, même partiel, retire le crédit de la mensualité correspondante.

+

Mes abonnements

+@foreach (var subscription in Model.Subscriptions) +{ +
+

@(subscription.Pack == "Premium" ? "Premium" : "Découverte")

+

État PayPal : @subscription.State. Référence : @subscription.PayPalId

+

Tarif souscrit : @subscription.PriceExcludingTax.ToString("F2") € HT, @subscription.PriceIncludingTax.ToString("F2") € TTC. Crédit mensuel : @subscription.MonthlyPoints points.

+

Début UTC : @subscription.StartsAt?.ToUniversalTime().ToString("u"). Prochain prélèvement UTC : @subscription.NextBillingAt?.ToUniversalTime().ToString("u").

+ @if (subscription.PayPalId is not null && subscription.State is not ("CANCELLED" or "EXPIRED")) + { +
+ + +
+ } +
+} +
+ +
+

CGU de PostIt et de Yavsc

diff --git a/src/Yavsc.Org/Views/Shared/_Layout.cshtml b/src/Yavsc.Org/Views/Shared/_Layout.cshtml index 653ffae0a..be37b3a63 100644 --- a/src/Yavsc.Org/Views/Shared/_Layout.cshtml +++ b/src/Yavsc.Org/Views/Shared/_Layout.cshtml @@ -42,6 +42,8 @@ diff --git a/src/Yavsc.Org/appsettings-org.json b/src/Yavsc.Org/appsettings-org.json index 25e017bfd..4d725f11a 100644 --- a/src/Yavsc.Org/appsettings-org.json +++ b/src/Yavsc.Org/appsettings-org.json @@ -1,4 +1,15 @@ { + "ResourceSubscriptions": { + "DiscoveryPlanId": "", + "PremiumPlanId": "", + "WebhookId": "", + "DiscoveryPriceExcludingTax": 1, + "PremiumPriceExcludingTax": 2, + "VatPercent": 20 + }, + "ResourceUsage": { + "UserLimits": {} + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/src/Yavsc.Server/Helpers/FileSystemHelpers.cs b/src/Yavsc.Server/Helpers/FileSystemHelpers.cs index 3d903e36f..d2d587f7e 100644 --- a/src/Yavsc.Server/Helpers/FileSystemHelpers.cs +++ b/src/Yavsc.Server/Helpers/FileSystemHelpers.cs @@ -166,6 +166,12 @@ namespace Yavsc.Server.Helpers } public static FileReceivedInfo ReceiveUserFile(this ApplicationUser user, string root, IFormFile f, string destFileName = null) { + var name = AbstractFileSystemHelpers.FilterFileName(destFileName ?? f.FileName); + var destination = new FileInfo(Path.Combine(root, name)); + var replacedBytes = destination.Exists ? destination.Length : 0L; + var available = user.GetEffectiveDiskQuota(DateTimeOffset.UtcNow) - user.DiskUsage + replacedBytes; + if (f.Length > available) + return new FileReceivedInfo(root, name) { QuotaOffense = true }; // f.FileName is parsed by ASP.NET Core from the // Content-Disposition header: it handles unquoted // values and the RFC 5987 filename* parameter that @@ -203,6 +209,7 @@ namespace Yavsc.Server.Helpers // TODO lock user's disk usage for this scope, // this process is not safe at concurrent access. long usage = user.DiskUsage; + var quota = user.GetEffectiveDiskQuota(DateTimeOffset.UtcNow); var item = new FileReceivedInfo (root, AbstractFileSystemHelpers.FilterFileName(destFileName)); @@ -212,8 +219,15 @@ namespace Yavsc.Server.Helpers item.Overridden = true; usage -= fi.Length; } + if (usage >= quota) + { + inputStream.Dispose(); + item.QuotaOffense = true; + return item; + } using (var dest = fi.OpenWrite()) { + dest.SetLength(0); using (inputStream) { const int blen = 1024; @@ -221,16 +235,20 @@ namespace Yavsc.Server.Helpers int len = 0; while (!token.IsCancellationRequested && (len = inputStream.Read(buffer, 0, blen)) > 0) { + if (len > quota - usage) + { + item.QuotaOffense = true; + break; + } dest.Write(buffer, 0, len); usage += len; - if (usage >= user.DiskQuota) break; } user.DiskUsage = usage; dest.Close(); inputStream.Close(); } } - if (usage >= user.DiskQuota) + if (usage > quota) { item.QuotaOffense = true; } diff --git a/src/Yavsc.Server/Helpers/ResourceUsageCountingStream.cs b/src/Yavsc.Server/Helpers/ResourceUsageCountingStream.cs new file mode 100644 index 000000000..926e94352 --- /dev/null +++ b/src/Yavsc.Server/Helpers/ResourceUsageCountingStream.cs @@ -0,0 +1,51 @@ +#nullable enable +namespace Yavsc.Server.Helpers; + +internal sealed class ResourceUsageCountingStream(Stream inner) : Stream +{ + private long _bytesRead; + private long _bytesWritten; + public long BytesRead => Interlocked.Read(ref _bytesRead); + public long BytesWritten => Interlocked.Read(ref _bytesWritten); + public override bool CanRead => inner.CanRead; + public override bool CanSeek => inner.CanSeek; + public override bool CanWrite => inner.CanWrite; + public override long Length => inner.Length; + public override long Position { get => inner.Position; set => inner.Position = value; } + public override void Flush() => inner.Flush(); + public override Task FlushAsync(CancellationToken cancellationToken) => inner.FlushAsync(cancellationToken); + public override long Seek(long offset, SeekOrigin origin) => inner.Seek(offset, origin); + public override void SetLength(long value) => inner.SetLength(value); + + public override int Read(byte[] buffer, int offset, int count) + { + var read = inner.Read(buffer, offset, count); + Interlocked.Add(ref _bytesRead, read); + return read; + } + + public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + => ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); + + public override async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) + { + var read = await inner.ReadAsync(buffer, cancellationToken); + Interlocked.Add(ref _bytesRead, read); + return read; + } + + public override void Write(byte[] buffer, int offset, int count) + { + inner.Write(buffer, offset, count); + Interlocked.Add(ref _bytesWritten, count); + } + + public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + => WriteAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); + + public override async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) + { + await inner.WriteAsync(buffer, cancellationToken); + Interlocked.Add(ref _bytesWritten, buffer.Length); + } +} diff --git a/src/Yavsc.Server/Helpers/ResourceUsageCredit.cs b/src/Yavsc.Server/Helpers/ResourceUsageCredit.cs new file mode 100644 index 000000000..230902fe6 --- /dev/null +++ b/src/Yavsc.Server/Helpers/ResourceUsageCredit.cs @@ -0,0 +1,26 @@ +namespace Yavsc.Server.Helpers; + +public static class ResourceUsageCredit +{ + public static decimal NetConsumption(decimal gross, decimal credits) => Math.Max(0m, gross - credits); + public static decimal Balance(decimal gross, decimal credits) => Math.Max(0m, credits - gross); + + public static decimal MonthlyGrant(decimal gross, decimal existingCredits, decimal allowance) + { + ArgumentOutOfRangeException.ThrowIfNegative(gross); + ArgumentOutOfRangeException.ThrowIfNegative(existingCredits); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(allowance); + var erased = Math.Min(allowance, NetConsumption(gross, existingCredits)); + // Round down only the carried credit to the persistence precision. + var carried = decimal.Round((allowance - erased) * 2m / 3m, 8, MidpointRounding.ToZero); + return decimal.Round(erased + carried, 8, MidpointRounding.ToZero); + } + + public static int BillingCycle(DateTimeOffset anchor, DateTimeOffset paidAt) + { + if (paidAt < anchor) throw new ArgumentOutOfRangeException(nameof(paidAt)); + var cycle = (paidAt.Year - anchor.Year) * 12 + paidAt.Month - anchor.Month; + if (anchor.AddMonths(cycle) > paidAt) cycle--; + return cycle; + } +} diff --git a/src/Yavsc.Server/Helpers/ResourceUsageExemptAttribute.cs b/src/Yavsc.Server/Helpers/ResourceUsageExemptAttribute.cs new file mode 100644 index 000000000..2f67b668e --- /dev/null +++ b/src/Yavsc.Server/Helpers/ResourceUsageExemptAttribute.cs @@ -0,0 +1,4 @@ +namespace Yavsc.Server.Helpers; + +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] +public sealed class ResourceUsageExemptAttribute : Attribute; diff --git a/src/Yavsc.Server/Helpers/ResourceUsageService.cs b/src/Yavsc.Server/Helpers/ResourceUsageService.cs new file mode 100644 index 000000000..10ccdd27e --- /dev/null +++ b/src/Yavsc.Server/Helpers/ResourceUsageService.cs @@ -0,0 +1,98 @@ +#nullable enable +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Yavsc.Abstract.Resources; +using Yavsc.Models; +using Yavsc.Settings; + +namespace Yavsc.Server.Helpers; + +public interface IResourceUsageService +{ + Task GetCurrentAsync(string userId, string userName, CancellationToken cancellationToken); + Task> GetOverviewAsync(CancellationToken cancellationToken); + Task TryAdmitAsync(string userId, string userName, decimal apiCalls, decimal bandwidthMb, + long anticipatedStorageBytes, CancellationToken cancellationToken); +} + +public sealed class ResourceUsageService( + ApplicationDbContext dbContext, + IOptions options, + TimeProvider? clock = null) : IResourceUsageService +{ + private readonly ResourceUsageOptions _options = options.Value; + + public async Task GetCurrentAsync( + string userId, string userName, CancellationToken cancellationToken) + { + var record = await ResourceUsageTracker.UpdateAsync(dbContext, userId, userName, + cancellationToken: cancellationToken); + var user = await dbContext.Users.AsNoTracking().SingleOrDefaultAsync(u => u.Id == userId, cancellationToken); + return ToSummary(record ?? throw new InvalidOperationException("Usage initialization failed."), user); + } + + public async Task> GetOverviewAsync(CancellationToken cancellationToken) + { + var records = await dbContext.ResourceUsageRecords.AsNoTracking() + .OrderByDescending(r => r.RecordedAtUtc).ThenByDescending(r => r.Id) + .ToListAsync(cancellationToken); + var ids = records.Select(r => r.UserId).Distinct().ToArray(); + var users = await dbContext.Users.AsNoTracking().Where(u => ids.Contains(u.Id)).ToDictionaryAsync(u => u.Id, cancellationToken); + return records.GroupBy(r => r.UserId).Select(g => ToSummary(g.First(), users.GetValueOrDefault(g.Key))).ToList(); + } + + public async Task TryAdmitAsync( + string userId, string userName, decimal apiCalls, decimal bandwidthMb, + long anticipatedStorageBytes, CancellationToken cancellationToken) + => await ResourceUsageTracker.UpdateAsync(dbContext, userId, userName, + apiCalls: apiCalls, bandwidthMb: bandwidthMb, + policy: _options, anticipatedStorageBytes: anticipatedStorageBytes, + cancellationToken: cancellationToken) is not null; + + private ResourceUsageSummary ToSummary(ResourceUsageRecord record, ApplicationUser? user) + { + var gross = _options.Calculate(record.ApiCalls, record.CpuSeconds, record.BandwidthMb, record.StorageMb); + var consumption = ResourceUsageCredit.NetConsumption(gross, record.CreditPoints); + var limit = _options.GetLimit(record.UserId); + return new ResourceUsageSummary + { + UserId = record.UserId, + UserName = record.UserName, + RecordedAtUtc = new DateTimeOffset(DateTime.SpecifyKind(record.RecordedAtUtc, DateTimeKind.Utc)), + ApiCalls = record.ApiCalls, + CpuSeconds = record.CpuSeconds, + BandwidthMb = record.BandwidthMb, + StorageMb = record.StorageMb, + EstimatedCompensation = record.EstimatedCompensation, + Currency = record.Currency, + Consumption = consumption, + GrossConsumption = gross, + CreditBalance = ResourceUsageCredit.Balance(gross, record.CreditPoints), + AppliedCredits = Math.Min(gross, record.CreditPoints), + ConsumptionLimit = limit, + RemainingConsumption = Math.Max(0m, limit - consumption) + ResourceUsageCredit.Balance(gross, record.CreditPoints), + IsQuotaReached = consumption >= limit, + DiskQuota = user?.GetEffectiveDiskQuota((clock ?? TimeProvider.System).GetUtcNow()), + PremiumDiskQuotaUntil = user?.PremiumDiskQuotaUntil, + DiscoveryDiskQuotaUntil = user?.DiscoveryDiskQuotaUntil + }; + } +} + +public static class ResourceUsageServiceCollectionExtensions +{ + public static IServiceCollection AddResourceUsage(this IServiceCollection services, IConfiguration configuration) + { + services.AddOptions() + .Bind(configuration.GetSection(ResourceUsageOptions.SectionName)) + .Validate(o => o.ApiCallWeight > 0m && o.CpuSecondWeight >= 0m + && o.BandwidthMbWeight >= 0m && o.StorageMbWeight >= 0m, + "ResourceUsage weights must be non-negative and ApiCallWeight must be positive.") + .Validate(o => o.DefaultLimit > 0m && o.UserLimits.All(p => !string.IsNullOrWhiteSpace(p.Key) && p.Value > 0m), + "ResourceUsage limits must be positive and keyed by user ID.") + .ValidateOnStart(); + return services.AddScoped(); + } +} diff --git a/src/Yavsc.Server/Helpers/ResourceUsageTelemetryMiddleware.cs b/src/Yavsc.Server/Helpers/ResourceUsageTelemetryMiddleware.cs index 8b1795167..9e0fd4365 100644 --- a/src/Yavsc.Server/Helpers/ResourceUsageTelemetryMiddleware.cs +++ b/src/Yavsc.Server/Helpers/ResourceUsageTelemetryMiddleware.cs @@ -1,54 +1,86 @@ #nullable enable using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Yavsc.Models; namespace Yavsc.Server.Helpers; -public sealed class ResourceUsageTelemetryMiddleware +public sealed class ResourceUsageTelemetryMiddleware( + RequestDelegate next, + IServiceScopeFactory scopeFactory, + ILogger logger) { - private readonly RequestDelegate _next; - private readonly IServiceScopeFactory _scopeFactory; - - public ResourceUsageTelemetryMiddleware( - RequestDelegate next, - IServiceScopeFactory scopeFactory) - { - _next = next; - _scopeFactory = scopeFactory; - } - - public async Task InvokeAsync(HttpContext context) + public async Task InvokeAsync(HttpContext context, IResourceUsageService usage) { var userId = context.User.GetUserId(); - if (string.IsNullOrWhiteSpace(userId)) + if (string.IsNullOrWhiteSpace(userId) || IsExempt(context)) { - await _next(context); + await next(context); return; } var userName = context.User.Identity?.Name ?? userId; var requestBytes = context.Request.ContentLength ?? 0L; + if (!await usage.TryAdmitAsync(userId, userName, 1m, ToMb(requestBytes), 0, + context.RequestAborted)) + { + logger.LogWarning("Resource quota refused a request for user {UserId}", userId); + await WriteQuotaProblemAsync(context); + return; + } + var originalRequest = context.Request.Body; + var originalResponse = context.Response.Body; + using var request = new ResourceUsageCountingStream(originalRequest); + using var response = new ResourceUsageCountingStream(originalResponse); + context.Request.Body = request; + context.Response.Body = response; try { - await _next(context); + await next(context); } finally { - var responseBytes = context.Response.ContentLength ?? 0L; - var totalBytes = requestBytes + responseBytes; - var bandwidthMb = totalBytes <= 0 ? 0m : (decimal)totalBytes / (1024m * 1024m); - - using var scope = _scopeFactory.CreateScope(); + context.Request.Body = originalRequest; + context.Response.Body = originalResponse; + // Known ingress was charged at admission. Count streamed responses + // and unknown-length ingress once, even on failure or cancellation. + var extraBytes = Math.Max(0L, request.BytesRead - requestBytes) + response.BytesWritten; + using var scope = scopeFactory.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); - await ResourceUsageTracker.RecordAsync( - db, - userId, - userName, - apiCalls: 1m, - bandwidthMb: bandwidthMb, - actorUserId: userId); + await ResourceUsageTracker.RecordAsync(db, userId, userName, + bandwidthMb: ToMb(extraBytes), actorUserId: userId); } } + + public static async Task WriteQuotaProblemAsync(HttpContext context) + { + var site = context.RequestServices.GetRequiredService>().Value; + var termsUrl = new Uri(new Uri(site.Authority.TrimEnd('/') + "/"), "Home/CGU").AbsoluteUri; + context.Response.StatusCode = StatusCodes.Status429TooManyRequests; + var problem = new ProblemDetails + { + Status = StatusCodes.Status429TooManyRequests, + Title = "Quota de consommation insuffisant", + Detail = "Cette opération dépasse le quota disponible. Consultez votre consommation ou contactez l'administration.", + Instance = context.Request.Path + }; + problem.Extensions["code"] = "resource_quota_exceeded"; + problem.Extensions["usagePath"] = Constants.APIPrefix + "/" + Constants.ResourceUsagePath + "/me"; + problem.Extensions["termsUrl"] = termsUrl; + problem.Extensions["subscriptionUrl"] = new Uri(new Uri(site.Authority.TrimEnd('/') + "/"), "ResourceSubscription").AbsoluteUri; + await context.Response.WriteAsJsonAsync(problem, + options: null, contentType: "application/problem+json", cancellationToken: context.RequestAborted); + } + + private static decimal ToMb(long bytes) => (decimal)bytes / (1024m * 1024m); + + private static bool IsExempt(HttpContext context) + => context.GetEndpoint()?.Metadata.GetMetadata() is not null + || context.Request.Path.Equals("/login", StringComparison.OrdinalIgnoreCase) + || context.Request.Path.Equals("/refresh", StringComparison.OrdinalIgnoreCase) + || context.Request.Path.Equals("/logout", StringComparison.OrdinalIgnoreCase); } diff --git a/src/Yavsc.Server/Helpers/ResourceUsageTracker.cs b/src/Yavsc.Server/Helpers/ResourceUsageTracker.cs index cad05a699..873dc10c4 100644 --- a/src/Yavsc.Server/Helpers/ResourceUsageTracker.cs +++ b/src/Yavsc.Server/Helpers/ResourceUsageTracker.cs @@ -1,11 +1,14 @@ #nullable enable using Microsoft.EntityFrameworkCore; using Yavsc.Models; +using Yavsc.Settings; namespace Yavsc.Server.Helpers; public static class ResourceUsageTracker { + private static readonly SemaphoreSlim LocalGate = new(1, 1); + public static async Task RecordAsync( ApplicationDbContext dbContext, string userId, @@ -15,55 +18,103 @@ public static class ResourceUsageTracker decimal apiCalls = 0m, decimal bandwidthMb = 0m, string? actorUserId = null, - string currency = Constants.ResourceUsageDefaultCurrency) + string currency = Constants.ResourceUsageDefaultCurrency, + CancellationToken cancellationToken = default) { - if (dbContext is null || string.IsNullOrWhiteSpace(userId)) + await UpdateAsync(dbContext, userId, userName, cpuSeconds, storageBytes, + apiCalls, bandwidthMb, actorUserId, currency, cancellationToken: cancellationToken); + } + + internal static async Task UpdateAsync( + ApplicationDbContext dbContext, string userId, string userName, + decimal cpuSeconds = 0m, long storageBytes = 0, decimal apiCalls = 0m, + decimal bandwidthMb = 0m, string? actorUserId = null, + string currency = Constants.ResourceUsageDefaultCurrency, + ResourceUsageOptions? policy = null, long anticipatedStorageBytes = 0, + CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(dbContext); + ArgumentException.ThrowIfNullOrWhiteSpace(userId); + ArgumentOutOfRangeException.ThrowIfNegative(cpuSeconds); + ArgumentOutOfRangeException.ThrowIfNegative(storageBytes); + ArgumentOutOfRangeException.ThrowIfNegative(apiCalls); + ArgumentOutOfRangeException.ThrowIfNegative(bandwidthMb); + ArgumentOutOfRangeException.ThrowIfNegative(anticipatedStorageBytes); + + return await WithUserLockAsync(dbContext, userId, async () => { - return; - } + var effectiveActor = string.IsNullOrWhiteSpace(actorUserId) ? userId : actorUserId; + var now = DateTime.UtcNow; + var storageMb = (decimal)storageBytes / (1024m * 1024m); + var record = await dbContext.ResourceUsageRecords + .Where(r => r.UserId == userId) + .OrderByDescending(r => r.RecordedAtUtc).ThenByDescending(r => r.Id) + .FirstOrDefaultAsync(cancellationToken); + if (record is not null) + await dbContext.Entry(record).ReloadAsync(cancellationToken); - var effectiveActor = string.IsNullOrWhiteSpace(actorUserId) ? userId : actorUserId; - var now = DateTime.UtcNow; - var storageMb = storageBytes <= 0 ? 0m : (decimal)storageBytes / (1024m * 1024m); - - var record = await dbContext.ResourceUsageRecords - .Where(r => r.UserId == userId) - .OrderByDescending(r => r.RecordedAtUtc) - .FirstOrDefaultAsync(); - - if (record is null) - { - record = new ResourceUsageRecord + if (policy is not null) { - UserId = userId, - UserName = string.IsNullOrWhiteSpace(userName) ? userId : userName, - RecordedAtUtc = now, - ApiCalls = apiCalls, - CpuSeconds = cpuSeconds, - BandwidthMb = bandwidthMb, - StorageMb = storageMb, - EstimatedCompensation = 0m, - Currency = currency, - UserCreated = effectiveActor, - UserModified = effectiveActor, - DateCreated = now, - DateModified = now, - }; - dbContext.ResourceUsageRecords.Add(record); - await dbContext.SaveChangesAsync(effectiveActor); - return; + var gross = record is null ? 0m : + policy.Calculate(record.ApiCalls, record.CpuSeconds, record.BandwidthMb, record.StorageMb); + var current = ResourceUsageCredit.NetConsumption(gross, record?.CreditPoints ?? 0m); + var projected = ResourceUsageCredit.NetConsumption(gross + policy.Calculate(apiCalls, cpuSeconds, bandwidthMb, + storageMb + (decimal)anticipatedStorageBytes / (1024m * 1024m)), record?.CreditPoints ?? 0m); + var limit = policy.GetLimit(userId); + if (current >= limit || projected > limit) return null; + } + + if (record is not null && apiCalls == 0m && cpuSeconds == 0m && bandwidthMb == 0m && storageBytes == 0) + return record; + + if (record is null) + { + record = new ResourceUsageRecord + { + UserId = userId, + UserName = string.IsNullOrWhiteSpace(userName) ? userId : userName, + Currency = currency, + UserCreated = effectiveActor, + DateCreated = now + }; + dbContext.ResourceUsageRecords.Add(record); + } + + record.UserName = string.IsNullOrWhiteSpace(userName) ? record.UserName : userName; + record.RecordedAtUtc = now; + record.ApiCalls += apiCalls; + record.CpuSeconds += cpuSeconds; + record.BandwidthMb += bandwidthMb; + record.StorageMb += storageMb; + record.Currency = string.IsNullOrWhiteSpace(currency) ? record.Currency : currency; + record.DateModified = now; + record.UserModified = effectiveActor; + + await dbContext.SaveChangesAsync(effectiveActor, cancellationToken); + return record; + }, cancellationToken); + } + + public static async Task WithUserLockAsync(ApplicationDbContext dbContext, string userId, + Func> action, CancellationToken cancellationToken) + { + // This lock is shared by accounting and subscription credits on all hosts. + var postgres = dbContext.Database.IsNpgsql(); + if (!postgres) await LocalGate.WaitAsync(cancellationToken); + try + { + await using var transaction = postgres && dbContext.Database.CurrentTransaction is null + ? await dbContext.Database.BeginTransactionAsync(cancellationToken) : null; + if (postgres) + await dbContext.Database.ExecuteSqlInterpolatedAsync( + $"SELECT pg_advisory_xact_lock(hashtextextended({userId}, 781932))", cancellationToken); + var result = await action(); + if (transaction is not null) await transaction.CommitAsync(cancellationToken); + return result; + } + finally + { + if (!postgres) LocalGate.Release(); } - - record.UserName = string.IsNullOrWhiteSpace(userName) ? record.UserName : userName; - record.RecordedAtUtc = now; - record.ApiCalls += apiCalls; - record.CpuSeconds += cpuSeconds; - record.BandwidthMb += bandwidthMb; - record.StorageMb += storageMb; - record.Currency = string.IsNullOrWhiteSpace(currency) ? record.Currency : currency; - record.DateModified = now; - record.UserModified = effectiveActor; - - await dbContext.SaveChangesAsync(effectiveActor); } } diff --git a/src/Yavsc.Server/Models/ApplicationDbContext.cs b/src/Yavsc.Server/Models/ApplicationDbContext.cs index ac4ecaff1..35b26ab73 100644 --- a/src/Yavsc.Server/Models/ApplicationDbContext.cs +++ b/src/Yavsc.Server/Models/ApplicationDbContext.cs @@ -96,6 +96,21 @@ namespace Yavsc.Models entity.HasIndex(r => new { r.UserId, r.RecordedAtUtc }); entity.Property(r => r.Currency).HasMaxLength(8).HasDefaultValue(Constants.ResourceUsageDefaultCurrency); entity.Property(r => r.UserName).HasMaxLength(256); + entity.Property(r => r.CreditPoints).HasPrecision(28, 8).HasDefaultValue(0m); + }); + + builder.Entity(entity => + { + entity.HasIndex(s => s.PayPalId).IsUnique(); + entity.HasIndex(s => s.UserId); + entity.HasOne().WithMany().HasForeignKey(s => s.UserId).OnDelete(DeleteBehavior.Restrict); + entity.Property(s => s.MonthlyPoints).HasPrecision(28, 8); + }); + builder.Entity(entity => + { + entity.HasIndex(p => new { p.SubscriptionId, p.Cycle }).IsUnique(); + entity.HasOne().WithMany().HasForeignKey(p => p.SubscriptionId).OnDelete(DeleteBehavior.Restrict); + entity.Property(p => p.GrantedPoints).HasPrecision(28, 8); }); // Customize the ASP.NET Identity model and override the defaults if needed. @@ -365,6 +380,8 @@ namespace Yavsc.Models public DbSet Estimates { get; set; } public DbSet Signatures { get; set; } public DbSet ResourceUsageRecords { get; set; } + public DbSet ResourceSubscriptions { get; set; } + public DbSet ResourceSubscriptionPayments { get; set; } public DbSet BankStatus { get; set; } public DbSet BalanceImpact { get; set; } diff --git a/src/Yavsc.Server/Models/ApplicationUser.cs b/src/Yavsc.Server/Models/ApplicationUser.cs index b2eb2c458..1f7c95e8b 100644 --- a/src/Yavsc.Server/Models/ApplicationUser.cs +++ b/src/Yavsc.Server/Models/ApplicationUser.cs @@ -93,7 +93,16 @@ namespace Yavsc.Models public virtual List? BankInfo { get; set; } - public long DiskQuota { get; set; } = 512 * 1024 * 1024; + public long DiskQuota { get; set; } = Constants.DefaultFSQ; + [JsonIgnore, System.Text.Json.Serialization.JsonIgnore] + public DateTimeOffset? PremiumDiskQuotaUntil { get; set; } + [JsonIgnore, System.Text.Json.Serialization.JsonIgnore] + public DateTimeOffset? DiscoveryDiskQuotaUntil { get; set; } + + public long GetEffectiveDiskQuota(DateTimeOffset now) + => PremiumDiskQuotaUntil > now ? checked(Constants.DefaultFSQ * 5L) + : DiscoveryDiskQuotaUntil > now ? checked(Constants.DefaultFSQ * 2L) : DiskQuota; + public long DiskUsage { get; set; } = 0; public long MaxFileSize { get; set; } = 512 * 1024 * 1024; diff --git a/src/Yavsc.Server/Models/Payment/ResourceSubscription.cs b/src/Yavsc.Server/Models/Payment/ResourceSubscription.cs new file mode 100644 index 000000000..27a9d342c --- /dev/null +++ b/src/Yavsc.Server/Models/Payment/ResourceSubscription.cs @@ -0,0 +1,41 @@ +#nullable enable +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace Yavsc.Models.Payment; + +public sealed class ResourceSubscription +{ + public Guid Id { get; set; } + [MaxLength(450)] + public string UserId { get; set; } = ""; + [MaxLength(32)] + public string Pack { get; set; } = ""; + [MaxLength(64)] + public string PlanId { get; set; } = ""; + [MaxLength(64)] + public string? PayPalId { get; set; } + [MaxLength(32)] + public string State { get; set; } = "CREATING"; + public decimal MonthlyPoints { get; set; } + [Column(TypeName = "numeric(18,2)")] + public decimal PriceExcludingTax { get; set; } + public decimal VatPercent { get; set; } + [Column(TypeName = "numeric(18,2)")] + public decimal PriceIncludingTax { get; set; } + public DateTimeOffset CreatedAt { get; set; } + public DateTimeOffset? StartsAt { get; set; } + public DateTimeOffset? NextBillingAt { get; set; } + public int LastCreditedCycle { get; set; } = -1; +} + +public sealed class ResourceSubscriptionPayment +{ + [MaxLength(64)] + public string Id { get; set; } = ""; + public Guid SubscriptionId { get; set; } + public int Cycle { get; set; } + public DateTimeOffset PaidAt { get; set; } + public decimal GrantedPoints { get; set; } + public bool Reversed { get; set; } +} diff --git a/src/Yavsc.Server/Models/ResourceUsageRecord.cs b/src/Yavsc.Server/Models/ResourceUsageRecord.cs index 4b8a8794d..784283272 100644 --- a/src/Yavsc.Server/Models/ResourceUsageRecord.cs +++ b/src/Yavsc.Server/Models/ResourceUsageRecord.cs @@ -23,6 +23,7 @@ public sealed class ResourceUsageRecord : ITrackedEntity public decimal CpuSeconds { get; set; } public decimal BandwidthMb { get; set; } public decimal StorageMb { get; set; } + public decimal CreditPoints { get; set; } public decimal EstimatedCompensation { get; set; } [Required] diff --git a/src/Yavsc.Server/Services/LiveProcessor.cs b/src/Yavsc.Server/Services/LiveProcessor.cs index 02bc5104d..1c84dea30 100644 --- a/src/Yavsc.Server/Services/LiveProcessor.cs +++ b/src/Yavsc.Server/Services/LiveProcessor.cs @@ -447,13 +447,18 @@ public sealed class LiveProcessor : ILiveProcessor, IDisposable var path = Path.Combine(destDir, Path.GetFileName(fileName)); var oldLength = File.Exists(path) ? new FileInfo(path).Length : 0; var usage = user.DiskUsage - oldLength; + if (usage >= user.GetEffectiveDiskQuota(_clock.GetUtcNow())) + { + await handler.Socket.CloseOutputAsync(WebSocketCloseStatus.PolicyViolation, "Available quota required.", ct); + return false; + } await using var file = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read); var buffer = new byte[Constants.WebSocketsMaxBufLen]; while (handler.Socket.State == WebSocketState.Open) { var received = await handler.Socket.ReceiveAsync(new ArraySegment(buffer), ct); if (received.MessageType == WebSocketMessageType.Close) break; - if (received.MessageType != WebSocketMessageType.Binary || usage + received.Count > user.DiskQuota) + if (received.MessageType != WebSocketMessageType.Binary || usage + received.Count > user.GetEffectiveDiskQuota(_clock.GetUtcNow())) { await handler.Socket.CloseOutputAsync(WebSocketCloseStatus.PolicyViolation, "Binary data and available quota required.", ct); return false; diff --git a/src/Yavsc.Server/Settings/ResourceSubscriptionOptions.cs b/src/Yavsc.Server/Settings/ResourceSubscriptionOptions.cs new file mode 100644 index 000000000..f17572cbe --- /dev/null +++ b/src/Yavsc.Server/Settings/ResourceSubscriptionOptions.cs @@ -0,0 +1,15 @@ +#nullable enable +namespace Yavsc.Settings; + +public sealed class ResourceSubscriptionOptions +{ + public const string SectionName = "ResourceSubscriptions"; + public string DiscoveryPlanId { get; set; } = ""; + public string PremiumPlanId { get; set; } = ""; + public string WebhookId { get; set; } = ""; + public decimal DiscoveryPriceExcludingTax { get; set; } = 1m; + public decimal PremiumPriceExcludingTax { get; set; } = 2m; + public decimal VatPercent { get; set; } = 20m; + + public decimal TotalPrice(decimal net) => decimal.Round(net * (1m + VatPercent / 100m), 2, MidpointRounding.AwayFromZero); +} diff --git a/src/Yavsc.Server/Settings/ResourceUsageOptions.cs b/src/Yavsc.Server/Settings/ResourceUsageOptions.cs new file mode 100644 index 000000000..2645e4cca --- /dev/null +++ b/src/Yavsc.Server/Settings/ResourceUsageOptions.cs @@ -0,0 +1,21 @@ +#nullable enable +namespace Yavsc.Settings; + +public sealed class ResourceUsageOptions +{ + public const string SectionName = "ResourceUsage"; + + public decimal ApiCallWeight { get; set; } = 0.2m; + public decimal CpuSecondWeight { get; set; } = 0.1m; + public decimal BandwidthMbWeight { get; set; } = 0.05m; + public decimal StorageMbWeight { get; set; } = 0.5m; + public decimal DefaultLimit { get; set; } = 1000m; + public Dictionary UserLimits { get; set; } = new(); + + public decimal GetLimit(string userId) + => UserLimits.TryGetValue(userId, out var limit) ? limit : DefaultLimit; + + public decimal Calculate(decimal apiCalls, decimal cpuSeconds, decimal bandwidthMb, decimal storageMb) + => apiCalls * ApiCallWeight + cpuSeconds * CpuSecondWeight + + bandwidthMb * BandwidthMbWeight + storageMb * StorageMbWeight; +} diff --git a/src/test/Yavsc.Api.Tests/Fixtures/ApiWebServerFixture.cs b/src/test/Yavsc.Api.Tests/Fixtures/ApiWebServerFixture.cs index 30815c4ec..c6dcd21d9 100644 --- a/src/test/Yavsc.Api.Tests/Fixtures/ApiWebServerFixture.cs +++ b/src/test/Yavsc.Api.Tests/Fixtures/ApiWebServerFixture.cs @@ -21,6 +21,7 @@ using Yavsc.Interface; using Yavsc.Server.Hubs; using Microsoft.AspNetCore.Identity; using Yavsc.Api.Services; +using Yavsc.Server.Helpers; namespace Yavsc.Api.Test.Fixtures; @@ -39,6 +40,7 @@ public sealed class ApiWebServerFixture : WebHostFixture protected override WebApplication BuildApp(WebApplicationBuilder builder) { + builder.Services.AddResourceUsage(builder.Configuration); if (UseNpgsqlProvider()) { var npgsqlConnectionString = EnsureNpgsqlDatabaseCreated(); @@ -76,7 +78,7 @@ public sealed class ApiWebServerFixture : WebHostFixture builder.Services.AddPodcasts(builder.Configuration); builder.Services.Configure(options => options.RootPath = podcastRoot); builder.Services.Configure(_ => { }); - builder.Services.Configure(settings => settings.Authority = "example.test"); + builder.Services.Configure(settings => settings.Authority = "https://example.test"); builder.Services.AddTransient(); builder.Services.AddInvoicing(builder.Configuration); builder.Services.AddSingleton(); @@ -130,6 +132,7 @@ public sealed class ApiWebServerFixture : WebHostFixture app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); + app.UseMiddleware(); app.MapControllers(); app.MapOpenApi().RequireAuthorization("AdministratorOnly"); diff --git a/src/test/Yavsc.Api.Tests/LiveProcessorTests.cs b/src/test/Yavsc.Api.Tests/LiveProcessorTests.cs index 53b18da8c..85249f13e 100644 --- a/src/test/Yavsc.Api.Tests/LiveProcessorTests.cs +++ b/src/test/Yavsc.Api.Tests/LiveProcessorTests.cs @@ -217,6 +217,29 @@ public sealed class LiveProcessorTests : IDisposable if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true); } + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task Legacy_stream_uses_paid_premium_quota_and_refuses_growth_after_expiry(bool paid) + { + var clock = new Clock(); + var processor = New(clock: clock); + Directory.CreateDirectory(_root); + var path = Path.Combine(_root, "disk.ts"); + await File.WriteAllBytesAsync(path, new byte[] { 9 }, _ct); + using var socket = new IncomingSocket(new byte[] { 1, 2, 3 }); + var context = new DefaultHttpContext(); + context.Features.Set(socket); + var user = new ApplicationUser + { + UserName = "legacy", DiskQuota = 10, DiskUsage = 100, + PremiumDiskQuotaUntil = clock.Now.AddSeconds(paid ? 1 : -1) + }; + Assert.Equal(paid, await processor.AcceptStream(context, user, _root, "disk.ts")); + Assert.Equal(paid ? new byte[] { 1, 2, 3 } : new byte[] { 9 }, await File.ReadAllBytesAsync(path, _ct)); + Assert.Empty(processor.Casters); + } + private sealed class Clock : TimeProvider { public DateTimeOffset Now { get; set; } = DateTimeOffset.UtcNow; diff --git a/src/test/Yavsc.Api.Tests/ResourceUsageApiControllerTests.cs b/src/test/Yavsc.Api.Tests/ResourceUsageApiControllerTests.cs index 836ab7929..694b8f624 100644 --- a/src/test/Yavsc.Api.Tests/ResourceUsageApiControllerTests.cs +++ b/src/test/Yavsc.Api.Tests/ResourceUsageApiControllerTests.cs @@ -40,6 +40,36 @@ public sealed class ResourceUsageApiControllerTests : IClassFixture(); + var user = new Yavsc.Models.ApplicationUser + { + Id = userId, UserName = userId, NormalizedUserName = userId.ToUpperInvariant(), + Email = $"{userId}@example.test", DiskQuota = 1234L, + PremiumDiskQuotaUntil = premium ? DateTimeOffset.UtcNow.AddDays(1) : null, + DiscoveryDiskQuotaUntil = premium ? null : DateTimeOffset.UtcNow.AddDays(1) + }; + db.Users.Add(user); + await db.SaveChangesAsync(ct); + using var http = NewClient(_fixture, userId); + var profile = await http.GetFromJsonAsync("/api/v1/account/me", ct); + Assert.NotNull(profile); + Assert.Equal(Yavsc.Constants.DefaultFSQ * multiplier, profile.DiskQuota); + user.PremiumDiskQuotaUntil = DateTimeOffset.UtcNow.AddSeconds(-1); + user.DiscoveryDiskQuotaUntil = DateTimeOffset.UtcNow.AddSeconds(-1); + await db.SaveChangesAsync(ct); + profile = await http.GetFromJsonAsync("/api/v1/account/me", ct); + Assert.NotNull(profile); + Assert.Equal(1234L, profile.DiskQuota); + } + [Fact] public async Task GetCurrentUserUsage_persists_snapshot_in_database() { @@ -100,9 +130,9 @@ public sealed class ResourceUsageApiControllerTests : IClassFixture(); var record = await db.ResourceUsageRecords - .SingleAsync(r => r.UserId == "telemetry-user"); + .SingleAsync(r => r.UserId == "quota-consultation-user"); - Assert.True(record.ApiCalls >= 1m); - Assert.True(record.BandwidthMb > 0m); + Assert.Equal(0m, record.ApiCalls); + Assert.Equal(0m, record.BandwidthMb); + } + + [Fact] + public async Task Exhausted_quota_blocks_operations_but_not_usage_consultation() + { + using var scope = _fixture.Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + await ResourceUsageTracker.RecordAsync(db, "exhausted-quota-user", "exhausted-quota-user", cpuSeconds: 10000m); + using var http = NewClient(_fixture, "exhausted-quota-user"); + + var refused = await http.GetAsync("/api/v1/notifications/me", TestContext.Current.CancellationToken); + Assert.Equal(HttpStatusCode.TooManyRequests, refused.StatusCode); + Assert.Equal("application/problem+json", refused.Content.Headers.ContentType?.MediaType); + Assert.Contains("resource_quota_exceeded", + await refused.Content.ReadAsStringAsync(TestContext.Current.CancellationToken)); + + var consulted = await http.GetAsync("/api/v1/resource-usage/me", TestContext.Current.CancellationToken); + Assert.Equal(HttpStatusCode.OK, consulted.StatusCode); + var summary = await consulted.Content.ReadFromJsonAsync(TestContext.Current.CancellationToken); + Assert.NotNull(summary); + Assert.Equal(1000m, summary.Consumption); + Assert.Equal(1000m, summary.ConsumptionLimit); + Assert.Equal(0m, summary.RemainingConsumption); + Assert.True(summary.IsQuotaReached); + Assert.Equal(0m, summary.ApiCalls); } [Fact] diff --git a/src/test/Yavsc.Blogs.Tests/FileSystemApiTests.cs b/src/test/Yavsc.Blogs.Tests/FileSystemApiTests.cs index 569e4af3d..0b7d2a9b9 100644 --- a/src/test/Yavsc.Blogs.Tests/FileSystemApiTests.cs +++ b/src/test/Yavsc.Blogs.Tests/FileSystemApiTests.cs @@ -10,6 +10,7 @@ using Yavsc.Models.Blog; using Yavsc.Models.Relationship; using Yavsc.Models.Workflow; using Yavsc.Tests.Shared; +using Yavsc.Server.Helpers; namespace Yavsc.Blogs.Tests; @@ -135,6 +136,35 @@ public sealed class FileSystemApiTests : IClassFixture "Expected the uploaded file on disk under the owner's personal root"); } + [Fact] + public async Task Upload_is_refused_before_writing_when_known_storage_would_exceed_quota() + { + _fixture.ResetDatabase(); + _fixture.SeedUser("storage-quota-user", u => u.DiskQuota = 10_000_000); + using (var scope = _fixture.Services.CreateScope()) + await ResourceUsageTracker.RecordAsync( + scope.ServiceProvider.GetRequiredService(), + "storage-quota-user", "storage-quota-user", cpuSeconds: 9999.8m, + cancellationToken: TestContext.Current.CancellationToken); + + const string fileName = "quota-refused-file.txt"; + using var form = new MultipartFormDataContent(); + form.Add(new ByteArrayContent(new byte[1048576]), "file", fileName); + using var http = NewClient("storage-quota-user"); + using var response = await http.PostAsync("/api/v1/fs", form, TestContext.Current.CancellationToken); + Assert.Equal(HttpStatusCode.TooManyRequests, response.StatusCode); + Assert.Contains("resource_quota_exceeded", + await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken)); + Assert.False(File.Exists(PhysicalFile(_fixture, "storage-quota-user", fileName))); + using var finalScope = _fixture.Services.CreateScope(); + var db = finalScope.ServiceProvider.GetRequiredService(); + Assert.Empty(db.UploadedFiles.Where(f => f.OwnerId == "storage-quota-user")); + var record = await db.ResourceUsageRecords.SingleAsync( + r => r.UserId == "storage-quota-user", TestContext.Current.CancellationToken); + Assert.Equal(0m, record.ApiCalls); + Assert.Equal(0m, record.StorageMb); + } + /// /// Regression for the "Mes fichiers" page listing nothing: the /// personal-storage tree is keyed by the user's login diff --git a/src/test/Yavsc.Blogs.Tests/FilterFileNameTests.cs b/src/test/Yavsc.Blogs.Tests/FilterFileNameTests.cs index d445c29e4..c97338781 100644 --- a/src/test/Yavsc.Blogs.Tests/FilterFileNameTests.cs +++ b/src/test/Yavsc.Blogs.Tests/FilterFileNameTests.cs @@ -27,6 +27,45 @@ public sealed class FilterFileNameTests Assert.Equal(filtered, AbstractFileSystemHelpers.FilterFileName(filtered)); } + [Theory] + [InlineData(true, 5)] + [InlineData(false, 2)] + public void Paid_upload_accepts_exact_effective_quota_and_refuses_overflow_without_touching_files(bool premium, long multiplier) + { + var root = Path.Combine(Path.GetTempPath(), $"yavsc-disk-{Guid.NewGuid():N}"); + Directory.CreateDirectory(root); + try + { + var limit = Yavsc.Constants.DefaultFSQ * multiplier; + var user = new ApplicationUser + { + DiskQuota = 10L, DiskUsage = limit - 3, + PremiumDiskQuotaUntil = premium ? DateTimeOffset.UtcNow.AddDays(1) : null, + DiscoveryDiskQuotaUntil = premium ? null : DateTimeOffset.UtcNow.AddDays(1) + }; + using var input = new MemoryStream(new byte[] { 1, 2, 3 }); + var received = user.ReceiveUserFile(root, new FormFile(input, 0, 3, "file", "disk.bin") + { + Headers = new HeaderDictionary(), ContentType = "application/octet-stream" + }); + Assert.False(received.QuotaOffense); + Assert.Equal(limit, user.DiskUsage); + using var extra = new MemoryStream(new byte[] { 4 }); + Assert.True(user.ReceiveUserFile(root, new FormFile(extra, 0, 1, "file", "extra.bin")).QuotaOffense); + Assert.False(File.Exists(Path.Combine(root, "extra.bin"))); + user.PremiumDiskQuotaUntil = DateTimeOffset.UtcNow.AddSeconds(-1); + user.DiscoveryDiskQuotaUntil = DateTimeOffset.UtcNow.AddSeconds(-1); + using var replacement = new MemoryStream(new byte[] { 5 }); + Assert.True(user.ReceiveUserFile(root, new FormFile(replacement, 0, 1, "file", "disk.bin")).QuotaOffense); + Assert.Equal(new byte[] { 1, 2, 3 }, File.ReadAllBytes(Path.Combine(root, "disk.bin"))); + Assert.Equal(limit, user.DiskUsage); + } + finally + { + Directory.Delete(root, recursive: true); + } + } + [Fact] public async Task Uploaded_accented_filename_is_accessible_through_static_files() { diff --git a/src/test/Yavsc.Blogs.Tests/Fixtures/BlogsWebServerFixture.cs b/src/test/Yavsc.Blogs.Tests/Fixtures/BlogsWebServerFixture.cs index 1e0dd81f7..7153c07eb 100644 --- a/src/test/Yavsc.Blogs.Tests/Fixtures/BlogsWebServerFixture.cs +++ b/src/test/Yavsc.Blogs.Tests/Fixtures/BlogsWebServerFixture.cs @@ -10,6 +10,7 @@ using Yavsc.Models.Blog; using Yavsc.Models.Relationship; using Yavsc.Services; using Yavsc.Tests.Shared; +using Yavsc.Server.Helpers; using static Yavsc.Constants; namespace Yavsc.Blogs.Tests; @@ -85,6 +86,7 @@ public sealed class BlogsWebServerFixture : WebHostFixture protected override WebApplication BuildApp(WebApplicationBuilder builder) { + builder.Services.AddResourceUsage(builder.Configuration); // Open the shared in-memory connection lazily on the first // fixture construction. Subsequent constructions (xUnit // creates one fixture instance per IClassFixture) reuse @@ -132,6 +134,7 @@ public sealed class BlogsWebServerFixture : WebHostFixture builder.Services.Configure(s => { s.UserFilesRoot = BlogFilesRoot; + s.Authority = "https://example.test"; }); // Real BlogSpotService — same instance the production host diff --git a/src/test/Yavsc.Org.Tests/Controllers/ResourceSubscriptionControllerTests.cs b/src/test/Yavsc.Org.Tests/Controllers/ResourceSubscriptionControllerTests.cs new file mode 100644 index 000000000..d1433d2a4 --- /dev/null +++ b/src/test/Yavsc.Org.Tests/Controllers/ResourceSubscriptionControllerTests.cs @@ -0,0 +1,248 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text.Json; +using System.Text.RegularExpressions; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Yavsc.Models; +using Yavsc.Models.Payment; +using Yavsc.Server.Helpers; +using Yavsc.Settings; +using Yavsc.Web.Services; +using Xunit; + +namespace Yavsc.Org.Tests.Controllers; + +public sealed class ResourceSubscriptionControllerTests : IClassFixture +{ + private readonly Factory _factory; + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + public ResourceSubscriptionControllerTests(Factory factory) + { + _factory = factory; + factory.PayPal.Payments.Clear(); + factory.PayPal.Verified = true; + factory.PayPal.CancelCount = 0; + using var scope = factory.Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + db.ResourceSubscriptionPayments.RemoveRange(db.ResourceSubscriptionPayments); + db.ResourceSubscriptions.RemoveRange(db.ResourceSubscriptions); + db.ResourceUsageRecords.RemoveRange(db.ResourceUsageRecords); + if (!db.Users.Any(u => u.Id == "listener")) + db.Users.Add(new ApplicationUser { Id = "listener", UserName = "Listener", Email = "listener@example.test" }); + if (!db.Users.Any(u => u.Id == "author")) + db.Users.Add(new ApplicationUser { Id = "author", UserName = "Author", Email = "author@example.test" }); + db.SaveChanges(); + } + + [Fact] + public async Task Subscription_management_requires_login_and_all_mutations_require_antiforgery() + { + using var anonymous = _factory.CreateClient(new() { AllowAutoRedirect = false }); + using var page = await anonymous.GetAsync("/ResourceSubscription", Ct); + Assert.Equal(HttpStatusCode.Redirect, page.StatusCode); + using var client = _factory.SignedInClient(); + using var create = await client.PostAsync("/ResourceSubscription/Create", new FormUrlEncodedContent( + new Dictionary { ["pack"] = "Discovery" }), Ct); + Assert.Equal(HttpStatusCode.BadRequest, create.StatusCode); + } + + [Fact] + public async Task Exhausted_free_usage_can_still_subscribe_and_browser_return_does_not_award_unpaid_credit() + { + using (var scope = _factory.Services.CreateScope()) + await ResourceUsageTracker.RecordAsync(scope.ServiceProvider.GetRequiredService(), + "listener", "Listener", cpuSeconds: 12000m, cancellationToken: Ct); + using var client = _factory.SignedInClient(); + var html = await client.GetStringAsync("/ResourceSubscription", Ct); + Assert.Contains("1200", html); + Assert.Contains("Premium", html); + var subscription = await StartAsync(client); + Assert.Equal("listener", subscription.UserId); + Assert.Equal(1000m, subscription.MonthlyPoints); + Assert.Equal(1.2m, subscription.PriceIncludingTax); + using var returned = await client.GetAsync($"/ResourceSubscription/Return?id={subscription.Id}", Ct); + Assert.Equal(HttpStatusCode.Redirect, returned.StatusCode); + var usage = await UsageAsync(); + Assert.Equal(1200m, usage.Consumption); + Assert.True(usage.IsQuotaReached); + } + + [Fact] + public async Task Verified_notification_reconciles_payment_once_and_unblocks_without_a_browser_return() + { + using (var scope = _factory.Services.CreateScope()) + await ResourceUsageTracker.RecordAsync(scope.ServiceProvider.GetRequiredService(), + "listener", "Listener", cpuSeconds: 12000m, cancellationToken: Ct); + using var client = _factory.SignedInClient(); + var subscription = await StartAsync(client); + _factory.PayPal.Payments.Add(new("PAY-1", "COMPLETED", 1.2m, "EUR", subscription.CreatedAt)); + using var first = await NotifyAsync(subscription.PayPalId!); + using var repeated = await NotifyAsync(subscription.PayPalId!); + Assert.Equal(HttpStatusCode.NoContent, first.StatusCode); + Assert.Equal(HttpStatusCode.NoContent, repeated.StatusCode); + var usage = await UsageAsync(); + Assert.Equal(200m, usage.Consumption); + Assert.False(usage.IsQuotaReached); + using var verificationScope = _factory.Services.CreateScope(); + Assert.Single(verificationScope.ServiceProvider.GetRequiredService().ResourceSubscriptionPayments); + } + + [Fact] + public async Task Premium_checkout_uses_three_quotas_and_its_own_vat_inclusive_price() + { + using var client = _factory.SignedInClient(); + using var response = await PostAsync(client, "/ResourceSubscription/Create", new() { ["pack"] = "Premium" }); + Assert.Equal(HttpStatusCode.Redirect, response.StatusCode); + using var scope = _factory.Services.CreateScope(); + var subscription = await scope.ServiceProvider.GetRequiredService().ResourceSubscriptions.SingleAsync(Ct); + Assert.Equal("P-PREMIUM", subscription.PlanId); + Assert.Equal(3000m, subscription.MonthlyPoints); + Assert.Equal(2.4m, subscription.PriceIncludingTax); + } + + [Theory] + [InlineData("[]")] + [InlineData("""{"event_type":"PAYMENT.SALE.COMPLETED","resource":[]}""")] + [InlineData("""{"event_type":"PAYMENT.SALE.REFUNDED","resource":{"sale_id":42}}""")] + [InlineData("""{"event_type":"PAYMENT.SALE.REFUNDED","resource":{"links":{}}}""")] + [InlineData("""{"event_type":"PAYMENT.SALE.REFUNDED","resource":{"links":[{"href":42}]}}""")] + public async Task Malformed_notification_shapes_are_rejected_without_credit(string json) + { + using var client = _factory.CreateClient(); + using var response = await client.PostAsync("/ResourceSubscription/Notify", + new StringContent(json, System.Text.Encoding.UTF8, "application/json"), Ct); + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + Assert.Equal(0m, (await UsageAsync()).CreditBalance); + } + + [Fact] + public async Task Forged_notification_does_not_reconcile_or_grant_a_credit() + { + using var client = _factory.SignedInClient(); + var subscription = await StartAsync(client); + _factory.PayPal.Payments.Add(new("PAY-1", "COMPLETED", 1.2m, "EUR", subscription.CreatedAt)); + _factory.PayPal.Verified = false; + using var notified = await NotifyAsync(subscription.PayPalId!); + Assert.Equal(HttpStatusCode.BadRequest, notified.StatusCode); + Assert.Equal(0m, (await UsageAsync()).CreditBalance); + } + + [Fact] + public async Task Cancellation_is_owned_post_only_and_retains_acquired_credit() + { + using var client = _factory.SignedInClient(); + var subscription = await StartAsync(client); + _factory.PayPal.Payments.Add(new("PAY-1", "COMPLETED", 1.2m, "EUR", subscription.CreatedAt)); + using var notified = await NotifyAsync(subscription.PayPalId!); + Assert.Equal(HttpStatusCode.NoContent, notified.StatusCode); + using var other = _factory.SignedInClient("author"); + using var refused = await PostAsync(other, "/ResourceSubscription/Cancel", new() { ["id"] = subscription.Id.ToString() }); + Assert.Equal(HttpStatusCode.NotFound, refused.StatusCode); + Assert.Equal(0, _factory.PayPal.CancelCount); + using var get = await client.GetAsync($"/ResourceSubscription/Cancel?id={subscription.Id}", Ct); + Assert.NotEqual(HttpStatusCode.OK, get.StatusCode); + using var cancelled = await PostAsync(client, "/ResourceSubscription/Cancel", new() { ["id"] = subscription.Id.ToString() }); + Assert.Equal(HttpStatusCode.Redirect, cancelled.StatusCode); + Assert.Equal(1, _factory.PayPal.CancelCount); + Assert.Equal(666.66666666m, (await UsageAsync()).CreditBalance); + } + + [Fact] + public async Task Refund_notification_finds_the_original_receipt_and_removes_its_credit() + { + using var client = _factory.SignedInClient(); + var subscription = await StartAsync(client); + _factory.PayPal.Payments.Add(new("PAY-1", "COMPLETED", 1.2m, "EUR", subscription.CreatedAt)); + using var first = await NotifyAsync(subscription.PayPalId!); + Assert.Equal(HttpStatusCode.NoContent, first.StatusCode); + _factory.PayPal.Payments[0] = _factory.PayPal.Payments[0] with { State = "REFUNDED" }; + using var notificationClient = _factory.CreateClient(); + using var refund = await notificationClient.PostAsJsonAsync("/ResourceSubscription/Notify", + new { event_type = "PAYMENT.SALE.REFUNDED", resource = new { sale_id = "PAY-1", id = "REFUND-1" } }, Ct); + Assert.Equal(HttpStatusCode.NoContent, refund.StatusCode); + Assert.Equal(0m, (await UsageAsync()).CreditBalance); + } + + private async Task StartAsync(HttpClient client) + { + using var response = await PostAsync(client, "/ResourceSubscription/Create", new() + { + ["pack"] = "Discovery", ["UserId"] = "author", ["Amount"] = "0.01", ["MonthlyPoints"] = "999999" + }); + Assert.Equal(HttpStatusCode.Redirect, response.StatusCode); + Assert.StartsWith("https://www.sandbox.paypal.com/", response.Headers.Location?.ToString()); + using var scope = _factory.Services.CreateScope(); + return await scope.ServiceProvider.GetRequiredService().ResourceSubscriptions.AsNoTracking().SingleAsync(Ct); + } + + private static async Task PostAsync(HttpClient client, string action, Dictionary fields) + { + var html = await client.GetStringAsync("/ResourceSubscription", Ct); + var match = Regex.Match(html, "name=\"__RequestVerificationToken\"[^>]*value=\"([^\"]+)\""); + Assert.True(match.Success); + fields["__RequestVerificationToken"] = WebUtility.HtmlDecode(match.Groups[1].Value); + return await client.PostAsync(action, new FormUrlEncodedContent(fields), Ct); + } + + private async Task UsageAsync() + { + using var scope = _factory.Services.CreateScope(); + return await scope.ServiceProvider.GetRequiredService().GetCurrentAsync("listener", "Listener", Ct); + } + + private async Task NotifyAsync(string paypalId) + { + using var client = _factory.CreateClient(); + return await client.PostAsJsonAsync("/ResourceSubscription/Notify", + new { event_type = "PAYMENT.SALE.COMPLETED", resource = new { billing_agreement_id = paypalId } }, Ct); + } + + public sealed class Factory : PodcastWebTests.Factory + { + public Gateway PayPal { get; } = new(); + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + base.ConfigureWebHost(builder); + builder.ConfigureTestServices(services => + { + services.AddSingleton(PayPal); + services.Configure(o => + { + o.DiscoveryPlanId = "P-DISCOVERY"; + o.PremiumPlanId = "P-PREMIUM"; + o.WebhookId = "WH-TEST"; + }); + }); + } + } + + public sealed class Gateway : IPayPalSubscriptionGateway + { + public bool Verified { get; set; } = true; + public int CancelCount { get; set; } + public List Payments { get; } = []; + private PayPalSubscriptionDetails _remote = null!; + public Task CreateAsync(ResourceSubscription subscription, string returnUrl, string cancelUrl, CancellationToken ct) + { + _remote = new("I-TEST", subscription.Id.ToString("N"), subscription.PlanId, "APPROVAL_PENDING", + subscription.CreatedAt, subscription.CreatedAt.AddMonths(1), "https://www.sandbox.paypal.com/approve"); + return Task.FromResult(_remote); + } + public Task GetAsync(string id, CancellationToken ct) => Task.FromResult(_remote); + public Task> TransactionsAsync(string id, DateTimeOffset from, DateTimeOffset to, CancellationToken ct) + => Task.FromResult>(Payments.ToArray()); + public Task CancelAsync(string id, CancellationToken ct) + { + CancelCount++; + _remote = _remote with { State = "CANCELLED", NextBillingAt = null }; + return Task.CompletedTask; + } + public Task VerifyWebhookAsync(JsonElement notification, IReadOnlyDictionary headers, CancellationToken ct) + => Verified ? Task.CompletedTask : throw new SubscriptionOperationException(400, "Signature invalide."); + } +} diff --git a/src/test/Yavsc.Org.Tests/Controllers/ResourceUsageTermsTests.cs b/src/test/Yavsc.Org.Tests/Controllers/ResourceUsageTermsTests.cs new file mode 100644 index 000000000..6064294ed --- /dev/null +++ b/src/test/Yavsc.Org.Tests/Controllers/ResourceUsageTermsTests.cs @@ -0,0 +1,30 @@ +using System.Net; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Yavsc.Settings; +using Xunit; + +namespace Yavsc.Org.Tests.Controllers; + +public sealed class ResourceUsageTermsTests +{ + [Fact] + public async Task Public_terms_describe_the_configured_rule_and_are_linked_from_the_footer() + { + using var factory = new TestWebApplicationFactory(); + var options = factory.Services.GetRequiredService>().Value; + options.DefaultLimit = 12345m; + using var client = factory.CreateClient(); + using var response = await client.GetAsync("/Home/CGU", TestContext.Current.CancellationToken); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + var html = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); + Assert.Contains("12345", html); + Assert.Contains("resource_quota_exceeded", html); + Assert.Contains("I =", html); + Assert.Contains("Home/CGU", html); + Assert.Contains((Yavsc.Constants.DefaultFSQ * 5L).ToString(), html); + Assert.Contains((Yavsc.Constants.DefaultFSQ * 2L).ToString(), html); + Assert.Contains("quota disque", html); + Assert.Contains("Découverte porte le quota disque", html); + } +} diff --git a/src/test/Yavsc.Server.Tests/PayPalSubscriptionGatewayTests.cs b/src/test/Yavsc.Server.Tests/PayPalSubscriptionGatewayTests.cs new file mode 100644 index 000000000..956773020 --- /dev/null +++ b/src/test/Yavsc.Server.Tests/PayPalSubscriptionGatewayTests.cs @@ -0,0 +1,208 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text.Json; +using Microsoft.Extensions.Options; +using Yavsc.Models.Payment; +using Yavsc.Settings; +using Yavsc.Web.Services; + +namespace Yavsc.Server.Tests; + +public sealed class PayPalSubscriptionGatewayTests +{ + private static CancellationToken Ct => TestContext.Current.CancellationToken; + private sealed class Handler : HttpMessageHandler + { + public string PlanCurrency { get; set; } = "EUR"; + public string PlanPrice { get; set; } = "1.20"; + public string Frequency { get; set; } = "MONTH"; + public bool AutoBillOutstanding { get; set; } + public string? RawResponse { get; set; } + public string Verification { get; set; } = "SUCCESS"; + public string ApprovalUrl { get; set; } = "https://www.sandbox.paypal.com/webapps/billing/subscriptions?ba_token=test"; + public string CustomId { get; set; } = ""; + public HttpStatusCode Status { get; set; } = HttpStatusCode.OK; + public List<(string Path, string Body, string? RequestId, string Host)> Requests { get; } = []; + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken ct) + { + var path = request.RequestUri!.AbsolutePath; + var body = request.Content is null ? "" : await request.Content.ReadAsStringAsync(ct); + Requests.Add((path, body, request.Headers.TryGetValues("PayPal-Request-Id", out var ids) ? ids.Single() : null, request.RequestUri.Host)); + if (Status != HttpStatusCode.OK) return new(Status) { Content = new StringContent("unavailable") }; + if (RawResponse is not null && path != "/v1/oauth2/token") + return new(HttpStatusCode.OK) { Content = new StringContent(RawResponse) }; + object response = path switch + { + "/v1/oauth2/token" => new { access_token = "synthetic-token" }, + "/v1/billing/plans/P-DISCOVERY" => new + { + id = "P-DISCOVERY", status = "ACTIVE", + billing_cycles = new[] { new + { + tenure_type = "REGULAR", total_cycles = 0, + frequency = new { interval_unit = Frequency, interval_count = 1 }, + pricing_scheme = new { fixed_price = new { currency_code = PlanCurrency, value = PlanPrice } } + }}, + payment_preferences = new { auto_bill_outstanding = AutoBillOutstanding, setup_fee = new { value = "0" } } + }, + "/v1/notifications/verify-webhook-signature" => new { verification_status = Verification }, + "/v1/billing/subscriptions/I-TEST/transactions" => new + { + transactions = new[] { new + { + id = "PAY-1", status = "COMPLETED", time = "2026-01-31T12:00:00Z", + amount_with_breakdown = new { gross_amount = new { value = "1.20", currency_code = "EUR" } } + }} + }, + _ => new + { + id = "I-TEST", custom_id = CustomId, plan_id = "P-DISCOVERY", status = "APPROVAL_PENDING", + start_time = "2026-01-31T12:00:00Z", + links = new[] { new { rel = "approve", href = ApprovalUrl } } + } + }; + return new(HttpStatusCode.OK) { Content = JsonContent.Create(response) }; + } + } + + private static PayPalSubscriptionGateway Gateway(HttpClient http, string mode = "sandbox") + => new(http, Options.Create(new PayPalSettings { Mode = mode, ClientId = "test-client", ClientSecret = "test-secret" }), + Options.Create(new ResourceSubscriptionOptions { WebhookId = "test-webhook" })); + + private static ResourceSubscription Subscription() => new() + { + Id = Guid.NewGuid(), UserId = "alice", Pack = "Discovery", PlanId = "P-DISCOVERY", + PriceExcludingTax = 1m, PriceIncludingTax = 1.2m + }; + + [Theory] + [InlineData("sandbox", "api-m.sandbox.paypal.com")] + [InlineData("production", "api-m.paypal.com")] + public async Task Creation_checks_monthly_ttc_plan_and_uses_local_idempotency_reference(string mode, string host) + { + var subscription = Subscription(); + using var handler = new Handler { CustomId = subscription.Id.ToString("N") }; + using var http = new HttpClient(handler); + var result = await Gateway(http, mode).CreateAsync(subscription, "https://site.example/return", "https://site.example/cancel", Ct); + Assert.Equal("I-TEST", result.Id); + var creation = Assert.Single(handler.Requests.Where(r => r.Path == "/v1/billing/subscriptions")); + Assert.Equal(subscription.Id.ToString("N"), creation.RequestId); + Assert.Contains(subscription.Id.ToString("N"), creation.Body); + Assert.Contains("P-DISCOVERY", creation.Body); + Assert.Contains("SUBSCRIBE_NOW", creation.Body); + Assert.All(handler.Requests, r => Assert.Equal(host, r.Host)); + } + + [Theory] + [InlineData("EUR", "1.00", "MONTH")] + [InlineData("USD", "1.20", "MONTH")] + [InlineData("EUR", "1.20", "YEAR")] + public async Task Wrong_plan_price_currency_or_period_is_refused_before_checkout(string currency, string price, string period) + { + using var handler = new Handler { PlanCurrency = currency, PlanPrice = price, Frequency = period }; + using var http = new HttpClient(handler); + var error = await Assert.ThrowsAsync(() => + Gateway(http).CreateAsync(Subscription(), "https://site.example/return", "https://site.example/cancel", Ct)); + Assert.Equal(503, error.StatusCode); + Assert.DoesNotContain(handler.Requests, r => r.Path == "/v1/billing/subscriptions"); + } + + [Fact] + public async Task Automatic_collection_of_outstanding_payments_is_refused_before_checkout() + { + using var handler = new Handler { AutoBillOutstanding = true }; + using var http = new HttpClient(handler); + var error = await Assert.ThrowsAsync(() => + Gateway(http).CreateAsync(Subscription(), "https://site.example/return", "https://site.example/cancel", Ct)); + Assert.Equal(503, error.StatusCode); + Assert.DoesNotContain(handler.Requests, r => r.Path == "/v1/billing/subscriptions"); + } + + [Theory] + [InlineData("""[]""")] + [InlineData("""{"id":"I-TEST","links":{}}""")] + [InlineData("""{"id":"I-TEST","plan_overridden":"false"}""")] + [InlineData("""{"id":"I-TEST","billing_info":[]}""")] + [InlineData("""{"id":"I-TEST","links":[{"rel":"approve","href":"https://www.paypal.com/a"},{"rel":"approve","href":"https://www.paypal.com/b"}]}""")] + public async Task Malformed_subscription_shapes_are_explicit_upstream_errors(string response) + { + using var handler = new Handler { RawResponse = response }; + using var http = new HttpClient(handler); + var error = await Assert.ThrowsAsync(() => Gateway(http).GetAsync("I-TEST", Ct)); + Assert.Equal(502, error.StatusCode); + } + + [Theory] + [InlineData("""{"billing_cycles":{}}""")] + [InlineData("""{"id":"P-DISCOVERY","status":"ACTIVE","billing_cycles":[{"tenure_type":"REGULAR","total_cycles":"0","frequency":{"interval_unit":"MONTH","interval_count":1},"pricing_scheme":{"fixed_price":{"currency_code":"EUR","value":"1.20"}}}],"payment_preferences":{"auto_bill_outstanding":false}}""")] + public async Task Malformed_plan_shapes_are_explicit_upstream_errors(string response) + { + using var handler = new Handler { RawResponse = response }; + using var http = new HttpClient(handler); + var error = await Assert.ThrowsAsync(() => + Gateway(http).CreateAsync(Subscription(), "https://site.example/return", "https://site.example/cancel", Ct)); + Assert.Equal(502, error.StatusCode); + } + + [Fact] + public async Task Approval_redirect_cannot_point_to_an_untrusted_site() + { + using var handler = new Handler { ApprovalUrl = "https://attacker.example/" }; + using var http = new HttpClient(handler); + var error = await Assert.ThrowsAsync(() => Gateway(http).GetAsync("I-TEST", Ct)); + Assert.Equal(502, error.StatusCode); + } + + [Fact] + public async Task Webhook_verification_uses_the_configured_id_and_requires_paypal_success() + { + using var handler = new Handler { Verification = "FAILURE" }; + using var http = new HttpClient(handler); + using var notification = JsonDocument.Parse("""{"id":"WH-TEST","event_type":"PAYMENT.SALE.COMPLETED"}"""); + var headers = new Dictionary + { + ["PAYPAL-AUTH-ALGO"] = "SHA256withRSA", ["PAYPAL-CERT-URL"] = "https://api.paypal.com/cert", + ["PAYPAL-TRANSMISSION-ID"] = "test-transmission", ["PAYPAL-TRANSMISSION-SIG"] = "test-signature", + ["PAYPAL-TRANSMISSION-TIME"] = "2026-01-31T12:00:00Z" + }; + var error = await Assert.ThrowsAsync(() => + Gateway(http).VerifyWebhookAsync(notification.RootElement, headers, Ct)); + Assert.Equal(400, error.StatusCode); + var verification = Assert.Single(handler.Requests.Where(r => r.Path.EndsWith("verify-webhook-signature"))); + Assert.Contains("test-webhook", verification.Body); + Assert.Contains("test-signature", verification.Body); + handler.Verification = "SUCCESS"; + await Gateway(http).VerifyWebhookAsync(notification.RootElement, headers, Ct); + } + + [Fact] + public async Task Transaction_history_is_read_with_bounded_ranges_and_invariant_euro_amounts() + { + using var handler = new Handler(); + using var http = new HttpClient(handler); + var from = new DateTimeOffset(2026, 1, 31, 12, 0, 0, TimeSpan.Zero); + var payments = await Gateway(http).TransactionsAsync("I-TEST", from, from.AddDays(61), Ct); + Assert.Equal(3, handler.Requests.Count(r => r.Path.EndsWith("/transactions"))); + Assert.All(payments, p => Assert.Equal(1.2m, p.Amount)); + Assert.All(payments, p => Assert.Equal("EUR", p.Currency)); + } + + [Fact] + public async Task Http_failure_is_an_explicit_payment_failure_not_a_successful_subscription() + { + using var handler = new Handler { Status = HttpStatusCode.ServiceUnavailable }; + using var http = new HttpClient(handler); + var error = await Assert.ThrowsAsync(() => Gateway(http).GetAsync("I-TEST", Ct)); + Assert.Equal(502, error.StatusCode); + } + + [Fact] + public async Task Invalid_paypal_id_is_refused_without_network_access() + { + using var handler = new Handler(); + using var http = new HttpClient(handler); + await Assert.ThrowsAsync(() => Gateway(http).GetAsync("../other", Ct)); + Assert.Empty(handler.Requests); + } +} diff --git a/src/test/Yavsc.Server.Tests/ResourceSubscriptionTests.cs b/src/test/Yavsc.Server.Tests/ResourceSubscriptionTests.cs new file mode 100644 index 000000000..0539e301a --- /dev/null +++ b/src/test/Yavsc.Server.Tests/ResourceSubscriptionTests.cs @@ -0,0 +1,486 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using System.Text.Json; +using Yavsc.Models; +using Yavsc.Models.Payment; +using Yavsc.Server.Helpers; +using Yavsc.Settings; +using Yavsc.Web.Services; + +namespace Yavsc.Server.Tests; + +public sealed class ResourceSubscriptionTests +{ + private static readonly DateTimeOffset Start = new(2026, 1, 31, 12, 0, 0, TimeSpan.Zero); + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + private sealed class Clock : TimeProvider + { + public DateTimeOffset Now { get; set; } = Start; + public override DateTimeOffset GetUtcNow() => Now; + } + + private sealed class Gateway : IPayPalSubscriptionGateway + { + public PayPalSubscriptionDetails Remote { get; set; } = null!; + public List Payments { get; } = []; + public Task GetAsync(string id, CancellationToken ct) => Task.FromResult(Remote); + public Task> TransactionsAsync(string id, DateTimeOffset from, DateTimeOffset to, CancellationToken ct) + => Task.FromResult>(Payments.ToArray()); + public Task CreateAsync(ResourceSubscription subscription, string returnUrl, string cancelUrl, CancellationToken ct) + => throw new NotSupportedException(); + public Task CancelAsync(string id, CancellationToken ct) => throw new NotSupportedException(); + public Task VerifyWebhookAsync(JsonElement notification, IReadOnlyDictionary headers, CancellationToken ct) + => throw new NotSupportedException(); + } + + private static ServiceProvider Provider(Clock clock, Gateway gateway, bool useBackendDefaults = false) + { + var name = Guid.NewGuid().ToString(); + var services = new ServiceCollection(); + services.AddDbContext(o => o.UseInMemoryDatabase(name)); + services.AddResourceUsage(new ConfigurationBuilder().Build()); + if (!useBackendDefaults) + services.Configure(o => + { + o.ApiCallWeight = 0.02m; + o.CpuSecondWeight = 1m; + o.BandwidthMbWeight = 0.1m; + o.StorageMbWeight = 0.3m; + o.DefaultLimit = 10000m; + }); + services.Configure(o => + { + o.DiscoveryPlanId = "P-DISCOVERY"; + o.PremiumPlanId = "P-PREMIUM"; + }); + services.AddSingleton(clock); + services.AddSingleton(gateway); + services.AddScoped(); + return services.BuildServiceProvider(); + } + + private static async Task BeginAsync(IServiceProvider services, Gateway gateway, string pack = "Discovery", decimal gross = 0m) + { + var db = services.GetRequiredService(); + db.Users.Add(new ApplicationUser { Id = "alice", UserName = "Alice", Email = "alice@example.test" }); + await db.SaveChangesAsync(Ct); + await ResourceUsageTracker.RecordAsync(db, "alice", "Alice", cpuSeconds: gross, cancellationToken: Ct); + var service = services.GetRequiredService(); + var subscription = await service.PrepareAsync("alice", pack, Ct); + gateway.Remote = new("I-TEST", subscription.Id.ToString("N"), subscription.PlanId, "ACTIVE", Start, Start.AddMonths(1), null); + await service.BindAsync(subscription, gateway.Remote, Ct); + return subscription; + } + + [Theory] + [InlineData(4000, 0, 10000, 8000)] + [InlineData(15000, 0, 10000, 10000)] + [InlineData(0, 0, 30000, 20000)] + [InlineData(4000, 5000, 30000, 20000)] + public void Monthly_credit_erases_usage_then_carries_two_thirds_of_unused_allowance( + decimal gross, decimal previous, decimal allowance, decimal grant) + => Assert.Equal(grant, ResourceUsageCredit.MonthlyGrant(gross, previous, allowance)); + + [Theory] + [InlineData("Premium", 2.4, 5)] + [InlineData("Discovery", 1.2, 2)] + public async Task Paid_disk_quota_uses_standard_reference_and_survives_cancellation_until_anniversary( + string pack, decimal amount, long multiplier) + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway, pack); + clock.Now = Start.AddMinutes(1); + var db = scope.ServiceProvider.GetRequiredService(); + var user = await db.Users.SingleAsync(Ct); + user.DiskQuota = 1234L; + user.DiskUsage = 5000L; + await db.SaveChangesAsync(Ct); + gateway.Payments.Add(new("PAY-DISK", "COMPLETED", amount, "EUR", Start)); + var service = scope.ServiceProvider.GetRequiredService(); + await service.ReconcileAsync(subscription, Ct); + Assert.Equal(Yavsc.Constants.DefaultFSQ * multiplier, user.GetEffectiveDiskQuota(clock.Now)); + Assert.Equal(Start.AddMonths(1), pack == "Premium" ? user.PremiumDiskQuotaUntil : user.DiscoveryDiskQuotaUntil); + var usage = await scope.ServiceProvider.GetRequiredService().GetCurrentAsync("alice", "Alice", Ct); + Assert.Equal(Yavsc.Constants.DefaultFSQ * multiplier, usage.DiskQuota); + Assert.Equal(user.PremiumDiskQuotaUntil, usage.PremiumDiskQuotaUntil); + Assert.Equal(user.DiscoveryDiskQuotaUntil, usage.DiscoveryDiskQuotaUntil); + gateway.Remote = gateway.Remote with { State = "CANCELLED", NextBillingAt = null }; + await service.ReconcileAsync(subscription, Ct); + Assert.Equal(Yavsc.Constants.DefaultFSQ * multiplier, user.GetEffectiveDiskQuota(Start.AddMonths(1).AddTicks(-1))); + Assert.Equal(1234L, user.GetEffectiveDiskQuota(Start.AddMonths(1))); + Assert.Equal(1234L, user.DiskQuota); + Assert.Equal(5000L, user.DiskUsage); + } + + [Theory] + [InlineData("Discovery", "PENDING", 1.2)] + [InlineData("Discovery", "FAILED", 1.2)] + [InlineData("Premium", "PENDING", 2.4)] + [InlineData("Premium", "FAILED", 2.4)] + public async Task Unpaid_subscriptions_do_not_grant_disk_bonus(string pack, string state, decimal amount) + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway, pack); + clock.Now = Start.AddMinutes(1); + gateway.Payments.Add(new("PAY-DISK", state, amount, "EUR", Start)); + await scope.ServiceProvider.GetRequiredService().ReconcileAsync(subscription, Ct); + var user = await scope.ServiceProvider.GetRequiredService().Users.SingleAsync(Ct); + Assert.Null(user.PremiumDiskQuotaUntil); + Assert.Null(user.DiscoveryDiskQuotaUntil); + Assert.Equal(user.DiskQuota, user.GetEffectiveDiskQuota(clock.Now)); + } + + [Theory] + [InlineData("Premium", 2.4)] + [InlineData("Discovery", 1.2)] + public async Task Refund_revokes_disk_bonus_but_not_another_paid_period(string pack, decimal amount) + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway, pack); + clock.Now = Start.AddMonths(1).AddMinutes(1); + gateway.Payments.Add(new("PAY-1", "COMPLETED", amount, "EUR", Start)); + gateway.Payments.Add(new("PAY-2", "COMPLETED", amount, "EUR", Start.AddMonths(1))); + var service = scope.ServiceProvider.GetRequiredService(); + await service.ReconcileAsync(subscription, Ct); + var user = await scope.ServiceProvider.GetRequiredService().Users.SingleAsync(Ct); + Assert.Equal(Start.AddMonths(2), pack == "Premium" ? user.PremiumDiskQuotaUntil : user.DiscoveryDiskQuotaUntil); + gateway.Payments[0] = gateway.Payments[0] with { State = "REFUNDED" }; + await service.ReconcileAsync(subscription, Ct); + Assert.Equal(Start.AddMonths(2), pack == "Premium" ? user.PremiumDiskQuotaUntil : user.DiscoveryDiskQuotaUntil); + gateway.Payments[1] = gateway.Payments[1] with { State = "PARTIALLY_REFUNDED" }; + await service.ReconcileAsync(subscription, Ct); + Assert.Null(user.PremiumDiskQuotaUntil); + Assert.Null(user.DiscoveryDiskQuotaUntil); + Assert.Equal(user.DiskQuota, user.GetEffectiveDiskQuota(clock.Now)); + } + + [Fact] + public async Task Cached_disk_tracker_refreshes_paid_entitlement_before_admission() + { + using var provider = Provider(new Clock(), new Gateway()); + using var scope = provider.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + db.Users.Add(new ApplicationUser + { + Id = "alice", UserName = "Alice", Email = "alice@example.test", DiskQuota = 10L, DiskUsage = 11L, + PremiumDiskQuotaUntil = DateTimeOffset.UtcNow.AddDays(1) + }); + await db.SaveChangesAsync(Ct); + var tracker = new DiskUsageTracker(Options.Create(new SiteSettings { DUUserListLen = 10 }), db); + Assert.False(tracker.GetSpace("Alice", Yavsc.Constants.DefaultFSQ * 5L)); + using var other = provider.CreateScope(); + var otherDb = other.ServiceProvider.GetRequiredService(); + var user = await otherDb.Users.SingleAsync(Ct); + user.PremiumDiskQuotaUntil = DateTimeOffset.UtcNow.AddSeconds(-1); + await otherDb.SaveChangesAsync(Ct); + Assert.False(tracker.GetSpace("Alice", 1L)); + } + + [Theory] + [InlineData("Discovery", 1.2, 1000, 3000)] + [InlineData("Premium", 2.4, 3000, 1000)] + public async Task Current_backend_defaults_give_one_or_three_thousand_monthly_points( + string pack, decimal amount, decimal monthlyPoints, decimal remainingUsage) + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway, useBackendDefaults: true); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway, pack, 40000m); + Assert.Equal(monthlyPoints, subscription.MonthlyPoints); + gateway.Payments.Add(new("PAY-DEFAULT", "COMPLETED", amount, "EUR", Start)); + clock.Now = Start.AddMinutes(1); + await scope.ServiceProvider.GetRequiredService().ReconcileAsync(subscription, Ct); + var usage = await scope.ServiceProvider.GetRequiredService().GetCurrentAsync("alice", "Alice", Ct); + Assert.Equal(4000m, usage.GrossConsumption); + Assert.Equal(remainingUsage, usage.Consumption); + Assert.Equal(1000m, usage.ConsumptionLimit); + Assert.Equal(0m, usage.RemainingConsumption); + } + + [Fact] + public void Overlapping_paid_periods_fall_back_from_premium_to_discovery_then_base_quota() + { + var user = new ApplicationUser + { + PremiumDiskQuotaUntil = Start.AddDays(1), + DiscoveryDiskQuotaUntil = Start.AddDays(2) + }; + Assert.Equal(Yavsc.Constants.DefaultFSQ * 5L, user.GetEffectiveDiskQuota(Start)); + Assert.Equal(Yavsc.Constants.DefaultFSQ * 2L, user.GetEffectiveDiskQuota(Start.AddDays(1))); + Assert.Equal(Yavsc.Constants.DefaultFSQ, user.GetEffectiveDiskQuota(Start.AddDays(2))); + } + + [Fact] + public void Free_disk_quota_matches_the_standard_database_default() + { + var user = new ApplicationUser(); + Assert.Equal(Yavsc.Constants.DefaultFSQ, user.DiskQuota); + Assert.Equal(Yavsc.Constants.DefaultFSQ, user.GetEffectiveDiskQuota(Start)); + } + + [Fact] + public void Carried_credit_is_rounded_down_at_database_precision() + => Assert.Equal(6666.66666666m, ResourceUsageCredit.MonthlyGrant(0m, 0m, 10000m)); + + [Theory] + [InlineData("2026-02-28T00:00:00Z", 1)] + [InlineData("2026-03-30T23:59:59Z", 1)] + [InlineData("2026-03-31T00:00:00Z", 2)] + [InlineData("2027-01-31T00:00:00Z", 12)] + public void Monthly_anniversaries_do_not_drift_after_a_short_month(string paidAt, int expected) + => Assert.Equal(expected, ResourceUsageCredit.BillingCycle( + new DateTimeOffset(Start.UtcDateTime.Date, TimeSpan.Zero), DateTimeOffset.Parse(paidAt))); + + [Fact] + public async Task First_payment_preserves_counters_erases_usage_and_repeated_notifications_do_not_duplicate_credit() + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway, gross: 4000m); + gateway.Payments.Add(new("PAY-1", "COMPLETED", 1.2m, "EUR", Start)); + clock.Now = Start.AddMinutes(1); + var service = scope.ServiceProvider.GetRequiredService(); + await service.ReconcileAsync(subscription, Ct); + await service.ReconcileAsync(subscription, Ct); + var usage = await scope.ServiceProvider.GetRequiredService().GetCurrentAsync("alice", "Alice", Ct); + Assert.Equal(4000m, usage.CpuSeconds); + Assert.Equal(4000m, usage.GrossConsumption); + Assert.Equal(0m, usage.Consumption); + Assert.Equal(4000m, usage.CreditBalance); + Assert.Equal(14000m, usage.RemainingConsumption); + Assert.Single(scope.ServiceProvider.GetRequiredService().ResourceSubscriptionPayments); + } + + [Fact] + public async Task Premium_gives_three_default_quotas_not_three_individual_overrides() + { + var gateway = new Gateway(); + using var provider = Provider(new Clock { Now = Start.AddMinutes(1) }, gateway); + using var scope = provider.CreateScope(); + scope.ServiceProvider.GetRequiredService>().Value.UserLimits["alice"] = 15000m; + var subscription = await BeginAsync(scope.ServiceProvider, gateway, "Premium", 35000m); + gateway.Payments.Add(new("PAY-1", "COMPLETED", 2.4m, "EUR", Start.AddMinutes(1))); + await scope.ServiceProvider.GetRequiredService().ReconcileAsync(subscription, Ct); + var usage = await scope.ServiceProvider.GetRequiredService().GetCurrentAsync("alice", "Alice", Ct); + Assert.Equal(30000m, subscription.MonthlyPoints); + Assert.Equal(2m, subscription.PriceExcludingTax); + Assert.Equal(2.4m, subscription.PriceIncludingTax); + Assert.Equal(5000m, usage.Consumption); + Assert.Equal(10000m, usage.RemainingConsumption); + } + + [Fact] + public async Task Old_carry_is_not_reduced_again_and_remains_spendable_after_cancellation() + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway, gross: 4000m); + var service = scope.ServiceProvider.GetRequiredService(); + gateway.Payments.Add(new("PAY-1", "COMPLETED", 1.2m, "EUR", Start)); + clock.Now = Start.AddDays(1); + await service.ReconcileAsync(subscription, Ct); + gateway.Payments.Add(new("PAY-2", "COMPLETED", 1.2m, "EUR", Start.AddMonths(1))); + clock.Now = Start.AddMonths(1).AddMinutes(1); + gateway.Remote = gateway.Remote with { State = "CANCELLED", NextBillingAt = null }; + await service.ReconcileAsync(subscription, Ct); + var usageService = scope.ServiceProvider.GetRequiredService(); + var usage = await usageService.GetCurrentAsync("alice", "Alice", Ct); + Assert.Equal(10666.66666666m, usage.CreditBalance); + Assert.Equal("CANCELLED", subscription.State); + await ResourceUsageTracker.RecordAsync(scope.ServiceProvider.GetRequiredService(), + "alice", "Alice", cpuSeconds: 5000m, cancellationToken: Ct); + usage = await usageService.GetCurrentAsync("alice", "Alice", Ct); + Assert.Equal(5666.66666666m, usage.CreditBalance); + Assert.Equal(0m, usage.Consumption); + } + + [Theory] + [InlineData("PENDING")] + [InlineData("FAILED")] + [InlineData("DECLINED")] + public async Task Approval_and_unpaid_months_never_grant_points(string state) + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway, gross: 12000m); + gateway.Payments.Add(new("PAY-1", state, 1.2m, "EUR", Start)); + clock.Now = Start.AddMinutes(1); + await scope.ServiceProvider.GetRequiredService().ReconcileAsync(subscription, Ct); + var usage = await scope.ServiceProvider.GetRequiredService().GetCurrentAsync("alice", "Alice", Ct); + Assert.Equal(12000m, usage.Consumption); + Assert.True(usage.IsQuotaReached); + Assert.Equal(0m, usage.CreditBalance); + } + + [Theory] + [InlineData("REFUNDED")] + [InlineData("PARTIALLY_REFUNDED")] + [InlineData("FAILED")] + public async Task Reversal_removes_the_awarded_credit_once_without_deleting_history(string state) + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway, gross: 12000m); + gateway.Payments.Add(new("PAY-1", "COMPLETED", 1.2m, "EUR", Start)); + clock.Now = Start.AddMinutes(1); + var service = scope.ServiceProvider.GetRequiredService(); + await service.ReconcileAsync(subscription, Ct); + gateway.Payments[0] = gateway.Payments[0] with { State = state }; + await service.ReconcileAsync(subscription, Ct); + await service.ReconcileAsync(subscription, Ct); + var usage = await scope.ServiceProvider.GetRequiredService().GetCurrentAsync("alice", "Alice", Ct); + Assert.Equal(12000m, usage.Consumption); + Assert.True(usage.IsQuotaReached); + Assert.True(Assert.Single(scope.ServiceProvider.GetRequiredService().ResourceSubscriptionPayments).Reversed); + } + + [Fact] + public async Task A_payment_unblocks_a_user_without_changing_the_free_limit() + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway, gross: 12000m); + var usage = scope.ServiceProvider.GetRequiredService(); + Assert.False(await usage.TryAdmitAsync("alice", "Alice", 1m, 0m, 0, Ct)); + gateway.Payments.Add(new("PAY-1", "COMPLETED", 1.2m, "EUR", Start)); + clock.Now = Start.AddMinutes(1); + await scope.ServiceProvider.GetRequiredService().ReconcileAsync(subscription, Ct); + Assert.True(await usage.TryAdmitAsync("alice", "Alice", 1m, 0m, 0, Ct)); + Assert.Equal(10000m, (await usage.GetCurrentAsync("alice", "Alice", Ct)).ConsumptionLimit); + } + + [Theory] + [InlineData("USD", 1.2)] + [InlineData("EUR", 1)] + public async Task Wrong_currency_or_net_instead_of_gross_price_does_not_grant_credit(string currency, decimal amount) + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway); + gateway.Payments.Add(new("PAY-1", "COMPLETED", amount, currency, Start)); + clock.Now = Start.AddMinutes(1); + var error = await Assert.ThrowsAsync(() => + scope.ServiceProvider.GetRequiredService().ReconcileAsync(subscription, Ct)); + Assert.Equal(409, error.StatusCode); + Assert.Empty(scope.ServiceProvider.GetRequiredService().ResourceSubscriptionPayments); + } + + [Fact] + public async Task Another_custom_account_or_plan_cannot_be_bound() + { + var gateway = new Gateway(); + using var provider = Provider(new Clock(), gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway); + var service = scope.ServiceProvider.GetRequiredService(); + await Assert.ThrowsAsync(() => + service.BindAsync(subscription, gateway.Remote with { CustomId = Guid.NewGuid().ToString("N") }, Ct)); + await Assert.ThrowsAsync(() => + service.BindAsync(subscription, gateway.Remote with { PlanId = "P-OTHER" }, Ct)); + } + + [Fact] + public async Task Concurrent_notifications_in_distinct_scopes_award_the_same_payment_once() + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var initialScope = provider.CreateScope(); + await BeginAsync(initialScope.ServiceProvider, gateway, gross: 4000m); + gateway.Payments.Add(new("PAY-1", "COMPLETED", 1.2m, "EUR", Start)); + clock.Now = Start.AddMinutes(1); + using var firstScope = provider.CreateScope(); + using var secondScope = provider.CreateScope(); + var first = firstScope.ServiceProvider.GetRequiredService(); + var second = secondScope.ServiceProvider.GetRequiredService(); + var firstSubscription = Assert.Single(await first.GetAllAsync("alice", Ct)); + var secondSubscription = Assert.Single(await second.GetAllAsync("alice", Ct)); + await Task.WhenAll( + Task.Run(() => first.ReconcileAsync(firstSubscription, Ct), Ct), + Task.Run(() => second.ReconcileAsync(secondSubscription, Ct), Ct)); + using var verificationScope = provider.CreateScope(); + var db = verificationScope.ServiceProvider.GetRequiredService(); + Assert.Single(db.ResourceSubscriptionPayments); + Assert.Equal(8000m, (await db.ResourceUsageRecords.SingleAsync(Ct)).CreditPoints); + } + + [Fact] + public async Task An_older_pending_payment_can_complete_after_a_more_recent_month_without_double_credit() + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway, gross: 12000m); + gateway.Payments.Add(new("PAY-1", "PENDING", 1.2m, "EUR", Start)); + gateway.Payments.Add(new("PAY-2", "COMPLETED", 1.2m, "EUR", Start.AddMonths(1))); + clock.Now = Start.AddMonths(1).AddMinutes(1); + var service = scope.ServiceProvider.GetRequiredService(); + await service.ReconcileAsync(subscription, Ct); + gateway.Payments[0] = gateway.Payments[0] with { State = "COMPLETED" }; + await service.ReconcileAsync(subscription, Ct); + await service.ReconcileAsync(subscription, Ct); + var db = scope.ServiceProvider.GetRequiredService(); + Assert.Equal(2, await db.ResourceSubscriptionPayments.CountAsync(Ct)); + Assert.Equal(1, subscription.LastCreditedCycle); + Assert.Equal(17333.33333333m, (await db.ResourceUsageRecords.SingleAsync(Ct)).CreditPoints); + } + + [Fact] + public async Task Two_different_payment_ids_in_the_same_month_do_not_award_two_credits() + { + var clock = new Clock(); + var gateway = new Gateway(); + using var provider = Provider(clock, gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway); + gateway.Payments.Add(new("PAY-1", "COMPLETED", 1.2m, "EUR", Start)); + clock.Now = Start.AddMinutes(1); + var service = scope.ServiceProvider.GetRequiredService(); + await service.ReconcileAsync(subscription, Ct); + gateway.Payments.Add(new("PAY-2", "COMPLETED", 1.2m, "EUR", Start)); + var error = await Assert.ThrowsAsync(() => service.ReconcileAsync(subscription, Ct)); + Assert.Equal(409, error.StatusCode); + var db = scope.ServiceProvider.GetRequiredService(); + Assert.Single(db.ResourceSubscriptionPayments); + Assert.Equal(6666.66666666m, (await db.ResourceUsageRecords.SingleAsync(Ct)).CreditPoints); + } + + [Fact] + public async Task Preparing_the_same_pack_is_idempotent_and_changing_pack_requires_cancellation() + { + var gateway = new Gateway(); + using var provider = Provider(new Clock(), gateway); + using var scope = provider.CreateScope(); + var subscription = await BeginAsync(scope.ServiceProvider, gateway); + var service = scope.ServiceProvider.GetRequiredService(); + Assert.Equal(subscription.Id, (await service.PrepareAsync("alice", "Discovery", Ct)).Id); + await Assert.ThrowsAsync(() => service.PrepareAsync("alice", "Premium", Ct)); + await Assert.ThrowsAsync(() => service.PrepareAsync("alice", "unknown", Ct)); + } +} diff --git a/src/test/Yavsc.Server.Tests/ResourceUsageTests.cs b/src/test/Yavsc.Server.Tests/ResourceUsageTests.cs new file mode 100644 index 000000000..7cbd30f67 --- /dev/null +++ b/src/test/Yavsc.Server.Tests/ResourceUsageTests.cs @@ -0,0 +1,283 @@ +using System.Security.Claims; +using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; +using Yavsc.Models; +using Yavsc.Server.Helpers; +using Yavsc.Settings; + +namespace Yavsc.Server.Tests; + +public sealed class ResourceUsageTests +{ + private static ServiceProvider NewProvider(Action? configure = null) + { + var services = new ServiceCollection(); + var databaseName = Guid.NewGuid().ToString(); + services.AddLogging(); + services.AddDbContext(o => o.UseInMemoryDatabase(databaseName)); + services.AddResourceUsage(new ConfigurationBuilder().Build()); + services.Configure(o => + { + o.ApiCallWeight = 0.02m; + o.CpuSecondWeight = 1m; + o.BandwidthMbWeight = 0.1m; + o.StorageMbWeight = 0.3m; + o.DefaultLimit = 10000m; + }); + services.Configure(o => o.Authority = "https://instance.example"); + if (configure is not null) services.Configure(configure); + return services.BuildServiceProvider(); + } + + private static DefaultHttpContext NewContext(IServiceProvider services) + => new() + { + RequestServices = services, + User = new ClaimsPrincipal(new ClaimsIdentity([new Claim("sub", "alice")], "Bearer")), + Response = { Body = new MemoryStream() } + }; + + [Fact] + public void Configured_formula_uses_exact_decimal_points() + { + var rule = new ResourceUsageOptions + { + ApiCallWeight = 0.2m, CpuSecondWeight = 0.1m, + BandwidthMbWeight = 0.05m, StorageMbWeight = 0.5m, DefaultLimit = 1000m + }; + Assert.Equal(29.34m, rule.Calculate(12.5m, 3.2m, 18.4m, 51.2m)); + Assert.Equal(0m, rule.Calculate(0m, 0m, 0m, 0m)); + Assert.Equal(0.002m, rule.Calculate(0.01m, 0m, 0m, 0m)); + Assert.Equal(1000m, rule.GetLimit("unknown-user")); + } + + [Fact] + public async Task Summary_uses_latest_snapshot_and_individual_limit_without_summing_snapshots() + { + using var provider = NewProvider(o => o.UserLimits["alice"] = 100m); + using var scope = provider.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + db.ResourceUsageRecords.AddRange( + new ResourceUsageRecord { UserId = "alice", UserName = "old", CpuSeconds = 500m, RecordedAtUtc = DateTime.UtcNow.AddDays(-1) }, + new ResourceUsageRecord { UserId = "alice", UserName = "alice", ApiCalls = 10m, CpuSeconds = 3m, BandwidthMb = 2m, StorageMb = 4m }); + await db.SaveChangesAsync(); + var usage = scope.ServiceProvider.GetRequiredService(); + var summary = await usage.GetCurrentAsync("alice", "alice", TestContext.Current.CancellationToken); + Assert.Equal(4.6m, summary.Consumption); + Assert.Equal(100m, summary.ConsumptionLimit); + Assert.Equal(95.4m, summary.RemainingConsumption); + Assert.False(summary.IsQuotaReached); + var overview = await usage.GetOverviewAsync(TestContext.Current.CancellationToken); + Assert.Single(overview); + Assert.Equal(summary.Consumption, overview[0].Consumption); + } + + [Fact] + public async Task Concurrent_admissions_allow_exact_limit_without_lost_updates_or_duplicate_snapshots() + { + using var provider = NewProvider(o => o.DefaultLimit = 0.04m); + var results = await Task.WhenAll(Enumerable.Range(0, 20).Select(async _ => + { + using var scope = provider.CreateScope(); + return await scope.ServiceProvider.GetRequiredService() + .TryAdmitAsync("alice", "alice", 1m, 0m, 0, TestContext.Current.CancellationToken); + })); + Assert.Equal(2, results.Count(accepted => accepted)); + using var finalScope = provider.CreateScope(); + var db = finalScope.ServiceProvider.GetRequiredService(); + var record = Assert.Single(await db.ResourceUsageRecords.ToListAsync(TestContext.Current.CancellationToken)); + Assert.Equal(2m, record.ApiCalls); + var summary = await finalScope.ServiceProvider.GetRequiredService() + .GetCurrentAsync("alice", "alice", TestContext.Current.CancellationToken); + Assert.True(summary.IsQuotaReached); + Assert.Equal(0m, summary.RemainingConsumption); + } + + [Fact] + public async Task Concurrent_accounting_preserves_all_resource_deltas() + { + using var provider = NewProvider(); + await Task.WhenAll(Enumerable.Range(0, 20).Select(async _ => + { + using var scope = provider.CreateScope(); + await ResourceUsageTracker.RecordAsync( + scope.ServiceProvider.GetRequiredService(), + "alice", "alice", cpuSeconds: 0.1m, apiCalls: 1m, + storageBytes: 1048576, bandwidthMb: 0.5m); + })); + using var finalScope = provider.CreateScope(); + var record = Assert.Single(await finalScope.ServiceProvider.GetRequiredService() + .ResourceUsageRecords.ToListAsync(TestContext.Current.CancellationToken)); + Assert.Equal(20m, record.ApiCalls); + Assert.Equal(2m, record.CpuSeconds); + Assert.Equal(20m, record.StorageMb); + Assert.Equal(10m, record.BandwidthMb); + } + + [Fact] + public async Task Known_ingress_or_storage_cost_is_refused_before_consumption() + { + using var provider = NewProvider(o => o.DefaultLimit = 0.11m); + using var scope = provider.CreateScope(); + var usage = scope.ServiceProvider.GetRequiredService(); + Assert.False(await usage.TryAdmitAsync("alice", "alice", 1m, 1m, 0, TestContext.Current.CancellationToken)); + Assert.False(await usage.TryAdmitAsync("alice", "alice", 0m, 0m, 1048576, TestContext.Current.CancellationToken)); + Assert.Empty(await scope.ServiceProvider.GetRequiredService() + .ResourceUsageRecords.ToListAsync(TestContext.Current.CancellationToken)); + } + + [Fact] + public async Task Observed_cost_can_exceed_limit_then_new_operations_are_refused_until_limit_is_increased() + { + using var provider = NewProvider(o => o.DefaultLimit = 1m); + using var scope = provider.CreateScope(); + var usage = scope.ServiceProvider.GetRequiredService(); + Assert.True(await usage.TryAdmitAsync("alice", "alice", 1m, 0m, 0, TestContext.Current.CancellationToken)); + await ResourceUsageTracker.RecordAsync(scope.ServiceProvider.GetRequiredService(), + "alice", "alice", cpuSeconds: 2m, cancellationToken: TestContext.Current.CancellationToken); + Assert.False(await usage.TryAdmitAsync("alice", "alice", 1m, 0m, 0, TestContext.Current.CancellationToken)); + var summary = await usage.GetCurrentAsync("alice", "alice", TestContext.Current.CancellationToken); + Assert.Equal(2.02m, summary.Consumption); + Assert.Equal(0m, summary.RemainingConsumption); + Assert.True(summary.IsQuotaReached); + + var raised = new ResourceUsageService(scope.ServiceProvider.GetRequiredService(), + Options.Create(new ResourceUsageOptions + { + DefaultLimit = 3m, ApiCallWeight = 0.02m, CpuSecondWeight = 1m, + BandwidthMbWeight = 0.1m, StorageMbWeight = 0.3m + })); + Assert.True(await raised.TryAdmitAsync("alice", "alice", 1m, 0m, 0, TestContext.Current.CancellationToken)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task Middleware_counts_streamed_bodies_once_with_or_without_declared_ingress(bool declared) + { + using var provider = NewProvider(); + using var scope = provider.CreateScope(); + var context = NewContext(scope.ServiceProvider); + using var originalRequest = new MemoryStream(new byte[1048576]); + using var originalResponse = new MemoryStream(); + context.Request.Body = originalRequest; + context.Response.Body = originalResponse; + if (declared) context.Request.ContentLength = 1048576; + var middleware = new ResourceUsageTelemetryMiddleware( + async ctx => + { + await ctx.Request.Body.CopyToAsync(Stream.Null); + await ctx.Response.BodyWriter.WriteAsync(new byte[2 * 1048576]); + }, + provider.GetRequiredService(), + NullLogger.Instance); + await middleware.InvokeAsync(context, scope.ServiceProvider.GetRequiredService()); + Assert.Same(originalRequest, context.Request.Body); + Assert.Same(originalResponse, context.Response.Body); + Assert.True(originalRequest.CanRead); + Assert.True(originalResponse.CanWrite); + var summary = await scope.ServiceProvider.GetRequiredService() + .GetCurrentAsync("alice", "alice", TestContext.Current.CancellationToken); + Assert.Equal(1m, summary.ApiCalls); + Assert.Equal(3m, summary.BandwidthMb); + Assert.Equal(0.32m, summary.Consumption); + } + + [Fact] + public async Task Failed_execution_is_accounted_and_original_streams_are_restored() + { + using var provider = NewProvider(); + using var scope = provider.CreateScope(); + var context = NewContext(scope.ServiceProvider); + var originalResponse = context.Response.Body; + var middleware = new ResourceUsageTelemetryMiddleware( + async ctx => + { + await ctx.Response.Body.WriteAsync(new byte[1048576]); + throw new InvalidOperationException("Expected test failure"); + }, provider.GetRequiredService(), + NullLogger.Instance); + await Assert.ThrowsAsync(() => + middleware.InvokeAsync(context, scope.ServiceProvider.GetRequiredService())); + Assert.Same(originalResponse, context.Response.Body); + var summary = await scope.ServiceProvider.GetRequiredService() + .GetCurrentAsync("alice", "alice", TestContext.Current.CancellationToken); + Assert.Equal(0.12m, summary.Consumption); + } + + [Theory] + [InlineData("/login", false)] + [InlineData("/refresh", false)] + [InlineData("/logout", false)] + [InlineData("/api/v1/resource-usage/me", true)] + public async Task Exempt_endpoints_remain_available_without_consumption(string path, bool metadata) + { + using var provider = NewProvider(o => o.DefaultLimit = 0.01m); + using var scope = provider.CreateScope(); + var context = NewContext(scope.ServiceProvider); + context.Request.Path = path; + if (metadata) + context.SetEndpoint(new Endpoint(_ => Task.CompletedTask, + new EndpointMetadataCollection(new ResourceUsageExemptAttribute()), "usage")); + var ran = false; + var middleware = new ResourceUsageTelemetryMiddleware( + _ => { ran = true; return Task.CompletedTask; }, + provider.GetRequiredService(), + NullLogger.Instance); + await middleware.InvokeAsync(context, scope.ServiceProvider.GetRequiredService()); + Assert.True(ran); + Assert.Empty(await scope.ServiceProvider.GetRequiredService() + .ResourceUsageRecords.ToListAsync(TestContext.Current.CancellationToken)); + } + + [Fact] + public async Task Rejected_request_returns_problem_details_without_running_or_charging() + { + using var provider = NewProvider(o => o.DefaultLimit = 0.01m); + using var scope = provider.CreateScope(); + var context = NewContext(scope.ServiceProvider); + var middleware = new ResourceUsageTelemetryMiddleware( + _ => throw new InvalidOperationException("Must not run"), + provider.GetRequiredService(), + NullLogger.Instance); + await middleware.InvokeAsync(context, scope.ServiceProvider.GetRequiredService()); + Assert.Equal(429, context.Response.StatusCode); + Assert.StartsWith("application/problem+json", context.Response.ContentType); + context.Response.Body.Position = 0; + using var reader = new StreamReader(context.Response.Body); + var payload = await reader.ReadToEndAsync(TestContext.Current.CancellationToken); + Assert.Contains("resource_quota_exceeded", payload); + Assert.Contains("https://instance.example/Home/CGU", payload); + Assert.Empty(await scope.ServiceProvider.GetRequiredService() + .ResourceUsageRecords.ToListAsync(TestContext.Current.CancellationToken)); + } + + [Fact] + public async Task Negative_counter_deltas_are_explicitly_rejected() + { + using var provider = NewProvider(); + using var scope = provider.CreateScope(); + await Assert.ThrowsAsync(() => + ResourceUsageTracker.RecordAsync(scope.ServiceProvider.GetRequiredService(), + "alice", "alice", cpuSeconds: -1m, cancellationToken: TestContext.Current.CancellationToken)); + } + + [Theory] + [InlineData("ResourceUsage:ApiCallWeight", "0")] + [InlineData("ResourceUsage:BandwidthMbWeight", "-1")] + [InlineData("ResourceUsage:DefaultLimit", "0")] + [InlineData("ResourceUsage:UserLimits:alice", "-1")] + public void Invalid_configuration_is_rejected(string key, string value) + { + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary { [key] = value }).Build(); + var services = new ServiceCollection().AddResourceUsage(configuration); + using var provider = services.BuildServiceProvider(); + Assert.Throws(() => + provider.GetRequiredService>().Value); + } +}