Token rotation used to be immediate cutover — push gap from when ops rotates to when the customer's .env is updated and portal restarted. Now the old token keeps working for 24h after rotation, so customer ops has a full workday to swap it in without dropping a single push tick. Backend - Customer entity gains PreviousTokenHash + PreviousTokenExpiresAt (both nullable). Non-unique index on PreviousTokenHash so the OR-lookup in FindByTokenAsync stays cheap. - CustomerService.RotateTokenAsync(id, graceWindow=null, ct): copies the existing TokenHash into PreviousTokenHash with PreviousTokenExpiresAt = now + graceWindow (default 24h, lifted to CustomerService.DefaultTokenGracePeriod), then issues a new current token. Second rotation overwrites the previous slot — at most one previous token is ever honoured. - CustomerService.FindByTokenAsync matches either current OR (previous AND PreviousTokenExpiresAt > now). IsActive=false still rejects both. - DTO exposes PreviousTokenExpiresAt so the UI can render the grace window status. - New EF migration AddPreviousTokenGraceWindow on AdminDbContext. Frontend - Customers table "Token" column shows an "Old token valid until …" orange tag with a tooltip whenever the grace window is active, plus the issue/rotation date as before. - TokenShownOnceModal mentions the 24h grace window so ops knows they have time to update .env without urgency. - Rotate-token popconfirm copy updated to reflect the new behavior. Tests (+5, 61/61 passing) - CustomerTokenGraceTests covers: create doesn't set previous; rotate moves current into previous slot with future expiry; zero grace window rejects original immediately; second rotation overwrites previous (original dies, first-rotation becomes the new previous); inactive customer rejects both current AND previous. Verified end-to-end on the dev host - Migration applied cleanly on the existing admin_fleet DB (existing DEV0001 customer got NULL previous columns, no data loss). - Created GRACE01 → got token1. - Rotated → got token2. PreviousTokenExpiresAt = +24h. Both token1 and token2 push successfully (200). - Rotated again → got token3. token1 push now returns 401 (gone). token2 push still 200 (now the previous). token3 push 200 (current). Docs - FLEET-DESIGN.md §6 rewritten — no longer "immediate cutover". - §11 "open seams" row for this feature marked as shipped. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
105 lines
3.7 KiB
C#
105 lines
3.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Tau.Acuvim.Portal.Data;
|
|
using Tau.Acuvim.Portal.DTOs;
|
|
using Tau.Acuvim.Portal.Services;
|
|
|
|
namespace Tau.Acuvim.Portal.Tests;
|
|
|
|
public class CustomerTokenGraceTests
|
|
{
|
|
private static AdminDbContext NewDb()
|
|
{
|
|
var opts = new DbContextOptionsBuilder<AdminDbContext>()
|
|
.UseInMemoryDatabase(databaseName: "grace-" + Guid.NewGuid())
|
|
.Options;
|
|
return new AdminDbContext(opts);
|
|
}
|
|
|
|
private static async Task<(CustomerService svc, Guid id, string originalToken)> SeedAsync()
|
|
{
|
|
var db = NewDb();
|
|
var svc = new CustomerService(db);
|
|
var created = await svc.CreateAsync(new CreateCustomerRequest("ABC0001", "Test"));
|
|
return (svc, created.Customer.Id, created.Token);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_DoesNotSetPreviousToken()
|
|
{
|
|
var (svc, id, _) = await SeedAsync();
|
|
var list = await svc.ListAsync();
|
|
var c = list.Single(x => x.Id == id);
|
|
Assert.Null(c.PreviousTokenExpiresAt);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Rotate_MovesCurrentTokenIntoPreviousSlot()
|
|
{
|
|
var (svc, id, originalToken) = await SeedAsync();
|
|
|
|
var rotated = await svc.RotateTokenAsync(id);
|
|
Assert.NotNull(rotated);
|
|
|
|
// Original token still accepted within grace window.
|
|
var found = await svc.FindByTokenAsync(originalToken);
|
|
Assert.NotNull(found);
|
|
|
|
// New token also accepted.
|
|
var newToken = rotated!.Token;
|
|
Assert.NotEqual(originalToken, newToken);
|
|
var foundNew = await svc.FindByTokenAsync(newToken);
|
|
Assert.NotNull(foundNew);
|
|
|
|
// PreviousTokenExpiresAt populated and in the near future.
|
|
var listed = (await svc.ListAsync()).Single(x => x.Id == id);
|
|
Assert.NotNull(listed.PreviousTokenExpiresAt);
|
|
Assert.True(listed.PreviousTokenExpiresAt > DateTime.UtcNow);
|
|
Assert.True(listed.PreviousTokenExpiresAt <= DateTime.UtcNow + CustomerService.DefaultTokenGracePeriod + TimeSpan.FromMinutes(1));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Rotate_WithZeroGrace_RejectsOriginalImmediately()
|
|
{
|
|
var (svc, id, originalToken) = await SeedAsync();
|
|
|
|
await svc.RotateTokenAsync(id, graceWindow: TimeSpan.Zero);
|
|
|
|
// PreviousExpiresAt is now-ish; the strict-greater-than check in FindByTokenAsync
|
|
// rejects it. So the original is dead.
|
|
var found = await svc.FindByTokenAsync(originalToken);
|
|
Assert.Null(found);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SecondRotation_OverwritesPreviousSlot_OriginalDies()
|
|
{
|
|
var (svc, id, originalToken) = await SeedAsync();
|
|
|
|
var firstRotation = await svc.RotateTokenAsync(id);
|
|
var secondRotation = await svc.RotateTokenAsync(id);
|
|
|
|
Assert.NotNull(firstRotation);
|
|
Assert.NotNull(secondRotation);
|
|
|
|
// Original is gone — the previous slot now holds the first-rotation token.
|
|
Assert.Null(await svc.FindByTokenAsync(originalToken));
|
|
|
|
// First-rotation token is still accepted (it's the new previous).
|
|
Assert.NotNull(await svc.FindByTokenAsync(firstRotation!.Token));
|
|
|
|
// Second-rotation token is accepted (current).
|
|
Assert.NotNull(await svc.FindByTokenAsync(secondRotation!.Token));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task InactiveCustomer_RejectsBothCurrentAndPrevious()
|
|
{
|
|
var (svc, id, originalToken) = await SeedAsync();
|
|
var rotated = await svc.RotateTokenAsync(id);
|
|
await svc.UpdateAsync(id, new UpdateCustomerRequest("Test", IsActive: false));
|
|
|
|
Assert.Null(await svc.FindByTokenAsync(originalToken));
|
|
Assert.Null(await svc.FindByTokenAsync(rotated!.Token));
|
|
}
|
|
}
|