Account merge saga: consumer tests through the real host and the fake mover, live-Postgres races, docs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
cdf64b2a10
commit
41f4cc3853
8 changed files with 897 additions and 5 deletions
|
|
@ -1,6 +1,6 @@
|
|||
# Account merge ledger
|
||||
|
||||
What a merge of two Bifrost accounts does to Bifrost records, who owns it, and how an account stands under one. Design: `/opt/commons/handoff/heimdall-account-merge-design-20261113.md` sections 3, 5 and 7. This is the ledger only: no page, no Heimdall call, and nothing calls `CommitAsync` or `UndoLocalAsync` yet (the saga cut does).
|
||||
What a merge of two Bifrost accounts does to Bifrost records, who owns it, and how an account stands under one. Design: `/opt/commons/handoff/heimdall-account-merge-design-20261113.md` sections 3, 5 and 7. The ledger owns what a merge does to Bifrost records; the saga (below) owns the merge's lifecycle around it and every Heimdall call. No page calls the saga yet (cuts D1 and D2), so no merge can be created in production by this code alone.
|
||||
|
||||
## Owner
|
||||
|
||||
|
|
@ -42,7 +42,31 @@ Left untouched (counted by the plan): consent receipts, passkeys, ledger entries
|
|||
|
||||
`UndoLocalAsync(mergeId)` needs `HeimdallReversed` (Undone is a no-op). In one save it replays the journal backward: it un-voids votes whose motion is still Open and appraisals, moves a moved vote or appraisal back only if the survivor did not re-cast it since, restores the folded status, restores the survivor's status only while it is still Suspended with this merge's reason, moves patron events back and derives both tiers again. What it keeps it records as `KeptAfterUndo`; nothing is updated or deleted in the journal, only appended to.
|
||||
|
||||
`FinalizeDueAsync(now)` moves Committed merges whose grace has ended to `Final`, one save per merge; `AccountMergeSweeper` runs it every five minutes. `State` is the merge's concurrency token, so a transition computed from a stale read (the sweeper's Final over the saga's UndoRequested, or the reverse) fails its save and the other transition stands; the sweep skips a merge it lost. `UndoLocalAsync` also refuses a merge with `FinalizedUtc` set: undo is never allowed past Final.
|
||||
`FinalizeDueAsync(now)` moves Committed merges whose grace has ended to `Final`, one save per merge; `AccountMergeSweeper` runs it every 30 seconds, after the saga's due steps (see Saga). `State` is the merge's concurrency token, so a transition computed from a stale read (the sweeper's Final over the saga's UndoRequested, or the reverse) fails its save and the other transition stands; the sweep skips a merge it lost. `UndoLocalAsync` also refuses a merge with `FinalizedUtc` set: undo is never allowed past Final.
|
||||
|
||||
## Saga
|
||||
|
||||
`Features/Membership/AccountMergeSaga.cs` is the one owner of the merge row's lifecycle outside the ledger's Committed, Final and Undone, and the only caller of `HeimdallIdentityMoveClient.MoveAsync` (whose closed `Moved`/`Refused`/`Unknown` answer it consumes as is; the client retries nothing). It writes Confirmed, HeimdallMoved, Failed, UndoRequested and HeimdallReversed, the return UndoRequested to Committed, the move ids and the sealed proofs; it never writes a vote, membership or journal row (it calls `CommitAsync` and `UndoLocalAsync`), reads no standing of its own, and never judges a proof's tier or freshness (Heimdall's).
|
||||
|
||||
| State | Written by | Key and proofs | A crash here |
|
||||
| --- | --- | --- | --- |
|
||||
| Confirmed | `ConfirmAsync`: one insert, the consent record | `HeimdallMoveId` minted (random, never derived from the merge id); both sides' Heimdall bearers sealed | Before the insert nothing exists. After it the sweep calls Heimdall with the stored id; a lost answer replays to the same receipt, because Heimdall answers a replay before it judges liveness. |
|
||||
| HeimdallMoved | `AdvanceAsync` on `Moved`, one save | proofs wiped; receipt tiers and moved login ids stored | `CommitAsync` is total and idempotent; the sweep retries it. |
|
||||
| Failed | `AdvanceAsync` on any `Refused` | proofs wiped; `FailureCode` is Heimdall's code | Terminal. Heimdall applied nothing, Bifrost changed nothing, the folded account stands as before. |
|
||||
| Committed | ledger `CommitAsync`, one save with every reduction | grace 7 days | A crash before the save leaves HeimdallMoved. |
|
||||
| UndoRequested | `RequestUndoAsync`: the survivor, fresh survivor proofs, inside the grace | `HeimdallReverseMoveId` minted; survivor bearers sealed | The sweep replays the reversal (past the grace too). A refused reversal returns the merge to Committed, proofs wiped, `LastOutcome` `undo_<code>`. |
|
||||
| HeimdallReversed | `AdvanceAsync` on the reversal's `Moved` | proofs wiped | `UndoLocalAsync` is retried. |
|
||||
| Undone, Final | ledger | | |
|
||||
|
||||
`AdvanceAsync(mergeId)` is the one step function: the confirm's inline advance, the sweeper's resume and an undo request all run it, forward move and reversal alike (same seal, same outcome handling, same backoff). Every transition is one save guarded by `State`, the concurrency token; the loser of a race clears its tracker, reloads and continues from the stored state, so two advances make one Heimdall move and one commit. `Proposed` is declared by the ledger and written by nobody in this cut.
|
||||
|
||||
`ConfirmAsync` refuses (writing nothing) a third-party actor (`NotParty`), a plan with blocks (`Blocked`, codes only), a plan other than the one the member saw (`PreviewStale`: `MergePlan.Digest(survivorId, foldedId)`, a SHA-256 over a length-prefixed canonical text of the plan), and any proof of another Heimdall account (`ProofAccountMismatch`). A second live merge folding the same account, or a chain, is refused by the database (the live-merge unique index and the chain trigger) and answered `Blocked(merge_in_flight)`. Confirm and the undo request run through `AccountMergeLedger.WriteAsAsync` for the acting member, like every writer that acts for an account.
|
||||
|
||||
**Heimdall down.** `Unknown` (a timeout, a 5xx, a 429, an unrecognised 4xx, `move_id_conflict`) never fails a merge: the saga counts `Attempts`, sets `NextAttemptUtc` (15 s, 1 min, 5 min, then 15 min) and replays the same stored id; the folded account stays Frozen until Heimdall answers. Heimdall's answer to a replay of a move it already stored does not depend on the proofs' age, but a move that never reached Heimdall does: an outage longer than the proofs' freshness (300 s for a login's weak proof, 600 s for a passkey's strong one) ends, when Heimdall is back, in `Refused proof_stale` and a Failed merge; the member confirms again with fresh sign-ins. A replay whose proofs no longer verify (Heimdall's key regenerated) answers `move_id_conflict`, stays Unknown and can never settle: that is follow-up `merge-stuck-operator-path`, as is a receipt that names an account other than the destination (`receipt_mismatch`: proofs wiped, no retry) and a sealed payload the key ring can no longer open (`proofs_unreadable`: retried in an hour, state unchanged, because Heimdall may already have moved).
|
||||
|
||||
**Sealed proofs.** The saga holds the two Heimdall bearers (`ruling merge-sealed-proofs-until-move-final`) only between Confirmed (or UndoRequested) and Heimdall's final answer: protected with Data Protection (purpose `Bifrost.AccountMerge.Proofs.v1`), never logged, wiped in the save that records Moved or Refused. Logs name the merge id, state, outcome class and refusal code; never a token, account id, identity id or provider user id. Proof coverage is Heimdall's rule (`move-proof-strict-passkey`): a proof through one login moves only that login, and only an asserted passkey speaks for the whole account, so a folded account's every login must be proved (or a passkey asserted) or Heimdall answers `identity_not_proved`. A `moveId` replayed with other identities returns the first receipt (`move-replay-first-receipt`).
|
||||
|
||||
`AccountMergeSweeper` is the one merge loop (30 s): per tick, in one scope, `AccountMergeSaga.AdvanceDueAsync` (every in-flight merge whose `NextAttemptUtc` is null or due, oldest first, a failure logged and skipped), then `FinalizeDueAsync`.
|
||||
|
||||
## Merged is terminal
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,19 @@
|
|||
using Bifrost.Web.Configuration;
|
||||
using Bifrost.Web.Data;
|
||||
using Bifrost.Web.Domain;
|
||||
using Bifrost.Web.Features.Heimdall;
|
||||
using Bifrost.Web.Features.Membership;
|
||||
using Bifrost.Web.Features.Motions;
|
||||
using Bifrost.Web.Features.Patronage;
|
||||
using Bifrost.Web.Features.Shared;
|
||||
using Bifrost.Web.Tests.Support;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Npgsql;
|
||||
|
||||
namespace Bifrost.Web.Tests;
|
||||
|
|
@ -400,7 +407,7 @@ public sealed class AccountMergePostgresTests(MergePostgres postgres) : IClassFi
|
|||
{
|
||||
var error = await Assert.ThrowsAnyAsync<Exception>(() => db.GetService<IMigrator>().MigrateAsync("20261113120000_AddPasskeyCredentials"));
|
||||
Assert.Contains("roll-forward only", Pg(error).MessageText);
|
||||
Assert.Equal("20261114120000_AddAccountMerges", (await db.Database.GetAppliedMigrationsAsync()).Last());
|
||||
Assert.Equal("20261114130000_AddAccountMergeSaga", (await db.Database.GetAppliedMigrationsAsync()).Last());
|
||||
}
|
||||
|
||||
await ExecuteAsync(world, "DELETE FROM \"AccountMergeEntries\"; DELETE FROM \"AccountMerges\"");
|
||||
|
|
@ -408,7 +415,7 @@ public sealed class AccountMergePostgresTests(MergePostgres postgres) : IClassFi
|
|||
await down.GetService<IMigrator>().MigrateAsync("20261113120000_AddPasskeyCredentials");
|
||||
Assert.Equal("20261113120000_AddPasskeyCredentials", (await down.Database.GetAppliedMigrationsAsync()).Last());
|
||||
await down.GetService<IMigrator>().MigrateAsync(); // and up again, trigger and index and all
|
||||
Assert.Equal("20261114120000_AddAccountMerges", (await down.Database.GetAppliedMigrationsAsync()).Last());
|
||||
Assert.Equal("20261114130000_AddAccountMergeSaga", (await down.Database.GetAppliedMigrationsAsync()).Last());
|
||||
}
|
||||
|
||||
// ---- restage and intake journal ----
|
||||
|
|
@ -676,4 +683,154 @@ public sealed class AccountMergePostgresTests(MergePostgres postgres) : IClassFi
|
|||
await freeze!.WaitAsync(TimeSpan.FromSeconds(30));
|
||||
Assert.True(blocked);
|
||||
}
|
||||
|
||||
// ---- the saga: races the State token and the live-merge index carry ----
|
||||
|
||||
/// <summary>Holds the first save of each of two contexts until both have arrived: both have read, neither has written.</summary>
|
||||
private sealed class FirstSaveRendezvous : SaveChangesInterceptor
|
||||
{
|
||||
private readonly Barrier _barrier = new(2);
|
||||
private readonly HashSet<Guid> _seen = [];
|
||||
|
||||
public override async ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
|
||||
{
|
||||
bool first;
|
||||
lock (_seen)
|
||||
{
|
||||
first = _seen.Add(eventData.Context!.ContextId.InstanceId);
|
||||
}
|
||||
|
||||
if (first)
|
||||
{
|
||||
await Task.Run(() => _barrier.SignalAndWait(TimeSpan.FromSeconds(30)), cancellationToken);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The fake Heimdall's account model behind the real move client, one key ring for every saga built over it.</summary>
|
||||
private sealed class Heimdall
|
||||
{
|
||||
private readonly EphemeralDataProtectionProvider _keys = new();
|
||||
private readonly HeimdallIdentityMoveClient _mover;
|
||||
|
||||
public Heimdall(AccountMergeLedgerTests.World clockOwner)
|
||||
{
|
||||
Fake = new FakeHeimdall { RequiredAppSecret = HeimdallHost.AppSecret, Now = () => clockOwner.Clock.Now };
|
||||
var options = new HeimdallOptions { BaseUrl = HeimdallShapes.Issuer, AppSecret = HeimdallHost.AppSecret };
|
||||
var client = new HttpClient(Fake);
|
||||
HeimdallAuthService.ConfigureClient(client, options);
|
||||
_mover = new HeimdallIdentityMoveClient(client, Options.Create(options), NullLogger<HeimdallIdentityMoveClient>.Instance);
|
||||
}
|
||||
|
||||
public FakeHeimdall Fake { get; }
|
||||
|
||||
public AccountMergeSaga Saga(AccountMergeLedgerTests.World world, BifrostDbContext db) =>
|
||||
new(db, world.Ledger(db), _mover, _keys, world.Clock, NullLogger<AccountMergeSaga>.Instance);
|
||||
|
||||
/// <summary>A world account with one login at the fake Heimdall and a weak proof through it.</summary>
|
||||
public MergeProof Proof(UserAccount account, string provider) =>
|
||||
new(Fake.Moves.Issue(account.HeimdallAccountId, Fake.Moves.AddLogin(account.HeimdallAccountId, provider)), account.HeimdallAccountId, provider);
|
||||
}
|
||||
|
||||
[PostgresFact]
|
||||
public async Task Two_concurrent_advances_of_one_confirmed_merge_make_one_move_commit_once_and_journal_once()
|
||||
{
|
||||
var world = await postgres.NewWorldAsync();
|
||||
var survivor = await world.AccountAsync("survivor");
|
||||
var folded = await world.AccountAsync("folded");
|
||||
var motion = await world.MotionAsync();
|
||||
await world.VoteAsync(motion, folded, VoteChoice.For);
|
||||
var heimdall = new Heimdall(world);
|
||||
var survivorProof = heimdall.Proof(survivor, "twitch");
|
||||
var foldedProof = heimdall.Proof(folded, "discord");
|
||||
Guid mergeId;
|
||||
await using (var db = world.Db())
|
||||
{
|
||||
var saga = heimdall.Saga(world, db);
|
||||
var digest = (await world.Ledger(db).PlanAsync(survivor.Id, folded.Id, default)).Digest(survivor.Id, folded.Id);
|
||||
mergeId = Assert.IsType<ConfirmOutcome.Confirmed>(await saga.ConfirmAsync(
|
||||
new ConfirmMergeRequest(survivor.Id, survivor.Id, folded.Id, digest, survivorProof, [foldedProof]), default)).MergeId;
|
||||
}
|
||||
|
||||
var racers = AccountMergeLedgerTests.World.OnPostgres(ConnectionOf(world), new FirstSaveRendezvous());
|
||||
async Task<AccountMerge> Advance()
|
||||
{
|
||||
await using var db = racers.Db();
|
||||
return await heimdall.Saga(racers, db).AdvanceAsync(mergeId, default);
|
||||
}
|
||||
|
||||
var both = await Task.WhenAll(Task.Run(Advance), Task.Run(Advance));
|
||||
|
||||
Assert.All(both, x => Assert.Equal(AccountMergeState.Committed, x.State));
|
||||
Assert.Single(heimdall.Fake.Moves.Stored);
|
||||
var journal = await world.JournalAsync(new AccountMerge { Id = mergeId });
|
||||
Assert.Equal(journal.Count, journal.Select(x => x.Seq).Distinct().Count());
|
||||
Assert.Single(journal, x => x.Kind == AccountMergeEntryKind.Vote);
|
||||
Assert.Equal(survivor.Id, Assert.Single(await world.AllVotesAsync(motion)).UserAccountId);
|
||||
}
|
||||
|
||||
[PostgresFact]
|
||||
public async Task Two_confirms_folding_the_same_account_leave_one_row_and_the_other_is_blocked_by_the_database()
|
||||
{
|
||||
var world = await postgres.NewWorldAsync();
|
||||
var first = await world.AccountAsync("first");
|
||||
var second = await world.AccountAsync("second");
|
||||
var folded = await world.AccountAsync("folded");
|
||||
var heimdall = new Heimdall(world);
|
||||
var proofs = new Dictionary<Guid, (MergeProof Survivor, string Digest)>();
|
||||
foreach (var survivor in new[] { first, second })
|
||||
{
|
||||
await using var db = world.Db();
|
||||
proofs[survivor.Id] = (heimdall.Proof(survivor, "twitch"), (await world.Ledger(db).PlanAsync(survivor.Id, folded.Id, default)).Digest(survivor.Id, folded.Id));
|
||||
}
|
||||
|
||||
var foldedProof = heimdall.Proof(folded, "discord");
|
||||
var racers = AccountMergeLedgerTests.World.OnPostgres(ConnectionOf(world), new FirstSaveRendezvous());
|
||||
async Task<ConfirmOutcome> Confirm(UserAccount survivor)
|
||||
{
|
||||
await using var db = racers.Db();
|
||||
return await heimdall.Saga(racers, db).ConfirmAsync(
|
||||
new ConfirmMergeRequest(survivor.Id, survivor.Id, folded.Id, proofs[survivor.Id].Digest, proofs[survivor.Id].Survivor, [foldedProof]), default);
|
||||
}
|
||||
|
||||
var results = await Task.WhenAll(Task.Run(() => Confirm(first)), Task.Run(() => Confirm(second)));
|
||||
|
||||
Assert.Single(results.OfType<ConfirmOutcome.Confirmed>());
|
||||
var blocked = Assert.Single(results.OfType<ConfirmOutcome.Blocked>());
|
||||
Assert.Contains(AccountMergeSaga.MergeInFlight, blocked.Codes);
|
||||
await using var check = world.Db();
|
||||
Assert.Equal(1, await check.AccountMerges.CountAsync());
|
||||
}
|
||||
|
||||
[PostgresFact]
|
||||
public async Task The_saga_migration_adds_and_drops_its_columns_and_keys_and_its_Down_is_refused_while_any_merge_exists()
|
||||
{
|
||||
var world = await postgres.NewWorldAsync();
|
||||
await world.MergeAsync(await world.AccountAsync("survivor"), await world.AccountAsync("folded"), AccountMergeState.Confirmed);
|
||||
await using (var db = world.Db())
|
||||
{
|
||||
var error = await Assert.ThrowsAnyAsync<Exception>(() => db.GetService<IMigrator>().MigrateAsync("20261114120000_AddAccountMerges"));
|
||||
Assert.Contains("AddAccountMergeSaga cannot be reverted", Pg(error).MessageText);
|
||||
Assert.Equal("20261114130000_AddAccountMergeSaga", (await db.Database.GetAppliedMigrationsAsync()).Last());
|
||||
}
|
||||
|
||||
await ExecuteAsync(world, "DELETE FROM \"AccountMerges\"");
|
||||
await using var down = world.Db();
|
||||
await down.GetService<IMigrator>().MigrateAsync("20261114120000_AddAccountMerges");
|
||||
Assert.Equal(0, await ScalarAsync(world, "SELECT count(*) FROM information_schema.columns WHERE table_name = 'AccountMerges' AND column_name IN ('HeimdallMoveId','SealedProofs','Attempts')"));
|
||||
Assert.Equal(0, await ScalarAsync(world, "SELECT count(*) FROM pg_indexes WHERE tablename = 'AccountMerges' AND indexname LIKE 'IX_AccountMerges_Heimdall%'"));
|
||||
await down.GetService<IMigrator>().MigrateAsync();
|
||||
Assert.Equal(3, await ScalarAsync(world, "SELECT count(*) FROM information_schema.columns WHERE table_name = 'AccountMerges' AND column_name IN ('HeimdallMoveId','SealedProofs','Attempts')"));
|
||||
Assert.Equal(2, await ScalarAsync(world, "SELECT count(*) FROM pg_indexes WHERE tablename = 'AccountMerges' AND indexname LIKE 'IX_AccountMerges_Heimdall%'"));
|
||||
}
|
||||
|
||||
private static async Task<long> ScalarAsync(AccountMergeLedgerTests.World world, string sql)
|
||||
{
|
||||
await using var connection = new NpgsqlConnection(ConnectionOf(world));
|
||||
await connection.OpenAsync();
|
||||
await using var command = new NpgsqlCommand(sql, connection);
|
||||
return (long)(await command.ExecuteScalarAsync())!;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
677
tests/Bifrost.Web.Tests/AccountMergeSagaTests.cs
Normal file
677
tests/Bifrost.Web.Tests/AccountMergeSagaTests.cs
Normal file
|
|
@ -0,0 +1,677 @@
|
|||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using Bifrost.Web.Data;
|
||||
using Bifrost.Web.Domain;
|
||||
using Bifrost.Web.Features.Heimdall;
|
||||
using Bifrost.Web.Features.Membership;
|
||||
using Bifrost.Web.Tests.Support;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace Bifrost.Web.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The merge saga from its consumers' side: a request scope of the real host (DI, the move client's HttpClient, Data Protection, the shared
|
||||
/// TestClock) driving Confirm, Advance and RequestUndo against the fake Heimdall's account model (Heimdall 4fe367a's contract). Every
|
||||
/// operation takes a fresh scope, as a request or a sweeper tick would, so what survives is only what was saved.
|
||||
/// </summary>
|
||||
public sealed class AccountMergeSagaTests
|
||||
{
|
||||
private const string Passkey = "bifrost-passkey";
|
||||
|
||||
private sealed record Party(Guid Id, string HeimdallId, IReadOnlyDictionary<string, string> Logins);
|
||||
|
||||
/// <summary>A host plus the helpers every test needs: accounts with logins at the fake Heimdall, proofs, a scope per operation.</summary>
|
||||
private sealed class Rig : IDisposable
|
||||
{
|
||||
public HeimdallHost Host { get; } = new();
|
||||
|
||||
public FakeMoves Moves => Host.Heimdall.Moves;
|
||||
|
||||
public TestClock Clock => Host.Clock;
|
||||
|
||||
public void Dispose() => Host.Dispose();
|
||||
|
||||
public async Task<Party> AccountAsync(string name, params string[] providers)
|
||||
{
|
||||
var heimdallId = HeimdallShapes.NewAccountId();
|
||||
var logins = providers.ToDictionary(x => x, x => Moves.AddLogin(heimdallId, x));
|
||||
using var scope = Host.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<BifrostDbContext>();
|
||||
var account = new UserAccount
|
||||
{
|
||||
DisplayName = name, BifrostIdentity = name, NormalizedBifrostIdentity = BifrostIdentityService.NormalizeIdentity(name),
|
||||
HeimdallAccountId = heimdallId, CreatedAtUtc = Clock.Now, LastSeenAtUtc = Clock.Now
|
||||
};
|
||||
account.Membership = new Membership { UserAccountId = account.Id, Status = MembershipStatus.Active };
|
||||
db.UserAccounts.Add(account);
|
||||
await db.SaveChangesAsync();
|
||||
return new Party(account.Id, heimdallId, logins);
|
||||
}
|
||||
|
||||
/// <summary>A fresh sign-in through the login of <paramref name="provider"/>: a weak proof of that login only.</summary>
|
||||
public MergeProof Weak(Party who, string provider) => new(Moves.Issue(who.HeimdallId, who.Logins[provider]), who.HeimdallId, provider);
|
||||
|
||||
/// <summary>A fresh sign-in of <paramref name="holder"/> through a login that came from <paramref name="origin"/> (a login the merge moved to it).</summary>
|
||||
public MergeProof Moved(Party holder, Party origin, string provider) => new(Moves.Issue(holder.HeimdallId, origin.Logins[provider]), holder.HeimdallId, provider);
|
||||
|
||||
/// <summary>A passkey assertion: a strong proof that speaks for the whole account.</summary>
|
||||
public MergeProof Strong(Party who) => new(Moves.Issue(who.HeimdallId, stepUpMethod: Passkey), who.HeimdallId, "passkey");
|
||||
|
||||
public async Task<T> InScopeAsync<T>(Func<IServiceProvider, Task<T>> work)
|
||||
{
|
||||
using var scope = Host.Services.CreateScope();
|
||||
return await work(scope.ServiceProvider);
|
||||
}
|
||||
|
||||
public async Task<string> DigestAsync(Party survivor, Party folded) =>
|
||||
await InScopeAsync(async sp => (await sp.GetRequiredService<AccountMergeLedger>().PlanAsync(survivor.Id, folded.Id, default)).Digest(survivor.Id, folded.Id));
|
||||
|
||||
public async Task<ConfirmOutcome> ConfirmAsync(Party survivor, Party folded, MergeProof survivorProof, params MergeProof[] foldedProofs)
|
||||
{
|
||||
var digest = await DigestAsync(survivor, folded);
|
||||
return await InScopeAsync(sp => sp.GetRequiredService<AccountMergeSaga>().ConfirmAsync(
|
||||
new ConfirmMergeRequest(survivor.Id, survivor.Id, folded.Id, digest, survivorProof, foldedProofs), default));
|
||||
}
|
||||
|
||||
public async Task<Guid> ConfirmedAsync(Party survivor, Party folded, MergeProof survivorProof, params MergeProof[] foldedProofs) =>
|
||||
Assert.IsType<ConfirmOutcome.Confirmed>(await ConfirmAsync(survivor, folded, survivorProof, foldedProofs)).MergeId;
|
||||
|
||||
public Task<AccountMerge> AdvanceAsync(Guid id) => InScopeAsync(sp => sp.GetRequiredService<AccountMergeSaga>().AdvanceAsync(id, default));
|
||||
|
||||
public Task<int> AdvanceDueAsync() => InScopeAsync(sp => sp.GetRequiredService<AccountMergeSaga>().AdvanceDueAsync(Clock.Now, default));
|
||||
|
||||
public Task<AccountMerge> MergeAsync(Guid id) =>
|
||||
InScopeAsync(sp => sp.GetRequiredService<BifrostDbContext>().AccountMerges.AsNoTracking().SingleAsync(x => x.Id == id));
|
||||
|
||||
public Task<List<AccountMergeEntry>> JournalAsync(Guid id) =>
|
||||
InScopeAsync(sp => sp.GetRequiredService<BifrostDbContext>().AccountMergeEntries.Where(x => x.MergeId == id).ToListAsync());
|
||||
|
||||
public Task<MergeStanding?> StandingAsync(Party who) =>
|
||||
InScopeAsync(sp => sp.GetRequiredService<AccountMergeLedger>().StandingAsync(who.Id, default));
|
||||
|
||||
public Task<int> MergeCountAsync() => InScopeAsync(sp => sp.GetRequiredService<BifrostDbContext>().AccountMerges.CountAsync());
|
||||
|
||||
public async Task<Motion> MotionAsync(Party author)
|
||||
{
|
||||
using var scope = Host.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<BifrostDbContext>();
|
||||
var motion = new Motion
|
||||
{
|
||||
CreatedByUserAccountId = author.Id, Title = "m", Status = MotionStatus.Open, Kind = MotionKind.Standard, OpensAtUtc = Clock.Now,
|
||||
ClosesAtUtc = Clock.Now.AddDays(3), CreatedAtUtc = Clock.Now, ApprovalThreshold = 0.5m
|
||||
};
|
||||
db.Motions.Add(motion);
|
||||
await db.SaveChangesAsync();
|
||||
return motion;
|
||||
}
|
||||
|
||||
public async Task VoteAsync(Motion motion, Party who, VoteChoice choice)
|
||||
{
|
||||
using var scope = Host.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<BifrostDbContext>();
|
||||
db.Votes.Add(new Vote { MotionId = motion.Id, UserAccountId = who.Id, Choice = choice, Weight = 1m, CastAtUtc = Clock.Now.AddHours(1) });
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public Task<string[]> SeenMoveIdsAsync() =>
|
||||
Task.FromResult(Moves.Requests.Select(x => JsonDocument.Parse(x.Body).RootElement.GetProperty("moveId").GetString()!).ToArray());
|
||||
}
|
||||
|
||||
// ---- the happy path and the stored move id ----
|
||||
|
||||
[Fact]
|
||||
public async Task Two_weak_folded_proofs_and_a_survivor_proof_confirm_and_advance_to_committed_with_a_seven_day_grace_and_one_stored_move()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord", "patreon");
|
||||
|
||||
var digest = await rig.DigestAsync(survivor, folded);
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"), rig.Weak(folded, "patreon"));
|
||||
var confirmed = await rig.MergeAsync(id);
|
||||
Assert.Equal(AccountMergeState.Confirmed, confirmed.State);
|
||||
Assert.Equal(survivor.Id, confirmed.ConfirmedByUserAccountId);
|
||||
Assert.Equal(rig.Clock.Now, confirmed.ConfirmedUtc);
|
||||
Assert.Equal("twitch", confirmed.SurvivorProofProvider);
|
||||
Assert.Equal("discord,patreon", confirmed.FoldedProofProviders);
|
||||
Assert.Equal(digest, confirmed.PreviewDigest);
|
||||
Assert.NotNull(confirmed.SealedProofs);
|
||||
Assert.Matches("^[0-9a-f]{32}$", confirmed.HeimdallMoveId!);
|
||||
Assert.Empty(rig.Moves.Requests); // confirm calls no Heimdall
|
||||
Assert.IsType<MergeStanding.Frozen>(await rig.StandingAsync(folded));
|
||||
|
||||
var done = await rig.AdvanceAsync(id);
|
||||
|
||||
Assert.Equal(AccountMergeState.Committed, done.State);
|
||||
Assert.Equal(done.CommittedUtc + AccountMergeLedger.Grace, done.GraceEndsUtc);
|
||||
var stored = Assert.Single(rig.Moves.Stored);
|
||||
Assert.Equal(confirmed.HeimdallMoveId, stored.MoveId);
|
||||
var final = await rig.MergeAsync(id);
|
||||
Assert.Null(final.SealedProofs);
|
||||
Assert.Equal("weak", final.ProofFromTier);
|
||||
Assert.Equal("weak", final.ProofToTier);
|
||||
Assert.Equal(folded.Logins.Values.Order(), JsonSerializer.Deserialize<string[]>(final.MovedIdentityIds!)!.Order());
|
||||
Assert.IsType<MergeStanding.Folded>(await rig.StandingAsync(folded));
|
||||
Assert.Equal(["twitch", "discord", "patreon"], rig.Moves.Logins[survivor.HeimdallId].Select(x => x.Provider));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_vote_on_the_same_open_motion_from_both_accounts_counts_only_the_survivors_after_the_saga_lands()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var motion = await rig.MotionAsync(survivor);
|
||||
await rig.VoteAsync(motion, survivor, VoteChoice.For);
|
||||
await rig.VoteAsync(motion, folded, VoteChoice.Against);
|
||||
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"));
|
||||
await rig.AdvanceAsync(id);
|
||||
|
||||
await rig.InScopeAsync(async sp =>
|
||||
{
|
||||
var db = sp.GetRequiredService<BifrostDbContext>();
|
||||
Assert.Equal(survivor.Id, (await db.Votes.SingleAsync(x => x.MotionId == motion.Id)).UserAccountId);
|
||||
var all = await db.Votes.IgnoreQueryFilters().Where(x => x.MotionId == motion.Id).ToListAsync();
|
||||
Assert.Equal(id, Assert.Single(all, x => x.UserAccountId == folded.Id).VoidedByMergeId);
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Heimdall_sees_the_move_id_stored_on_the_row_and_it_is_not_derived_from_the_merge_id()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"));
|
||||
Assert.NotEqual(id.ToString("N"), (await rig.MergeAsync(id)).HeimdallMoveId);
|
||||
const string overwritten = "overwritten0123456789abcdef0123456789";
|
||||
await rig.InScopeAsync(async sp =>
|
||||
{
|
||||
var db = sp.GetRequiredService<BifrostDbContext>();
|
||||
(await db.AccountMerges.SingleAsync(x => x.Id == id)).HeimdallMoveId = overwritten;
|
||||
await db.SaveChangesAsync();
|
||||
return 0;
|
||||
});
|
||||
|
||||
await rig.AdvanceAsync(id);
|
||||
|
||||
Assert.Equal(overwritten, Assert.Single(rig.Moves.Stored).MoveId);
|
||||
}
|
||||
|
||||
// ---- crashes and lost answers ----
|
||||
|
||||
[Fact]
|
||||
public async Task A_lost_answer_leaves_the_merge_confirmed_and_a_fresh_scope_after_the_proofs_expired_settles_it_by_replay()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"));
|
||||
rig.Moves.MoveApplyThenFail = HttpStatusCode.InternalServerError;
|
||||
|
||||
var lost = await rig.AdvanceAsync(id);
|
||||
|
||||
Assert.Equal(AccountMergeState.Confirmed, lost.State);
|
||||
Assert.Equal(1, lost.Attempts);
|
||||
Assert.Equal(rig.Clock.Now + TimeSpan.FromSeconds(15), lost.NextAttemptUtc);
|
||||
Assert.Equal("HTTP 500", lost.LastOutcome);
|
||||
Assert.Single(rig.Moves.Stored);
|
||||
rig.Moves.MoveApplyThenFail = null;
|
||||
rig.Clock.Advance(TimeSpan.FromSeconds(601)); // every proof is stale and the bearers expired by now; Heimdall answers a replay first
|
||||
|
||||
Assert.Equal(1, await rig.AdvanceDueAsync());
|
||||
|
||||
Assert.Equal(AccountMergeState.Committed, (await rig.MergeAsync(id)).State);
|
||||
Assert.Single(rig.Moves.Stored);
|
||||
Assert.Equal(2, (await rig.SeenMoveIdsAsync()).Length);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_crash_on_the_save_that_writes_HeimdallMoved_is_resumed_with_the_same_move_id_and_one_stored_move()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"));
|
||||
rig.Host.Fault.When = db => db.ChangeTracker.Entries<AccountMerge>().Any(x => x.Entity.State == AccountMergeState.HeimdallMoved);
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => rig.AdvanceAsync(id));
|
||||
rig.Host.Fault.When = null;
|
||||
Assert.Equal(AccountMergeState.Confirmed, (await rig.MergeAsync(id)).State);
|
||||
Assert.NotNull((await rig.MergeAsync(id)).SealedProofs);
|
||||
|
||||
Assert.Equal(AccountMergeState.Committed, (await rig.AdvanceAsync(id)).State);
|
||||
|
||||
var seen = await rig.SeenMoveIdsAsync();
|
||||
Assert.Equal(2, seen.Length);
|
||||
Assert.Single(seen.Distinct());
|
||||
Assert.Single(rig.Moves.Stored);
|
||||
Assert.Equal(seen[0], (await rig.MergeAsync(id)).HeimdallMoveId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_crash_on_the_Committed_save_leaves_HeimdallMoved_and_the_next_advance_commits_once_without_calling_Heimdall_again()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var motion = await rig.MotionAsync(survivor);
|
||||
await rig.VoteAsync(motion, folded, VoteChoice.For);
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"));
|
||||
rig.Host.Fault.When = db => db.ChangeTracker.Entries<AccountMerge>().Any(x => x.Entity.State == AccountMergeState.Committed);
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => rig.AdvanceAsync(id));
|
||||
rig.Host.Fault.When = null;
|
||||
var moved = await rig.MergeAsync(id);
|
||||
Assert.Equal(AccountMergeState.HeimdallMoved, moved.State);
|
||||
Assert.Null(moved.SealedProofs);
|
||||
Assert.Empty(await rig.JournalAsync(id));
|
||||
|
||||
Assert.Equal(AccountMergeState.Committed, (await rig.AdvanceAsync(id)).State);
|
||||
Assert.Equal(AccountMergeState.Committed, (await rig.AdvanceAsync(id)).State);
|
||||
|
||||
Assert.Single(rig.Moves.Requests);
|
||||
var journal = await rig.JournalAsync(id);
|
||||
Assert.Equal(journal.Count, journal.Select(x => x.Seq).Distinct().Count());
|
||||
Assert.Single(journal, x => x.Kind == AccountMergeEntryKind.Vote);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task The_sweepers_own_loop_resumes_a_confirmed_merge_after_a_restart_and_finalizes_it_after_the_grace()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"));
|
||||
|
||||
await RunOneSweepAsync(rig);
|
||||
Assert.Equal(AccountMergeState.Committed, (await rig.MergeAsync(id)).State);
|
||||
|
||||
rig.Clock.Advance(AccountMergeLedger.Grace + TimeSpan.FromMinutes(1));
|
||||
await RunOneSweepAsync(rig);
|
||||
Assert.Equal(AccountMergeState.Final, (await rig.MergeAsync(id)).State);
|
||||
}
|
||||
|
||||
/// <summary>Starts the production sweeper (its first pass runs at once, before its 30 s timer) and stops it once the pass is over.</summary>
|
||||
private static async Task RunOneSweepAsync(Rig rig)
|
||||
{
|
||||
var sweeper = new AccountMergeSweeper(rig.Host.Services.GetRequiredService<IServiceScopeFactory>(), rig.Clock, NullLogger<AccountMergeSweeper>.Instance);
|
||||
await sweeper.StartAsync(default);
|
||||
var before = rig.Clock.Now;
|
||||
var deadline = DateTime.UtcNow.AddSeconds(20);
|
||||
while (DateTime.UtcNow < deadline && sweeper.ExecuteTask is { IsCompleted: false } && await rig.InScopeAsync(async sp =>
|
||||
await sp.GetRequiredService<BifrostDbContext>().AccountMerges.AnyAsync(x =>
|
||||
x.State == AccountMergeState.Confirmed || x.State == AccountMergeState.HeimdallMoved || (x.State == AccountMergeState.Committed && x.GraceEndsUtc <= before))))
|
||||
{
|
||||
await Task.Delay(50);
|
||||
}
|
||||
|
||||
await sweeper.StopAsync(default);
|
||||
}
|
||||
|
||||
// ---- refusals ----
|
||||
|
||||
[Theory]
|
||||
[InlineData("proof_stale")]
|
||||
[InlineData("identity_not_proved")]
|
||||
[InlineData("provider_conflict")]
|
||||
[InlineData("already_moved")]
|
||||
public async Task A_coded_refusal_fails_the_merge_wipes_the_proofs_and_changes_nothing_in_Bifrost(string code)
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", code == "provider_conflict" ? ["discord"] : ["twitch"]);
|
||||
var folded = await rig.AccountAsync("folded", "discord", "patreon");
|
||||
var motion = await rig.MotionAsync(survivor);
|
||||
await rig.VoteAsync(motion, folded, VoteChoice.For);
|
||||
MergeProof[] foldedProofs = code == "identity_not_proved"
|
||||
? [rig.Weak(folded, "discord")] // the patreon login is not proved: no partial move
|
||||
: [rig.Weak(folded, "discord"), rig.Weak(folded, "patreon")];
|
||||
var survivorProof = rig.Weak(survivor, code == "provider_conflict" ? "discord" : "twitch");
|
||||
if (code == "already_moved")
|
||||
{
|
||||
// Heimdall already moved the folded account elsewhere; Bifrost's merge does not know.
|
||||
var elsewhere = await rig.AccountAsync("elsewhere", "twitch2");
|
||||
var move = new HeimdallMoveRequest("preexisting-move-0000", [rig.Strong(folded).AccessToken], rig.Weak(elsewhere, "twitch2").AccessToken);
|
||||
Assert.IsType<HeimdallMoveOutcome.Moved>(await rig.InScopeAsync(sp => sp.GetRequiredService<HeimdallIdentityMoveClient>().MoveAsync(move, default)));
|
||||
foldedProofs = [rig.Strong(folded)];
|
||||
}
|
||||
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, survivorProof, foldedProofs);
|
||||
if (code == "proof_stale")
|
||||
{
|
||||
rig.Clock.Advance(TimeSpan.FromSeconds(400));
|
||||
}
|
||||
|
||||
var failed = await rig.AdvanceAsync(id);
|
||||
|
||||
Assert.Equal(AccountMergeState.Failed, failed.State);
|
||||
Assert.Equal(code, failed.FailureCode);
|
||||
Assert.Equal(rig.Clock.Now, failed.FailedUtc);
|
||||
var stored = await rig.MergeAsync(id);
|
||||
Assert.Null(stored.SealedProofs);
|
||||
Assert.Empty(await rig.JournalAsync(id));
|
||||
Assert.Null(await rig.StandingAsync(folded));
|
||||
await rig.InScopeAsync(async sp =>
|
||||
{
|
||||
var vote = await sp.GetRequiredService<BifrostDbContext>().Votes.SingleAsync();
|
||||
Assert.Equal(folded.Id, vote.UserAccountId);
|
||||
return 0;
|
||||
});
|
||||
Assert.DoesNotContain(rig.Moves.Stored, x => x.MoveId == stored.HeimdallMoveId);
|
||||
}
|
||||
|
||||
// ---- confirm's consent checks ----
|
||||
|
||||
[Fact]
|
||||
public async Task Confirm_refuses_a_third_party_a_changed_plan_a_proof_of_another_account_and_a_blocked_pair_and_writes_no_row()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var stranger = await rig.AccountAsync("stranger", "github");
|
||||
var digest = await rig.DigestAsync(survivor, folded);
|
||||
var good = rig.Weak(survivor, "twitch");
|
||||
var foldedProof = rig.Weak(folded, "discord");
|
||||
|
||||
Task<ConfirmOutcome> Confirm(Guid actor, string digestSeen, MergeProof survivorProof, params MergeProof[] proofs) =>
|
||||
rig.InScopeAsync(sp => sp.GetRequiredService<AccountMergeSaga>().ConfirmAsync(
|
||||
new ConfirmMergeRequest(actor, survivor.Id, folded.Id, digestSeen, survivorProof, proofs), default));
|
||||
|
||||
Assert.IsType<ConfirmOutcome.NotParty>(await Confirm(stranger.Id, digest, good, foldedProof));
|
||||
Assert.IsType<ConfirmOutcome.ProofAccountMismatch>(await Confirm(survivor.Id, digest, rig.Weak(stranger, "github"), foldedProof));
|
||||
Assert.IsType<ConfirmOutcome.ProofAccountMismatch>(await Confirm(survivor.Id, digest, good, rig.Weak(survivor, "twitch")));
|
||||
var motion = await rig.MotionAsync(survivor);
|
||||
await rig.VoteAsync(motion, folded, VoteChoice.For); // the plan the member saw is no longer the plan
|
||||
Assert.IsType<ConfirmOutcome.PreviewStale>(await Confirm(survivor.Id, digest, good, foldedProof));
|
||||
Assert.Equal(0, await rig.MergeCountAsync());
|
||||
|
||||
var blocked = Assert.IsType<ConfirmOutcome.Blocked>(await rig.InScopeAsync(sp => sp.GetRequiredService<AccountMergeSaga>().ConfirmAsync(
|
||||
new ConfirmMergeRequest(survivor.Id, survivor.Id, survivor.Id, "x", good, [good]), default)));
|
||||
Assert.Contains("same_account", blocked.Codes);
|
||||
Assert.Equal(0, await rig.MergeCountAsync());
|
||||
|
||||
// a second confirm of the same fold, after the first
|
||||
var fresh = await rig.DigestAsync(survivor, folded);
|
||||
Assert.IsType<ConfirmOutcome.Confirmed>(await Confirm(survivor.Id, fresh, good, foldedProof));
|
||||
var second = Assert.IsType<ConfirmOutcome.Blocked>(await Confirm(survivor.Id, fresh, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord")));
|
||||
Assert.Contains(AccountMergeSaga.MergeInFlight, second.Codes);
|
||||
Assert.Equal(1, await rig.MergeCountAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_malformed_confirm_throws_before_any_row_and_neither_the_message_nor_the_proof_text_carry_a_token()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
const string canary = "CANARY-bearer-9f3a";
|
||||
var proof = new MergeProof(canary, folded.HeimdallId, "discord");
|
||||
Assert.DoesNotContain(canary, proof.ToString());
|
||||
Assert.DoesNotContain(folded.HeimdallId, proof.ToString());
|
||||
var request = new ConfirmMergeRequest(survivor.Id, survivor.Id, folded.Id, "d", new MergeProof(canary, survivor.HeimdallId, "twitch"), [proof]);
|
||||
Assert.DoesNotContain(canary, request.ToString());
|
||||
|
||||
var nine = Enumerable.Repeat(proof, 9).ToList();
|
||||
var tooMany = await Assert.ThrowsAsync<ArgumentException>(() =>
|
||||
rig.InScopeAsync(sp => sp.GetRequiredService<AccountMergeSaga>().ConfirmAsync(request with { FoldedProofs = nine }, default)));
|
||||
var tooLong = await Assert.ThrowsAsync<ArgumentException>(() =>
|
||||
rig.InScopeAsync(sp => sp.GetRequiredService<AccountMergeSaga>().ConfirmAsync(
|
||||
request with { FoldedProofs = [proof with { AccessToken = canary + new string('x', HeimdallIdentityMoveClient.MaxProofLength) }] }, default)));
|
||||
|
||||
Assert.DoesNotContain(canary, tooMany.ToString());
|
||||
Assert.DoesNotContain(canary, tooLong.ToString());
|
||||
Assert.Equal(0, await rig.MergeCountAsync());
|
||||
}
|
||||
|
||||
// ---- sealing ----
|
||||
|
||||
[Fact]
|
||||
public async Task Sealed_proofs_never_hold_a_bearer_in_the_clear_are_wiped_at_each_final_answer_and_no_log_carries_a_bearer_or_an_account()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var survivorProof = rig.Weak(survivor, "twitch");
|
||||
var foldedProof = rig.Weak(folded, "discord");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, survivorProof, foldedProof);
|
||||
var sealedText = (await rig.MergeAsync(id)).SealedProofs!;
|
||||
Assert.DoesNotContain(survivorProof.AccessToken, sealedText);
|
||||
Assert.DoesNotContain(foldedProof.AccessToken, sealedText);
|
||||
Assert.DoesNotContain("bearer", sealedText);
|
||||
|
||||
await rig.AdvanceAsync(id);
|
||||
Assert.Null((await rig.MergeAsync(id)).SealedProofs);
|
||||
|
||||
var undoProof = rig.Moved(survivor, folded, "discord"); // the moved login now lives on the survivor
|
||||
Assert.IsType<UndoOutcome.Requested>(await RequestUndoAsync(rig, id, survivor, undoProof));
|
||||
var requested = (await rig.MergeAsync(id)).SealedProofs!;
|
||||
Assert.DoesNotContain(undoProof.AccessToken, requested);
|
||||
await rig.AdvanceAsync(id);
|
||||
Assert.Null((await rig.MergeAsync(id)).SealedProofs);
|
||||
|
||||
var log = string.Join('\n', rig.Host.Logs.Lines);
|
||||
foreach (var secret in new[] { survivorProof.AccessToken, foldedProof.AccessToken, undoProof.AccessToken, survivor.HeimdallId, folded.HeimdallId }
|
||||
.Concat(folded.Logins.Values).Concat(survivor.Logins.Values))
|
||||
{
|
||||
Assert.DoesNotContain(secret, log);
|
||||
}
|
||||
|
||||
Assert.Contains(id.ToString(), log);
|
||||
}
|
||||
|
||||
private static Task<UndoOutcome> RequestUndoAsync(Rig rig, Guid id, Party actor, params MergeProof[] proofs) =>
|
||||
rig.InScopeAsync(sp => sp.GetRequiredService<AccountMergeSaga>().RequestUndoAsync(id, actor.Id, proofs, default));
|
||||
|
||||
[Fact]
|
||||
public async Task Unreadable_sealed_proofs_change_no_state_and_retry_in_an_hour_and_the_error_log_names_only_the_merge()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"));
|
||||
await rig.InScopeAsync(async sp =>
|
||||
{
|
||||
var db = sp.GetRequiredService<BifrostDbContext>();
|
||||
(await db.AccountMerges.SingleAsync(x => x.Id == id)).SealedProofs = "CfDJ8-not-a-protected-payload";
|
||||
await db.SaveChangesAsync();
|
||||
return 0;
|
||||
});
|
||||
|
||||
var stuck = await rig.AdvanceAsync(id);
|
||||
|
||||
Assert.Equal(AccountMergeState.Confirmed, stuck.State);
|
||||
Assert.Equal(AccountMergeSaga.ProofsUnreadable, stuck.LastOutcome);
|
||||
Assert.Equal(rig.Clock.Now + TimeSpan.FromHours(1), stuck.NextAttemptUtc);
|
||||
Assert.Empty(rig.Moves.Requests);
|
||||
Assert.Contains(rig.Host.Logs.Lines, x => x.Contains(id.ToString()) && x.Contains("unreadable"));
|
||||
Assert.Equal(0, await rig.AdvanceDueAsync()); // not due for an hour
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_receipt_for_another_account_is_recorded_wipes_the_proofs_and_is_never_retried()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"));
|
||||
await rig.InScopeAsync(async sp =>
|
||||
{
|
||||
var db = sp.GetRequiredService<BifrostDbContext>();
|
||||
(await db.UserAccounts.SingleAsync(x => x.Id == survivor.Id)).HeimdallAccountId = HeimdallShapes.NewAccountId(); // Bifrost's own link drifted
|
||||
await db.SaveChangesAsync();
|
||||
return 0;
|
||||
});
|
||||
|
||||
var stuck = await rig.AdvanceAsync(id);
|
||||
await rig.AdvanceAsync(id);
|
||||
|
||||
Assert.Equal(AccountMergeState.Confirmed, stuck.State);
|
||||
Assert.Equal(AccountMergeSaga.ReceiptMismatch, stuck.LastOutcome);
|
||||
Assert.Null((await rig.MergeAsync(id)).SealedProofs);
|
||||
Assert.Single(rig.Moves.Requests);
|
||||
Assert.IsType<MergeStanding.Frozen>(await rig.StandingAsync(folded));
|
||||
}
|
||||
|
||||
// ---- Heimdall down ----
|
||||
|
||||
[Fact]
|
||||
public async Task While_Heimdall_is_down_the_merge_stays_confirmed_and_the_folded_account_frozen_then_commits_when_it_answers()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Strong(survivor), rig.Strong(folded)); // strong proofs outlive the outage below
|
||||
rig.Moves.MoveDown = new HttpRequestException("down");
|
||||
|
||||
Assert.Equal(1, await rig.AdvanceDueAsync());
|
||||
Assert.Equal(0, await rig.AdvanceDueAsync()); // backed off 15 s
|
||||
rig.Clock.Advance(TimeSpan.FromSeconds(16));
|
||||
Assert.Equal(1, await rig.AdvanceDueAsync());
|
||||
rig.Clock.Advance(TimeSpan.FromSeconds(61));
|
||||
Assert.Equal(1, await rig.AdvanceDueAsync());
|
||||
|
||||
var down = await rig.MergeAsync(id);
|
||||
Assert.Equal(AccountMergeState.Confirmed, down.State);
|
||||
Assert.Equal(3, down.Attempts);
|
||||
Assert.Equal(rig.Clock.Now + TimeSpan.FromMinutes(5), down.NextAttemptUtc);
|
||||
Assert.IsType<MergeStanding.Frozen>(await rig.StandingAsync(folded));
|
||||
Assert.Empty(rig.Moves.Stored);
|
||||
|
||||
rig.Moves.MoveDown = null;
|
||||
rig.Clock.Advance(TimeSpan.FromMinutes(5));
|
||||
Assert.Equal(1, await rig.AdvanceDueAsync());
|
||||
|
||||
Assert.Equal(AccountMergeState.Committed, (await rig.MergeAsync(id)).State);
|
||||
}
|
||||
|
||||
// ---- undo ----
|
||||
|
||||
[Fact]
|
||||
public async Task Undo_with_survivor_proofs_of_the_moved_logins_reverses_the_move_and_ends_Undone()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord", "patreon");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"), rig.Weak(folded, "patreon"));
|
||||
await rig.AdvanceAsync(id);
|
||||
var forward = (await rig.MergeAsync(id)).HeimdallMoveId;
|
||||
|
||||
Assert.IsType<UndoOutcome.Requested>(await RequestUndoAsync(rig, id, survivor, rig.Moved(survivor, folded, "discord"), rig.Moved(survivor, folded, "patreon")));
|
||||
var requested = await rig.MergeAsync(id);
|
||||
Assert.Equal(AccountMergeState.UndoRequested, requested.State);
|
||||
Assert.Matches("^[0-9a-f]{32}$", requested.HeimdallReverseMoveId!);
|
||||
Assert.NotEqual(forward, requested.HeimdallReverseMoveId);
|
||||
Assert.IsType<MergeStanding.Folded>(await rig.StandingAsync(folded));
|
||||
|
||||
var done = await rig.AdvanceAsync(id);
|
||||
|
||||
Assert.Equal(AccountMergeState.Undone, done.State);
|
||||
Assert.NotNull((await rig.MergeAsync(id)).ReversedUtc);
|
||||
Assert.Equal(2, rig.Moves.Stored.Count);
|
||||
var reversal = Assert.Single(rig.Moves.Stored, x => x.ReversesMoveId is not null);
|
||||
Assert.Equal(forward, reversal.ReversesMoveId);
|
||||
Assert.Equal(requested.HeimdallReverseMoveId, reversal.MoveId);
|
||||
Assert.Equal(["discord", "patreon"], rig.Moves.Logins[folded.HeimdallId].Select(x => x.Provider).Order());
|
||||
Assert.Null(await rig.StandingAsync(folded));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_refused_undo_returns_the_merge_to_Committed_with_the_proofs_wiped_and_the_sweep_finalizes_it_after_the_grace()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"));
|
||||
var committed = await rig.AdvanceAsync(id);
|
||||
rig.Moves.Logins[survivor.HeimdallId].RemoveAll(x => x.Provider == "discord"); // the survivor unlinked a moved login
|
||||
|
||||
Assert.IsType<UndoOutcome.Requested>(await RequestUndoAsync(rig, id, survivor, rig.Weak(survivor, "twitch")));
|
||||
var back = await rig.AdvanceAsync(id);
|
||||
|
||||
Assert.Equal(AccountMergeState.Committed, back.State);
|
||||
var stored = await rig.MergeAsync(id);
|
||||
Assert.Equal("undo_not_reversible", stored.LastOutcome);
|
||||
Assert.Null(stored.SealedProofs);
|
||||
Assert.Equal(committed.GraceEndsUtc, stored.GraceEndsUtc);
|
||||
Assert.IsType<MergeStanding.Folded>(await rig.StandingAsync(folded));
|
||||
|
||||
rig.Clock.Advance(AccountMergeLedger.Grace + TimeSpan.FromMinutes(1));
|
||||
await rig.InScopeAsync(async sp =>
|
||||
{
|
||||
await sp.GetRequiredService<AccountMergeSaga>().AdvanceDueAsync(rig.Clock.Now, default);
|
||||
return await sp.GetRequiredService<AccountMergeLedger>().FinalizeDueAsync(rig.Clock.Now, default);
|
||||
});
|
||||
Assert.Equal(AccountMergeState.Final, (await rig.MergeAsync(id)).State);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Undo_is_requested_only_inside_the_grace_only_by_the_survivor_and_only_from_Committed()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"));
|
||||
Assert.IsType<UndoOutcome.NotCommitted>(await RequestUndoAsync(rig, id, survivor, rig.Weak(survivor, "twitch")));
|
||||
Assert.IsType<UndoOutcome.NotCommitted>(await RequestUndoAsync(rig, Guid.NewGuid(), survivor, rig.Weak(survivor, "twitch")));
|
||||
await rig.AdvanceAsync(id);
|
||||
|
||||
Assert.IsType<UndoOutcome.NotSurvivor>(await RequestUndoAsync(rig, id, folded, rig.Weak(folded, "discord")));
|
||||
Assert.IsType<UndoOutcome.ProofAccountMismatch>(await RequestUndoAsync(rig, id, survivor, rig.Weak(folded, "discord")));
|
||||
Assert.Equal(AccountMergeState.Committed, (await rig.MergeAsync(id)).State);
|
||||
|
||||
var proof = rig.Weak(survivor, "twitch");
|
||||
rig.Clock.Advance(AccountMergeLedger.Grace + TimeSpan.FromSeconds(1));
|
||||
Assert.IsType<UndoOutcome.GraceOver>(await RequestUndoAsync(rig, id, survivor, proof)); // Committed, but the grace ended
|
||||
await rig.InScopeAsync(sp => sp.GetRequiredService<AccountMergeLedger>().FinalizeDueAsync(rig.Clock.Now, default));
|
||||
Assert.IsType<UndoOutcome.GraceOver>(await RequestUndoAsync(rig, id, survivor, proof)); // Final
|
||||
Assert.Equal(AccountMergeState.Final, (await rig.MergeAsync(id)).State);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task An_unknown_answer_to_the_reversal_is_replayed_under_the_same_reverse_id_even_past_the_grace()
|
||||
{
|
||||
using var rig = new Rig();
|
||||
var survivor = await rig.AccountAsync("survivor", "twitch");
|
||||
var folded = await rig.AccountAsync("folded", "discord");
|
||||
var id = await rig.ConfirmedAsync(survivor, folded, rig.Weak(survivor, "twitch"), rig.Weak(folded, "discord"));
|
||||
await rig.AdvanceAsync(id);
|
||||
Assert.IsType<UndoOutcome.Requested>(await RequestUndoAsync(rig, id, survivor, rig.Moved(survivor, folded, "discord")));
|
||||
rig.Moves.MoveApplyThenFail = HttpStatusCode.BadGateway;
|
||||
|
||||
Assert.Equal(AccountMergeState.UndoRequested, (await rig.AdvanceAsync(id)).State);
|
||||
rig.Moves.MoveApplyThenFail = null;
|
||||
rig.Clock.Advance(AccountMergeLedger.Grace + TimeSpan.FromDays(1));
|
||||
await rig.AdvanceDueAsync();
|
||||
|
||||
Assert.Equal(AccountMergeState.Undone, (await rig.MergeAsync(id)).State);
|
||||
var seen = (await rig.SeenMoveIdsAsync()).Skip(1).ToArray();
|
||||
Assert.Equal(2, seen.Length);
|
||||
Assert.Single(seen.Distinct());
|
||||
Assert.Equal(2, rig.Moves.Stored.Count);
|
||||
}
|
||||
|
||||
// ---- the wiring ----
|
||||
|
||||
[Fact]
|
||||
public void The_host_registers_exactly_one_merge_loop_in_production_and_the_saga_beside_the_ledger()
|
||||
{
|
||||
var program = File.ReadAllText(Path.Combine(RepoRoot(), "src", "Bifrost.Web", "Program.cs"));
|
||||
Assert.Single(System.Text.RegularExpressions.Regex.Matches(program, @"AddHostedService<.*Merge"));
|
||||
Assert.Contains("AddScoped<Bifrost.Web.Features.Membership.AccountMergeSaga>", program);
|
||||
}
|
||||
|
||||
private static string RepoRoot()
|
||||
{
|
||||
var dir = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "Bifrost.slnx")))
|
||||
{
|
||||
dir = dir.Parent;
|
||||
}
|
||||
|
||||
return dir?.FullName ?? throw new InvalidOperationException("repo root not found");
|
||||
}
|
||||
}
|
||||
|
|
@ -30,6 +30,7 @@ public sealed class MigrationDiscoveryTests
|
|||
"20261111120000_ClearLegacyConsentReceipts",
|
||||
"20261113120000_AddPasskeyCredentials",
|
||||
"20261114120000_AddAccountMerges",
|
||||
"20261114130000_AddAccountMergeSaga",
|
||||
];
|
||||
|
||||
// Database.GetMigrations() reads assembly metadata only; no connection is opened.
|
||||
|
|
|
|||
|
|
@ -91,7 +91,16 @@ public sealed class FakeMoves(Func<DateTimeOffset> now)
|
|||
|
||||
public bool IsRevoked(string token) => _revoked.Contains(_tokens[token].Sid);
|
||||
|
||||
/// <summary>Heimdall serializes a move by its id; so does the fake, because concurrent callers (the saga tests that race two advances) share its dictionaries.</summary>
|
||||
public HttpResponseMessage Route(HttpRequestMessage request, string body, string? requiredSecret)
|
||||
{
|
||||
lock (_moves)
|
||||
{
|
||||
return RouteLocked(request, body, requiredSecret);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpResponseMessage RouteLocked(HttpRequestMessage request, string body, string? requiredSecret)
|
||||
{
|
||||
var secret = request.Headers.TryGetValues("x-heimdall-app-secret", out var values) ? values.Single() : null;
|
||||
Requests.Add((secret, request.Headers.Authorization?.ToString(), body));
|
||||
|
|
|
|||
16
tests/Bifrost.Web.Tests/Support/FaultOnSave.cs
Normal file
16
tests/Bifrost.Web.Tests/Support/FaultOnSave.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
|
||||
namespace Bifrost.Web.Tests.Support;
|
||||
|
||||
/// <summary>
|
||||
/// Armed by a test, it throws on the save its predicate picks and lets every other save through: a crash between two writes (the
|
||||
/// SaveChangesInterceptor seam of AccountMergeLedgerTests). The host's contexts all carry it, so a crash in the saga's own scope is real.
|
||||
/// </summary>
|
||||
public sealed class FaultOnSave : SaveChangesInterceptor
|
||||
{
|
||||
public Func<DbContext, bool>? When { get; set; }
|
||||
|
||||
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default) =>
|
||||
When is { } when && when(eventData.Context!) ? throw new InvalidOperationException("injected save failure") : ValueTask.FromResult(result);
|
||||
}
|
||||
|
|
@ -814,6 +814,11 @@ public sealed class HeimdallHost : TestWebApplicationFactory
|
|||
|
||||
services.RemoveAll<TimeProvider>();
|
||||
services.AddSingleton<TimeProvider>(Clock);
|
||||
// The merge sweeper ticks on the wall clock; a host on a manual clock drives the saga itself (AccountMergeSagaTests runs the sweeper's own loop).
|
||||
foreach (var sweeper in services.Where(x => x.ImplementationType == typeof(Bifrost.Web.Features.Membership.AccountMergeSweeper)).ToList())
|
||||
{
|
||||
services.Remove(sweeper);
|
||||
}
|
||||
services.AddHttpClient(HeimdallTokenVerifier.ClientName).ConfigurePrimaryHttpMessageHandler(() => Heimdall);
|
||||
services.AddHttpClient<HeimdallAuthService>().ConfigurePrimaryHttpMessageHandler(() => Heimdall);
|
||||
services.AddHttpClient<HeimdallMemberClient>().ConfigurePrimaryHttpMessageHandler(() => Heimdall);
|
||||
|
|
|
|||
|
|
@ -20,6 +20,9 @@ public class TestWebApplicationFactory : WebApplicationFactory<Program>
|
|||
private static readonly InMemoryDatabaseRoot _databaseRoot = new();
|
||||
private readonly string _databaseName = $"bifrost-tests-{Guid.NewGuid():N}";
|
||||
|
||||
/// <summary>Armed by a test to crash a chosen save; every context of this host carries it.</summary>
|
||||
public FaultOnSave Fault { get; } = new();
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
builder.UseEnvironment("Testing");
|
||||
|
|
@ -60,7 +63,7 @@ public class TestWebApplicationFactory : WebApplicationFactory<Program>
|
|||
|
||||
services.AddDbContext<BifrostDbContext>(options =>
|
||||
{
|
||||
options.UseInMemoryDatabase(_databaseName, _databaseRoot);
|
||||
options.UseInMemoryDatabase(_databaseName, _databaseRoot).AddInterceptors(Fault);
|
||||
});
|
||||
|
||||
services.AddAuthentication(options =>
|
||||
|
|
|
|||
Loading…
Reference in a new issue