feat(invitations): send invitations into the candidate inbox atomically

This commit is contained in:
shira committed 2026-10-09 16:50:36 +03:00
1 parent fc35e27af5
commit 1a067221f7
46 files changed
+3276 -113

No files matched your search

@@ -191,3 +191,73 @@ func TestInvitationCursorIsBoundToActorAndOrganization(t *testing.T) {
t.Error("a cursor with trailing data was accepted")
}
}
func TestInvitationSendFailuresMapToTheContractStatuses(t *testing.T) {
ctx := context.Background()
send := func(err error) any {
response, unexpected := invitationSendFailure(ctx, err)
if unexpected != nil {
return unexpected
}
return fmt.Sprintf("%T", response)
}
for name, test := range map[string]struct {
err error
want string
}{
"forbidden": {fmt.Errorf("wrapped: %w", outbound.ErrOrganizationManagerRequired), "*api.SendOrganizationInvitationForbidden"},
"unknown invitation": {fmt.Errorf("wrapped: %w", outbound.ErrInvitationNotFound), "*api.SendOrganizationInvitationNotFound"},
"reused key": {invitationusecase.ErrIdempotencyKeyReused, "*api.SendOrganizationInvitationUnprocessableEntity"},
"bad key": {invitationusecase.ErrInvalidIdempotencyKey, "*api.SendOrganizationInvitationBadRequest"},
"stale version": {fmt.Errorf("wrapped: %w", domain.ErrInvitationVersionConflict), "*api.SendOrganizationInvitationPreconditionFailed"},
"blocked send": {fmt.Errorf("wrapped: %w", invitationusecase.ErrSendBlocked), "*api.SendOrganizationInvitationConflict"},
"quota": {&invitationusecase.QuotaExceededError{RetryAt: time.Now().Add(90 * time.Second)}, "*api.TooManyRequestsHeaders"},
} {
if got := send(test.err); got != test.want {
t.Errorf("%s: send failure = %v, want %s", name, got, test.want)
}
}
response, err := invitationSendFailure(ctx, fmt.Errorf("wrapped: %w", &invitationusecase.QuotaExceededError{RetryAt: time.Now().Add(90 * time.Second)}))
limited, ok := response.(*api.TooManyRequestsHeaders)
if err != nil || !ok || limited.Response.Code != api.ProblemCodeRateLimited {
t.Fatalf("quota failure = %T, %v; want 429 rate_limited", response, err)
}
if wait, set := limited.RetryAfter.Get(); !set || wait < 89 || wait > 90 {
t.Fatalf("Retry-After = %d, %v; want the seconds until the quota frees room", wait, set)
}
if _, passedOn := send(errors.New("storage failed")).(error); !passedOn {
t.Error("an unknown failure produced a public response instead of reaching the responder")
}
}
func TestCandidateInvitationCursorIsBoundToTheCandidateAndItsListing(t *testing.T) {
actorID := uuid.Must(uuid.NewV7())
position := outbound.CandidateInvitationPosition{
DeliveredAt: time.Date(2026, time.October, 9, 10, 0, 0, 123456000, time.UTC), InvitationID: uuid.Must(uuid.NewV7()),
}
cursor, err := encodeCandidateInvitationCursor(actorID, position)
if err != nil {
t.Fatalf("encodeCandidateInvitationCursor() error = %v", err)
}
decoded, err := decodeCandidateInvitationCursor(cursor, actorID)
if err != nil || !decoded.DeliveredAt.Equal(position.DeliveredAt) || decoded.InvitationID != position.InvitationID {
t.Fatalf("decodeCandidateInvitationCursor() = %+v, %v; want the same position", decoded, err)
}
if _, err := decodeCandidateInvitationCursor(cursor, uuid.Must(uuid.NewV7())); err == nil {
t.Error("a cursor of another candidate was accepted")
}
// An organization cursor of the same account does not continue the
// candidate's inbox, and the other way round.
organizationCursor, err := encodeInvitationCursor(actorID, actorID, outbound.InvitationListPosition{
CreatedAt: position.DeliveredAt, InvitationID: position.InvitationID,
})
if err != nil {
t.Fatalf("encodeInvitationCursor() error = %v", err)
}
if _, err := decodeCandidateInvitationCursor(organizationCursor, actorID); err == nil {
t.Error("an organization cursor continued the candidate's inbox")
}
if _, err := decodeInvitationCursor(cursor, actorID, actorID); err == nil {
t.Error("a candidate cursor continued an organization listing")
}
}