using System.Net;
using System.Net.Http.Headers;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Yavsc.Blogs.Tests.Fixtures;
using Yavsc.Models;
using Yavsc.Models.Billing;
using Yavsc.Models.Blog;
using Yavsc.Models.Relationship;
using Yavsc.Models.Workflow;
using Yavsc.Tests.Shared;
using Yavsc.Server.Helpers;
namespace Yavsc.Blogs.Tests;
///
/// Integration tests for the personal-storage file endpoints
/// (api/v1/fs) served by FileSystemApiController on the
/// Blogs host.
///
/// Two concerns:
///
/// - Upload records metadata: a multipart
/// POST api/v1/fs persists an row
/// owned by the caller and returns its fileId, so the client
/// can later attach that file to an estimate/query by
/// reference.
/// - Download authorization:
/// GET api/v1/fs/file/{fileId} serves the bytes from the
/// owner's personal space. The owner may always read; a party
/// (client or provider) of an estimate/query the file is attached
/// to may read; a third party is refused. The bytes live on the
/// Blogs host's disk — this is the cross-container reason the
/// download endpoint lives here and not on the API host.
///
///
[Collection("Yavsc Blogs")]
public sealed class FileSystemApiTests : IClassFixture
{
private readonly BlogsWebServerFixture _fixture;
public FileSystemApiTests(BlogsWebServerFixture fixture)
{
_fixture = fixture;
}
private HttpClient NewClient(string subject = "alice", string? name = null)
{
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
};
var http = new HttpClient(handler)
{
BaseAddress = new Uri(_fixture.Addresses.First(a => a.StartsWith("https://")))
};
// Pass a distinct name to mirror production (sub = GUID, name =
// login); defaults to subject for the existing tests where the
// two are equal.
http.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", TestTokenIssuer.Issue(subject, name: name));
return http;
}
private static string PhysicalFile(BlogsWebServerFixture fixture, string owner, string fileName) =>
Path.Combine(fixture.BlogFilesRoot, owner, fileName);
/// Write a physical file into the owner's personal space,
/// so GetFile can stream it back. Creates the directory if
/// needed.
private static void WritePhysicalFile(BlogsWebServerFixture fixture, string owner, string fileName, byte[] content)
{
var path = PhysicalFile(fixture, owner, fileName);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
File.WriteAllBytes(path, content);
}
private long SeedUploadedFile(string ownerId, string path, string contentType, long length)
{
using var scope = _fixture.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService();
var file = new UploadedFile
{
OwnerId = ownerId,
Path = path,
ContentType = contentType,
Length = length,
};
db.UploadedFiles.Add(file);
db.SaveChanges();
return file.Id;
}
[Theory]
[InlineData("note.txt", "note.txt")]
[InlineData("INSC_E03686381_SCHNEIDER_000113378AA__Troisi\u00e8me__.PDF",
"INSC_E03686381_SCHNEIDER_000113378AA__Troisi_232me__.PDF")]
public async Task Post_fs_creates_an_UploadedFile_row_owned_by_the_caller(
string originalName, string storedName)
{
_fixture.ResetDatabase();
// DiskQuota must be non-zero: ReceiveUserFile flags QuotaOffense
// once usage >= DiskQuota, and the controller skips the metadata
// row when QuotaOffense is set. A fresh user has quota 0.
_fixture.SeedUser("alice", u => u.DiskQuota = 10_000_000);
var payload = System.Text.Encoding.UTF8.GetBytes("hello personal storage");
using var form = new MultipartFormDataContent();
var fileContent = new ByteArrayContent(payload);
fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
form.Add(fileContent, "file", originalName);
using var http = NewClient("alice");
var response = await http.PostAsync("/api/v1/fs", form, TestContext.Current.CancellationToken);
var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
Assert.True(response.StatusCode == HttpStatusCode.OK,
$"Expected 200, got {(int)response.StatusCode} ({response.StatusCode}): {body}");
using var doc = JsonDocument.Parse(body);
var item = Assert.Single(doc.RootElement.EnumerateArray());
var fileId = item.GetProperty("fileId").GetInt64();
Assert.True(fileId > 0, $"Expected a positive fileId, got {fileId}. Body: {body}");
Assert.Equal(storedName, item.GetProperty("fileName").GetString());
Assert.False(item.GetProperty("quotaOffense").GetBoolean());
using var scope = _fixture.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService();
var row = Assert.Single(db.UploadedFiles.Where(u => u.OwnerId == "alice"));
Assert.Equal(storedName, row.Path);
Assert.Equal("text/plain", row.ContentType);
Assert.Equal(payload.Length, row.Length);
Assert.True(File.Exists(PhysicalFile(_fixture, "alice", storedName)),
"Expected the uploaded file on disk under the owner's personal root");
}
[Fact]
public async Task Same_size_replacement_is_admitted_near_the_limit_and_renaming_keeps_refundability()
{
_fixture.ResetDatabase();
const string owner = "storage-replacement";
_fixture.SeedUser(owner, u => u.DiskQuota = 10_000_000);
using var http = NewClient(owner);
var ct = TestContext.Current.CancellationToken;
async Task Upload()
{
using var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(new byte[1048576]), "file", "original.bin");
using var response = await http.PostAsync("/api/v1/fs", form, ct);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
await Upload();
using (var scope = _fixture.Services.CreateScope())
{
var policy = scope.ServiceProvider.GetRequiredService>().Value;
var cpu = (policy.DefaultLimit - policy.StorageMbWeight * 1.5m) / policy.CpuSecondWeight;
await ResourceUsageTracker.RecordAsync(scope.ServiceProvider.GetRequiredService(),
owner, owner, cpuSeconds: cpu, cancellationToken: ct);
}
await Upload();
using var moved = await http.PostAsync("/api/fsc/mvf",
new StringContent("""{"id":"original.bin","to":"renamed.bin"}""", System.Text.Encoding.UTF8, "application/json"), ct);
Assert.Equal(HttpStatusCode.OK, moved.StatusCode);
using var deleted = await http.DeleteAsync("/api/v1/fs/renamed.bin", ct);
Assert.Equal(HttpStatusCode.OK, deleted.StatusCode);
using var verify = _fixture.Services.CreateScope();
var db = verify.ServiceProvider.GetRequiredService();
Assert.Equal(0m, (await db.ResourceUsageRecords.SingleAsync(r => r.UserId == owner, ct)).StorageMb);
Assert.Empty(await db.ResourceStorageRecords.Where(r => r.UserId == owner).ToListAsync(ct));
}
[Fact]
public async Task Replacement_and_deletion_refund_tracked_storage_but_not_a_legacy_file()
{
_fixture.ResetDatabase();
const string owner = "storage-refunds";
_fixture.SeedUser(owner, u => u.DiskQuota = 10_000_000);
using var http = NewClient(owner);
var ct = TestContext.Current.CancellationToken;
async Task Upload(int length)
{
using var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(new byte[length]), "file", "refunded.bin");
using var response = await http.PostAsync("/api/v1/fs", form, ct);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
await Upload(2 * 1048576);
await Upload(1048576);
using (var scope = _fixture.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService();
Assert.Equal(1m, (await db.ResourceUsageRecords.SingleAsync(r => r.UserId == owner, ct)).StorageMb);
Assert.Equal(1048576L, (await db.ResourceStorageRecords.SingleAsync(r => r.UserId == owner, ct)).Bytes);
await ResourceUsageTracker.RecordAsync(db, owner, owner, cpuSeconds: 20000m, cancellationToken: ct);
}
using var deleted = await http.DeleteAsync("/api/v1/fs/refunded.bin", ct);
Assert.Equal(HttpStatusCode.OK, deleted.StatusCode);
Assert.False(File.Exists(PhysicalFile(_fixture, owner, "refunded.bin")));
WritePhysicalFile(_fixture, owner, "legacy.bin", new byte[1048576]);
using var legacy = await http.DeleteAsync("/api/v1/fs/legacy.bin", ct);
Assert.Equal(HttpStatusCode.OK, legacy.StatusCode);
using var verify = _fixture.Services.CreateScope();
var final = verify.ServiceProvider.GetRequiredService();
var usage = await final.ResourceUsageRecords.SingleAsync(r => r.UserId == owner, ct);
Assert.Equal(0m, usage.StorageMb);
Assert.Equal(20000m, usage.CpuSeconds);
Assert.Empty(await final.ResourceStorageRecords.Where(r => r.UserId == owner).ToListAsync(ct));
}
[Fact]
public async Task Upload_is_refused_before_writing_when_known_storage_would_exceed_quota()
{
_fixture.ResetDatabase();
_fixture.SeedUser("storage-quota-user", u => u.DiskQuota = 10_000_000);
using (var scope = _fixture.Services.CreateScope())
await ResourceUsageTracker.RecordAsync(
scope.ServiceProvider.GetRequiredService(),
"storage-quota-user", "storage-quota-user", cpuSeconds: 9999.8m,
cancellationToken: TestContext.Current.CancellationToken);
const string fileName = "quota-refused-file.txt";
using var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(new byte[1048576]), "file", fileName);
using var http = NewClient("storage-quota-user");
using var response = await http.PostAsync("/api/v1/fs", form, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.TooManyRequests, response.StatusCode);
Assert.Contains("resource_quota_exceeded",
await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
Assert.False(File.Exists(PhysicalFile(_fixture, "storage-quota-user", fileName)));
using var finalScope = _fixture.Services.CreateScope();
var db = finalScope.ServiceProvider.GetRequiredService();
Assert.Empty(db.UploadedFiles.Where(f => f.OwnerId == "storage-quota-user"));
var record = await db.ResourceUsageRecords.SingleAsync(
r => r.UserId == "storage-quota-user", TestContext.Current.CancellationToken);
Assert.Equal(0m, record.ApiCalls);
Assert.Equal(0m, record.StorageMb);
}
///
/// Regression for the "Mes fichiers" page listing nothing: the
/// personal-storage tree is keyed by the user's login
/// (UserName) — EnsureDestinationDirectory writes
/// under {Blog}/{name-claim}, the download endpoint and
/// avatar paths use user.UserName — but GET api/v1/fs
/// passed User.GetUserId() (the sub claim, a GUID in
/// production) to GetUserFiles, so the listing looked under
/// {Blog}/{sub-GUID}, an empty tree. This test decouples
/// sub from the login (mirroring production) and asserts the
/// listing returns the file stored under the login.
///
[Fact]
public async Task Get_fs_lists_files_stored_under_the_user_login()
{
_fixture.ResetDatabase();
// sub = "alice-sub" (GUID-like), login = "alice" — the shape
// ProfileService mints in production. DiskQuota non-zero so the
// upload persists the UploadedFile row (see Post_fs test).
_fixture.SeedUser("alice", u =>
{
u.Id = "alice-sub";
u.DiskQuota = 10_000_000;
});
var payload = System.Text.Encoding.UTF8.GetBytes("list me");
using var form = new MultipartFormDataContent();
var fileContent = new ByteArrayContent(payload);
fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
form.Add(fileContent, "file", "note.txt");
// Upload with a token whose sub is the GUID-like id and whose
// name is the login — EnsureDestinationDirectory keys off "name".
using (var http = NewClient("alice-sub", name: "alice"))
{
var post = await http.PostAsync("/api/v1/fs", form, TestContext.Current.CancellationToken);
var postBody = await post.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
Assert.True(post.StatusCode == HttpStatusCode.OK,
$"Upload failed: {(int)post.StatusCode} ({post.StatusCode}): {postBody}");
}
// The file landed under the login, not the subject.
Assert.True(File.Exists(PhysicalFile(_fixture, "alice", "note.txt")),
"Expected the uploaded file under {BlogFilesRoot}/alice/note.txt (the login)");
Assert.False(Directory.Exists(Path.Combine(_fixture.BlogFilesRoot, "alice-sub")),
"The subject-named directory must NOT exist — storage is keyed by the login");
// GET /api/v1/fs must list under the login and find the file.
using var get = NewClient("alice-sub", name: "alice");
var response = await get.GetAsync("/api/v1/fs", TestContext.Current.CancellationToken);
var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
Assert.True(response.StatusCode == HttpStatusCode.OK,
$"List failed: {(int)response.StatusCode} ({response.StatusCode}): {body}");
using var doc = JsonDocument.Parse(body);
Assert.True(doc.RootElement.TryGetProperty("files", out var filesEl),
$"Response has no 'files' property. Body: {body}");
var names = filesEl.EnumerateArray().Select(f => f.GetProperty("name").GetString()).ToArray();
Assert.Contains("note.txt", names);
// The UploadedFile row (OwnerId = sub) is enriched onto the
// entry, so the client can attach the file by reference.
var noteEntry = filesEl.EnumerateArray()
.First(f => f.GetProperty("name").GetString() == "note.txt");
Assert.True(noteEntry.TryGetProperty("id", out var idEl) && idEl.GetInt64() > 0,
"Expected the enriched UploadedFile id on the listed entry");
}
[Fact]
public async Task GetFile_owner_can_download_their_own_file()
{
_fixture.ResetDatabase();
_fixture.SeedUser("alice");
var bytes = System.Text.Encoding.UTF8.GetBytes("owner payload");
WritePhysicalFile(_fixture, "alice", "owner-note.txt", bytes);
var fileId = SeedUploadedFile("alice", "owner-note.txt", "text/plain", bytes.Length);
using var http = NewClient("alice");
var response = await http.GetAsync(
$"/api/v1/fs/file/{fileId}", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var downloaded = await response.Content.ReadAsByteArrayAsync(TestContext.Current.CancellationToken);
Assert.Equal(bytes, downloaded);
}
[Fact]
public async Task GetFile_refuses_a_third_party()
{
_fixture.ResetDatabase();
_fixture.SeedUser("alice");
_fixture.SeedUser("carol");
var bytes = System.Text.Encoding.UTF8.GetBytes("private to alice");
WritePhysicalFile(_fixture, "alice", "secret.txt", bytes);
var fileId = SeedUploadedFile("alice", "secret.txt", "text/plain", bytes.Length);
// carol is neither the owner nor a party to any estimate/query
// this file is attached to.
using var http = NewClient("carol");
var response = await http.GetAsync(
$"/api/v1/fs/file/{fileId}", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
}
///
/// Seed the graph needed to prove a party (the client of an
/// estimate the file is attached to) can download a file they do
/// not own: alice owns the file and is the provider; bob is the
/// client; the file is attached to the estimate by reference.
///
private long SeedPartyDownloadGraph(out long estimateId, out string fileName)
{
_fixture.ResetDatabase();
_fixture.SeedUser("alice");
_fixture.SeedUser("bob");
fileName = "party-spec.pdf";
var bytes = System.Text.Encoding.UTF8.GetBytes("party payload");
using var scope = _fixture.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService();
// Location + PerformerProfile(alice): Estimate.OwnerId is a FK
// to PerformerProfile.PerformerId, so the provider profile must
// exist before the estimate can be inserted.
var location = new Location { Address = "1 rue du Test", Latitude = 48.8, Longitude = 2.3 };
db.Locations.Add(location);
db.SaveChanges();
db.Performers.Add(new PerformerProfile
{
PerformerId = "alice",
SIREN = "12345678901234",
OrganizationAddressId = location.Id,
ExerciseCountryCode = "fr",
AcceptNotifications = true,
AcceptPublicContact = true,
Active = true,
Rate = 5,
WebSite = "https://alice.dev",
});
db.SaveChanges();
var estimate = new Estimate
{
OwnerId = "alice",
ClientId = "bob",
CommandType = "Rdv",
Title = "Devis party test",
Description = "Devis",
AttachedFiles = new List(),
AttachedGraphics = new List(),
Bill = new List
{
new() { Name = "Prestation", Description = "Prestation", Count = 1, UnitaryCost = 10m, Currency = "EUR" },
},
};
db.Estimates.Add(estimate);
db.SaveChanges();
estimateId = estimate.Id;
var file = new UploadedFile
{
OwnerId = "alice",
Path = fileName,
ContentType = "application/pdf",
Length = bytes.Length,
};
db.UploadedFiles.Add(file);
db.SaveChanges();
db.EstimateAttachedFiles.Add(new EstimateAttachedFile
{
FileId = file.Id,
EstimateId = estimate.Id,
});
db.SaveChanges();
// The bytes must exist on disk for GetFile to stream them.
WritePhysicalFile(_fixture, "alice", fileName, bytes);
return file.Id;
}
[Fact]
public async Task GetFile_party_client_of_an_attached_estimate_can_download()
{
var fileId = SeedPartyDownloadGraph(out _, out _);
// bob is the client of the estimate the file is attached to:
// a party, authorized to read even though alice owns the file.
using var http = NewClient("bob");
var response = await http.GetAsync(
$"/api/v1/fs/file/{fileId}", TestContext.Current.CancellationToken);
var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
Assert.True(response.StatusCode == HttpStatusCode.OK,
$"Expected 200, got {(int)response.StatusCode} ({response.StatusCode}): {body}");
var downloaded = await response.Content.ReadAsByteArrayAsync(TestContext.Current.CancellationToken);
Assert.Equal(System.Text.Encoding.UTF8.GetBytes("party payload"), downloaded);
}
[Fact]
public async Task GetFile_party_provider_of_an_attached_estimate_can_download()
{
var fileId = SeedPartyDownloadGraph(out _, out _);
// alice is the owner too, so this also exercises the owner path;
// the provider of an attached estimate is authorized regardless.
using var http = NewClient("alice");
var response = await http.GetAsync(
$"/api/v1/fs/file/{fileId}", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task GetFile_unknown_id_returns_404()
{
_fixture.ResetDatabase();
_fixture.SeedUser("alice");
using var http = NewClient("alice");
var response = await http.GetAsync(
"/api/v1/fs/file/999999", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
///
/// Regression for the "delete returns 40x yet the file is gone"
/// report: DeleteUserDirOrFile read FileInfo.Length
/// after fi.Delete(), and FileInfo.Length
/// throws FileNotFoundException once the file no longer
/// exists. The controller's catch turned that into a 400
/// even though the file had already been removed — the user saw
/// "le fichier n'existe pas sur le serveur" while the delete in
/// fact succeeded. This test asserts a clean 200 and that the
/// file is gone from disk.
///
[Fact]
public async Task Delete_fs_removes_the_file_and_returns_200()
{
_fixture.ResetDatabase();
_fixture.SeedUser("alice");
var bytes = System.Text.Encoding.UTF8.GetBytes("delete me");
WritePhysicalFile(_fixture, "alice", "doomed.txt", bytes);
using var http = NewClient("alice");
var response = await http.DeleteAsync(
"/api/v1/fs/doomed.txt", TestContext.Current.CancellationToken);
var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
Assert.True(response.StatusCode == HttpStatusCode.OK,
$"Expected 200, got {(int)response.StatusCode} ({response.StatusCode}): {body}");
Assert.False(File.Exists(PhysicalFile(_fixture, "alice", "doomed.txt")),
"The file must be gone from disk after a successful delete.");
}
}