147 lines
7.1 KiB
Go
147 lines
7.1 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestNewCompanyQuotaUsesStoredVerificationAndEffectivePolicy(t *testing.T) {
|
|
at := time.Date(2026, 10, 7, 12, 30, 0, 0, time.FixedZone("owner", 3*60*60))
|
|
createdAt := at.Add(-30 * 24 * time.Hour).UTC()
|
|
organization := companyQuotaTestOrganization(t, string(OrganizationVerificationE0), string(OrganizationBasisConfirmedEmail), createdAt, createdAt)
|
|
policy := companyQuotaTestPolicy(t, at.UTC().Add(-time.Hour), false)
|
|
|
|
quota, err := NewCompanyQuota(organization, policy, at)
|
|
if err != nil {
|
|
t.Fatalf("NewCompanyQuota() error = %v", err)
|
|
}
|
|
if quota.OrganizationID() != organization.ID() || quota.PolicyVersionID() != policy.ID() ||
|
|
quota.Tier() != OrganizationTierE0 || quota.VerificationLevel() != OrganizationVerificationE0 ||
|
|
quota.VerificationBasis() != OrganizationBasisConfirmedEmail || !quota.VerificationChangedAt().Equal(createdAt) {
|
|
t.Fatalf("quota identity = org %s, policy %s, tier %s, verification %s/%s at %s", quota.OrganizationID(), quota.PolicyVersionID(), quota.Tier(), quota.VerificationLevel(), quota.VerificationBasis(), quota.VerificationChangedAt())
|
|
}
|
|
if !quota.At().Equal(at.UTC()) || quota.At().Location() != time.UTC {
|
|
t.Fatalf("At() = %s, want %s in UTC", quota.At(), at.UTC())
|
|
}
|
|
if quota.InvitationLimit() != 10 || quota.CardViewLimit() != 50 ||
|
|
quota.InvitationWindow() != 7*24*time.Hour || quota.CardViewWindow() != 24*time.Hour {
|
|
t.Fatalf("quota limits/windows = %d/%s and %d/%s", quota.InvitationLimit(), quota.InvitationWindow(), quota.CardViewLimit(), quota.CardViewWindow())
|
|
}
|
|
if want := at.UTC().Add(-7 * 24 * time.Hour); !quota.InvitationWindowStart().Equal(want) {
|
|
t.Fatalf("InvitationWindowStart() = %s, want %s", quota.InvitationWindowStart(), want)
|
|
}
|
|
if want := at.UTC().Add(-24 * time.Hour); !quota.CardViewWindowStart().Equal(want) {
|
|
t.Fatalf("CardViewWindowStart() = %s, want %s", quota.CardViewWindowStart(), want)
|
|
}
|
|
}
|
|
|
|
func TestNewCompanyQuotaRequiresBothStoredE2VerificationAndActivePolicyTier(t *testing.T) {
|
|
at := time.Date(2026, 10, 7, 12, 30, 0, 0, time.UTC)
|
|
organization := companyQuotaTestOrganization(t, string(OrganizationVerificationE2), string(OrganizationBasisRequisitesAndAuthority), at.Add(-time.Hour), at.Add(-time.Hour))
|
|
|
|
if _, err := NewCompanyQuota(organization, companyQuotaTestPolicy(t, at.Add(-time.Hour), false), at); !errors.Is(err, ErrCompanyQuotaDenied) {
|
|
t.Fatalf("NewCompanyQuota(E2, inactive policy tier) error = %v, want ErrCompanyQuotaDenied", err)
|
|
}
|
|
|
|
quota, err := NewCompanyQuota(organization, companyQuotaTestPolicy(t, at.Add(-time.Hour), true), at)
|
|
if err != nil {
|
|
t.Fatalf("NewCompanyQuota(E2, active policy tier) error = %v", err)
|
|
}
|
|
if quota.Tier() != OrganizationTierE2 || quota.InvitationLimit() != 100 || quota.CardViewLimit() != 400 {
|
|
t.Fatalf("E2 quota = tier %s, limits %d/%d", quota.Tier(), quota.InvitationLimit(), quota.CardViewLimit())
|
|
}
|
|
}
|
|
|
|
func TestNewCompanyQuotaDeniesE3WithoutFallingBackToE0(t *testing.T) {
|
|
at := time.Date(2026, 10, 7, 12, 30, 0, 0, time.UTC)
|
|
organization := companyQuotaTestOrganization(t, string(OrganizationVerificationE3), string(OrganizationBasisManualReview), at.Add(-time.Hour), at.Add(-time.Hour))
|
|
|
|
if _, err := NewCompanyQuota(organization, companyQuotaTestPolicy(t, at.Add(-time.Hour), true), at); !errors.Is(err, ErrCompanyQuotaDenied) {
|
|
t.Fatalf("NewCompanyQuota(E3) error = %v, want ErrCompanyQuotaDenied", err)
|
|
}
|
|
}
|
|
|
|
func TestNewCompanyQuotaRejectsUntrustedOrBackdatedInputs(t *testing.T) {
|
|
at := time.Date(2026, 10, 7, 12, 30, 0, 0, time.UTC)
|
|
organization := companyQuotaTestOrganization(t, string(OrganizationVerificationE0), string(OrganizationBasisConfirmedEmail), at.Add(-time.Hour), at.Add(-time.Hour))
|
|
policy := companyQuotaTestPolicy(t, at.Add(-time.Hour), false)
|
|
futurePolicy := companyQuotaTestPolicy(t, at.Add(time.Hour), false)
|
|
beforeEpoch := time.Date(1969, 12, 31, 23, 59, 59, 0, time.UTC)
|
|
createdInFuture := companyQuotaTestOrganization(t, string(OrganizationVerificationE0), string(OrganizationBasisConfirmedEmail), at.Add(time.Hour), at.Add(time.Hour))
|
|
verifiedInFuture := companyQuotaTestOrganization(t, string(OrganizationVerificationE1), string(OrganizationBasisCorporateDomain), at.Add(-time.Hour), at.Add(time.Hour))
|
|
|
|
tests := []struct {
|
|
name string
|
|
organization Organization
|
|
policy PolicyVersion
|
|
at time.Time
|
|
want error
|
|
}{
|
|
{name: "zero organization", organization: Organization{}, policy: policy, at: at, want: ErrInvalidCompanyQuota},
|
|
{name: "zero policy", organization: organization, policy: PolicyVersion{}, at: at, want: ErrInvalidCompanyQuota},
|
|
{name: "zero application time", organization: organization, policy: policy, want: ErrInvalidCompanyQuota},
|
|
{name: "negative application time", organization: organization, policy: policy, at: beforeEpoch, want: ErrInvalidCompanyQuota},
|
|
{name: "organization created after request time", organization: createdInFuture, policy: policy, at: at, want: ErrInvalidCompanyQuota},
|
|
{name: "verification changed after request time", organization: verifiedInFuture, policy: policy, at: at, want: ErrInvalidCompanyQuota},
|
|
{name: "policy effective after request time", organization: organization, policy: futurePolicy, at: at, want: ErrInvalidCompanyQuota},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if _, err := NewCompanyQuota(test.organization, test.policy, test.at); !errors.Is(err, test.want) {
|
|
t.Fatalf("NewCompanyQuota() error = %v, want %v", err, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func companyQuotaTestOrganization(t *testing.T, level, basis string, createdAt, changedAt time.Time) Organization {
|
|
t.Helper()
|
|
updatedAt := createdAt
|
|
if changedAt.After(updatedAt) {
|
|
updatedAt = changedAt
|
|
}
|
|
organization, err := RestoreOrganization(StoredOrganization{
|
|
ID: uuid.New(),
|
|
Name: "Example Company",
|
|
VerificationLevel: level,
|
|
VerificationBasis: basis,
|
|
VerificationChangedAt: changedAt,
|
|
Version: 1,
|
|
CreatedAt: createdAt,
|
|
UpdatedAt: updatedAt,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("RestoreOrganization() error = %v", err)
|
|
}
|
|
return organization
|
|
}
|
|
|
|
func companyQuotaTestPolicy(t *testing.T, effectiveAt time.Time, activateE2 bool) PolicyVersion {
|
|
t.Helper()
|
|
policy := DemoProductPolicyV1()
|
|
policy.OrganizationQuotas.TierE2.Active = activateE2
|
|
draft, err := NewPolicyVersionDraft(NewPolicyVersionDraftInput{
|
|
Key: ProductPolicyKey, Kind: PolicyKindDemo, Author: DemoPolicySeedAuthor, Policy: policy,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewPolicyVersionDraft() error = %v", err)
|
|
}
|
|
numbered, err := draft.WithVersion(1)
|
|
if err != nil {
|
|
t.Fatalf("WithVersion() error = %v", err)
|
|
}
|
|
hash := numbered.DocumentHash()
|
|
version, err := RestorePolicyVersion(StoredPolicyVersion{
|
|
ID: numbered.ID(), Key: string(numbered.Key()), Version: numbered.Version(), Kind: string(numbered.Kind()),
|
|
Author: string(numbered.Author()), EffectiveAt: effectiveAt, CreatedAt: effectiveAt,
|
|
Document: numbered.Document(), DocumentHash: hash[:],
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("RestorePolicyVersion() error = %v", err)
|
|
}
|
|
return version
|
|
}
|