feat(invitations): check contact grants on every read, cap pending invitations, and expire them at the deadline
This commit is contained in:
1 parent
676b3c6e24
commit
12a7213d4a
67 files changed
+4255
-149
No files matched your search
@@ -0,0 +1,172 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestNewContactDisclosureReadsOnlyTheGrantedConfirmedKinds(t *testing.T) {
|
||||
now := time.Date(2026, time.October, 9, 10, 0, 0, 0, time.UTC)
|
||||
candidateID, organizationID, invitationID := uuid.Must(uuid.NewV7()), uuid.Must(uuid.NewV7()), uuid.Must(uuid.NewV7())
|
||||
profile := contactTestProfile(t, candidateID, now, map[string]ProfileClaimInput[string]{
|
||||
"full_name": {Value: "Test Candidate", Status: ProfileClaimConfirmed},
|
||||
"phone": {Value: "+70000000000", Status: ProfileClaimConfirmed},
|
||||
"telegram": {Value: "@candidate_test", Status: ProfileClaimConfirmed},
|
||||
})
|
||||
grant := contactTestGrant(t, candidateID, organizationID, invitationID, now, "email", "phone", "telegram")
|
||||
|
||||
disclosure, err := NewContactDisclosure(grant, profile)
|
||||
if err != nil {
|
||||
t.Fatalf("NewContactDisclosure() error = %v", err)
|
||||
}
|
||||
if disclosure.InvitationID() != invitationID || disclosure.GrantID() != grant.ID() || !disclosure.GrantedAt().Equal(now) {
|
||||
t.Fatalf("disclosure identity = %v %v %v, want the grant's", disclosure.InvitationID(), disclosure.GrantID(), disclosure.GrantedAt())
|
||||
}
|
||||
if !equalContactFields(disclosure.Fields(), ContactFieldEmail, ContactFieldPhone, ContactFieldTelegram) {
|
||||
t.Fatalf("fields = %v, want the grant's three kinds", disclosure.Fields())
|
||||
}
|
||||
// The profile holds no email, so the granted email has no value.
|
||||
if value, ok := disclosure.Email(); ok || value != "" {
|
||||
t.Fatalf("email = %q, %v; want no value for a kind the profile does not hold", value, ok)
|
||||
}
|
||||
if value, ok := disclosure.Phone(); !ok || value != "+70000000000" {
|
||||
t.Fatalf("phone = %q, %v; want the confirmed number", value, ok)
|
||||
}
|
||||
if value, ok := disclosure.Telegram(); !ok || value != "@candidate_test" {
|
||||
t.Fatalf("telegram = %q, %v; want the confirmed name", value, ok)
|
||||
}
|
||||
// The full name is held but not granted, so it stays undisclosed.
|
||||
if value, ok := disclosure.FullName(); ok || value != "" {
|
||||
t.Fatalf("full name = %q, %v; want none because the grant does not cover it", value, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfirmedContactValueIgnoresUnconfirmedSuggestions(t *testing.T) {
|
||||
source, err := RestoreProfileClaimSource(ProfileOriginImported, ProfileClaimTrustLevel, "", "extractor-1", time.Time{})
|
||||
if err != nil {
|
||||
t.Fatalf("RestoreProfileClaimSource() error = %v", err)
|
||||
}
|
||||
suggestion, err := RestoreProfileClaim("candidate@example.test", ProfileClaimExtracted, source)
|
||||
if err != nil {
|
||||
t.Fatalf("RestoreProfileClaim() error = %v", err)
|
||||
}
|
||||
if value := confirmedContactValue(suggestion, true); value != nil {
|
||||
t.Fatalf("confirmedContactValue(suggestion) = %q, want nil", *value)
|
||||
}
|
||||
if value := confirmedContactValue(suggestion, false); value != nil {
|
||||
t.Fatal("confirmedContactValue(missing claim) returned a value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewContactDisclosureRefusesAGrantThatIsNotActive(t *testing.T) {
|
||||
now := time.Date(2026, time.October, 9, 10, 0, 0, 0, time.UTC)
|
||||
candidateID, organizationID, invitationID := uuid.Must(uuid.NewV7()), uuid.Must(uuid.NewV7()), uuid.Must(uuid.NewV7())
|
||||
profile := contactTestProfile(t, candidateID, now, map[string]ProfileClaimInput[string]{
|
||||
"email": {Value: "candidate@example.test", Status: ProfileClaimConfirmed},
|
||||
})
|
||||
grant := contactTestGrant(t, candidateID, organizationID, invitationID, now, "email")
|
||||
stored := grant.Stored()
|
||||
revokedAt := now.Add(time.Hour)
|
||||
stored.RevokedAt, stored.RevocationReason = &revokedAt, string(ContactRevocationVisibilityClosed)
|
||||
revoked, err := RestoreContactGrant(stored)
|
||||
if err != nil {
|
||||
t.Fatalf("RestoreContactGrant() error = %v", err)
|
||||
}
|
||||
if _, err := NewContactDisclosure(revoked, profile); !errors.Is(err, ErrInvalidContactGrant) {
|
||||
t.Fatalf("NewContactDisclosure(revoked) error = %v, want ErrInvalidContactGrant", err)
|
||||
}
|
||||
if _, err := NewContactDisclosure(ContactGrant{}, profile); !errors.Is(err, ErrInvalidContactGrant) {
|
||||
t.Fatalf("NewContactDisclosure(zero) error = %v, want ErrInvalidContactGrant", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContactDisclosureRedactsItsValuesInFormatting(t *testing.T) {
|
||||
now := time.Date(2026, time.October, 9, 10, 0, 0, 0, time.UTC)
|
||||
candidateID, organizationID, invitationID := uuid.Must(uuid.NewV7()), uuid.Must(uuid.NewV7()), uuid.Must(uuid.NewV7())
|
||||
profile := contactTestProfile(t, candidateID, now, map[string]ProfileClaimInput[string]{
|
||||
"phone": {Value: "+70000000001", Status: ProfileClaimConfirmed},
|
||||
})
|
||||
disclosure, err := NewContactDisclosure(contactTestGrant(t, candidateID, organizationID, invitationID, now, "phone"), profile)
|
||||
if err != nil {
|
||||
t.Fatalf("NewContactDisclosure() error = %v", err)
|
||||
}
|
||||
for _, rendered := range []string{
|
||||
fmt.Sprint(disclosure), fmt.Sprintf("%+v", disclosure), fmt.Sprintf("%#v", disclosure),
|
||||
} {
|
||||
if strings.Contains(rendered, "+70000000001") || strings.Contains(rendered, candidateID.String()) {
|
||||
t.Fatalf("formatted disclosure leaks a value or the candidate: %q", rendered)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// contactTestGrant builds an active grant of the kinds, as the acceptance does.
|
||||
func contactTestGrant(t *testing.T, candidateID, organizationID, invitationID uuid.UUID, at time.Time, kinds ...string) ContactGrant {
|
||||
t.Helper()
|
||||
grant, err := RestoreContactGrant(StoredContactGrant{
|
||||
ID: uuid.Must(uuid.NewV7()), OrganizationID: organizationID, CandidateID: candidateID,
|
||||
InvitationID: invitationID, ConsentRecordID: uuid.Must(uuid.NewV7()), TermsVersion: 1,
|
||||
Fields: canonicalKinds(kinds...), ConsentTextVersion: "c4-2026-10-01", GrantedAt: at,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("RestoreContactGrant() error = %v", err)
|
||||
}
|
||||
return grant
|
||||
}
|
||||
|
||||
// contactTestProfile builds a profile whose claims have the given statuses.
|
||||
func contactTestProfile(t *testing.T, candidateID uuid.UUID, at time.Time, claims map[string]ProfileClaimInput[string]) CandidateProfile {
|
||||
t.Helper()
|
||||
var input CandidateProfileInput
|
||||
for kind, claim := range claims {
|
||||
switch kind {
|
||||
case "full_name":
|
||||
input.FullName = &claim
|
||||
case "email":
|
||||
input.Email = &claim
|
||||
case "phone":
|
||||
input.Phone = &claim
|
||||
case "telegram":
|
||||
input.Telegram = &claim
|
||||
}
|
||||
}
|
||||
empty, err := EmptyCandidateProfile(candidateID)
|
||||
if err != nil {
|
||||
t.Fatalf("EmptyCandidateProfile() error = %v", err)
|
||||
}
|
||||
profile, _, err := empty.Replace(input, 0, at)
|
||||
if err != nil {
|
||||
t.Fatalf("Replace() error = %v", err)
|
||||
}
|
||||
return profile
|
||||
}
|
||||
|
||||
// canonicalKinds returns the kinds in the order of the stored form.
|
||||
func canonicalKinds(kinds ...string) []string {
|
||||
order := []string{"full_name", "email", "phone", "telegram"}
|
||||
var canonical []string
|
||||
for _, kind := range order {
|
||||
for _, given := range kinds {
|
||||
if given == kind {
|
||||
canonical = append(canonical, kind)
|
||||
}
|
||||
}
|
||||
}
|
||||
return canonical
|
||||
}
|
||||
|
||||
func equalContactFields(got []ContactField, want ...ContactField) bool {
|
||||
if len(got) != len(want) {
|
||||
return false
|
||||
}
|
||||
for index := range got {
|
||||
if got[index] != want[index] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in new issue
Block a user