feat: add resource quotas, PayPal subscriptions and paid disk allowances
Calculate and enforce configurable resource consumption, expose balances in PostIt and document the rule in CGU. Add confirmed monthly PayPal credits and disk quotas of twice the free default for Discovery and five times for Premium, with payment reconciliation, migrations and regression tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
0728d2a3f7
commit
650e5f5e30
69 changed files with 19111 additions and 203 deletions
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue