feat(invitations): send invitations into the candidate inbox atomically
This commit is contained in:
1 parent
fc35e27af5
commit
1a067221f7
46 files changed
+3276
-113
No files matched your search
@@ -31,6 +31,11 @@ type companyQuotaFixture struct {
|
||||
organizations organizationFixture
|
||||
policies outbound.PolicyRepository
|
||||
version domain.PolicyVersion
|
||||
// invitationAuthor and invitationCandidate are the accounts of the drafts
|
||||
// that invitation usage names: a charge refers to a stored invitation of
|
||||
// the same organization.
|
||||
invitationAuthor uuid.UUID
|
||||
invitationCandidate uuid.UUID
|
||||
}
|
||||
|
||||
func TestCompanyQuota(t *testing.T) {
|
||||
@@ -59,6 +64,8 @@ func TestCompanyQuota(t *testing.T) {
|
||||
ctx: ctx, pool: pool, clock: clock, transactor: transactor,
|
||||
service: companyquota.NewService(clock.Now), organizations: organizationFixture,
|
||||
policies: policyStore.Policies(), version: version,
|
||||
invitationAuthor: organizationFixture.employer(t, "quota-invitation-author@example.test").ID,
|
||||
invitationCandidate: identity.createUser(t, "quota-invitation-candidate@example.test", domain.ScenarioCandidate, clock.Now().Add(-time.Hour)).ID,
|
||||
}
|
||||
|
||||
t.Run("E0 shares limits across requests and concurrent final reservations", func(t *testing.T) {
|
||||
@@ -66,7 +73,7 @@ func TestCompanyQuota(t *testing.T) {
|
||||
var firstCandidate uuid.UUID
|
||||
var boundaryCandidate uuid.UUID
|
||||
for range 9 {
|
||||
fixture.reserveInvitationAllowed(t, organization, uuid.New())
|
||||
fixture.reserveInvitationAllowed(t, organization, fixture.newInvitationID(t, organization))
|
||||
}
|
||||
for index := range 49 {
|
||||
candidateID := fixture.newCandidateID(t)
|
||||
@@ -87,11 +94,12 @@ func TestCompanyQuota(t *testing.T) {
|
||||
results := make(chan result, 4)
|
||||
var workers sync.WaitGroup
|
||||
for range 2 {
|
||||
invitationID := fixture.newInvitationID(t, organization)
|
||||
workers.Add(1)
|
||||
go func() {
|
||||
defer workers.Done()
|
||||
<-start
|
||||
decision, err := fixture.reserveInvitation(organization, uuid.New())
|
||||
decision, err := fixture.reserveInvitation(organization, invitationID)
|
||||
results <- result{decision: decision, err: err, kind: "invitation"}
|
||||
}()
|
||||
}
|
||||
@@ -156,7 +164,7 @@ func TestCompanyQuota(t *testing.T) {
|
||||
call func(domain.Organization, uuid.UUID) (outbound.CompanyQuotaDecision, error)
|
||||
}{
|
||||
{name: "card view at the 24-hour boundary", id: boundaryCandidate, call: fixture.reserveCardView},
|
||||
{name: "invitation remains charged during the first day", id: uuid.New(), call: fixture.reserveInvitation},
|
||||
{name: "invitation remains charged during the first day", id: fixture.newInvitationID(t, organization), call: fixture.reserveInvitation},
|
||||
} {
|
||||
decision, err := step.call(organization, step.id)
|
||||
if err != nil {
|
||||
@@ -174,7 +182,7 @@ func TestCompanyQuota(t *testing.T) {
|
||||
fixture.assertQuotaRowCounts(t, organization.ID(), 10, 2)
|
||||
|
||||
fixture.clock.Set(atDay.Add(6 * 24 * time.Hour))
|
||||
newInvitation := uuid.New()
|
||||
newInvitation := fixture.newInvitationID(t, organization)
|
||||
decision, err := fixture.reserveInvitation(organization, newInvitation)
|
||||
if err != nil || !decision.Allowed || decision.Reused {
|
||||
t.Fatalf("ReserveInvitation(at 7 days) = %+v, %v; want a new allowed invitation", decision, err)
|
||||
@@ -183,7 +191,7 @@ func TestCompanyQuota(t *testing.T) {
|
||||
|
||||
// The same invitation ID stays idempotent beyond the rolling window,
|
||||
// independently of any request idempotency key used by its caller.
|
||||
oldInvitationID := uuid.New()
|
||||
oldInvitationID := fixture.newInvitationID(t, organization)
|
||||
fixture.reserveInvitationAllowed(t, organization, oldInvitationID)
|
||||
fixture.clock.Advance(8 * 24 * time.Hour)
|
||||
reused, err := fixture.reserveInvitation(organization, oldInvitationID)
|
||||
@@ -195,9 +203,9 @@ func TestCompanyQuota(t *testing.T) {
|
||||
t.Run("E1 enforces the stored 40 and 150 limits", func(t *testing.T) {
|
||||
organization, _ := fixture.newOrganization(t, "e1-boundary", domain.OrganizationVerificationE1)
|
||||
for range 40 {
|
||||
fixture.reserveInvitationAllowed(t, organization, uuid.New())
|
||||
fixture.reserveInvitationAllowed(t, organization, fixture.newInvitationID(t, organization))
|
||||
}
|
||||
deniedInvitation, err := fixture.reserveInvitation(organization, uuid.New())
|
||||
deniedInvitation, err := fixture.reserveInvitation(organization, fixture.newInvitationID(t, organization))
|
||||
if err != nil || deniedInvitation.Allowed || !deniedInvitation.RetryAt.Equal(fixture.clock.Now().Add(7*24*time.Hour)) {
|
||||
t.Fatalf("E1 invitation 41 = %+v, %v; want denial for one week", deniedInvitation, err)
|
||||
}
|
||||
@@ -229,7 +237,7 @@ func TestCompanyQuota(t *testing.T) {
|
||||
fixture.version = activePolicy
|
||||
|
||||
verified, _ := fixture.newOrganization(t, "active-e2", domain.OrganizationVerificationE2)
|
||||
invitation, err := fixture.reserveInvitation(verified, uuid.New())
|
||||
invitation, err := fixture.reserveInvitation(verified, fixture.newInvitationID(t, verified))
|
||||
if err != nil || !invitation.Allowed || invitation.Reused {
|
||||
t.Fatalf("ReserveInvitation(verified E2) = %+v, %v; want an allowed new reservation", invitation, err)
|
||||
}
|
||||
@@ -255,7 +263,7 @@ func TestCompanyQuota(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("build forged E2 snapshot: %v", err)
|
||||
}
|
||||
if err := fixture.reserveWithSnapshot(e0Organization, forgedQuota, uuid.New()); !errors.Is(err, outbound.ErrCompanyQuotaSnapshotChanged) {
|
||||
if err := fixture.reserveWithSnapshot(e0Organization, forgedQuota, fixture.newInvitationID(t, e0Organization)); !errors.Is(err, outbound.ErrCompanyQuotaSnapshotChanged) {
|
||||
t.Fatalf("reserve with forged E2 snapshot against stored E0 organization error = %v, want snapshot mismatch", err)
|
||||
}
|
||||
fixture.assertQuotaRowCounts(t, e0Organization.ID(), 0, 0)
|
||||
@@ -285,7 +293,9 @@ func TestCompanyQuota(t *testing.T) {
|
||||
t.Run("candidate deletion clears card-view data but leaves invitation quota evidence", func(t *testing.T) {
|
||||
organization, _ := fixture.newOrganization(t, "candidate-delete", domain.OrganizationVerificationE0)
|
||||
candidateID := fixture.newCandidateID(t)
|
||||
fixture.reserveInvitationAllowed(t, organization, candidateID)
|
||||
// The charged invitation names another candidate: a candidate with an
|
||||
// invitation cannot be deleted until a deletion procedure settles it.
|
||||
fixture.reserveInvitationAllowed(t, organization, fixture.newInvitationID(t, organization))
|
||||
fixture.reserveCardViewAllowed(t, organization, candidateID)
|
||||
|
||||
if _, err := fixture.pool.Exec(fixture.ctx, `DELETE FROM users WHERE id = $1`, candidateID); err != nil {
|
||||
@@ -298,8 +308,9 @@ func TestCompanyQuota(t *testing.T) {
|
||||
organization, _ := fixture.newOrganization(t, "snapshot-check", domain.OrganizationVerificationE0)
|
||||
quota := fixture.quota(t, organization, fixture.version)
|
||||
rollbackMarker := errors.New("test transaction rollback")
|
||||
rolledBackInvitation := fixture.newInvitationID(t, organization)
|
||||
err := fixture.transactor.WithinTransaction(fixture.ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
||||
decision, err := unit.CompanyQuotas().ReserveInvitation(ctx, quota, uuid.New())
|
||||
decision, err := unit.CompanyQuotas().ReserveInvitation(ctx, quota, rolledBackInvitation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -319,7 +330,7 @@ func TestCompanyQuota(t *testing.T) {
|
||||
WHERE id = $1`, organization.ID(), fixture.clock.Now().Add(-time.Minute)); err != nil {
|
||||
t.Fatalf("change stored organization verification: %v", err)
|
||||
}
|
||||
staleTrustErr := fixture.reserveWithSnapshot(organization, quota, uuid.New())
|
||||
staleTrustErr := fixture.reserveWithSnapshot(organization, quota, fixture.newInvitationID(t, organization))
|
||||
if !errors.Is(staleTrustErr, outbound.ErrCompanyQuotaSnapshotChanged) {
|
||||
t.Fatalf("reserve with stale organization trust error = %v, want snapshot mismatch", staleTrustErr)
|
||||
}
|
||||
@@ -335,7 +346,7 @@ func TestCompanyQuota(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("publish next effective product policy: %v", err)
|
||||
}
|
||||
stalePolicyErr := fixture.reserveWithSnapshot(policyOrganization, stalePolicyQuota, uuid.New())
|
||||
stalePolicyErr := fixture.reserveWithSnapshot(policyOrganization, stalePolicyQuota, fixture.newInvitationID(t, policyOrganization))
|
||||
if !errors.Is(stalePolicyErr, outbound.ErrCompanyQuotaSnapshotChanged) {
|
||||
t.Fatalf("reserve with stale policy error = %v, want snapshot mismatch", stalePolicyErr)
|
||||
}
|
||||
@@ -353,7 +364,7 @@ func TestCompanyQuota(t *testing.T) {
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
organization, _ := fixture.newOrganization(t, "denied-"+string(test.level), test.level)
|
||||
_, err := fixture.reserveInvitation(organization, uuid.New())
|
||||
_, err := fixture.reserveInvitation(organization, fixture.newInvitationID(t, organization))
|
||||
if !errors.Is(err, domain.ErrCompanyQuotaDenied) {
|
||||
t.Fatalf("ReserveInvitation(%s) error = %v, want fail-closed denial", test.level, err)
|
||||
}
|
||||
@@ -393,6 +404,34 @@ func (fixture companyQuotaFixture) newOrganization(
|
||||
return stored, owner
|
||||
}
|
||||
|
||||
// newInvitationID stores a draft of the organization and returns its
|
||||
// identifier, so the usage that names it satisfies the foreign key.
|
||||
func (fixture companyQuotaFixture) newInvitationID(t *testing.T, organization domain.Organization) uuid.UUID {
|
||||
t.Helper()
|
||||
invitationID, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
t.Fatalf("NewV7() error = %v", err)
|
||||
}
|
||||
draft, err := domain.NewInvitationDraft(domain.NewInvitationDraftInput{
|
||||
ID: invitationID, OrganizationID: organization.ID(), CandidateID: fixture.invitationCandidate, ActorID: fixture.invitationAuthor,
|
||||
PolicyVersionID: fixture.version.ID(), At: fixture.clock.Now().Add(-time.Minute),
|
||||
Content: domain.InvitationDraftContent{ResponsePeriod: 7, Terms: invitationStorageTerms()},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
event, err := draft.DraftEvent(fixture.invitationAuthor, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("DraftEvent() error = %v", err)
|
||||
}
|
||||
if err := fixture.transactor.WithinTransaction(fixture.ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
||||
return unit.Invitations().Create(ctx, draft, event)
|
||||
}); err != nil {
|
||||
t.Fatalf("store the draft of the charge: %v", err)
|
||||
}
|
||||
return invitationID
|
||||
}
|
||||
|
||||
func (fixture companyQuotaFixture) newCandidateID(t *testing.T) uuid.UUID {
|
||||
t.Helper()
|
||||
candidate := fixture.organizations.identity.createUser(
|
||||
|
||||
Reference in new issue
Block a user