Replace the implicit 'ACL empty = private' convention with an
explicit two-axis model: Visibility is the master switch, the
ACL is the exception list.
Semantics (matches what BlogSpotService.Index / Details enforce):
Visibility.Public + empty ACL : every caller sees it
Visibility.Public + non-empty : only author + ACL circles + admin
Visibility.Private + any ACL : only author + admin (ACL ignored)
ACL is preserved across
Private/Public flips so
reopening is lossless
The Public+non-empty shape is the 'restrict by exception' case:
open by default, narrowed by the ACL. This is intentionally
different from the previous behaviour, where a Public post
with a non-empty ACL was effectively ACL-restricted anyway —
the new model makes that explicit and removes ambiguity.
Server (Yavsc.Blogs / Yavsc.Server)
- New enum Visibility { Private, Public } in
Yavsc.Abstract.Blogspot (so the wire DTO and the EF entity
share the same type). Stored as int via .HasConversion<int>()
on BlogPost.Visibility. Default Private on construction;
the column default in the migration is 0 so existing rows
land Private without any data migration.
- BlogSpotService.Index: filter rewritten to honour the two-
axis model. Authenticated and anonymous callers now share
the same shape (Public+emptyACL visible to all, otherwise
scoped). Admin reads still go through PermissionHandler.
- PermissionHandler.IsPublic: dropped the blogSpotPublications
lookup, replaced with the Visibility + empty-ACL check that
matches the new model. PermissionHandler.IsSponsor and
IsOwner unchanged.
- UserHelpers.UserPosts (the per-author feed for
/CircleMembers/Details and similar): mirror of the
Index filter, so the two code paths can't silently diverge.
- BlogPostEditViewModel.Publish untouched on this commit. It
still controls whether a row exists in BlogSpotPublication;
the two systems coexist (Publish = 'is this draft published',
Visibility = 'who can read it'). Follow-up to consolidate.
EF migration (Yavsc.Org/Migrations/20260818143013_AddBlogPostVisibility)
- Scaffolded by 'dotnet ef migrations add', not hand-edited,
per the repo preference for generated migrations.
- Adds the new Visibility column (int, NOT NULL, default 0).
- Also drops three shadow-state ClientId1 foreign keys and
their indexes/columns on ClientScopes, ClientRedirectUris,
ClientGrantTypes. These shadow FKs were created by EF from
HasOne<Client>().HasForeignKey(e => e.ClientId) mappings
that have long since been removed from
ApplicationDbContext.OnModelCreating, but the snapshot was
never regenerated against the current model. The columns
are nullable ints with no production data, so the drop is
lossless. Without this, EF Core would keep emitting
warnings on every migration add and the model would drift
further from reality.
DTO wire (Yavsc.Abstract.Blogspot.BlogPost)
- Visibility property added to BlogPostDto. System.Text.Json
serialises the enum as its underlying int, so the JSON
shape is a plain number, no JsonConverter needed.
Client UI (PostIt)
- MainPageViewModel: DraftVisibility ObservableProperty
mirroring the existing DraftTitle/DraftArticle pattern.
Initialised to Private so a fresh draft is private by
default. Save command writes the chosen value into the
BlogPostDto payload for both CreatePostAsync and
UpdatePostAsync. OnSelectedPostChanged hydrates the buffer
from the server-supplied value.
- AllVisibilities property on the VM exposes [Private, Public]
in that order, bound by the ComboBox in MainPage.axaml.
- VisibilityLabelConverter (PostIt.Views) maps the enum to
French user-facing labels ('Privé' / 'Public'); registered
in App.axaml as a static resource.
- MainPage.axaml: a new ComboBox row in the editor pane
between Title and Article. Uses the existing 'no hardcoded
Background without Foreground' lesson so dark mode works.
Tests (Yavsc.Blogs.Tests)
- BlogVisibilityTests (5 [Fact]): drive GET /api/v1/blog with
Visibility fixtures seeded directly in the in-memory DB:
* Private + ACL: only the author sees it
* Public + empty ACL: any authenticated caller sees it
* Public + non-empty ACL: caller without ACL membership
does NOT see it
* Private + ACL: ACL is ignored, only the author sees it
* Visibility round-trips through the JSON wire (int 1)
- UserHelpersVisibilityTests (4 [Fact]): exercise the helper
directly so the two code paths (Index filter vs per-author
feed) can't diverge silently. Same fixture, no HTTP.
Test totals: 29/29 Yavsc.Blogs.Tests (was 20, +5 BlogVisibility
+4 UserHelpersVisibility), 51/51 PostIt.Tests (no change), 44/44
Yavsc.Org.Tests (no change).
Out of scope (tracked in MEMORY.md, 2026-08-18):
- i18n: only the new 'Visibilité :' label is localised; the
rest of MainPage.axaml is still hard-coded French.
- BlogPostEditViewModel.Publish ↔ Visibility consolidation
(which system wins when both are set on the same post?).
- Org-side UI for editing Visibility (the admin web Yavsc
still edits posts without a visibility field).
167 lines
5.4 KiB
C#
167 lines
5.4 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Yavsc.Blogspot;
|
|
using Yavsc.Models;
|
|
using Yavsc.Models.Access;
|
|
using Yavsc.Models.Blog;
|
|
using Yavsc.Models.Relationship;
|
|
using Yavsc.Server.Helpers;
|
|
|
|
namespace Yavsc.Blogs.Tests;
|
|
|
|
/// <summary>
|
|
/// Tests that <see cref="UserHelpers.UserPosts"/> (the
|
|
/// "posts-by-author-for-this-reader" query) honours the same
|
|
/// Visibility rules as <see cref="BlogSpotService.Index"/>.
|
|
///
|
|
/// <para>The two code paths duplicate the ACL/Visibility filter
|
|
/// (one in the listing query, one in the per-author query);
|
|
/// these tests catch the case where the two diverge — the kind
|
|
/// of regression that's easy to miss in a code review because
|
|
/// both filters look correct in isolation.</para>
|
|
///
|
|
/// <para>Uses the same in-memory <c>ApplicationDbContext</c>
|
|
/// scaffold as <see cref="BlogsWebServerFixture"/> but
|
|
/// exercises the helper directly, without going through HTTP,
|
|
/// because <see cref="UserHelpers.UserPosts"/> is the unit
|
|
/// under test.</para>
|
|
/// </summary>
|
|
[Collection("JwtClaimMapping")]
|
|
public sealed class UserHelpersVisibilityTests : IClassFixture<BlogsWebServerFixture>
|
|
{
|
|
private readonly BlogsWebServerFixture _fixture;
|
|
|
|
public UserHelpersVisibilityTests(BlogsWebServerFixture fixture)
|
|
{
|
|
_fixture = fixture;
|
|
}
|
|
|
|
private void ResetDatabase()
|
|
{
|
|
using var scope = _fixture.Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
db.Database.EnsureDeleted();
|
|
db.Database.EnsureCreated();
|
|
|
|
db.Users.Add(new ApplicationUser
|
|
{
|
|
Id = "alice",
|
|
UserName = "alice",
|
|
Email = "alice@example.com",
|
|
EmailConfirmed = true,
|
|
});
|
|
db.Users.Add(new ApplicationUser
|
|
{
|
|
Id = "bob",
|
|
UserName = "bob",
|
|
Email = "bob@example.com",
|
|
EmailConfirmed = true,
|
|
});
|
|
db.SaveChanges();
|
|
}
|
|
|
|
private long SeedPost(string authorId, Visibility visibility, params long[] aclCircleIds)
|
|
{
|
|
using var scope = _fixture.Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
var post = new BlogPost
|
|
{
|
|
AuthorId = authorId,
|
|
Title = $"post-by-{authorId}",
|
|
Article = "test article",
|
|
Visibility = visibility,
|
|
DateCreated = DateTime.UtcNow,
|
|
DateModified = DateTime.UtcNow,
|
|
};
|
|
db.BlogSpot.Add(post);
|
|
db.SaveChanges();
|
|
foreach (var cid in aclCircleIds)
|
|
{
|
|
db.CircleAuthorizationToBlogPost.Add(new CircleAuthorizationToBlogPost
|
|
{
|
|
BlogPostId = post.Id,
|
|
CircleId = cid,
|
|
Comment = false,
|
|
});
|
|
}
|
|
db.SaveChanges();
|
|
return post.Id;
|
|
}
|
|
|
|
private long SeedCircle(string ownerId, string name)
|
|
{
|
|
using var scope = _fixture.Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
var circle = new Circle { OwnerId = ownerId, Name = name };
|
|
db.Circle.Add(circle);
|
|
db.SaveChanges();
|
|
return circle.Id;
|
|
}
|
|
|
|
private void AddMember(long circleId, string memberId)
|
|
{
|
|
using var scope = _fixture.Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
db.CircleMembers.Add(new CircleMember { CircleId = circleId, MemberId = memberId });
|
|
db.SaveChanges();
|
|
}
|
|
|
|
private List<long> UserPostsIds(string posterId, string readerId)
|
|
{
|
|
using var scope = _fixture.Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
return db.UserPosts(posterId, readerId).Select(p => p.Id).ToList();
|
|
}
|
|
|
|
[Fact]
|
|
public void UserPosts_returns_only_private_posts_to_their_author()
|
|
{
|
|
ResetDatabase();
|
|
SeedPost("alice", Visibility.Private);
|
|
|
|
var aliceSees = UserPostsIds("alice", "alice");
|
|
var bobSees = UserPostsIds("alice", "bob");
|
|
|
|
Assert.Single(aliceSees);
|
|
Assert.Empty(bobSees);
|
|
}
|
|
|
|
[Fact]
|
|
public void UserPosts_returns_public_posts_with_empty_ACL_to_anyone()
|
|
{
|
|
ResetDatabase();
|
|
SeedPost("alice", Visibility.Public);
|
|
|
|
var bobSees = UserPostsIds("alice", "bob");
|
|
Assert.Single(bobSees);
|
|
}
|
|
|
|
[Fact]
|
|
public void UserPosts_narrows_public_posts_with_nonempty_ACL()
|
|
{
|
|
ResetDatabase();
|
|
var circleId = SeedCircle("alice", "Famille");
|
|
AddMember(circleId, "alice");
|
|
// AddMember above adds alice, but we want bob NOT in
|
|
// the circle, so we add bob to a different circle only:
|
|
var otherCircleId = SeedCircle("alice", "Travail");
|
|
AddMember(otherCircleId, "bob");
|
|
// Make the post readable only to Famille:
|
|
SeedPost("alice", Visibility.Public, circleId);
|
|
|
|
var bobSees = UserPostsIds("alice", "bob");
|
|
Assert.Empty(bobSees);
|
|
}
|
|
|
|
[Fact]
|
|
public void UserPosts_lets_acl_members_read_public_posts_even_if_not_author()
|
|
{
|
|
ResetDatabase();
|
|
var circleId = SeedCircle("alice", "Famille");
|
|
AddMember(circleId, "bob");
|
|
SeedPost("alice", Visibility.Public, circleId);
|
|
|
|
var bobSees = UserPostsIds("alice", "bob");
|
|
Assert.Single(bobSees);
|
|
}
|
|
}
|