diff --git a/src/Yavsc.Blogs.Tests/BlogAclApiTests.cs b/src/Yavsc.Blogs.Tests/BlogAclApiTests.cs index 5af647cc..b1bfd525 100644 --- a/src/Yavsc.Blogs.Tests/BlogAclApiTests.cs +++ b/src/Yavsc.Blogs.Tests/BlogAclApiTests.cs @@ -39,74 +39,13 @@ public sealed class BlogAclApiTests : IClassFixture { private readonly BlogsWebServerFixture _fixture; - public long CircleId { get; private set; } - public long PostId { get; private set; } public BlogAclApiTests(BlogsWebServerFixture fixture) { _fixture = fixture; - ResetDatabaseWithAlice(); } - - /// Reset the in-memory database and seed alice. - /// The shared SQLite :memory: store persists across - /// requests, so each test starts from a clean slate. - private void ResetDatabaseWithAlice() - { - using var scope = _fixture.Services.CreateScope(); - var db = scope.ServiceProvider.GetRequiredService(); - db.Database.EnsureDeleted(); - db.Database.EnsureCreated(); - - db.Users.Add(new ApplicationUser - { - Id = "alice", - UserName = "alice", - Email = "alice@example.com", - EmailConfirmed = true, - FullName = "Alice Dupont", - Avatar = "/avatars/alice.png", - }); - db.SaveChanges(); - CircleId = SeedCircle("alice", "test", isPublic: true); - PostId = SeedBlogPost("alice", "Billet ACL test"); - } - - /// Create a circle owned by - /// directly in the SQLite store and return its server-assigned - /// id. - private long SeedCircle(string ownerId, string name, bool isPublic = false) - { - using var scope = _fixture.Services.CreateScope(); - var db = scope.ServiceProvider.GetRequiredService(); - var circle = new Circle { OwnerId = ownerId, Name = name, Public = isPublic }; - db.Circle.Add(circle); - db.SaveChanges(); - return circle.Id; - } - - /// Create a blog post owned by - /// directly in the SQLite store and return its server-assigned - /// id. - private long SeedBlogPost(string authorId, string title) - { - using var scope = _fixture.Services.CreateScope(); - var db = scope.ServiceProvider.GetRequiredService(); - var post = new BlogPost - { - AuthorId = authorId, - Title = title, - Article = "Test article body.", - DateCreated = DateTime.UtcNow, - DateModified = DateTime.UtcNow, - }; - db.BlogSpot.Add(post); - db.SaveChanges(); - return post.Id; - } - private string BlogAclUrl() => $"{_fixture.Addresses.First(a => a.StartsWith("https://"))}/{APIPrefix}/blogacl"; @@ -152,8 +91,8 @@ public sealed class BlogAclApiTests : IClassFixture // Mirror PostIt's payload: scalar FK ids only, no nav props. var payload = new CircleAuthorizationToBlogPost { - CircleId = CircleId, - BlogPostId = PostId, + CircleId = _fixture.CircleId, + BlogPostId = _fixture.PostId, }; var response = await http.PostAsJsonAsync(BlogAclUrl(), payload, @@ -187,15 +126,13 @@ public sealed class BlogAclApiTests : IClassFixture // The prod circle already exists with Name="test", Public=true, // owned by the caller. We seed the same shape pre-POST so the // test reproduces the prod scenario end-to-end. - var circleId = SeedCircle("alice", "test", isPublic: true); - var postId = SeedBlogPost("alice", "Billet ACL test"); using var http = NewClient("alice"); var payload = new PostAccessControlRulePayload { - CircleId = circleId, - BlogPostId = postId + CircleId = _fixture.CircleId, + BlogPostId = _fixture.PostId }; var response = await http.PostAsJsonAsync(BlogAclUrl(), payload, @@ -268,10 +205,8 @@ public sealed class BlogAclApiTests : IClassFixture public async Task PostCircleAuthorization_never_returns_500( Dictionary payload) { - ResetDatabaseWithAlice(); using var http = NewClient("alice"); - var response = await http.PostAsJsonAsync( BlogAclUrl(), payload, TestContext.Current.CancellationToken); diff --git a/src/Yavsc.Blogs.Tests/BlogsWebServerFixture.cs b/src/Yavsc.Blogs.Tests/BlogsWebServerFixture.cs index 7be6cd35..12e0801e 100644 --- a/src/Yavsc.Blogs.Tests/BlogsWebServerFixture.cs +++ b/src/Yavsc.Blogs.Tests/BlogsWebServerFixture.cs @@ -1,5 +1,3 @@ -using System.Text; -using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.Data.Sqlite; @@ -8,6 +6,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; using Yavsc.Blogs.Controllers; using Yavsc.Models; +using Yavsc.Models.Blog; +using Yavsc.Models.Relationship; using Yavsc.Services; using Yavsc.Tests.Shared; @@ -59,6 +59,9 @@ public sealed class BlogsWebServerFixture : WebHostFixture { protected override int HttpsPort => 5103; + public long CircleId { get; private set; } + public long PostId { get; private set; } + // A single SqliteConnection held open at the static level, // mirroring how Yavsc.Org.Tests.WebServerFixture hoists its // shared configuration into static slots. Closing the @@ -282,4 +285,63 @@ public sealed class BlogsWebServerFixture : WebHostFixture { } } + + + + /// Reset the in-memory database and seed alice. + /// The shared SQLite :memory: store persists across + /// requests, so each test starts from a clean slate. + private void ResetDatabaseWithAlice() + { + using var scope = Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + db.Database.EnsureDeleted(); + db.Database.EnsureCreated(); + + db.Users.Add(new ApplicationUser + { + Id = "alice", + UserName = "alice", + Email = "alice@example.com", + EmailConfirmed = true, + FullName = "Alice Dupont", + Avatar = "/avatars/alice.png", + }); + db.SaveChanges(); + CircleId = SeedCircle("alice", "test", isPublic: true); + PostId = SeedBlogPost("alice", "Billet ACL test"); + } + + /// Create a circle owned by + /// directly in the SQLite store and return its server-assigned + /// id. + private long SeedCircle(string ownerId, string name, bool isPublic = false) + { + using var scope = Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + var circle = new Circle { OwnerId = ownerId, Name = name, Public = isPublic }; + db.Circle.Add(circle); + db.SaveChanges(); + return circle.Id; + } + + /// Create a blog post owned by + /// directly in the SQLite store and return its server-assigned + /// id. + private long SeedBlogPost(string authorId, string title) + { + using var scope = Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + var post = new BlogPost + { + AuthorId = authorId, + Title = title, + Article = "Test article body.", + DateCreated = DateTime.UtcNow, + DateModified = DateTime.UtcNow, + }; + db.BlogSpot.Add(post); + db.SaveChanges(); + return post.Id; + } }