feat: add resource quotas, PayPal subscriptions and paid disk allowances

Calculate and enforce configurable resource consumption, expose balances in PostIt and document the rule in CGU. Add confirmed monthly PayPal credits and disk quotas of twice the free default for Discovery and five times for Premium, with payment reconciliation, migrations and regression tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Paul Schneider 2026-11-08 05:02:23 +00:00
commit 650e5f5e30
Signed by: notazof
GPG key ID: AD4B9B545FBD7D12
69 changed files with 19111 additions and 203 deletions

View file

@ -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.

View file

@ -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://<Yavsc.Org host>/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**.

View file

@ -1,4 +1,7 @@
{
"ResourceUsage": {
"UserLimits": {}
},
"Site": {
"Authority": "https://preprod-yavsc.$DOMAIN",
"Audience": ["api"],

View file

@ -1,4 +1,7 @@
{
"ResourceUsage": {
"UserLimits": {}
},
"Site": {
"Title": "Yavsc Blogs",
"Description": "A collection of blogs about software development, technology, and programming.",

View file

@ -1,4 +1,15 @@
{
"ResourceSubscriptions": {
"DiscoveryPlanId": "",
"PremiumPlanId": "",
"WebhookId": "",
"DiscoveryPriceExcludingTax": 1,
"PremiumPriceExcludingTax": 2,
"VatPercent": 20
},
"ResourceUsage": {
"UserLimits": {}
},
"Logging": {
"LogLevel": {
"Default": "Information"

View file

@ -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

View file

@ -0,0 +1,19 @@
using PostIt.ViewModels;
namespace PostIt.Tests;
public sealed class AboutTermsTests
{
[Fact]
public void Terms_follow_the_configured_instance_and_changes_to_it()
{
var settings = new Settings();
settings.Authentication.Authority = "https://first.example/";
var model = new AboutPageViewModel(settings);
Assert.Equal("https://first.example/Home/CGU", model.TermsUri.AbsoluteUri);
Assert.Equal("https://first.example/ResourceSubscription", model.SubscriptionsUri.AbsoluteUri);
settings.Authentication.Authority = "https://second.example";
Assert.Equal("https://second.example/Home/CGU", model.TermsUri.AbsoluteUri);
Assert.Equal("https://second.example/ResourceSubscription", model.SubscriptionsUri.AbsoluteUri);
}
}

View file

@ -15,6 +15,32 @@ namespace PostIt.Tests;
public class AdministrationAccessTests
{
[Fact]
public async Task Profile_quota_failure_keeps_the_consumption_visible_without_enabling_profile_changes()
{
await using var api = new ProfileTransport("Performer") { ExhaustedQuota = true };
var model = new UserProfilePageViewModel(api, usageClient: api.UsageClient);
await model.RefreshAsync();
Assert.True(model.ResourceUsage?.IsQuotaReached);
Assert.False(model.IsProfileLoaded);
Assert.False(model.SaveProfileCommand.CanExecute(null));
Assert.Contains("resource_quota_exceeded", model.StatusMessage);
}
[Fact]
public async Task Administration_quota_failure_keeps_the_authorized_usage_overview()
{
await using var api = new ProfileTransport("Administrator");
var session = new SessionStatusViewModel { Api = api };
await session.RefreshAsync();
api.ExhaustedQuota = true;
var model = new AdministrationPageViewModel(api, api.UsageClient, session);
await model.RefreshAsync();
Assert.True(Assert.Single(model.UsageOverview).IsQuotaReached);
Assert.Empty(model.Users);
Assert.Contains("les usages restent consultables", model.StatusMessage);
}
[AvaloniaTheory]
[InlineData("Moderator", false)]
[InlineData("Administrator", true)]
@ -242,6 +268,7 @@ public class AdministrationAccessTests
public Task<Me>? PendingProfile { get; set; }
public bool FailProfile { get; set; }
public bool FailUsage { get; set; }
public bool ExhaustedQuota { get; set; }
public bool HideIpAddressInCalls { get; set; }
public ResourceUsageApiClient UsageClient => new(this, Settings.ApiUrl);
@ -268,6 +295,8 @@ public class AdministrationAccessTests
object result;
if (path.EndsWith("/account/me"))
{
if (ExhaustedQuota)
throw new HttpRequestException("resource_quota_exceeded", null, System.Net.HttpStatusCode.TooManyRequests);
if (FailProfile)
throw new HttpRequestException("Profile unavailable");
result = PendingProfile is not null ? await PendingProfile
@ -277,11 +306,22 @@ public class AdministrationAccessTests
{
if (FailUsage)
throw new HttpRequestException("Usage unavailable");
var usage = new ResourceUsageSummary { UserId = "alice-id", UserName = "alice", ApiCalls = 12m };
var usage = new ResourceUsageSummary
{
UserId = "alice-id", UserName = "alice", ApiCalls = 12m,
Consumption = ExhaustedQuota ? 10000m : 0.24m,
ConsumptionLimit = 10000m,
RemainingConsumption = ExhaustedQuota ? 0m : 9999.76m,
IsQuotaReached = ExhaustedQuota
};
result = path.EndsWith("/admin") ? new List<ResourceUsageSummary> { usage } : usage;
}
else if (path.EndsWith("/users"))
{
if (ExhaustedQuota)
throw new HttpRequestException("resource_quota_exceeded", null, System.Net.HttpStatusCode.TooManyRequests);
result = new List<UserInfo> { new() { UserId = "alice-id", UserName = "alice" } };
}
else
throw new InvalidOperationException($"Unexpected endpoint: {path}");

View file

@ -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

View file

@ -96,6 +96,17 @@ public partial class AdministrationPageViewModel : ViewModelBase
IsBusy = true;
try
{
var usage = await _usageClient.GetAdminOverviewAsync().ConfigureAwait(true)
?? throw new InvalidOperationException("Le serveur a renvoyé une liste d’usages vide.");
if (!_session.IsAdministrator)
{
ClearAdministrationData();
StatusMessage = "Le rôle Administrator est requis.";
return;
}
foreach (var item in usage)
UsageOverview.Add(item);
var query = SearchText.Trim();
var usersEndpoint = string.IsNullOrWhiteSpace(query)
? new Uri(new Uri(_api.Settings.ApiUrl.TrimEnd('/') + "/", UriKind.Absolute), "users").ToString()
@ -103,11 +114,10 @@ public partial class AdministrationPageViewModel : ViewModelBase
var users = await _api.CallAsync<List<UserInfo>>(HttpMethod.Get, usersEndpoint).ConfigureAwait(true)
?? throw new InvalidOperationException("Le serveur a renvoyé une liste d’utilisateurs vide.");
var usage = await _usageClient.GetAdminOverviewAsync().ConfigureAwait(true)
?? throw new InvalidOperationException("Le serveur a renvoyé une liste d’usages vide.");
if (!_session.IsAdministrator)
{
ClearAdministrationData();
StatusMessage = "Le rôle Administrator est requis.";
return;
}
@ -116,8 +126,6 @@ public partial class AdministrationPageViewModel : ViewModelBase
{
Users.Add(user);
}
foreach (var item in usage)
UsageOverview.Add(item);
OnPropertyChanged(nameof(FilteredUsers));
OnPropertyChanged(nameof(SelectedUsage));
@ -125,6 +133,16 @@ public partial class AdministrationPageViewModel : ViewModelBase
await LoadToolsAsync();
StatusMessage = Users.Count > 0 ? $"{Users.Count} utilisateur(s) chargé(s)." : "Aucun utilisateur trouvé.";
}
catch (HttpRequestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
{
if (!_session.IsAdministrator)
{
ClearAdministrationData();
StatusMessage = "Le rôle Administrator est requis.";
}
else
StatusMessage = $"Quota insuffisant : les usages restent consultables. {ex.Message}";
}
catch (Exception ex)
{
ClearAdministrationData();

View file

@ -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;
}
}

View file

@ -17,6 +17,9 @@ public partial class UserProfilePageViewModel : ViewModelBase
private readonly YavscApiClient? _api;
private readonly SessionStatusViewModel? _sessionStatus;
private readonly ResourceUsageApiClient? _usageClient;
private readonly AboutPageViewModel _subscriptionLinks;
public IAsyncRelayCommand OpenSubscriptionsCommand => _subscriptionLinks.OpenSubscriptionsCommand;
public string? SubscriptionLinkError => _subscriptionLinks.StatusMessage;
[ObservableProperty]
public partial ResourceUsageSummary? ResourceUsage { get; set; }
@ -158,6 +161,8 @@ public partial class UserProfilePageViewModel : ViewModelBase
_api = api;
_sessionStatus = sessionStatus;
_usageClient = usageClient;
_subscriptionLinks = new AboutPageViewModel(api?.Settings);
_subscriptionLinks.PropertyChanged += (_, _) => OnPropertyChanged(nameof(SubscriptionLinkError));
}
public UserProfilePageViewModel() : this(null, null) { }
@ -167,19 +172,18 @@ public partial class UserProfilePageViewModel : ViewModelBase
[RelayCommand]
public async Task RefreshAsync()
{
if (_api is null)
{
IsProfileLoaded = false;
StatusMessage = "Client API de profil indisponible.";
return;
}
IsBusy = true;
IsProfileLoaded = false;
ResourceUsage = null;
UsageError = null;
try
{
await LoadUsageAsync();
if (_api is null)
{
StatusMessage = "Client API de profil indisponible.";
return;
}
var me = await _api.CallAsync<Me>(HttpMethod.Get, new Uri(new Uri(_api.Settings.ApiUrl.TrimEnd('/') + "/", UriKind.Absolute), "account/me").ToString()).ConfigureAwait(true);
if (me is null)
{
@ -218,18 +222,6 @@ public partial class UserProfilePageViewModel : ViewModelBase
}
IsProfileLoaded = true;
StatusMessage = "Profil chargé.";
if (_usageClient is not null)
{
try
{
ResourceUsage = await _usageClient.GetCurrentAsync()
?? throw new InvalidOperationException("Le serveur a renvoyé un usage vide.");
}
catch (Exception ex)
{
UsageError = $"Impossible de charger mon usage : {ex.Message}";
}
}
}
catch (Exception ex)
{
@ -264,6 +256,24 @@ public partial class UserProfilePageViewModel : ViewModelBase
}
}
private async Task LoadUsageAsync()
{
if (_usageClient is null)
{
UsageError = "Client API de consommation indisponible.";
return;
}
try
{
ResourceUsage = await _usageClient.GetCurrentAsync()
?? throw new InvalidOperationException("Le serveur a renvoyé un usage vide.");
}
catch (Exception ex) when (ex is HttpRequestException or JsonException or InvalidOperationException)
{
UsageError = $"Impossible de charger mon usage : {ex.Message}";
}
}
public ProfileUpdateRequest BuildProfileUpdateRequest()
{
static string NormalizeField(string? value, string placeholder)

View file

@ -34,5 +34,12 @@
<Button Content="Signaler un problème"
Command="{Binding OpenIssuesCommand}"
HorizontalAlignment="Center"/>
<Button Content="CGU de PostIt et de Yavsc"
Command="{Binding OpenTermsCommand}"
HorizontalAlignment="Center"/>
<Button Content="Abonnements Découverte et Premium"
Command="{Binding OpenSubscriptionsCommand}"
HorizontalAlignment="Center"/>
<TextBlock Text="{Binding StatusMessage}" TextWrapping="Wrap" MaxWidth="420"/>
</StackPanel>
</ContentPage>

View file

@ -119,8 +119,13 @@
<TextBlock Text="Usage des ressources" FontWeight="Bold"/>
<TextBlock Text="{Binding ApiCalls, StringFormat='Appels API : {0:F0}'}"/>
<TextBlock Text="{Binding CpuSeconds, StringFormat='CPU : {0:F3} s'}"/>
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mo'}"/>
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage : {0:F3} Mo'}"/>
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mio'}"/>
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage ajouté : {0:F3} Mio'}"/>
<TextBlock Text="{Binding Consumption, StringFormat='Consommation : {0:F3} points'}"/>
<TextBlock Text="{Binding CreditBalance, StringFormat='Crédit reporté : {0:F3} points'}"/>
<TextBlock Text="{Binding ConsumptionLimit, StringFormat='Seuil : {0:F3} points'}"/>
<TextBlock Text="{Binding RemainingConsumption, StringFormat='Solde : {0:F3} points'}"/>
<TextBlock Text="Quota atteint" IsVisible="{Binding IsQuotaReached}"/>
<TextBlock Text="{Binding EstimatedCompensation, StringFormat='Estimation : {0:F2}'}"/>
<TextBlock Text="{Binding Currency}"/>
</StackPanel>
@ -141,8 +146,13 @@
<TextBlock Text="{Binding UserId}"/>
<TextBlock Text="{Binding ApiCalls, StringFormat='Appels API : {0:F0}'}"/>
<TextBlock Text="{Binding CpuSeconds, StringFormat='CPU : {0:F3} s'}"/>
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mo'}"/>
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage : {0:F3} Mo'}"/>
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mio'}"/>
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage ajouté : {0:F3} Mio'}"/>
<TextBlock Text="{Binding Consumption, StringFormat='Consommation : {0:F3} points'}"/>
<TextBlock Text="{Binding CreditBalance, StringFormat='Crédit reporté : {0:F3} points'}"/>
<TextBlock Text="{Binding ConsumptionLimit, StringFormat='Seuil : {0:F3} points'}"/>
<TextBlock Text="{Binding RemainingConsumption, StringFormat='Solde : {0:F3} points'}"/>
<TextBlock Text="Quota atteint" IsVisible="{Binding IsQuotaReached}"/>
<TextBlock Text="{Binding EstimatedCompensation, StringFormat='Estimation : {0:F2}'}"/>
<TextBlock Text="{Binding Currency}"/>
</StackPanel>

View file

@ -31,11 +31,22 @@
<StackPanel Spacing="4">
<TextBlock Text="Mon usage des ressources" FontSize="18" FontWeight="SemiBold"/>
<TextBlock Text="{Binding UsageError}" TextWrapping="Wrap"/>
<Button Content="Abonnements Découverte et Premium"
Command="{Binding OpenSubscriptionsCommand}"/>
<TextBlock Text="{Binding SubscriptionLinkError}" TextWrapping="Wrap"/>
<StackPanel DataContext="{Binding ResourceUsage}" Spacing="4">
<TextBlock Text="{Binding ApiCalls, StringFormat='Appels API : {0:F0}'}"/>
<TextBlock Text="{Binding CpuSeconds, StringFormat='CPU : {0:F3} s'}"/>
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mo'}"/>
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage : {0:F3} Mo'}"/>
<TextBlock Text="{Binding BandwidthMb, StringFormat='Bande passante : {0:F3} Mio'}"/>
<TextBlock Text="{Binding StorageMb, StringFormat='Stockage ajouté : {0:F3} Mio'}"/>
<TextBlock Text="{Binding Consumption, StringFormat='Consommation : {0:F3} points'}"/>
<TextBlock Text="{Binding GrossConsumption, StringFormat='Consommation brute : {0:F3} points'}"/>
<TextBlock Text="{Binding AppliedCredits, StringFormat='Usage effacé : {0:F3} points'}"/>
<TextBlock Text="{Binding CreditBalance, StringFormat='Crédit reporté disponible : {0:F3} points'}"/>
<TextBlock Text="{Binding ConsumptionLimit, StringFormat='Seuil : {0:F3} points'}"/>
<TextBlock Text="{Binding RemainingConsumption, StringFormat='Solde : {0:F3} points'}"/>
<TextBlock Text="Quota atteint : les opérations consommatrices sont bloquées."
IsVisible="{Binding IsQuotaReached}" TextWrapping="Wrap"/>
<TextBlock Text="{Binding EstimatedCompensation, StringFormat='Estimation : {0:F2}'}"/>
<TextBlock Text="{Binding Currency}"/>
</StackPanel>

View file

@ -2,9 +2,7 @@ namespace Yavsc.Abstract.Resources;
/// <summary>
/// Lightweight usage snapshot for PostIt / Yavsc resource accounting.
/// The initial implementation is intentionally intentionally small and
/// serialisable so it can be exposed through the API and consumed by the
/// PostIt client without requiring a full database model yet.
/// Counters are cumulative; consumption is expressed in points, not currency.
/// </summary>
public sealed class ResourceUsageSummary
{
@ -17,6 +15,17 @@ public sealed class ResourceUsageSummary
public decimal BandwidthMb { get; set; }
public decimal StorageMb { get; set; }
public decimal Consumption { get; set; }
public decimal GrossConsumption { get; set; }
public decimal CreditBalance { get; set; }
public decimal AppliedCredits { get; set; }
public decimal ConsumptionLimit { get; set; }
public decimal RemainingConsumption { get; set; }
public bool IsQuotaReached { get; set; }
public long? DiskQuota { get; set; }
public DateTimeOffset? PremiumDiskQuotaUntil { get; set; }
public DateTimeOffset? DiscoveryDiskQuotaUntil { get; set; }
public decimal EstimatedCompensation { get; set; }
public string Currency { get; set; } = Constants.ResourceUsageDefaultCurrency;
}

View file

@ -1,96 +1,29 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Yavsc.Abstract.Resources;
using Yavsc.Models;
using Yavsc.Server.Helpers;
namespace Yavsc.ApiControllers.Administration;
[Produces("application/json")]
[Authorize]
[ResourceUsageExempt]
[Route(Constants.APIPrefix + "/" + Constants.ResourceUsagePath)]
public sealed class ResourceUsageApiController : Controller
public sealed class ResourceUsageApiController(IResourceUsageService usage) : Controller
{
private readonly ApplicationDbContext _dbContext;
public ResourceUsageApiController(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
private static ResourceUsageSummary ToSummary(ResourceUsageRecord record)
=> new()
{
UserId = record.UserId,
UserName = record.UserName,
RecordedAtUtc = new DateTimeOffset(record.RecordedAtUtc, TimeSpan.Zero),
ApiCalls = record.ApiCalls,
CpuSeconds = record.CpuSeconds,
BandwidthMb = record.BandwidthMb,
StorageMb = record.StorageMb,
EstimatedCompensation = record.EstimatedCompensation,
Currency = record.Currency
};
[HttpGet(Constants.ResourceUsageCurrentUserRoute)]
public async Task<ActionResult<ResourceUsageSummary>> GetCurrentUserUsage()
public async Task<ActionResult<ResourceUsageSummary>> GetCurrentUserUsage(CancellationToken cancellationToken)
{
var userId = User.GetUserId();
if (string.IsNullOrWhiteSpace(userId))
{
return Unauthorized();
}
var userName = User.Identity?.Name ?? Constants.ResourceUsageCurrentUserDisplayName;
var record = await _dbContext.ResourceUsageRecords
.Where(r => r.UserId == userId)
.OrderByDescending(r => r.RecordedAtUtc)
.FirstOrDefaultAsync();
if (record is null)
{
var zeroUsage = new ResourceUsageRecord
{
UserId = userId,
UserName = userName,
RecordedAtUtc = DateTime.UtcNow,
ApiCalls = 0m,
CpuSeconds = 0m,
BandwidthMb = 0m,
StorageMb = 0m,
EstimatedCompensation = 0m,
Currency = Constants.ResourceUsageDefaultCurrency,
UserCreated = userId,
UserModified = userId,
DateCreated = DateTime.UtcNow,
DateModified = DateTime.UtcNow,
};
_dbContext.ResourceUsageRecords.Add(zeroUsage);
await _dbContext.SaveChangesAsync(userId);
return Ok(ToSummary(zeroUsage));
}
return Ok(ToSummary(record));
if (string.IsNullOrWhiteSpace(userId)) return Unauthorized();
return Ok(await usage.GetCurrentAsync(userId,
User.Identity?.Name ?? Constants.ResourceUsageCurrentUserDisplayName, cancellationToken));
}
[HttpGet(Constants.ResourceUsageAdminRoute)]
public async Task<ActionResult<List<ResourceUsageSummary>>> GetAdminOverview()
public async Task<ActionResult<List<ResourceUsageSummary>>> GetAdminOverview(CancellationToken cancellationToken)
{
if (!User.IsInRole(Constants.AdminName))
{
return Forbid();
}
var latestByUser = (await _dbContext.ResourceUsageRecords
.OrderByDescending(r => r.RecordedAtUtc)
.ToListAsync())
.GroupBy(r => r.UserId)
.Select(g => g.First())
.OrderByDescending(r => r.RecordedAtUtc)
.ToList();
return Ok(latestByUser.Select(ToSummary).ToList());
if (!User.IsInRole(Constants.AdminName)) return Forbid();
return Ok(await usage.GetOverviewAsync(cancellationToken));
}
}

View file

@ -119,7 +119,7 @@ namespace Yavsc.WebApi.Controllers
}).ToArray();
user.PostsCounter = await _dbContext.BlogSpot.LongCountAsync(x => x.AuthorId == uid);
user.DiskUsage = userData.DiskUsage;
user.DiskQuota = userData.DiskQuota;
user.DiskQuota = userData.GetEffectiveDiskQuota(DateTimeOffset.UtcNow);
user.Credits = userData.AccountBalance?.Credits ?? 0;
user.BankAccounts = (userData.BankInfo ?? [])
.Select(bank => new BankAccountInfo

View file

@ -28,6 +28,7 @@ internal class Program
var services = builder.Services;
services.AddSiteAdministration();
services.AddResourceUsage(builder.Configuration);
// Anthropic client
builder.Services.AddSingleton<AnthropicClient>(_ =>
@ -151,9 +152,9 @@ internal class Program
app
.UseRouting()
.UseAuthentication()
.UseCors("default")
.UseAuthorization()
.UseMiddleware<ResourceUsageTelemetryMiddleware>()
.UseCors("default");
.UseMiddleware<ResourceUsageTelemetryMiddleware>();
app.MapIdentityApi<ApplicationUser>().RequireAuthorization("ApiScope");
app.MapDefaultControllerRoute();
app.UseSession();

View file

@ -0,0 +1,14 @@
@baseUrl = https://localhost:5104
@token = replace-with-access-token
### Current user's cumulative counters, indicator and effective quota (also accessible at the limit)
GET {{baseUrl}}/api/v1/resource-usage/me
Authorization: Bearer {{token}}
### Latest snapshot per user (administrator role required)
GET {{baseUrl}}/api/v1/resource-usage/admin
Authorization: Bearer {{token}}
### Consuming operation: returns 429 Problem Details when quota is exhausted
GET {{baseUrl}}/api/v1/notifications/me
Authorization: Bearer {{token}}

View file

@ -1,4 +1,7 @@
{
"ResourceUsage": {
"UserLimits": {}
},
"Site": {
"Authority": "https://yavsc.pschneider.fr",
"Audience": ["api"],

View file

@ -99,9 +99,20 @@ namespace Yavsc.Blogs.Controllers
}
[HttpPost("{*subdir}")]
public async Task<IActionResult> Post([ValidRemoteUserFilePath] string subdir="")
public async Task<IActionResult> Post([FromServices] IResourceUsageService usage,
[ValidRemoteUserFilePath] string subdir="", CancellationToken cancellationToken = default)
{
if (!ModelState.IsValid) return new BadRequestObjectResult(ModelState);
var uid = User.GetUserId();
var form = await Request.ReadFormAsync(cancellationToken);
var anticipatedBytes = form.Files.Sum(f => f.Length);
if (!await usage.TryAdmitAsync(uid, User.Identity?.Name ?? uid, 0m, 0m,
anticipatedBytes, cancellationToken))
{
_logger.LogWarning("Resource quota refused file storage for user {UserId}", uid);
await ResourceUsageTelemetryMiddleware.WriteQuotaProblemAsync(HttpContext);
return new EmptyResult();
}
string destDir = null;
List<FileReceivedInfo> received = new List<FileReceivedInfo>();
InvalidPathException pathex = null;
@ -117,14 +128,13 @@ namespace Yavsc.Blogs.Controllers
}
_logger.LogInformation($"Receiving files, saved in '{destDir}' (specified as '{subdir}').");
var uid = User.GetUserId();
var user = dbContext.Users.Single(
u => u.Id == uid
);
int i=0;
_logger.LogInformation($"Receiving {Request.Form.Files.Count} files.");
_logger.LogInformation($"Receiving {form.Files.Count} files.");
foreach (var f in Request.Form.Files)
foreach (var f in form.Files)
{
var item = user.ReceiveUserFile(destDir, f);
if (!item.QuotaOffense)

View file

@ -21,6 +21,7 @@ internal class Program
builder.AddConfiguration("blogs");
var services = builder.Services;
services.AddResourceUsage(builder.Configuration);
var siteSection = builder.Configuration.GetSection("Site");
services.Configure<SiteSettings>(siteSection);
@ -100,9 +101,9 @@ internal class Program
app
.UseRouting()
.UseAuthentication()
.UseCors("default")
.UseAuthorization()
.UseMiddleware<ResourceUsageTelemetryMiddleware>()
.UseCors("default")
;
app.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Program")
.LogInformation($"Yavsc.Blogs started, Authority is '{authority}''");

View file

@ -1,4 +1,7 @@
{
"ResourceUsage": {
"UserLimits": {}
},
"Site": {
"Title": "Yavsc Blogs",
"Description": "A collection of blogs about software development, technology, and programming.",

View file

@ -117,7 +117,7 @@ namespace Yavsc.Controllers
Avatar = user.Avatar,
BankInfo = user.BankInfo,
DiskUsage = user.DiskUsage,
DiskQuota = user.DiskQuota,
DiskQuota = user.GetEffectiveDiskQuota(DateTimeOffset.UtcNow),
DedicatedCalendarId = user.DedicatedGoogleCalendar,
EMail = user.Email,
EmailConfirmed = await _userManager.IsEmailConfirmedAsync(user),

View file

@ -138,6 +138,10 @@ namespace Yavsc.Controllers
{
return View();
}
public IActionResult CGU()
{
return View();
}
public IActionResult AboutMarkdown()
{
return View();

View file

@ -0,0 +1,167 @@
#nullable enable
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Options;
using Yavsc.Models.Payment;
using Yavsc.Server.Helpers;
using Yavsc.Settings;
using Yavsc.ViewModels;
using Yavsc.Web.Services;
namespace Yavsc.Controllers;
[Authorize(Policy = "WebUser")]
[ResourceUsageExempt]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
public sealed class ResourceSubscriptionController(IResourceSubscriptionService subscriptions,
IPayPalSubscriptionGateway paypal, IResourceUsageService usage,
IOptions<ResourceSubscriptionOptions> options, IOptions<ResourceUsageOptions> usageOptions,
IOptions<SiteSettings> site, ILogger<ResourceSubscriptionController> logger) : Controller
{
[HttpGet]
public async Task<IActionResult> Index(CancellationToken ct)
=> View(new ResourceSubscriptionPage(options.Value, usageOptions.Value.DefaultLimit,
await usage.GetCurrentAsync(User.GetUserId(), User.Identity?.Name ?? User.GetUserId(), ct),
await subscriptions.GetAllAsync(User.GetUserId(), ct)));
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Create(string pack, CancellationToken ct)
{
var subscription = await subscriptions.PrepareAsync(User.GetUserId(), pack, ct);
PayPalSubscriptionDetails remote;
if (subscription.PayPalId is null)
{
var root = SiteRoot();
remote = await paypal.CreateAsync(subscription,
root + "ResourceSubscription/Return?id=" + subscription.Id,
root + "ResourceSubscription", ct);
await subscriptions.BindAsync(subscription, remote, ct);
}
else
{
remote = await paypal.GetAsync(subscription.PayPalId, ct);
await subscriptions.BindAsync(subscription, remote, ct);
}
if (remote.ApprovalUrl is not null && remote.State == "APPROVAL_PENDING")
return Redirect(remote.ApprovalUrl);
await subscriptions.ReconcileAsync(subscription, ct);
return RedirectToAction(nameof(Index));
}
[HttpGet]
public async Task<IActionResult> Return(Guid id, CancellationToken ct)
{
var subscription = await OwnedAsync(id, ct);
if (subscription is null) return NotFound();
await subscriptions.ReconcileAsync(subscription, ct);
return RedirectToAction(nameof(Index));
}
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Refresh(CancellationToken ct)
{
foreach (var subscription in await subscriptions.GetAllAsync(User.GetUserId(), ct))
await subscriptions.ReconcileAsync(subscription, ct);
return RedirectToAction(nameof(Index));
}
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Cancel(Guid id, CancellationToken ct)
{
var subscription = await OwnedAsync(id, ct);
if (subscription is null || subscription.PayPalId is null) return NotFound();
await subscriptions.ReconcileAsync(subscription, ct);
if (subscription.State is not ("CANCELLED" or "EXPIRED"))
await paypal.CancelAsync(subscription.PayPalId, ct);
await subscriptions.ReconcileAsync(subscription, ct);
return RedirectToAction(nameof(Index));
}
[AllowAnonymous, HttpPost, IgnoreAntiforgeryToken, RawPayPalNotification, RequestSizeLimit(64 * 1024)]
public async Task<IActionResult> Notify(CancellationToken ct)
{
JsonDocument notification;
try
{
notification = await JsonDocument.ParseAsync(Request.Body, cancellationToken: ct);
}
catch (JsonException ex)
{
logger.LogWarning(ex, "Malformed PayPal subscription notification");
return BadRequest();
}
using (notification)
{
var headers = Request.Headers.ToDictionary(pair => pair.Key, pair => pair.Value.ToString(), StringComparer.OrdinalIgnoreCase);
await paypal.VerifyWebhookAsync(notification.RootElement, headers, ct);
var value = notification.RootElement;
if (value.ValueKind != JsonValueKind.Object
|| !value.TryGetProperty("event_type", out var type) || type.ValueKind != JsonValueKind.String
|| !value.TryGetProperty("resource", out var resource) || resource.ValueKind != JsonValueKind.Object)
throw new SubscriptionOperationException(400, "Notification PayPal incomplete.");
var eventType = type.GetString()!;
if (!eventType.StartsWith("BILLING.SUBSCRIPTION.", StringComparison.Ordinal)
&& eventType is not ("PAYMENT.SALE.COMPLETED" or "PAYMENT.SALE.REFUNDED" or "PAYMENT.SALE.REVERSED"))
return NoContent();
ResourceSubscription? subscription;
var key = eventType.StartsWith("BILLING.", StringComparison.Ordinal) ? "id" : "billing_agreement_id";
var paypalId = NotificationText(resource, key);
if (paypalId is not null)
subscription = await subscriptions.FindPayPalAsync(paypalId, ct);
else
{
var transactionId = NotificationText(resource, "sale_id") ?? NotificationText(resource, "id");
subscription = transactionId is null ? null : await subscriptions.FindPaymentAsync(transactionId, ct);
if (subscription is null && resource.TryGetProperty("links", out var links))
{
if (links.ValueKind != JsonValueKind.Array)
throw new SubscriptionOperationException(400, "Liens de notification PayPal invalides.");
foreach (var link in links.EnumerateArray())
{
if (link.ValueKind != JsonValueKind.Object)
throw new SubscriptionOperationException(400, "Lien de notification PayPal invalide.");
if (!Uri.TryCreate(NotificationText(link, "href"), UriKind.Absolute, out var uri)
|| uri.Scheme != "https" || uri.Host is not ("api.paypal.com" or "api.sandbox.paypal.com" or "api-m.paypal.com" or "api-m.sandbox.paypal.com")
|| !uri.AbsolutePath.StartsWith("/v1/payments/sale/", StringComparison.Ordinal)) continue;
subscription = await subscriptions.FindPaymentAsync(uri.Segments[^1], ct);
if (subscription is not null) break;
}
}
}
if (subscription is null) return NotFound();
// The signed notification is a trigger, not the payment authority.
// Re-fetch the bound subscription and all transactions from PayPal.
await subscriptions.ReconcileAsync(subscription, ct);
return NoContent();
}
}
private async Task<ResourceSubscription?> OwnedAsync(Guid id, CancellationToken ct)
=> (await subscriptions.GetAllAsync(User.GetUserId(), ct)).SingleOrDefault(s => s.Id == id);
private static string? NotificationText(JsonElement value, string property)
{
if (!value.TryGetProperty(property, out var found)) return null;
return found.ValueKind == JsonValueKind.String && !string.IsNullOrWhiteSpace(found.GetString())
? found.GetString() : throw new SubscriptionOperationException(400, "Reference de notification PayPal invalide.");
}
private string SiteRoot()
{
if (!Uri.TryCreate(site.Value.ExternalUrl, UriKind.Absolute, out var uri)
|| uri.Scheme != "https" || !string.IsNullOrEmpty(uri.UserInfo))
throw new SubscriptionOperationException(503, "L'adresse HTTPS publique du site n'est pas configuree.");
return site.Value.ExternalUrl.TrimEnd('/') + "/";
}
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var executed = await next();
if (executed.Exception is not SubscriptionOperationException error || Response.HasStarted) return;
logger.LogError(error, "Resource subscription operation failed for {Action}", context.ActionDescriptor.DisplayName);
executed.ExceptionHandled = true;
executed.Result = Problem(statusCode: error.StatusCode, title: "Abonnement indisponible", detail: error.Message);
}
}

View file

@ -83,6 +83,19 @@ public static class HostingExtensions
builder.Services.AddSwaggerGen();
IServiceCollection services = LoadConfiguration(builder);
services.AddResourceUsage(builder.Configuration);
services.AddOptions<ResourceSubscriptionOptions>()
.Bind(builder.Configuration.GetSection(ResourceSubscriptionOptions.SectionName))
.Validate(o => o.DiscoveryPriceExcludingTax > 0m && o.PremiumPriceExcludingTax > 0m
&& decimal.Round(o.DiscoveryPriceExcludingTax, 2) == o.DiscoveryPriceExcludingTax
&& decimal.Round(o.PremiumPriceExcludingTax, 2) == o.PremiumPriceExcludingTax
&& o.VatPercent >= 0m && o.VatPercent <= 100m,
"ResourceSubscriptions prices and VAT are invalid.")
.ValidateOnStart();
services.AddHttpClient<Yavsc.Web.Services.IPayPalSubscriptionGateway, Yavsc.Web.Services.PayPalSubscriptionGateway>()
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false });
services.AddScoped<Yavsc.Web.Services.IResourceSubscriptionService, Yavsc.Web.Services.ResourceSubscriptionService>();
services.TryAddSingleton(TimeProvider.System);
services.AddSession();

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,107 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Yavsc.Migrations
{
/// <inheritdoc />
public partial class ResourceSubscriptions : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<decimal>(
name: "CreditPoints",
table: "ResourceUsageRecords",
type: "numeric(28,8)",
precision: 28,
scale: 8,
nullable: false,
defaultValue: 0m);
migrationBuilder.CreateTable(
name: "ResourceSubscriptions",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
UserId = table.Column<string>(type: "character varying(450)", maxLength: 450, nullable: false),
Pack = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
PlanId = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
PayPalId = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
State = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
MonthlyPoints = table.Column<decimal>(type: "numeric(28,8)", precision: 28, scale: 8, nullable: false),
PriceExcludingTax = table.Column<decimal>(type: "numeric(18,2)", nullable: false),
VatPercent = table.Column<decimal>(type: "numeric", nullable: false),
PriceIncludingTax = table.Column<decimal>(type: "numeric(18,2)", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
StartsAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
NextBillingAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
LastCreditedCycle = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ResourceSubscriptions", x => x.Id);
table.ForeignKey(
name: "FK_ResourceSubscriptions_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
name: "ResourceSubscriptionPayments",
columns: table => new
{
Id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
SubscriptionId = table.Column<Guid>(type: "uuid", nullable: false),
Cycle = table.Column<int>(type: "integer", nullable: false),
PaidAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
GrantedPoints = table.Column<decimal>(type: "numeric(28,8)", precision: 28, scale: 8, nullable: false),
Reversed = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ResourceSubscriptionPayments", x => x.Id);
table.ForeignKey(
name: "FK_ResourceSubscriptionPayments_ResourceSubscriptions_Subscrip~",
column: x => x.SubscriptionId,
principalTable: "ResourceSubscriptions",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_ResourceSubscriptionPayments_SubscriptionId_Cycle",
table: "ResourceSubscriptionPayments",
columns: new[] { "SubscriptionId", "Cycle" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_ResourceSubscriptions_PayPalId",
table: "ResourceSubscriptions",
column: "PayPalId",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_ResourceSubscriptions_UserId",
table: "ResourceSubscriptions",
column: "UserId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ResourceSubscriptionPayments");
migrationBuilder.DropTable(
name: "ResourceSubscriptions");
migrationBuilder.DropColumn(
name: "CreditPoints",
table: "ResourceUsageRecords");
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,29 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Yavsc.Migrations
{
/// <inheritdoc />
public partial class PremiumDiskQuota : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTimeOffset>(
name: "PremiumDiskQuotaUntil",
table: "AspNetUsers",
type: "timestamp with time zone",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "PremiumDiskQuotaUntil",
table: "AspNetUsers");
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,29 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Yavsc.Migrations
{
/// <inheritdoc />
public partial class DiscoveryDiskQuota : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTimeOffset>(
name: "DiscoveryDiskQuotaUntil",
table: "AspNetUsers",
type: "timestamp with time zone",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "DiscoveryDiskQuotaUntil",
table: "AspNetUsers");
}
}
}

View file

@ -1150,6 +1150,9 @@ namespace Yavsc.Migrations
.HasMaxLength(512)
.HasColumnType("character varying(512)");
b.Property<DateTimeOffset?>("DiscoveryDiskQuotaUntil")
.HasColumnType("timestamp with time zone");
b.Property<long>("DiskQuota")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
@ -1202,6 +1205,9 @@ namespace Yavsc.Migrations
b.Property<long?>("PostalAddressId")
.HasColumnType("bigint");
b.Property<DateTimeOffset?>("PremiumDiskQuotaUntil")
.HasColumnType("timestamp with time zone");
b.Property<string>("SecurityStamp")
.HasColumnType("text");
@ -2803,6 +2809,101 @@ namespace Yavsc.Migrations
b.ToTable("PerformerDonations");
});
modelBuilder.Entity("Yavsc.Models.Payment.ResourceSubscription", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<int>("LastCreditedCycle")
.HasColumnType("integer");
b.Property<decimal>("MonthlyPoints")
.HasPrecision(28, 8)
.HasColumnType("numeric(28,8)");
b.Property<DateTimeOffset?>("NextBillingAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("Pack")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)");
b.Property<string>("PayPalId")
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<string>("PlanId")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<decimal>("PriceExcludingTax")
.HasColumnType("numeric(18,2)");
b.Property<decimal>("PriceIncludingTax")
.HasColumnType("numeric(18,2)");
b.Property<DateTimeOffset?>("StartsAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("State")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)");
b.Property<string>("UserId")
.IsRequired()
.HasMaxLength(450)
.HasColumnType("character varying(450)");
b.Property<decimal>("VatPercent")
.HasColumnType("numeric");
b.HasKey("Id");
b.HasIndex("PayPalId")
.IsUnique();
b.HasIndex("UserId");
b.ToTable("ResourceSubscriptions");
});
modelBuilder.Entity("Yavsc.Models.Payment.ResourceSubscriptionPayment", b =>
{
b.Property<string>("Id")
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<int>("Cycle")
.HasColumnType("integer");
b.Property<decimal>("GrantedPoints")
.HasPrecision(28, 8)
.HasColumnType("numeric(28,8)");
b.Property<DateTimeOffset>("PaidAt")
.HasColumnType("timestamp with time zone");
b.Property<bool>("Reversed")
.HasColumnType("boolean");
b.Property<Guid>("SubscriptionId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("SubscriptionId", "Cycle")
.IsUnique();
b.ToTable("ResourceSubscriptionPayments");
});
modelBuilder.Entity("Yavsc.Models.Relationship.Circle", b =>
{
b.Property<long>("Id")
@ -2997,6 +3098,12 @@ namespace Yavsc.Migrations
b.Property<decimal>("CpuSeconds")
.HasColumnType("numeric");
b.Property<decimal>("CreditPoints")
.ValueGeneratedOnAdd()
.HasPrecision(28, 8)
.HasColumnType("numeric(28,8)")
.HasDefaultValue(0m);
b.Property<string>("Currency")
.IsRequired()
.ValueGeneratedOnAdd()
@ -4623,6 +4730,24 @@ namespace Yavsc.Migrations
.IsRequired();
});
modelBuilder.Entity("Yavsc.Models.Payment.ResourceSubscription", b =>
{
b.HasOne("Yavsc.Models.ApplicationUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
});
modelBuilder.Entity("Yavsc.Models.Payment.ResourceSubscriptionPayment", b =>
{
b.HasOne("Yavsc.Models.Payment.ResourceSubscription", null)
.WithMany()
.HasForeignKey("SubscriptionId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
});
modelBuilder.Entity("Yavsc.Models.Relationship.Circle", b =>
{
b.HasOne("Yavsc.Models.ApplicationUser", null)

View file

@ -0,0 +1,38 @@
@baseUrl = https://localhost:5001
@antiforgeryToken = replace-from-subscription-page
@sessionCookie = replace-with-web-session-and-antiforgery-cookies
### Subscription management remains accessible with an exhausted quota
GET {{baseUrl}}/ResourceSubscription
Cookie: {{sessionCookie}}
### Start the Discovery checkout (Premium uses pack=Premium)
POST {{baseUrl}}/ResourceSubscription/Create
Cookie: {{sessionCookie}}
Content-Type: application/x-www-form-urlencoded
pack=Discovery&__RequestVerificationToken={{antiforgeryToken}}
### Reconcile confirmed payments from PayPal, including missed webhooks
POST {{baseUrl}}/ResourceSubscription/Refresh
Cookie: {{sessionCookie}}
Content-Type: application/x-www-form-urlencoded
__RequestVerificationToken={{antiforgeryToken}}
### Cancel an owned subscription
POST {{baseUrl}}/ResourceSubscription/Cancel
Cookie: {{sessionCookie}}
Content-Type: application/x-www-form-urlencoded
id=replace-with-local-subscription-uuid&__RequestVerificationToken={{antiforgeryToken}}
### A browser return only reconciles server-verified payments
GET {{baseUrl}}/ResourceSubscription/Return?id=replace-with-local-subscription-uuid
Cookie: {{sessionCookie}}
### A forged notification must be rejected; real headers are supplied by PayPal
POST {{baseUrl}}/ResourceSubscription/Notify
Content-Type: application/json
{"event_type":"PAYMENT.SALE.COMPLETED","resource":{"billing_agreement_id":"I-EXAMPLE"}}

View file

@ -1,4 +1,5 @@
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
using Yavsc;
using Yavsc.Models;
using Yavsc.Services;
@ -40,7 +41,7 @@ public class DiskUsageTracker : IDiskUsageTracker
DUTInfo usage = new DUTInfo
{
Usage = user.DiskUsage,
Quota = user.DiskQuota
Quota = user.GetEffectiveDiskQuota(DateTimeOffset.UtcNow)
};
DiskUsage.Add(username, usage);
if (DiskUsage.Count > ulistLength)
@ -71,6 +72,8 @@ public class DiskUsageTracker : IDiskUsageTracker
public bool GetSpace(string userName, long space)
{
var info = GetInfo(userName);
var user = context.Users.AsNoTracking().Single(u => u.UserName == userName);
info.Quota = user.GetEffectiveDiskQuota(DateTimeOffset.UtcNow);
if (info.Quota < info.Usage + space) return false;
info.Usage += space;
#pragma warning disable CS4014

View file

@ -0,0 +1,249 @@
#nullable enable
using System.Globalization;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Options;
using Yavsc.Models.Payment;
using Yavsc.Settings;
namespace Yavsc.Web.Services;
public sealed class SubscriptionOperationException(int statusCode, string message) : Exception(message)
{
public int StatusCode { get; } = statusCode;
}
public sealed record PayPalSubscriptionDetails(string Id, string CustomId, string PlanId, string State,
DateTimeOffset? StartsAt, DateTimeOffset? NextBillingAt, string? ApprovalUrl);
public sealed record PayPalSubscriptionTransaction(string Id, string State, decimal Amount, string Currency, DateTimeOffset PaidAt);
public interface IPayPalSubscriptionGateway
{
Task<PayPalSubscriptionDetails> CreateAsync(ResourceSubscription subscription, string returnUrl, string cancelUrl, CancellationToken ct);
Task<PayPalSubscriptionDetails> GetAsync(string id, CancellationToken ct);
Task<IReadOnlyList<PayPalSubscriptionTransaction>> TransactionsAsync(string id, DateTimeOffset from, DateTimeOffset to, CancellationToken ct);
Task CancelAsync(string id, CancellationToken ct);
Task VerifyWebhookAsync(JsonElement notification, IReadOnlyDictionary<string, string> headers, CancellationToken ct);
}
public sealed class PayPalSubscriptionGateway(HttpClient http, IOptions<PayPalSettings> paypalOptions,
IOptions<ResourceSubscriptionOptions> subscriptionOptions) : IPayPalSubscriptionGateway
{
private static readonly Regex SubscriptionId = new("^I-[A-Z0-9]{1,62}$", RegexOptions.CultureInvariant);
private string Origin => paypalOptions.Value.Mode switch
{
"production" => "https://api-m.paypal.com",
"sandbox" => "https://api-m.sandbox.paypal.com",
_ => throw new SubscriptionOperationException(503, "Le paiement PayPal REST n'est pas configure.")
};
public async Task<PayPalSubscriptionDetails> CreateAsync(ResourceSubscription subscription,
string returnUrl, string cancelUrl, CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(subscriptionOptions.Value.WebhookId))
throw new SubscriptionOperationException(503, "Le webhook PayPal des abonnements n'est pas configure.");
await ValidatePlanAsync(subscription, ct);
using var result = await SendAsync(HttpMethod.Post, "/v1/billing/subscriptions", new
{
plan_id = subscription.PlanId,
custom_id = subscription.Id.ToString("N"),
application_context = new
{
brand_name = "Yavsc", user_action = "SUBSCRIBE_NOW", shipping_preference = "NO_SHIPPING",
return_url = returnUrl, cancel_url = cancelUrl
}
}, subscription.Id.ToString("N"), ct);
return ReadSubscription(result.RootElement);
}
private async Task ValidatePlanAsync(ResourceSubscription subscription, CancellationToken ct)
{
if (!Regex.IsMatch(subscription.PlanId, "^P-[A-Z0-9]{1,62}$", RegexOptions.CultureInvariant))
throw new SubscriptionOperationException(503, "Le plan PayPal du pack n'est pas configure.");
using var result = await SendAsync(HttpMethod.Get, "/v1/billing/plans/" + subscription.PlanId, null, null, ct);
var plan = Object(result.RootElement);
var cycles = Array(Property(plan, "billing_cycles")).ToArray();
if (Text(plan, "id") != subscription.PlanId || Text(plan, "status") != "ACTIVE" || cycles.Length != 1)
throw new SubscriptionOperationException(503, "Le plan PayPal doit etre actif, mensuel et sans periode d'essai.");
var cycle = cycles[0];
var frequency = Property(cycle, "frequency");
var price = Property(Property(cycle, "pricing_scheme"), "fixed_price");
var preferences = Object(Property(plan, "payment_preferences"));
if (Text(cycle, "tenure_type") != "REGULAR" || Integer(Property(cycle, "total_cycles")) != 0
|| Text(frequency, "interval_unit") != "MONTH" || Integer(Property(frequency, "interval_count")) != 1
|| Text(price, "currency_code") != "EUR" || Amount(Property(price, "value")) != subscription.PriceIncludingTax
|| Boolean(Property(preferences, "auto_bill_outstanding"))
|| (preferences.TryGetProperty("setup_fee", out var fee) && Amount(Property(fee, "value")) != 0m)
|| (plan.TryGetProperty("taxes", out var taxes) && Amount(Property(taxes, "percentage")) != 0m)
|| (plan.TryGetProperty("quantity_supported", out var quantity) && Boolean(quantity)))
throw new SubscriptionOperationException(503, "Le plan PayPal doit correspondre au tarif TTC du pack, sans frais supplementaires ni regroupement des impayes.");
}
public async Task<PayPalSubscriptionDetails> GetAsync(string id, CancellationToken ct)
{
CheckId(id);
using var result = await SendAsync(HttpMethod.Get, "/v1/billing/subscriptions/" + id, null, null, ct);
return ReadSubscription(result.RootElement);
}
public async Task<IReadOnlyList<PayPalSubscriptionTransaction>> TransactionsAsync(
string id, DateTimeOffset from, DateTimeOffset to, CancellationToken ct)
{
CheckId(id);
var all = new List<PayPalSubscriptionTransaction>();
// PayPal accepts bounded date ranges. Adjacent ranges can repeat an
// endpoint transaction; reconciliation deduplicates by PayPal ID.
while (from < to)
{
var end = from.AddDays(30) < to ? from.AddDays(30) : to;
string Date(DateTimeOffset value) => Uri.EscapeDataString(value.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture));
using var result = await SendAsync(HttpMethod.Get,
$"/v1/billing/subscriptions/{id}/transactions?start_time={Date(from)}&end_time={Date(end)}", null, null, ct);
foreach (var item in Array(Property(result.RootElement, "transactions")))
{
var amount = Property(Property(item, "amount_with_breakdown"), "gross_amount");
all.Add(new(Text(item, "id"), Text(item, "status"), Amount(Property(amount, "value")),
Text(amount, "currency_code"), ReadDate(Text(item, "time"))));
}
from = end;
}
return all;
}
public async Task CancelAsync(string id, CancellationToken ct)
{
CheckId(id);
using var result = await SendAsync(HttpMethod.Post, $"/v1/billing/subscriptions/{id}/cancel",
new { reason = "Resiliation demandee par l'abonne Yavsc" }, null, ct);
}
public async Task VerifyWebhookAsync(JsonElement notification, IReadOnlyDictionary<string, string> headers, CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(subscriptionOptions.Value.WebhookId))
throw new SubscriptionOperationException(503, "Le webhook PayPal des abonnements n'est pas configure.");
string Header(string name) => headers.TryGetValue(name, out var value) && !string.IsNullOrWhiteSpace(value)
? value : throw new SubscriptionOperationException(400, "Signature PayPal manquante.");
using var result = await SendAsync(HttpMethod.Post, "/v1/notifications/verify-webhook-signature", new
{
auth_algo = Header("PAYPAL-AUTH-ALGO"), cert_url = Header("PAYPAL-CERT-URL"),
transmission_id = Header("PAYPAL-TRANSMISSION-ID"), transmission_sig = Header("PAYPAL-TRANSMISSION-SIG"),
transmission_time = Header("PAYPAL-TRANSMISSION-TIME"),
webhook_id = subscriptionOptions.Value.WebhookId, webhook_event = notification
}, null, ct);
if (Text(result.RootElement, "verification_status") != "SUCCESS")
throw new SubscriptionOperationException(400, "Notification PayPal non verifiee.");
}
private async Task<JsonDocument> SendAsync(HttpMethod method, string path, object? payload, string? requestId, CancellationToken ct)
{
var settings = paypalOptions.Value;
if (string.IsNullOrWhiteSpace(settings.ClientId) || string.IsNullOrWhiteSpace(settings.ClientSecret))
throw new SubscriptionOperationException(503, "Les identifiants REST PayPal ne sont pas configures.");
try
{
using var tokenRequest = new HttpRequestMessage(HttpMethod.Post, Origin + "/v1/oauth2/token");
tokenRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes(settings.ClientId + ":" + settings.ClientSecret)));
tokenRequest.Content = new FormUrlEncodedContent(new Dictionary<string, string> { ["grant_type"] = "client_credentials" });
using var tokenResponse = await http.SendAsync(tokenRequest, ct);
if (!tokenResponse.IsSuccessStatusCode)
throw new SubscriptionOperationException(502, "L'authentification PayPal a echoue.");
using var token = JsonDocument.Parse(await tokenResponse.Content.ReadAsStringAsync(ct));
using var request = new HttpRequestMessage(method, Origin + path);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Text(token.RootElement, "access_token"));
if (requestId is not null) request.Headers.Add("PayPal-Request-Id", requestId);
if (payload is not null) request.Content = JsonContent.Create(payload);
using var response = await http.SendAsync(request, ct);
if (!response.IsSuccessStatusCode)
throw new SubscriptionOperationException(502, "PayPal a refuse l'operation. Verifiez l'etat de l'abonnement avant de recommencer.");
return response.StatusCode == System.Net.HttpStatusCode.NoContent
? JsonDocument.Parse("{}") : JsonDocument.Parse(await response.Content.ReadAsStringAsync(ct));
}
catch (HttpRequestException)
{
throw new SubscriptionOperationException(502, "PayPal est indisponible. Aucun paiement n'est presume reussi.");
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
{
throw new SubscriptionOperationException(504, "PayPal n'a pas repondu a temps. Verifiez l'abonnement avant de recommencer.");
}
catch (JsonException)
{
throw new SubscriptionOperationException(502, "PayPal a retourne une reponse invalide.");
}
}
private static PayPalSubscriptionDetails ReadSubscription(JsonElement value)
{
value = Object(value);
var id = Text(value, "id");
CheckId(id);
if (value.TryGetProperty("plan_overridden", out var overridden) && Boolean(overridden))
throw new SubscriptionOperationException(409, "Le plan de cet abonnement a ete modifie.");
if ((value.TryGetProperty("quantity", out var quantity) && Amount(quantity) != 1m)
|| (value.TryGetProperty("shipping_amount", out var shipping) && Amount(Property(shipping, "value")) != 0m))
throw new SubscriptionOperationException(409, "L'abonnement comporte une quantite ou des frais non autorises.");
string? approval = null;
if (value.TryGetProperty("links", out var links))
{
var approvals = Array(links).Where(link => Text(link, "rel") == "approve")
.Select(link => Text(link, "href")).ToArray();
if (approvals.Length > 1)
throw new SubscriptionOperationException(502, "Plusieurs liens d'approbation PayPal ont ete retournes.");
approval = approvals.SingleOrDefault();
}
if (approval is not null && (!Uri.TryCreate(approval, UriKind.Absolute, out var uri)
|| uri.Scheme != "https" || !string.IsNullOrEmpty(uri.UserInfo)
|| uri.Host is not ("www.paypal.com" or "www.sandbox.paypal.com")))
throw new SubscriptionOperationException(502, "Lien d'approbation PayPal invalide.");
DateTimeOffset? Date(JsonElement element, string key) => element.TryGetProperty(key, out _)
? ReadDate(Text(element, key)) : null;
var next = value.TryGetProperty("billing_info", out var info) ? Date(Object(info), "next_billing_time") : null;
return new(id, Text(value, "custom_id"), Text(value, "plan_id"), Text(value, "status"),
Date(value, "start_time"), next, approval);
}
private static void CheckId(string id)
{
if (!SubscriptionId.IsMatch(id)) throw new SubscriptionOperationException(400, "Reference d'abonnement PayPal invalide.");
}
private static JsonElement Property(JsonElement value, string property)
=> value.ValueKind == JsonValueKind.Object && value.TryGetProperty(property, out var found)
? found : throw new SubscriptionOperationException(502, "Reponse PayPal incomplete.");
private static JsonElement Object(JsonElement value)
=> value.ValueKind == JsonValueKind.Object
? value : throw new SubscriptionOperationException(502, "Objet PayPal invalide.");
private static JsonElement.ArrayEnumerator Array(JsonElement value)
=> value.ValueKind == JsonValueKind.Array
? value.EnumerateArray() : throw new SubscriptionOperationException(502, "Liste PayPal invalide.");
private static int Integer(JsonElement value)
=> value.ValueKind == JsonValueKind.Number && value.TryGetInt32(out var number)
? number : throw new SubscriptionOperationException(502, "Entier PayPal invalide.");
private static bool Boolean(JsonElement value)
=> value.ValueKind is JsonValueKind.True or JsonValueKind.False
? value.GetBoolean() : throw new SubscriptionOperationException(502, "Booleen PayPal invalide.");
private static string Text(JsonElement value, string property)
{
var found = Property(value, property);
return found.ValueKind == JsonValueKind.String && !string.IsNullOrWhiteSpace(found.GetString())
? found.GetString()! : throw new SubscriptionOperationException(502, "Reponse PayPal invalide.");
}
private static decimal Amount(JsonElement value)
=> value.ValueKind == JsonValueKind.String
&& decimal.TryParse(value.GetString(), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var amount)
? amount : throw new SubscriptionOperationException(502, "Montant PayPal invalide.");
private static DateTimeOffset ReadDate(string value)
=> DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var date)
? date.ToUniversalTime() : throw new SubscriptionOperationException(502, "Date PayPal invalide.");
}

View file

@ -0,0 +1,177 @@
#nullable enable
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Yavsc.Models;
using Yavsc.Models.Payment;
using Yavsc.Server.Helpers;
using Yavsc.Settings;
namespace Yavsc.Web.Services;
public interface IResourceSubscriptionService
{
Task<List<ResourceSubscription>> GetAllAsync(string userId, CancellationToken ct);
Task<ResourceSubscription?> FindPayPalAsync(string paypalId, CancellationToken ct);
Task<ResourceSubscription?> FindPaymentAsync(string transactionId, CancellationToken ct);
Task<ResourceSubscription> PrepareAsync(string userId, string pack, CancellationToken ct);
Task BindAsync(ResourceSubscription subscription, PayPalSubscriptionDetails remote, CancellationToken ct);
Task ReconcileAsync(ResourceSubscription subscription, CancellationToken ct);
}
public sealed class ResourceSubscriptionService(ApplicationDbContext db, IPayPalSubscriptionGateway paypal,
IOptions<ResourceSubscriptionOptions> options, IOptions<ResourceUsageOptions> usageOptions,
TimeProvider clock) : IResourceSubscriptionService
{
public Task<List<ResourceSubscription>> GetAllAsync(string userId, CancellationToken ct)
=> db.ResourceSubscriptions.Where(s => s.UserId == userId).OrderByDescending(s => s.CreatedAt).ToListAsync(ct);
public Task<ResourceSubscription?> FindPayPalAsync(string paypalId, CancellationToken ct)
=> db.ResourceSubscriptions.SingleOrDefaultAsync(s => s.PayPalId == paypalId, ct);
public Task<ResourceSubscription?> FindPaymentAsync(string transactionId, CancellationToken ct)
=> db.ResourceSubscriptions.Join(db.ResourceSubscriptionPayments.Where(p => p.Id == transactionId),
s => s.Id, p => p.SubscriptionId, (s, p) => s).SingleOrDefaultAsync(ct);
public Task<ResourceSubscription> PrepareAsync(string userId, string pack, CancellationToken ct)
{
if (pack is not ("Discovery" or "Premium"))
throw new SubscriptionOperationException(400, "Pack inconnu.");
return ResourceUsageTracker.WithUserLockAsync(db, userId, async () =>
{
var existing = await db.ResourceSubscriptions.Where(s => s.UserId == userId
&& s.State != "CANCELLED" && s.State != "EXPIRED").SingleOrDefaultAsync(ct);
if (existing is not null)
{
if (existing.Pack != pack)
throw new SubscriptionOperationException(409, "Resiliez l'abonnement existant avant de changer de pack.");
if (existing.State == "CREATING" && existing.CreatedAt < clock.GetUtcNow().AddHours(-24))
throw new SubscriptionOperationException(409, "Une creation PayPal ancienne doit etre rapprochee par l'administration avant tout nouvel essai.");
return existing;
}
var settings = options.Value;
var plan = pack == "Premium" ? settings.PremiumPlanId : settings.DiscoveryPlanId;
if (string.IsNullOrWhiteSpace(plan))
throw new SubscriptionOperationException(503, "Ce pack PayPal n'est pas configure.");
var price = pack == "Premium" ? settings.PremiumPriceExcludingTax : settings.DiscoveryPriceExcludingTax;
var subscription = new ResourceSubscription
{
Id = Guid.NewGuid(), UserId = userId, Pack = pack, PlanId = plan,
CreatedAt = DateTimeOffset.FromUnixTimeSeconds(clock.GetUtcNow().ToUnixTimeSeconds()),
MonthlyPoints = usageOptions.Value.DefaultLimit * (pack == "Premium" ? 3m : 1m),
PriceExcludingTax = price, VatPercent = settings.VatPercent, PriceIncludingTax = settings.TotalPrice(price)
};
db.ResourceSubscriptions.Add(subscription);
await db.SaveChangesAsync(ct);
return subscription;
}, ct);
}
public Task BindAsync(ResourceSubscription subscription, PayPalSubscriptionDetails remote, CancellationToken ct)
=> ResourceUsageTracker.WithUserLockAsync(db, subscription.UserId, async () =>
{
await db.Entry(subscription).ReloadAsync(ct);
CheckBinding(subscription, remote);
subscription.PayPalId = remote.Id;
subscription.State = remote.State;
subscription.StartsAt = remote.StartsAt;
subscription.NextBillingAt = remote.NextBillingAt;
await db.SaveChangesAsync(ct);
return true;
}, ct);
public async Task ReconcileAsync(ResourceSubscription subscription, CancellationToken ct)
{
if (subscription.PayPalId is null) return;
var remote = await paypal.GetAsync(subscription.PayPalId, ct);
CheckBinding(subscription, remote);
var transactions = await paypal.TransactionsAsync(remote.Id, subscription.CreatedAt, clock.GetUtcNow(), ct);
await ResourceUsageTracker.WithUserLockAsync(db, subscription.UserId, async () =>
{
await db.Entry(subscription).ReloadAsync(ct);
CheckBinding(subscription, remote);
subscription.State = remote.State;
subscription.StartsAt = remote.StartsAt;
subscription.NextBillingAt = remote.NextBillingAt;
var record = await db.ResourceUsageRecords.Where(r => r.UserId == subscription.UserId)
.OrderByDescending(r => r.RecordedAtUtc).ThenByDescending(r => r.Id).FirstOrDefaultAsync(ct);
if (record is not null) await db.Entry(record).ReloadAsync(ct);
foreach (var payment in transactions.DistinctBy(p => p.Id).OrderBy(p => p.PaidAt).ThenBy(p => p.Id))
{
if (payment.State is not ("COMPLETED" or "REFUNDED" or "PARTIALLY_REFUNDED" or "FAILED" or "DECLINED")) continue;
if (payment.Currency != "EUR" || payment.Amount != subscription.PriceIncludingTax
|| payment.PaidAt > clock.GetUtcNow() || payment.PaidAt < subscription.CreatedAt)
throw new SubscriptionOperationException(409, "La mensualite PayPal ne correspond pas au tarif de cet abonnement.");
if (remote.StartsAt is null)
throw new SubscriptionOperationException(409, "La date de debut PayPal est manquante.");
var anchor = new DateTimeOffset(remote.StartsAt.Value.UtcDateTime.Date, TimeSpan.Zero);
if (payment.PaidAt < anchor)
throw new SubscriptionOperationException(409, "Le paiement precede le debut de l'abonnement.");
var cycle = ResourceUsageCredit.BillingCycle(anchor, payment.PaidAt);
var receipt = await db.ResourceSubscriptionPayments.FindAsync([payment.Id], ct);
if (receipt is not null)
{
if (receipt.SubscriptionId != subscription.Id)
throw new SubscriptionOperationException(409, "La transaction PayPal est deja affectee a un autre abonnement.");
if (payment.State != "COMPLETED" && !receipt.Reversed)
{
if (record is null || record.CreditPoints < receipt.GrantedPoints)
throw new SubscriptionOperationException(409, "Le solde des credits ne permet pas le rapprochement du remboursement.");
record.CreditPoints -= receipt.GrantedPoints;
receipt.Reversed = true;
}
continue;
}
if (payment.State is "FAILED" or "DECLINED") continue;
if (await db.ResourceSubscriptionPayments.AnyAsync(p => p.SubscriptionId == subscription.Id && p.Cycle == cycle, ct))
throw new SubscriptionOperationException(409, "Plusieurs paiements PayPal correspondent a la meme mensualite. Contactez l'administration.");
if (record is null)
{
var name = await db.Users.Where(u => u.Id == subscription.UserId).Select(u => u.UserName).SingleAsync(ct);
record = new ResourceUsageRecord
{
UserId = subscription.UserId, UserName = name ?? subscription.UserId,
UserCreated = subscription.UserId, UserModified = subscription.UserId
};
db.ResourceUsageRecords.Add(record);
}
var gross = usageOptions.Value.Calculate(record.ApiCalls, record.CpuSeconds, record.BandwidthMb, record.StorageMb);
var grant = payment.State == "COMPLETED"
? ResourceUsageCredit.MonthlyGrant(gross, record.CreditPoints, subscription.MonthlyPoints) : 0m;
record.CreditPoints += grant;
db.ResourceSubscriptionPayments.Add(new ResourceSubscriptionPayment
{
Id = payment.Id, SubscriptionId = subscription.Id, Cycle = cycle, PaidAt = payment.PaidAt,
GrantedPoints = grant, Reversed = payment.State != "COMPLETED"
});
subscription.LastCreditedCycle = Math.Max(subscription.LastCreditedCycle, cycle);
// Make subsequent transactions in this reconciliation see the receipt.
await db.SaveChangesAsync(subscription.UserId, ct);
}
await db.SaveChangesAsync(subscription.UserId, ct);
var owner = await db.Users.SingleAsync(u => u.Id == subscription.UserId, ct);
await db.Entry(owner).ReloadAsync(ct);
var paidPeriods = await db.ResourceSubscriptions.Where(s => s.UserId == subscription.UserId
&& (s.Pack == "Premium" || s.Pack == "Discovery") && s.StartsAt != null)
.Join(db.ResourceSubscriptionPayments.Where(p => !p.Reversed), s => s.Id, p => p.SubscriptionId,
(s, p) => new { s.Pack, s.StartsAt, p.Cycle }).ToListAsync(ct);
owner.PremiumDiskQuotaUntil = paidPeriods.Where(p => p.Pack == "Premium")
.Select(p => p.StartsAt!.Value.AddMonths(p.Cycle + 1))
.Cast<DateTimeOffset?>().Max();
owner.DiscoveryDiskQuotaUntil = paidPeriods.Where(p => p.Pack == "Discovery")
.Select(p => p.StartsAt!.Value.AddMonths(p.Cycle + 1))
.Cast<DateTimeOffset?>().Max();
await db.SaveChangesAsync(subscription.UserId, ct);
return true;
}, ct);
}
private static void CheckBinding(ResourceSubscription subscription, PayPalSubscriptionDetails remote)
{
if (remote.CustomId != subscription.Id.ToString("N") || remote.PlanId != subscription.PlanId
|| (subscription.PayPalId is not null && subscription.PayPalId != remote.Id)
|| (subscription.StartsAt is not null && subscription.StartsAt != remote.StartsAt)
|| (subscription.State is "CANCELLED" or "EXPIRED" && remote.State is not ("CANCELLED" or "EXPIRED"))
|| remote.State is not ("APPROVAL_PENDING" or "APPROVED" or "ACTIVE" or "SUSPENDED" or "CANCELLED" or "EXPIRED"))
throw new SubscriptionOperationException(409, "L'abonnement PayPal ne correspond pas au compte et au pack attendus.");
}
}

View file

@ -0,0 +1,9 @@
#nullable enable
using Yavsc.Abstract.Resources;
using Yavsc.Models.Payment;
using Yavsc.Settings;
namespace Yavsc.ViewModels;
public sealed record ResourceSubscriptionPage(ResourceSubscriptionOptions Prices, decimal FreeQuota,
ResourceUsageSummary Usage, IReadOnlyList<ResourceSubscription> Subscriptions);

View file

@ -0,0 +1,138 @@
@using Yavsc.Settings
@using Yavsc.Server.Helpers
@inject Microsoft.Extensions.Options.IOptions<ResourceUsageOptions> UsageOptions
@inject Microsoft.Extensions.Options.IOptions<ResourceSubscriptionOptions> SubscriptionOptions
@{
ViewBag.Title = "CGU de PostIt et de Yavsc";
var rule = UsageOptions.Value;
var packs = SubscriptionOptions.Value;
var userId = User.GetUserId();
var limit = string.IsNullOrWhiteSpace(userId) ? rule.DefaultLimit : rule.GetLimit(userId);
}
<h1>Conditions d'utilisation de PostIt et de Yavsc : consommation des ressources</h1>
<p>
PostIt est un client de Yavsc. Les opérations authentifiées sur les services API et Blogs
consomment des ressources communes, comptabilisées par identifiant de compte,
quel que soit le client utilisé.
</p>
<h2>Calcul de l'indicateur</h2>
<p>L'indicateur est une somme pondérée, exprimée en points :</p>
<p>
<strong>
I = @rule.ApiCallWeight × appels API
+ @rule.CpuSecondWeight × secondes CPU enregistrées
+ @rule.BandwidthMbWeight × Mio transférés
+ @rule.StorageMbWeight × Mio ajoutés au stockage
</strong>
</p>
<ul>
<li>Un Mio vaut 1 048 576 octets ; les anciens champs de l'API portent le suffixe « Mb ».</li>
<li>Les compteurs sont cumulés depuis le début de leur comptabilisation, sans remise à zéro mensuelle.</li>
<li>
Les transferts comprennent le corps entrant déclaré ou lu et les octets de réponse écrits,
y compris les réponses diffusées sans taille annoncée. Les en-têtes et le surcoût du transport ne sont pas inclus.
</li>
<li>
Le stockage est le volume cumulé ajouté par les opérations instrumentées (fichiers personnels et génération de PDF),
pas l'espace disque actuellement occupé : suppression et remplacement ne remboursent pas de points.
</li>
<li>
Le compteur historique « CPU » contient les durées enregistrées par les traitements instrumentés,
notamment le temps écoulé de génération de PDF ; ce n'est pas une mesure exhaustive du processeur.
Les ressources locales de PostIt, les appels directs entre appareils et le trafic WebSocket ne sont pas mesurés par cet indicateur HTTP.
</li>
</ul>
<p>
Le calcul utilise les valeurs décimales non arrondies ; l'arrondi de l'affichage ne décide pas du blocage.
Cet indicateur n'est ni un prix, ni une facture, ni une estimation de compensation financière.
</p>
<h2>Inscription gratuite et abonnements mensuels</h2>
<p>
L'inscription gratuite donne accès au quota décrit ci-dessous. Elle ne renouvelle pas automatiquement
ce quota chaque mois. Les compteurs historiques ne sont jamais supprimés par un abonnement :
les crédits sont déduits de l'indicateur brut pour obtenir la consommation nette soumise au seuil.
</p>
<ul>
<li>
<strong>Découverte</strong> : @packs.DiscoveryPriceExcludingTax.ToString("F2") € HT par mois,
TVA @packs.VatPercent %, soit @packs.TotalPrice(packs.DiscoveryPriceExcludingTax).ToString("F2") € TTC prélevés ;
chaque mensualité payée permet d'effacer jusqu'à @rule.DefaultLimit points.
</li>
<li>
<strong>Premium</strong> : @packs.PremiumPriceExcludingTax.ToString("F2") € HT par mois,
TVA @packs.VatPercent %, soit @packs.TotalPrice(packs.PremiumPriceExcludingTax).ToString("F2") € TTC prélevés ;
chaque mensualité payée permet d'effacer jusqu'à @(rule.DefaultLimit * 3m) points.
</li>
</ul>
<p>
Le prix et le volume mensuel sont fixés à la souscription, sur la base du quota gratuit par défaut
(pas d'une éventuelle exception individuelle). Le premier crédit est accordé dès le premier paiement
confirmé par PayPal, puis à chaque mensualité payée, à la date anniversaire UTC du début de l'abonnement.
Pour une date de fin de mois absente du mois suivant, l'anniversaire est le dernier jour de ce mois.
Il n'y a pas de remise à zéro calendaire ou de crédit accordé sur la seule approbation dans le navigateur.
</p>
<p>
Le crédit reporté existant est utilisé en premier. Le nouveau crédit mensuel efface ensuite
la consommation nette restante, sans la rendre négative ; les <strong>deux tiers du crédit mensuel
inutilisé</strong> sont conservés. Le tiers restant est perdu. Les reports antérieurs ne subissent
pas une nouvelle réduction, n'expirent pas et restent acquis après résiliation.
Le report est arrondi vers zéro à huit décimales de point.
Exemple : avec @(rule.DefaultLimit * 0.4m) points de consommation et un crédit Découverte de @rule.DefaultLimit points,
@(rule.DefaultLimit * 0.4m) points sont effacés et @(ResourceUsageCredit.MonthlyGrant(rule.DefaultLimit * 0.4m, 0m, rule.DefaultLimit) - rule.DefaultLimit * 0.4m) points sont reportés.
</p>
<p>
Les mensualités en attente ou impayées ne donnent aucun crédit. Les paiements sont rapprochés côté serveur
avec PayPal, de manière idempotente ; une notification répétée ne donne pas de crédit supplémentaire.
Si une notification est retardée, le crédit prend effet lors de son rapprochement sur la consommation
alors enregistrée. Un remboursement, même partiel, retire le crédit de la mensualité concernée.
La résiliation arrête les futurs prélèvements, sans effacer les crédits des paiements non remboursés.
Un seul abonnement non résilié est autorisé par compte ; un changement de pack nécessite sa résiliation préalable.
</p>
<p>
<a asp-controller="ResourceSubscription" asp-action="Index">Souscrire, consulter ou résilier un abonnement</a>.
Cette page et la consultation du quota restent accessibles lorsque le seuil est atteint.
</p>
<p>
L'inscription gratuite dispose du quota disque standard (@Constants.DefaultFSQ octets).
Découverte porte le quota disque à deux fois ce quota (@(Constants.DefaultFSQ * 2L) octets),
et Premium à cinq fois ce quota (@(Constants.DefaultFSQ * 5L) octets),
pendant la mensualité payée, même après résiliation jusqu'à son échéance UTC.
À l'échéance sans renouvellement payé, ou après remboursement de la mensualité, le quota habituel reprend.
Aucun fichier existant n'est supprimé ; si l'espace occupé dépasse le quota redevenu applicable,
de nouveaux téléversements sont refusés. La taille maximale autorisée d'un fichier n'augmente pas.
</p>
<h2>Seuil et contrôle des opérations</h2>
<p>
Le seuil par défaut de cette instance est de <strong>@rule.DefaultLimit points</strong>.
Un seuil individuel peut être fixé par l'administration.
@if (!string.IsNullOrWhiteSpace(userId))
{
<span>Le seuil actuellement applicable à votre compte est de <strong>@limit points</strong>.</span>
}
</p>
<p>
Une opération est refusée si le quota est déjà atteint ou si son coût connu dépasse le solde disponible.
L'appel API et le corps entrant de taille connue sont comptabilisés avant son exécution.
Pour un téléversement de fichiers personnels, le volume de fichiers est aussi contrôlé avant l'écriture.
Le coût restant, inconnu à l'avance, est constaté après l'opération.
Une opération en cours peut donc porter l'indicateur au-delà du seuil, notamment en cas d'opérations simultanées ;
les suivantes sont alors refusées. Il ne s'agit pas d'une interruption stricte à chaque octet ou seconde consommée.
</p>
<p>
Un refus est signalé par le code HTTP <strong>429</strong> et le motif <code>resource_quota_exceeded</code>.
Les opérations exécutées, même en échec, peuvent consommer des points ; une tentative refusée par le contrôle
d'admission n'est pas comptabilisée. La consultation de la consommation et des présentes CGU,
ainsi que la connexion et le renouvellement de session, restent accessibles.
Le compte n'est pas suspendu : les opérations redeviennent possibles dès que le seuil configuré autorise leur coût.
</p>
<p>
Dans PostIt, consultez « Mon profil » pour connaître votre indicateur et votre solde.
En cas de quota insuffisant, contactez l'administration via
<a asp-controller="Home" asp-action="Contact">la page de contact</a>.
Les coefficients et seuils sont configurés par l'exploitant ; toute modification des coefficients
recalcule l'indicateur à partir des compteurs existants.
</p>
<p><a asp-controller="Home" asp-action="Privacy">Protection des données</a></p>

View file

@ -0,0 +1,64 @@
@model Yavsc.ViewModels.ResourceSubscriptionPage
@{
ViewBag.Title = "Abonnements de consommation";
}
<h1>Abonnements Découverte et Premium</h1>
<p>L'inscription gratuite donne accès à @Model.FreeQuota points, sans renouvellement mensuel automatique.</p>
<p>
Consommation brute : @Model.Usage.GrossConsumption points.
Après crédits : <strong>@Model.Usage.Consumption points</strong>.
Crédit reporté disponible : <strong>@Model.Usage.CreditBalance points</strong>.
Solde utilisable avec le quota gratuit : @Model.Usage.RemainingConsumption points.
Quota disque actuel : @Model.Usage.DiskQuota octets.
</p>
<p>
Chaque mensualité PayPal confirmée efface la consommation disponible, dans la limite du pack.
Les deux tiers du crédit mensuel inutilisé sont conservés sans expiration, même après résiliation.
Le crédit déjà reporté est utilisé avant le nouveau crédit. Aucune mensualité impayée ne donne de crédit.
</p>
<div class="row">
@foreach (var pack in new[] { "Discovery", "Premium" })
{
var premium = pack == "Premium";
var name = premium ? "Premium" : "Découverte";
var price = premium ? Model.Prices.PremiumPriceExcludingTax : Model.Prices.DiscoveryPriceExcludingTax;
<section class="col-md-6">
<h2>@name</h2>
<p>@(Model.FreeQuota * (premium ? 3 : 1)) points par mensualité payée.</p>
<p>Quota disque : @(premium ? "cinq" : "deux") fois le quota gratuit standard
(@(Constants.DefaultFSQ * (premium ? 5L : 2L)) octets) pendant la mensualité payée,
même après résiliation jusqu'à son échéance. Aucun fichier supprimé au retour au quota habituel.</p>
<p>
@price.ToString("F2") € HT par mois + TVA @Model.Prices.VatPercent %,
soit <strong>@Model.Prices.TotalPrice(price).ToString("F2") € TTC prélevés par mois</strong>.
</p>
<form asp-action="Create" method="post">
<input type="hidden" name="pack" value="@pack" />
<button type="submit" class="btn btn-primary">Souscrire / reprendre @name avec PayPal</button>
</form>
</section>
}
</div>
<p>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.</p>
<p>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.</p>
<h2>Mes abonnements</h2>
@foreach (var subscription in Model.Subscriptions)
{
<section class="border rounded p-3 mb-3">
<h3>@(subscription.Pack == "Premium" ? "Premium" : "Découverte")</h3>
<p>État PayPal : @subscription.State. Référence : @subscription.PayPalId</p>
<p>Tarif souscrit : @subscription.PriceExcludingTax.ToString("F2") € HT, @subscription.PriceIncludingTax.ToString("F2") € TTC. Crédit mensuel : @subscription.MonthlyPoints points.</p>
<p>Début UTC : @subscription.StartsAt?.ToUniversalTime().ToString("u"). Prochain prélèvement UTC : @subscription.NextBillingAt?.ToUniversalTime().ToString("u").</p>
@if (subscription.PayPalId is not null && subscription.State is not ("CANCELLED" or "EXPIRED"))
{
<form asp-action="Cancel" method="post">
<input type="hidden" name="id" value="@subscription.Id" />
<button type="submit" class="btn btn-outline-danger">Résilier cet abonnement PayPal</button>
</form>
}
</section>
}
<form asp-action="Refresh" method="post">
<button type="submit" class="btn btn-secondary">Actualiser les paiements et les crédits depuis PayPal</button>
</form>
<p><a asp-controller="Home" asp-action="CGU">CGU de PostIt et de Yavsc</a></p>

View file

@ -42,6 +42,8 @@
<footer class="border-top footer text-muted">
<div class="container">
&copy; WTFPL 2025 - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
- <a asp-area="" asp-controller="Home" asp-action="CGU">CGU de PostIt et de Yavsc</a>
- <a asp-area="" asp-controller="ResourceSubscription" asp-action="Index">Abonnements</a>
</div>
</footer>

View file

@ -1,4 +1,15 @@
{
"ResourceSubscriptions": {
"DiscoveryPlanId": "",
"PremiumPlanId": "",
"WebhookId": "",
"DiscoveryPriceExcludingTax": 1,
"PremiumPriceExcludingTax": 2,
"VatPercent": 20
},
"ResourceUsage": {
"UserLimits": {}
},
"Logging": {
"LogLevel": {
"Default": "Information",

View file

@ -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;
}

View file

@ -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<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
public override async ValueTask<int> ReadAsync(Memory<byte> 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<byte> buffer, CancellationToken cancellationToken = default)
{
await inner.WriteAsync(buffer, cancellationToken);
Interlocked.Add(ref _bytesWritten, buffer.Length);
}
}

View file

@ -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;
}
}

View file

@ -0,0 +1,4 @@
namespace Yavsc.Server.Helpers;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class ResourceUsageExemptAttribute : Attribute;

View file

@ -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<ResourceUsageSummary> GetCurrentAsync(string userId, string userName, CancellationToken cancellationToken);
Task<List<ResourceUsageSummary>> GetOverviewAsync(CancellationToken cancellationToken);
Task<bool> TryAdmitAsync(string userId, string userName, decimal apiCalls, decimal bandwidthMb,
long anticipatedStorageBytes, CancellationToken cancellationToken);
}
public sealed class ResourceUsageService(
ApplicationDbContext dbContext,
IOptions<ResourceUsageOptions> options,
TimeProvider? clock = null) : IResourceUsageService
{
private readonly ResourceUsageOptions _options = options.Value;
public async Task<ResourceUsageSummary> 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<List<ResourceUsageSummary>> 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<bool> 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<ResourceUsageOptions>()
.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<IResourceUsageService, ResourceUsageService>();
}
}

View file

@ -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<ResourceUsageTelemetryMiddleware> 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<ApplicationDbContext>();
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<IOptions<SiteSettings>>().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<ResourceUsageExemptAttribute>() is not null
|| context.Request.Path.Equals("/login", StringComparison.OrdinalIgnoreCase)
|| context.Request.Path.Equals("/refresh", StringComparison.OrdinalIgnoreCase)
|| context.Request.Path.Equals("/logout", StringComparison.OrdinalIgnoreCase);
}

View file

@ -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<ResourceUsageRecord?> 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<T> WithUserLockAsync<T>(ApplicationDbContext dbContext, string userId,
Func<Task<T>> 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);
}
}

View file

@ -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<ResourceSubscription>(entity =>
{
entity.HasIndex(s => s.PayPalId).IsUnique();
entity.HasIndex(s => s.UserId);
entity.HasOne<ApplicationUser>().WithMany().HasForeignKey(s => s.UserId).OnDelete(DeleteBehavior.Restrict);
entity.Property(s => s.MonthlyPoints).HasPrecision(28, 8);
});
builder.Entity<ResourceSubscriptionPayment>(entity =>
{
entity.HasIndex(p => new { p.SubscriptionId, p.Cycle }).IsUnique();
entity.HasOne<ResourceSubscription>().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<Estimate> Estimates { get; set; }
public DbSet<Signature> Signatures { get; set; }
public DbSet<ResourceUsageRecord> ResourceUsageRecords { get; set; }
public DbSet<ResourceSubscription> ResourceSubscriptions { get; set; }
public DbSet<ResourceSubscriptionPayment> ResourceSubscriptionPayments { get; set; }
public DbSet<AccountBalance> BankStatus { get; set; }
public DbSet<BalanceImpact> BalanceImpact { get; set; }

View file

@ -93,7 +93,16 @@ namespace Yavsc.Models
public virtual List<BankIdentity>? 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;

View file

@ -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; }
}

View file

@ -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]

View file

@ -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<byte>(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;

View file

@ -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);
}

View file

@ -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<string, decimal> 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;
}

View file

@ -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<PodcastOptions>(options => options.RootPath = podcastRoot);
builder.Services.Configure<GoogleAuthSettings>(_ => { });
builder.Services.Configure<SiteSettings>(settings => settings.Authority = "example.test");
builder.Services.Configure<SiteSettings>(settings => settings.Authority = "https://example.test");
builder.Services.AddTransient<IBillingService, BillingService>();
builder.Services.AddInvoicing(builder.Configuration);
builder.Services.AddSingleton<RecordingMessageDelivery>();
@ -130,6 +132,7 @@ public sealed class ApiWebServerFixture : WebHostFixture
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseMiddleware<ResourceUsageTelemetryMiddleware>();
app.MapControllers();
app.MapOpenApi().RequireAuthorization("AdministratorOnly");

View file

@ -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<IHttpWebSocketFeature>(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;

View file

@ -40,6 +40,36 @@ public sealed class ResourceUsageApiControllerTests : IClassFixture<ApiWebServer
return http;
}
[Theory]
[InlineData(true, 5)]
[InlineData(false, 2)]
public async Task Profile_reports_effective_paid_quota_then_base_quota_after_expiry(bool premium, long multiplier)
{
var ct = TestContext.Current.CancellationToken;
var userId = premium ? "premium-disk-profile" : "discovery-disk-profile";
using var scope = _fixture.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<Yavsc.Models.ApplicationDbContext>();
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<Yavsc.Abstract.Identity.Me>("/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<Yavsc.Abstract.Identity.Me>("/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<ApiWebServer
}
[Fact]
public async Task GetCurrentUserUsage_records_telemetry_values_from_authenticated_requests()
public async Task GetCurrentUserUsage_does_not_consume_quota()
{
using var http = NewClient(_fixture, "telemetry-user");
using var http = NewClient(_fixture, "quota-consultation-user");
var response = await http.GetAsync("/api/v1/resource-usage/me", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@ -110,10 +140,35 @@ public sealed class ResourceUsageApiControllerTests : IClassFixture<ApiWebServer
using var scope = _fixture.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<Yavsc.Models.ApplicationDbContext>();
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<Yavsc.Models.ApplicationDbContext>();
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<ResourceUsageSummary>(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]

View file

@ -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<BlogsWebServerFixture>
"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<ApplicationDbContext>(),
"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<ApplicationDbContext>();
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);
}
/// <summary>
/// Regression for the "Mes fichiers" page listing nothing: the
/// personal-storage tree is keyed by the user's <b>login</b>

View file

@ -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()
{

View file

@ -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<SiteSettings>(s =>
{
s.UserFilesRoot = BlogFilesRoot;
s.Authority = "https://example.test";
});
// Real BlogSpotService — same instance the production host

View file

@ -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<ResourceSubscriptionControllerTests.Factory>
{
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<ApplicationDbContext>();
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<string, string> { ["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<ApplicationDbContext>(),
"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<ApplicationDbContext>(),
"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<ApplicationDbContext>().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<ApplicationDbContext>().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<ResourceSubscription> 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<ApplicationDbContext>().ResourceSubscriptions.AsNoTracking().SingleAsync(Ct);
}
private static async Task<HttpResponseMessage> PostAsync(HttpClient client, string action, Dictionary<string, string> 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<Yavsc.Abstract.Resources.ResourceUsageSummary> UsageAsync()
{
using var scope = _factory.Services.CreateScope();
return await scope.ServiceProvider.GetRequiredService<IResourceUsageService>().GetCurrentAsync("listener", "Listener", Ct);
}
private async Task<HttpResponseMessage> 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<IPayPalSubscriptionGateway>(PayPal);
services.Configure<ResourceSubscriptionOptions>(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<PayPalSubscriptionTransaction> Payments { get; } = [];
private PayPalSubscriptionDetails _remote = null!;
public Task<PayPalSubscriptionDetails> 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<PayPalSubscriptionDetails> GetAsync(string id, CancellationToken ct) => Task.FromResult(_remote);
public Task<IReadOnlyList<PayPalSubscriptionTransaction>> TransactionsAsync(string id, DateTimeOffset from, DateTimeOffset to, CancellationToken ct)
=> Task.FromResult<IReadOnlyList<PayPalSubscriptionTransaction>>(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<string, string> headers, CancellationToken ct)
=> Verified ? Task.CompletedTask : throw new SubscriptionOperationException(400, "Signature invalide.");
}
}

View file

@ -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<IOptions<ResourceUsageOptions>>().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);
}
}

View file

@ -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<HttpResponseMessage> 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<SubscriptionOperationException>(() =>
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<SubscriptionOperationException>(() =>
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<SubscriptionOperationException>(() => 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<SubscriptionOperationException>(() =>
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<SubscriptionOperationException>(() => 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<string, string>
{
["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<SubscriptionOperationException>(() =>
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<SubscriptionOperationException>(() => 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<SubscriptionOperationException>(() => Gateway(http).GetAsync("../other", Ct));
Assert.Empty(handler.Requests);
}
}

View file

@ -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<PayPalSubscriptionTransaction> Payments { get; } = [];
public Task<PayPalSubscriptionDetails> GetAsync(string id, CancellationToken ct) => Task.FromResult(Remote);
public Task<IReadOnlyList<PayPalSubscriptionTransaction>> TransactionsAsync(string id, DateTimeOffset from, DateTimeOffset to, CancellationToken ct)
=> Task.FromResult<IReadOnlyList<PayPalSubscriptionTransaction>>(Payments.ToArray());
public Task<PayPalSubscriptionDetails> 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<string, string> 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<ApplicationDbContext>(o => o.UseInMemoryDatabase(name));
services.AddResourceUsage(new ConfigurationBuilder().Build());
if (!useBackendDefaults)
services.Configure<ResourceUsageOptions>(o =>
{
o.ApiCallWeight = 0.02m;
o.CpuSecondWeight = 1m;
o.BandwidthMbWeight = 0.1m;
o.StorageMbWeight = 0.3m;
o.DefaultLimit = 10000m;
});
services.Configure<ResourceSubscriptionOptions>(o =>
{
o.DiscoveryPlanId = "P-DISCOVERY";
o.PremiumPlanId = "P-PREMIUM";
});
services.AddSingleton<TimeProvider>(clock);
services.AddSingleton<IPayPalSubscriptionGateway>(gateway);
services.AddScoped<IResourceSubscriptionService, ResourceSubscriptionService>();
return services.BuildServiceProvider();
}
private static async Task<ResourceSubscription> BeginAsync(IServiceProvider services, Gateway gateway, string pack = "Discovery", decimal gross = 0m)
{
var db = services.GetRequiredService<ApplicationDbContext>();
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<IResourceSubscriptionService>();
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<ApplicationDbContext>();
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<IResourceSubscriptionService>();
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<IResourceUsageService>().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<IResourceSubscriptionService>().ReconcileAsync(subscription, Ct);
var user = await scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().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<IResourceSubscriptionService>();
await service.ReconcileAsync(subscription, Ct);
var user = await scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().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<ApplicationDbContext>();
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<ApplicationDbContext>();
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<IResourceSubscriptionService>().ReconcileAsync(subscription, Ct);
var usage = await scope.ServiceProvider.GetRequiredService<IResourceUsageService>().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<IResourceSubscriptionService>();
await service.ReconcileAsync(subscription, Ct);
await service.ReconcileAsync(subscription, Ct);
var usage = await scope.ServiceProvider.GetRequiredService<IResourceUsageService>().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<ApplicationDbContext>().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<IOptions<ResourceUsageOptions>>().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<IResourceSubscriptionService>().ReconcileAsync(subscription, Ct);
var usage = await scope.ServiceProvider.GetRequiredService<IResourceUsageService>().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<IResourceSubscriptionService>();
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<IResourceUsageService>();
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<ApplicationDbContext>(),
"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<IResourceSubscriptionService>().ReconcileAsync(subscription, Ct);
var usage = await scope.ServiceProvider.GetRequiredService<IResourceUsageService>().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<IResourceSubscriptionService>();
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<IResourceUsageService>().GetCurrentAsync("alice", "Alice", Ct);
Assert.Equal(12000m, usage.Consumption);
Assert.True(usage.IsQuotaReached);
Assert.True(Assert.Single(scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().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<IResourceUsageService>();
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<IResourceSubscriptionService>().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<SubscriptionOperationException>(() =>
scope.ServiceProvider.GetRequiredService<IResourceSubscriptionService>().ReconcileAsync(subscription, Ct));
Assert.Equal(409, error.StatusCode);
Assert.Empty(scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().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<IResourceSubscriptionService>();
await Assert.ThrowsAsync<SubscriptionOperationException>(() =>
service.BindAsync(subscription, gateway.Remote with { CustomId = Guid.NewGuid().ToString("N") }, Ct));
await Assert.ThrowsAsync<SubscriptionOperationException>(() =>
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<IResourceSubscriptionService>();
var second = secondScope.ServiceProvider.GetRequiredService<IResourceSubscriptionService>();
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<ApplicationDbContext>();
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<IResourceSubscriptionService>();
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<ApplicationDbContext>();
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<IResourceSubscriptionService>();
await service.ReconcileAsync(subscription, Ct);
gateway.Payments.Add(new("PAY-2", "COMPLETED", 1.2m, "EUR", Start));
var error = await Assert.ThrowsAsync<SubscriptionOperationException>(() => service.ReconcileAsync(subscription, Ct));
Assert.Equal(409, error.StatusCode);
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
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<IResourceSubscriptionService>();
Assert.Equal(subscription.Id, (await service.PrepareAsync("alice", "Discovery", Ct)).Id);
await Assert.ThrowsAsync<SubscriptionOperationException>(() => service.PrepareAsync("alice", "Premium", Ct));
await Assert.ThrowsAsync<SubscriptionOperationException>(() => service.PrepareAsync("alice", "unknown", Ct));
}
}

View file

@ -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<ResourceUsageOptions>? configure = null)
{
var services = new ServiceCollection();
var databaseName = Guid.NewGuid().ToString();
services.AddLogging();
services.AddDbContext<ApplicationDbContext>(o => o.UseInMemoryDatabase(databaseName));
services.AddResourceUsage(new ConfigurationBuilder().Build());
services.Configure<ResourceUsageOptions>(o =>
{
o.ApiCallWeight = 0.02m;
o.CpuSecondWeight = 1m;
o.BandwidthMbWeight = 0.1m;
o.StorageMbWeight = 0.3m;
o.DefaultLimit = 10000m;
});
services.Configure<SiteSettings>(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<ApplicationDbContext>();
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<IResourceUsageService>();
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<IResourceUsageService>()
.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<ApplicationDbContext>();
var record = Assert.Single(await db.ResourceUsageRecords.ToListAsync(TestContext.Current.CancellationToken));
Assert.Equal(2m, record.ApiCalls);
var summary = await finalScope.ServiceProvider.GetRequiredService<IResourceUsageService>()
.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<ApplicationDbContext>(),
"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<ApplicationDbContext>()
.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<IResourceUsageService>();
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<ApplicationDbContext>()
.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<IResourceUsageService>();
Assert.True(await usage.TryAdmitAsync("alice", "alice", 1m, 0m, 0, TestContext.Current.CancellationToken));
await ResourceUsageTracker.RecordAsync(scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(),
"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<ApplicationDbContext>(),
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<IServiceScopeFactory>(),
NullLogger<ResourceUsageTelemetryMiddleware>.Instance);
await middleware.InvokeAsync(context, scope.ServiceProvider.GetRequiredService<IResourceUsageService>());
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<IResourceUsageService>()
.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<IServiceScopeFactory>(),
NullLogger<ResourceUsageTelemetryMiddleware>.Instance);
await Assert.ThrowsAsync<InvalidOperationException>(() =>
middleware.InvokeAsync(context, scope.ServiceProvider.GetRequiredService<IResourceUsageService>()));
Assert.Same(originalResponse, context.Response.Body);
var summary = await scope.ServiceProvider.GetRequiredService<IResourceUsageService>()
.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<IServiceScopeFactory>(),
NullLogger<ResourceUsageTelemetryMiddleware>.Instance);
await middleware.InvokeAsync(context, scope.ServiceProvider.GetRequiredService<IResourceUsageService>());
Assert.True(ran);
Assert.Empty(await scope.ServiceProvider.GetRequiredService<ApplicationDbContext>()
.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<IServiceScopeFactory>(),
NullLogger<ResourceUsageTelemetryMiddleware>.Instance);
await middleware.InvokeAsync(context, scope.ServiceProvider.GetRequiredService<IResourceUsageService>());
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<ApplicationDbContext>()
.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<ArgumentOutOfRangeException>(() =>
ResourceUsageTracker.RecordAsync(scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(),
"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<string, string?> { [key] = value }).Build();
var services = new ServiceCollection().AddResourceUsage(configuration);
using var provider = services.BuildServiceProvider();
Assert.Throws<OptionsValidationException>(() =>
provider.GetRequiredService<IOptions<ResourceUsageOptions>>().Value);
}
}