530 lines
23 KiB
Go
530 lines
23 KiB
Go
//go:build integration
|
|
|
|
package integration_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
postgresadapter "lincodex/backend/internal/adapter/secondary/postgres"
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
"lincodex/backend/internal/usecase/companyquota"
|
|
"lincodex/backend/internal/usecase/policy"
|
|
)
|
|
|
|
const companyQuotaTestTimeout = 5 * time.Minute
|
|
|
|
type companyQuotaFixture struct {
|
|
ctx context.Context
|
|
pool *pgxpool.Pool
|
|
clock *testClock
|
|
transactor *postgresadapter.Transactor
|
|
service *companyquota.Service
|
|
organizations organizationFixture
|
|
policies outbound.PolicyRepository
|
|
version domain.PolicyVersion
|
|
}
|
|
|
|
func TestCompanyQuota(t *testing.T) {
|
|
env := bootstrap(t, postgresService)
|
|
ctx, cancel := context.WithTimeout(t.Context(), companyQuotaTestTimeout)
|
|
defer cancel()
|
|
databaseURL := env.createIsolatedDatabase(ctx, t)
|
|
applyMigrations(ctx, t, databaseURL)
|
|
pool := newPoolFor(ctx, t, databaseURL)
|
|
|
|
clock := &testClock{now: time.Now().UTC().Add(5 * time.Minute).Truncate(time.Microsecond)}
|
|
identity := identityFixture{ctx: ctx, pool: pool, store: postgresadapter.NewIdentityStore(pool), now: clock.Now()}
|
|
transactor := postgresadapter.NewTransactor(pool)
|
|
organizationFixture := organizationFixture{
|
|
ctx: ctx, pool: pool, identity: identity, transactor: transactor, now: clock.Now(),
|
|
}
|
|
policyStore := postgresadapter.NewPolicyStore(pool)
|
|
if _, err := policy.NewPublisher(policyStore.Policies(), clock.Now).EnsureDemoProductPolicy(ctx); err != nil {
|
|
t.Fatalf("EnsureDemoProductPolicy() error = %v", err)
|
|
}
|
|
version, err := policyStore.Policies().GetEffective(ctx, domain.ProductPolicyKey, clock.Now())
|
|
if err != nil {
|
|
t.Fatalf("GetEffective(product) error = %v", err)
|
|
}
|
|
fixture := companyQuotaFixture{
|
|
ctx: ctx, pool: pool, clock: clock, transactor: transactor,
|
|
service: companyquota.NewService(clock.Now), organizations: organizationFixture,
|
|
policies: policyStore.Policies(), version: version,
|
|
}
|
|
|
|
t.Run("E0 shares limits across requests and concurrent final reservations", func(t *testing.T) {
|
|
organization, _ := fixture.newOrganization(t, "e0-boundary", domain.OrganizationVerificationE0)
|
|
var firstCandidate uuid.UUID
|
|
var boundaryCandidate uuid.UUID
|
|
for range 9 {
|
|
fixture.reserveInvitationAllowed(t, organization, uuid.New())
|
|
}
|
|
for index := range 49 {
|
|
candidateID := fixture.newCandidateID(t)
|
|
if index == 0 {
|
|
firstCandidate = candidateID
|
|
} else if index == 1 {
|
|
boundaryCandidate = candidateID
|
|
}
|
|
fixture.reserveCardViewAllowed(t, organization, candidateID)
|
|
}
|
|
|
|
type result struct {
|
|
decision outbound.CompanyQuotaDecision
|
|
err error
|
|
kind string
|
|
}
|
|
start := make(chan struct{})
|
|
results := make(chan result, 4)
|
|
var workers sync.WaitGroup
|
|
for range 2 {
|
|
workers.Add(1)
|
|
go func() {
|
|
defer workers.Done()
|
|
<-start
|
|
decision, err := fixture.reserveInvitation(organization, uuid.New())
|
|
results <- result{decision: decision, err: err, kind: "invitation"}
|
|
}()
|
|
}
|
|
for range 2 {
|
|
candidateID := fixture.newCandidateID(t)
|
|
workers.Add(1)
|
|
go func() {
|
|
defer workers.Done()
|
|
<-start
|
|
decision, err := fixture.reserveCardView(organization, candidateID)
|
|
results <- result{decision: decision, err: err, kind: "view"}
|
|
}()
|
|
}
|
|
close(start)
|
|
workers.Wait()
|
|
close(results)
|
|
|
|
allowedInvitations, deniedInvitations := 0, 0
|
|
allowedViews, deniedViews := 0, 0
|
|
for result := range results {
|
|
if result.err != nil {
|
|
t.Fatalf("concurrent quota reservation error = %v", result.err)
|
|
}
|
|
switch result.kind {
|
|
case "invitation":
|
|
if result.decision.Allowed && result.decision.RetryAt.IsZero() {
|
|
allowedInvitations++
|
|
} else if !result.decision.Allowed && result.decision.RetryAt.Equal(fixture.clock.Now().Add(7*24*time.Hour)) {
|
|
deniedInvitations++
|
|
} else {
|
|
t.Fatalf("unexpected invitation decision: %+v", result.decision)
|
|
}
|
|
case "view":
|
|
if result.decision.Allowed && result.decision.RetryAt.IsZero() {
|
|
allowedViews++
|
|
} else if !result.decision.Allowed && result.decision.RetryAt.Equal(fixture.clock.Now().Add(24*time.Hour)) {
|
|
deniedViews++
|
|
} else {
|
|
t.Fatalf("unexpected card-view decision: %+v", result.decision)
|
|
}
|
|
default:
|
|
t.Fatalf("unexpected reservation kind %q", result.kind)
|
|
}
|
|
}
|
|
if allowedInvitations != 1 || deniedInvitations != 1 || allowedViews != 1 || deniedViews != 1 {
|
|
t.Fatalf("concurrent invitation/view results: allowed=%d/%d denied=%d/%d", allowedInvitations, allowedViews, deniedInvitations, deniedViews)
|
|
}
|
|
fixture.assertQuotaRowCounts(t, organization.ID(), 10, 50)
|
|
fixture.assertStoredEvidence(t, organization.ID(), domain.OrganizationVerificationE0, domain.OrganizationBasisConfirmedEmail)
|
|
|
|
// A repeated card in the active window is shared by the organization.
|
|
fixture.clock.Advance(23 * time.Hour)
|
|
repeated, err := fixture.reserveCardView(organization, firstCandidate)
|
|
if err != nil || !repeated.Allowed || !repeated.Reused {
|
|
t.Fatalf("ReserveCardView(repeat) = %+v, %v; want an organization-level reuse", repeated, err)
|
|
}
|
|
fixture.clock.Set(fixture.clock.Now().Add(time.Hour))
|
|
atDay := fixture.clock.Now()
|
|
for _, step := range []struct {
|
|
name string
|
|
id uuid.UUID
|
|
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},
|
|
} {
|
|
decision, err := step.call(organization, step.id)
|
|
if err != nil {
|
|
t.Fatalf("%s error = %v", step.name, err)
|
|
}
|
|
if step.name == "card view at the 24-hour boundary" {
|
|
if !decision.Allowed || decision.Reused {
|
|
t.Fatalf("%s decision = %+v, want a new allowed view", step.name, decision)
|
|
}
|
|
} else if decision.Allowed || !decision.RetryAt.Equal(atDay.Add(6*24*time.Hour)) {
|
|
t.Fatalf("%s decision = %+v, want retry at the 7-day boundary", step.name, decision)
|
|
}
|
|
}
|
|
fixture.assertActiveQuotaCounts(t, organization.ID(), atDay, 10, 2)
|
|
fixture.assertQuotaRowCounts(t, organization.ID(), 10, 2)
|
|
|
|
fixture.clock.Set(atDay.Add(6 * 24 * time.Hour))
|
|
newInvitation := uuid.New()
|
|
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)
|
|
}
|
|
fixture.assertActiveQuotaCounts(t, organization.ID(), fixture.clock.Now(), 1, 0)
|
|
|
|
// The same invitation ID stays idempotent beyond the rolling window,
|
|
// independently of any request idempotency key used by its caller.
|
|
oldInvitationID := uuid.New()
|
|
fixture.reserveInvitationAllowed(t, organization, oldInvitationID)
|
|
fixture.clock.Advance(8 * 24 * time.Hour)
|
|
reused, err := fixture.reserveInvitation(organization, oldInvitationID)
|
|
if err != nil || !reused.Allowed || !reused.Reused {
|
|
t.Fatalf("ReserveInvitation(old ID after 8 days) = %+v, %v; want retained idempotent usage", reused, err)
|
|
}
|
|
})
|
|
|
|
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())
|
|
}
|
|
deniedInvitation, err := fixture.reserveInvitation(organization, uuid.New())
|
|
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)
|
|
}
|
|
for range 150 {
|
|
fixture.reserveCardViewAllowed(t, organization, fixture.newCandidateID(t))
|
|
}
|
|
deniedView, err := fixture.reserveCardView(organization, fixture.newCandidateID(t))
|
|
if err != nil || deniedView.Allowed || !deniedView.RetryAt.Equal(fixture.clock.Now().Add(24*time.Hour)) {
|
|
t.Fatalf("E1 card view 151 = %+v, %v; want denial for 24 hours", deniedView, err)
|
|
}
|
|
fixture.assertQuotaRowCounts(t, organization.ID(), 40, 150)
|
|
fixture.assertStoredEvidence(t, organization.ID(), domain.OrganizationVerificationE1, domain.OrganizationBasisCorporateDomain)
|
|
})
|
|
|
|
t.Run("active E2 policy permits verified organizations and rejects a forged tier snapshot", func(t *testing.T) {
|
|
policyDocument := domain.DemoProductPolicyV1()
|
|
policyDocument.OrganizationQuotas.TierE2.Active = true
|
|
activePolicy, err := policy.NewPublisher(fixture.policies, fixture.clock.Now).Publish(fixture.ctx, policy.PublishInput{
|
|
Key: domain.ProductPolicyKey, Kind: domain.PolicyKindDemo, Author: "system:quota-test",
|
|
EffectiveAt: fixture.clock.Now(), Policy: policyDocument,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("publish active E2 policy: %v", err)
|
|
}
|
|
if quota := activePolicy.Policy().OrganizationQuotas.TierE2; !quota.Active ||
|
|
quota.InvitationsPerWindow != 100 || quota.DistinctCardViewsPerWindow != 400 {
|
|
t.Fatalf("active E2 policy limits = %+v, want 100 invitations and 400 views", quota)
|
|
}
|
|
fixture.version = activePolicy
|
|
|
|
verified, _ := fixture.newOrganization(t, "active-e2", domain.OrganizationVerificationE2)
|
|
invitation, err := fixture.reserveInvitation(verified, uuid.New())
|
|
if err != nil || !invitation.Allowed || invitation.Reused {
|
|
t.Fatalf("ReserveInvitation(verified E2) = %+v, %v; want an allowed new reservation", invitation, err)
|
|
}
|
|
view, err := fixture.reserveCardView(verified, fixture.newCandidateID(t))
|
|
if err != nil || !view.Allowed || view.Reused {
|
|
t.Fatalf("ReserveCardView(verified E2) = %+v, %v; want an allowed new reservation", view, err)
|
|
}
|
|
fixture.assertQuotaRowCounts(t, verified.ID(), 1, 1)
|
|
fixture.assertStoredEvidence(t, verified.ID(), domain.OrganizationVerificationE2, domain.OrganizationBasisRequisitesAndAuthority)
|
|
|
|
e0Organization, _ := fixture.newOrganization(t, "forged-e2", domain.OrganizationVerificationE0)
|
|
forgedE2, err := domain.RestoreOrganization(domain.StoredOrganization{
|
|
ID: e0Organization.ID(), Name: e0Organization.Profile().Name(),
|
|
VerificationLevel: string(domain.OrganizationVerificationE2),
|
|
VerificationBasis: string(domain.OrganizationBasisRequisitesAndAuthority),
|
|
VerificationChangedAt: fixture.clock.Now().Add(-time.Minute),
|
|
Version: e0Organization.Version(), CreatedAt: e0Organization.CreatedAt(), UpdatedAt: e0Organization.UpdatedAt(),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("restore forged E2 snapshot for E0 organization: %v", err)
|
|
}
|
|
forgedQuota, err := domain.NewCompanyQuota(forgedE2, activePolicy, fixture.clock.Now())
|
|
if err != nil {
|
|
t.Fatalf("build forged E2 snapshot: %v", err)
|
|
}
|
|
if err := fixture.reserveWithSnapshot(e0Organization, forgedQuota, uuid.New()); !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)
|
|
})
|
|
|
|
t.Run("a repeated view refreshes monotonically when application time moves backward", func(t *testing.T) {
|
|
organization, _ := fixture.newOrganization(t, "view-clock", domain.OrganizationVerificationE0)
|
|
candidateID := fixture.newCandidateID(t)
|
|
fixture.reserveCardViewAllowed(t, organization, candidateID)
|
|
firstSeen := fixture.clock.Now()
|
|
fixture.clock.Advance(time.Hour)
|
|
fixture.reserveCardViewReused(t, organization, candidateID)
|
|
fixture.clock.Set(firstSeen.Add(30 * time.Minute))
|
|
fixture.reserveCardViewReused(t, organization, candidateID)
|
|
|
|
var lastSeen time.Time
|
|
if err := fixture.pool.QueryRow(fixture.ctx, `
|
|
SELECT last_seen_at FROM company_card_view_quota_usage
|
|
WHERE organization_id = $1 AND candidate_id = $2`, organization.ID(), candidateID).Scan(&lastSeen); err != nil {
|
|
t.Fatalf("read repeated view timestamp: %v", err)
|
|
}
|
|
if !lastSeen.Equal(firstSeen.Add(time.Hour)) {
|
|
t.Fatalf("last_seen_at = %s, want monotonic time %s", lastSeen, firstSeen.Add(time.Hour))
|
|
}
|
|
})
|
|
|
|
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)
|
|
fixture.reserveCardViewAllowed(t, organization, candidateID)
|
|
|
|
if _, err := fixture.pool.Exec(fixture.ctx, `DELETE FROM users WHERE id = $1`, candidateID); err != nil {
|
|
t.Fatalf("delete candidate: %v", err)
|
|
}
|
|
fixture.assertQuotaRowCounts(t, organization.ID(), 1, 0)
|
|
})
|
|
|
|
t.Run("rollback and stale trust or policy snapshots write no usage", func(t *testing.T) {
|
|
organization, _ := fixture.newOrganization(t, "snapshot-check", domain.OrganizationVerificationE0)
|
|
quota := fixture.quota(t, organization, fixture.version)
|
|
rollbackMarker := errors.New("test transaction rollback")
|
|
err := fixture.transactor.WithinTransaction(fixture.ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
decision, err := unit.CompanyQuotas().ReserveInvitation(ctx, quota, uuid.New())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !decision.Allowed {
|
|
return errors.New("test invitation was unexpectedly denied")
|
|
}
|
|
return rollbackMarker
|
|
})
|
|
if !errors.Is(err, rollbackMarker) {
|
|
t.Fatalf("transaction rollback error = %v, want marker", err)
|
|
}
|
|
fixture.assertQuotaRowCounts(t, organization.ID(), 0, 0)
|
|
|
|
if _, err := fixture.pool.Exec(fixture.ctx, `
|
|
UPDATE organizations
|
|
SET verification_level = 'e1', verification_basis = 'corporate_domain', verification_changed_at = $2
|
|
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())
|
|
if !errors.Is(staleTrustErr, outbound.ErrCompanyQuotaSnapshotChanged) {
|
|
t.Fatalf("reserve with stale organization trust error = %v, want snapshot mismatch", staleTrustErr)
|
|
}
|
|
fixture.assertQuotaRowCounts(t, organization.ID(), 0, 0)
|
|
|
|
policyOrganization, _ := fixture.newOrganization(t, "policy-check", domain.OrganizationVerificationE0)
|
|
stalePolicyQuota := fixture.quota(t, policyOrganization, fixture.version)
|
|
publisher := policy.NewPublisher(fixture.policies, fixture.clock.Now)
|
|
_, err = publisher.Publish(fixture.ctx, policy.PublishInput{
|
|
Key: domain.ProductPolicyKey, Kind: domain.PolicyKindDemo, Author: "system:quota-test",
|
|
EffectiveAt: fixture.clock.Now(), Policy: domain.DemoProductPolicyV1(),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("publish next effective product policy: %v", err)
|
|
}
|
|
stalePolicyErr := fixture.reserveWithSnapshot(policyOrganization, stalePolicyQuota, uuid.New())
|
|
if !errors.Is(stalePolicyErr, outbound.ErrCompanyQuotaSnapshotChanged) {
|
|
t.Fatalf("reserve with stale policy error = %v, want snapshot mismatch", stalePolicyErr)
|
|
}
|
|
fixture.assertQuotaRowCounts(t, policyOrganization.ID(), 0, 0)
|
|
})
|
|
|
|
t.Run("inactive E2 and E3 fail closed before recording usage", func(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
level domain.OrganizationVerificationLevel
|
|
basis domain.OrganizationVerificationBasis
|
|
}{
|
|
{name: "inactive E2", level: domain.OrganizationVerificationE2, basis: domain.OrganizationBasisRequisitesAndAuthority},
|
|
{name: "E3", level: domain.OrganizationVerificationE3, basis: domain.OrganizationBasisManualReview},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
organization, _ := fixture.newOrganization(t, "denied-"+string(test.level), test.level)
|
|
_, err := fixture.reserveInvitation(organization, uuid.New())
|
|
if !errors.Is(err, domain.ErrCompanyQuotaDenied) {
|
|
t.Fatalf("ReserveInvitation(%s) error = %v, want fail-closed denial", test.level, err)
|
|
}
|
|
fixture.assertQuotaRowCounts(t, organization.ID(), 0, 0)
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) newOrganization(
|
|
t *testing.T,
|
|
name string,
|
|
level domain.OrganizationVerificationLevel,
|
|
) (domain.Organization, domain.User) {
|
|
t.Helper()
|
|
owner := fixture.organizations.employer(t, fmt.Sprintf("quota-%s-%s@example.test", name, uuid.NewString()))
|
|
organization := fixture.organizations.create(t, owner, mustOrganizationProfile(t, domain.OrganizationProfileInput{Name: "Quota " + name}), fixture.clock.Now().Add(-time.Minute))
|
|
if level != domain.OrganizationVerificationE0 {
|
|
basis := domain.OrganizationBasisCorporateDomain
|
|
switch level {
|
|
case domain.OrganizationVerificationE2:
|
|
basis = domain.OrganizationBasisRequisitesAndAuthority
|
|
case domain.OrganizationVerificationE3:
|
|
basis = domain.OrganizationBasisManualReview
|
|
}
|
|
if _, err := fixture.pool.Exec(fixture.ctx, `
|
|
UPDATE organizations
|
|
SET verification_level = $2, verification_basis = $3, verification_changed_at = $4
|
|
WHERE id = $1`, organization.ID(), string(level), string(basis), fixture.clock.Now().Add(-time.Minute)); err != nil {
|
|
t.Fatalf("set %s verification fixture: %v", level, err)
|
|
}
|
|
}
|
|
stored, _, err := fixture.organizations.read(owner.ID, organization.ID())
|
|
if err != nil {
|
|
t.Fatalf("read %s organization: %v", level, err)
|
|
}
|
|
return stored, owner
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) newCandidateID(t *testing.T) uuid.UUID {
|
|
t.Helper()
|
|
candidate := fixture.organizations.identity.createUser(
|
|
t,
|
|
fmt.Sprintf("quota-candidate-%s@example.test", uuid.NewString()),
|
|
domain.ScenarioCandidate,
|
|
fixture.clock.Now().Add(-time.Hour),
|
|
)
|
|
return candidate.ID
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) quota(t *testing.T, organization domain.Organization, version domain.PolicyVersion) domain.CompanyQuota {
|
|
t.Helper()
|
|
quota, err := domain.NewCompanyQuota(organization, version, fixture.clock.Now())
|
|
if err != nil {
|
|
t.Fatalf("NewCompanyQuota() error = %v", err)
|
|
}
|
|
return quota
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) reserveInvitation(organization domain.Organization, invitationID uuid.UUID) (outbound.CompanyQuotaDecision, error) {
|
|
var decision outbound.CompanyQuotaDecision
|
|
err := fixture.transactor.WithinTransaction(fixture.ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
var err error
|
|
decision, err = fixture.service.ReserveInvitation(ctx, unit, organization, invitationID)
|
|
return err
|
|
})
|
|
return decision, err
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) reserveCardView(organization domain.Organization, candidateID uuid.UUID) (outbound.CompanyQuotaDecision, error) {
|
|
var decision outbound.CompanyQuotaDecision
|
|
err := fixture.transactor.WithinTransaction(fixture.ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
var err error
|
|
decision, err = fixture.service.ReserveCardView(ctx, unit, organization, candidateID)
|
|
return err
|
|
})
|
|
return decision, err
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) reserveWithSnapshot(organization domain.Organization, quota domain.CompanyQuota, invitationID uuid.UUID) error {
|
|
return fixture.transactor.WithinTransaction(fixture.ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
_, err := unit.CompanyQuotas().ReserveInvitation(ctx, quota, invitationID)
|
|
return err
|
|
})
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) reserveInvitationAllowed(t *testing.T, organization domain.Organization, invitationID uuid.UUID) {
|
|
t.Helper()
|
|
decision, err := fixture.reserveInvitation(organization, invitationID)
|
|
if err != nil || !decision.Allowed || decision.Reused || !decision.RetryAt.IsZero() {
|
|
t.Fatalf("ReserveInvitation() = %+v, %v; want a new allowed reservation", decision, err)
|
|
}
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) reserveCardViewAllowed(t *testing.T, organization domain.Organization, candidateID uuid.UUID) {
|
|
t.Helper()
|
|
decision, err := fixture.reserveCardView(organization, candidateID)
|
|
if err != nil || !decision.Allowed || decision.Reused || !decision.RetryAt.IsZero() {
|
|
t.Fatalf("ReserveCardView() = %+v, %v; want a new allowed reservation", decision, err)
|
|
}
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) reserveCardViewReused(t *testing.T, organization domain.Organization, candidateID uuid.UUID) {
|
|
t.Helper()
|
|
decision, err := fixture.reserveCardView(organization, candidateID)
|
|
if err != nil || !decision.Allowed || !decision.Reused || !decision.RetryAt.IsZero() {
|
|
t.Fatalf("ReserveCardView(repeat) = %+v, %v; want an allowed reuse", decision, err)
|
|
}
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) assertQuotaRowCounts(t *testing.T, organizationID uuid.UUID, invitations, views int64) {
|
|
t.Helper()
|
|
var gotInvitations, gotViews int64
|
|
if err := fixture.pool.QueryRow(fixture.ctx, `
|
|
SELECT count(*) FROM company_invitation_quota_usage WHERE organization_id = $1`, organizationID).Scan(&gotInvitations); err != nil {
|
|
t.Fatalf("count invitation quota rows: %v", err)
|
|
}
|
|
if err := fixture.pool.QueryRow(fixture.ctx, `
|
|
SELECT count(*) FROM company_card_view_quota_usage WHERE organization_id = $1`, organizationID).Scan(&gotViews); err != nil {
|
|
t.Fatalf("count card-view quota rows: %v", err)
|
|
}
|
|
if gotInvitations != invitations || gotViews != views {
|
|
t.Fatalf("quota rows for organization = invitations %d, views %d; want %d/%d", gotInvitations, gotViews, invitations, views)
|
|
}
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) assertActiveQuotaCounts(t *testing.T, organizationID uuid.UUID, at time.Time, invitations, views int64) {
|
|
t.Helper()
|
|
var gotInvitations, gotViews int64
|
|
if err := fixture.pool.QueryRow(fixture.ctx, `
|
|
SELECT count(*) FROM company_invitation_quota_usage
|
|
WHERE organization_id = $1 AND reserved_at > $2`, organizationID, at.Add(-7*24*time.Hour)).Scan(&gotInvitations); err != nil {
|
|
t.Fatalf("count active invitation quota rows: %v", err)
|
|
}
|
|
if err := fixture.pool.QueryRow(fixture.ctx, `
|
|
SELECT count(*) FROM company_card_view_quota_usage
|
|
WHERE organization_id = $1 AND last_seen_at > $2`, organizationID, at.Add(-24*time.Hour)).Scan(&gotViews); err != nil {
|
|
t.Fatalf("count active card-view quota rows: %v", err)
|
|
}
|
|
if gotInvitations != invitations || gotViews != views {
|
|
t.Fatalf("active quota rows at %s = invitations %d, views %d; want %d/%d", at, gotInvitations, gotViews, invitations, views)
|
|
}
|
|
}
|
|
|
|
func (fixture companyQuotaFixture) assertStoredEvidence(
|
|
t *testing.T,
|
|
organizationID uuid.UUID,
|
|
level domain.OrganizationVerificationLevel,
|
|
basis domain.OrganizationVerificationBasis,
|
|
) {
|
|
t.Helper()
|
|
var invitationPolicy, viewPolicy uuid.UUID
|
|
var invitationLevel, viewLevel string
|
|
var invitationBasis, viewBasis string
|
|
if err := fixture.pool.QueryRow(fixture.ctx, `
|
|
SELECT policy_version_id, verification_level, verification_basis
|
|
FROM company_invitation_quota_usage WHERE organization_id = $1
|
|
ORDER BY reserved_at LIMIT 1`, organizationID).Scan(&invitationPolicy, &invitationLevel, &invitationBasis); err != nil {
|
|
t.Fatalf("read invitation quota evidence: %v", err)
|
|
}
|
|
if err := fixture.pool.QueryRow(fixture.ctx, `
|
|
SELECT policy_version_id, verification_level, verification_basis
|
|
FROM company_card_view_quota_usage WHERE organization_id = $1
|
|
ORDER BY last_seen_at LIMIT 1`, organizationID).Scan(&viewPolicy, &viewLevel, &viewBasis); err != nil {
|
|
t.Fatalf("read card-view quota evidence: %v", err)
|
|
}
|
|
if invitationPolicy != fixture.version.ID().UUID() || viewPolicy != fixture.version.ID().UUID() ||
|
|
invitationLevel != string(level) || viewLevel != string(level) ||
|
|
invitationBasis != string(basis) || viewBasis != string(basis) {
|
|
t.Fatalf("stored quota evidence = invitation %s/%s/%s view %s/%s/%s, want policy %s and %s/%s",
|
|
invitationPolicy, invitationLevel, invitationBasis, viewPolicy, viewLevel, viewBasis, fixture.version.ID(), level, basis)
|
|
}
|
|
}
|