Members: whitespace reasons read as legacy; ToggleRole and SetTier refuse unknown accounts

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Heimdall 2026-11-11 16:39:33 +00:00
commit 3790a1a216
4 changed files with 63 additions and 5 deletions

View file

@ -193,7 +193,7 @@
{
@if (user.Status == MembershipStatus.Suspended)
{
<div class="muted" style="margin-top: 0.8rem;">@(string.IsNullOrEmpty(user.SuspensionReason) ? Bifrost.Web.Pages.App.Members.IndexModel.LegacySuspensionText : "Suspended: " + user.SuspensionReason)</div>
<div class="muted" style="margin-top: 0.8rem;">@(string.IsNullOrWhiteSpace(user.SuspensionReason) ? Bifrost.Web.Pages.App.Members.IndexModel.LegacySuspensionText : "Suspended: " + user.SuspensionReason)</div>
}
<form method="post" asp-page-handler="@(user.Status == MembershipStatus.Suspended ? "Unsuspend" : "Suspend")" style="margin-top: 0.8rem;">
<input type="hidden" name="userAccountId" value="@user.UserAccountId" />

View file

@ -190,7 +190,13 @@ public sealed class IndexModel(
var user = await dbContext.UserAccounts
.Include(x => x.Membership!)
.ThenInclude(x => x.RoleAssignments)
.SingleAsync(x => x.Id == userAccountId, cancellationToken);
.SingleOrDefaultAsync(x => x.Id == userAccountId, cancellationToken);
if (user is null)
{
Notice = RefusalNotices[SuspensionOutcome.NoSuchAccount];
return RedirectToPage();
}
if (user.Membership is null)
{
@ -268,7 +274,13 @@ public sealed class IndexModel(
var user = await dbContext.UserAccounts
.Include(x => x.Membership!)
.ThenInclude(x => x.TierSnapshots)
.SingleAsync(x => x.Id == userAccountId, cancellationToken);
.SingleOrDefaultAsync(x => x.Id == userAccountId, cancellationToken);
if (user is null)
{
Notice = RefusalNotices[SuspensionOutcome.NoSuchAccount];
return RedirectToPage();
}
if (user.Membership is null)
{

View file

@ -229,7 +229,7 @@ public sealed class MembersPageNoticeTests(TestWebApplicationFactory factory) :
using (var scope = factory.Services.CreateScope())
{
legacyRows = await scope.ServiceProvider.GetRequiredService<BifrostDbContext>().Memberships
.CountAsync(x => x.Status == MembershipStatus.Suspended && (x.SuspensionReason == null || x.SuspensionReason == ""));
.CountAsync(x => x.Status == MembershipStatus.Suspended && (x.SuspensionReason == null || x.SuspensionReason.Trim() == ""));
}
Assert.Contains("Suspended before reasons were recorded", html);
@ -259,6 +259,52 @@ public sealed class MembersPageNoticeTests(TestWebApplicationFactory factory) :
Assert.DoesNotContain("before reasons were recorded", html);
}
[Theory]
[InlineData("ToggleRole")]
[InlineData("SetTier")]
public async Task ToggleRole_and_SetTier_on_an_unknown_account_are_refused_with_a_notice_and_write_nothing(string handler)
{
using var client = Client();
_ = await SignedInAdminIdAsync(client);
var before = await RowCountsAsync();
int receipts;
using (var scope = factory.Services.CreateScope())
{
receipts = await scope.ServiceProvider.GetRequiredService<BifrostDbContext>().AuditEvents.CountAsync();
}
var html = await PostThenFollowAsync(client, handler, Guid.NewGuid(), "", role: "Producer");
Assert.Contains("does not exist", Alert.Match(html).Groups[1].Value);
Assert.Equal(before, await RowCountsAsync());
using var after = factory.Services.CreateScope();
Assert.Equal(receipts, await after.ServiceProvider.GetRequiredService<BifrostDbContext>().AuditEvents.CountAsync());
}
[Fact]
public async Task A_whitespace_only_stored_reason_shows_the_neutral_text_to_an_admin()
{
using var client = Client();
_ = await SignedInAdminIdAsync(client);
var member = await SeedMemberAsync(MembershipStatus.Suspended);
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<BifrostDbContext>();
(await db.Memberships.SingleAsync(x => x.UserAccountId == member)).SuspensionReason = " ";
await db.SaveChangesAsync();
}
var html = await client.GetStringAsync("/App/Members");
Assert.DoesNotContain("Suspended: <", html);
using var check = factory.Services.CreateScope();
var db2 = check.ServiceProvider.GetRequiredService<BifrostDbContext>();
var whitespaceRows = await db2.Memberships.CountAsync(x => x.Status == MembershipStatus.Suspended && x.SuspensionReason != null && x.SuspensionReason.Trim() == "");
Assert.True(whitespaceRows >= 1);
Assert.Equal(await db2.Memberships.CountAsync(x => x.Status == MembershipStatus.Suspended && (x.SuspensionReason == null || x.SuspensionReason.Trim() == "")),
Regex.Matches(html, "Suspended before reasons were recorded").Count);
}
[Fact]
public async Task An_admin_toggling_their_own_platform_admin_role_is_refused_and_keeps_the_role()
{

View file

@ -65,7 +65,7 @@ public sealed class SuspensionReasonExposureTests
return model.Users;
}
Assert.All(await UsersSeenBy(admin: false), u => Assert.DoesNotContain(Canary, u.SuspensionReason));
Assert.All(await UsersSeenBy(admin: false), u => Assert.Equal(string.Empty, u.SuspensionReason)); // not even the legacy-row marker leaves the projection for a non-admin
Assert.Contains(await UsersSeenBy(admin: true), u => u.SuspensionReason == Canary);
}