refacto site settings
Some checks failed
Dotnet build and test / build (pull_request) Failing after 8m29s

This commit is contained in:
Paul Schneider 2026-09-17 22:22:18 +01:00
commit 3bc4142383
Signed by: notazof
GPG key ID: 1DD5D838E5343B06
33 changed files with 399 additions and 315 deletions

View file

@ -11,6 +11,7 @@ using Yavsc.Server.Helpers;
using Yavsc.Tests.Shared;
using Yavsc.Blogs.Tests.Fixtures;
using BlogPostDto = Yavsc.Blogspot.BlogPostDto;
using NuGet.Protocol;
namespace Yavsc.Blogs.Tests;
@ -338,10 +339,9 @@ public sealed class BlogApiTests : IClassFixture<BlogsWebServerFixture>
ResetAndSeedDefaultUser();
using var http = NewClient(subject: "tester");
var previousRoot = AbstractFileSystemHelpers.UserFilesDirName;
var tempRoot = Path.Combine(Path.GetTempPath(), "yavsc-blogs-tests-files-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(tempRoot);
AbstractFileSystemHelpers.UserFilesDirName = tempRoot;
// AttachFiles writes under the fixture-configured
// SiteSettings.Blog (BlogFilesRoot), not a local temp dir.
var filesRoot = _fixture.BlogFilesRoot;
try
{
@ -374,8 +374,10 @@ public sealed class BlogApiTests : IClassFixture<BlogsWebServerFixture>
DateModified = DateTime.UtcNow
};
var form = new MultipartFormDataContent();
form.Add(new StringContent(JsonSerializer.Serialize(update)), "blog");
var form = new MultipartFormDataContent
{
{ new StringContent(JsonSerializer.Serialize(update)), "blog" }
};
var fileBytes = System.Text.Encoding.UTF8.GetBytes("payload test");
var fileContent = new ByteArrayContent(fileBytes);
@ -405,8 +407,9 @@ public sealed class BlogApiTests : IClassFixture<BlogsWebServerFixture>
}
finally
{
AbstractFileSystemHelpers.UserFilesDirName = previousRoot;
try { Directory.Delete(tempRoot, recursive: true); } catch { }
// BlogFilesRoot is the fixture-shared temp root (one per
// test run); leave it in place for the other multipart
// tests. The OS reclaims it.
}
}
@ -480,10 +483,9 @@ public sealed class BlogApiTests : IClassFixture<BlogsWebServerFixture>
ResetAndSeedDefaultUser();
using var http = NewClient(subject: "tester");
var previousRoot = AbstractFileSystemHelpers.UserFilesDirName;
var tempRoot = Path.Combine(Path.GetTempPath(), "yavsc-blogs-tests-files-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(tempRoot);
AbstractFileSystemHelpers.UserFilesDirName = tempRoot;
// AttachFiles writes under the fixture-configured
// SiteSettings.Blog (BlogFilesRoot), not a local temp dir.
var filesRoot = _fixture.BlogFilesRoot;
try
{
@ -569,17 +571,18 @@ public sealed class BlogApiTests : IClassFixture<BlogsWebServerFixture>
// markdown link points:
// {UserFilesDirName}/{user}/blogs/{postId}/{fileName}.
var expectedFile = Path.Combine(
tempRoot, "tester", "blogs", created.Id.ToString(), "note.txt");
var written = Directory.Exists(tempRoot)
? string.Join(", ", Directory.EnumerateFiles(tempRoot, "*", SearchOption.AllDirectories))
filesRoot, "tester", "blogs", created.Id.ToString(), "note.txt");
var written = Directory.Exists(filesRoot)
? string.Join(", ", Directory.EnumerateFiles(filesRoot, "*", SearchOption.AllDirectories))
: "<none>";
Assert.True(File.Exists(expectedFile),
$"Expected uploaded file at {expectedFile}. Actually written: {written}");
}
finally
{
AbstractFileSystemHelpers.UserFilesDirName = previousRoot;
try { Directory.Delete(tempRoot, recursive: true); } catch { }
// BlogFilesRoot is the fixture-shared temp root (one per
// test run); leave it in place for the other multipart
// tests. The OS reclaims it.
}
}

View file

@ -63,6 +63,16 @@ public sealed class BlogsWebServerFixture : WebHostFixture
public long PostId { get; private set; }
public string DefaultUserLogin { get => "alice"; }
/// <summary>Root directory under which
/// <c>BlogSpotService.AttachFiles</c> writes uploaded files
/// during tests (the configured <c>SiteSettings.Blog</c>).
/// Attachments land at
/// <c>{BlogFilesRoot}/{userName}/blogs/{postId}/{fileName}</c>.
/// A fresh per-run temp directory, so tests can assert on the
/// physical file without touching the developer's real
/// user-files tree.</summary>
public string BlogFilesRoot { get; private set; } = string.Empty;
// A single SqliteConnection held open at the static level,
// mirroring how Yavsc.Org.Tests.WebServerFixture hoists its
// shared configuration into static slots. Closing the
@ -112,6 +122,18 @@ public sealed class BlogsWebServerFixture : WebHostFixture
builder.Services.AddSingleton<IFileSystemAuthManager>(
new NoopFileSystemAuthManager());
// BlogSpotService.AttachFiles writes uploaded files under
// siteSettings.Blog. Without a configured SiteSettings the
// IOptions<SiteSettings> value has Blog == null and
// Path.Combine(null, …) throws inside AttachFiles' catch-all,
// silently persisting no attachment. Point Blog at a
// per-run temp directory so the multipart tests can assert
// on real files.
builder.Services.Configure<SiteSettings>(s =>
{
s.Blog = BlogFilesRoot;
});
// Real BlogSpotService — same instance the production host
// builds (ApplicationDbContext, IAuthorizationService,
// IFileSystemAuthManager). With PermissionHandler registered