243 lines
11 KiB
Go
243 lines
11 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const testContactTextBody = "I consent to the transfer of the contact data I select to {organization} for this invitation."
|
|
|
|
// testContactText is the published C4 text of the domain tests.
|
|
func testContactText(t *testing.T) ConsentText {
|
|
t.Helper()
|
|
hash := HashConsentText(testContactTextBody)
|
|
text, err := RestoreConsentText(StoredConsentText{
|
|
Purpose: string(ConsentPurposeContactDisclosure), Version: "c4-2026-10-01", Body: testContactTextBody,
|
|
Hash: hash[:], Kind: string(ConsentTextKindDemo), PublishedAt: time.Date(2026, time.October, 1, 0, 0, 0, 0, time.UTC),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("RestoreConsentText() error = %v", err)
|
|
}
|
|
return text
|
|
}
|
|
|
|
// acceptedTestInvitation accepts the delivered test invitation an hour after
|
|
// its delivery, from its delivered state and at its current terms version.
|
|
func acceptedTestInvitation(t *testing.T) (Invitation, time.Time) {
|
|
t.Helper()
|
|
delivered, _ := deliveredTestInvitation(t)
|
|
at := delivered.UpdatedAt().Add(time.Hour)
|
|
accepted, err := delivered.Accept(delivered.TermsVersion(), at)
|
|
if err != nil {
|
|
t.Fatalf("Accept() error = %v", err)
|
|
}
|
|
return accepted, at
|
|
}
|
|
|
|
// testConsentRecordFor stores the C4 record of an acceptance the way the ledger
|
|
// does: the hash of the text with the organization name filled in.
|
|
func testStoredConsentRecord(t *testing.T, invitation Invitation, granted time.Time, fields ...string) StoredConsentRecord {
|
|
t.Helper()
|
|
shown, err := testContactText(t).RenderForOrganization("Acme")
|
|
if err != nil {
|
|
t.Fatalf("RenderForOrganization() error = %v", err)
|
|
}
|
|
hash := HashConsentText(shown)
|
|
return StoredConsentRecord{
|
|
ID: uuid.Must(uuid.NewV7()), SubjectID: invitation.CandidateID(), Purpose: string(ConsentPurposeContactDisclosure),
|
|
TextVersion: "c4-2026-10-01", TextHash: hash[:], Source: string(ConsentSourceInvitationAcceptance),
|
|
State: string(ConsentStateActive), GrantedAt: granted, OrganizationID: invitation.OrganizationID(),
|
|
OrganizationName: "Acme", FieldSet: fields, InvitationID: invitation.ID(), InvitationTermsVersion: invitation.TermsVersion(),
|
|
}
|
|
}
|
|
|
|
// testConsentRecordFor restores the stored C4 record of an acceptance.
|
|
func testConsentRecordFor(t *testing.T, invitation Invitation, granted time.Time, fields ...string) ConsentRecord {
|
|
t.Helper()
|
|
record, err := RestoreConsentRecord(testStoredConsentRecord(t, invitation, granted, fields...))
|
|
if err != nil {
|
|
t.Fatalf("RestoreConsentRecord() error = %v", err)
|
|
}
|
|
return record
|
|
}
|
|
|
|
func TestNewContactGrantCoversExactlyTheRecordedFields(t *testing.T) {
|
|
accepted, at := acceptedTestInvitation(t)
|
|
record := testConsentRecordFor(t, accepted, at, "email")
|
|
grant, err := NewContactGrant(record, accepted)
|
|
if err != nil {
|
|
t.Fatalf("NewContactGrant() error = %v", err)
|
|
}
|
|
if grant.OrganizationID() != accepted.OrganizationID() || grant.InvitationID() != accepted.ID() ||
|
|
grant.CandidateID() != accepted.CandidateID() || grant.ConsentRecordID() != record.ID() ||
|
|
grant.TermsVersion() != accepted.TermsVersion() || grant.ConsentTextVersion() != "c4-2026-10-01" ||
|
|
!grant.GrantedAt().Equal(at) || grant.Status() != ContactGrantActive {
|
|
t.Fatalf("grant = %+v, want the scope of the accepted invitation and its record", grant.Stored())
|
|
}
|
|
if fields := grant.Fields().Fields(); len(fields) != 1 || fields[0] != ContactFieldEmail {
|
|
t.Fatalf("fields = %v, want the email only", fields)
|
|
}
|
|
if _, revoked := grant.RevokedAt(); revoked {
|
|
t.Fatal("a new grant is revoked")
|
|
}
|
|
restored, err := RestoreContactGrant(grant.Stored())
|
|
if err != nil || restored.ID() != grant.ID() || !restored.Fields().Equal(grant.Fields()) {
|
|
t.Fatalf("RestoreContactGrant(stored) = %+v, %v; want the same grant", restored.Stored(), err)
|
|
}
|
|
}
|
|
|
|
func TestNewContactGrantNeedsAnAcceptedInvitationAndItsOwnRecord(t *testing.T) {
|
|
accepted, at := acceptedTestInvitation(t)
|
|
delivered, _ := deliveredTestInvitation(t)
|
|
|
|
if _, err := NewContactGrant(testConsentRecordFor(t, delivered, at, "email"), delivered); !errors.Is(err, ErrInvalidContactGrant) {
|
|
t.Fatalf("grant of a delivered invitation error = %v, want ErrInvalidContactGrant", err)
|
|
}
|
|
otherInvitation := testConsentRecordFor(t, delivered, at, "email")
|
|
if _, err := NewContactGrant(otherInvitation, accepted); !errors.Is(err, ErrInvalidContactGrant) {
|
|
t.Fatalf("grant with another invitation's record error = %v, want ErrInvalidContactGrant", err)
|
|
}
|
|
stored := testStoredConsentRecord(t, accepted, at, "email")
|
|
stored.RevokedAt = &at
|
|
stored.RevocationSource = string(ConsentRevocationSourceConsentSettings)
|
|
stored.State = string(ConsentStateRevoked)
|
|
revoked, err := RestoreConsentRecord(stored)
|
|
if err != nil {
|
|
t.Fatalf("RestoreConsentRecord(revoked) error = %v", err)
|
|
}
|
|
if _, err := NewContactGrant(revoked, accepted); !errors.Is(err, ErrInvalidContactGrant) {
|
|
t.Fatalf("grant of a revoked record error = %v, want ErrInvalidContactGrant", err)
|
|
}
|
|
}
|
|
|
|
func TestContactGrantShowsWhetherTheOrganizationCanReadItNow(t *testing.T) {
|
|
accepted, at := acceptedTestInvitation(t)
|
|
grant, err := NewContactGrant(testConsentRecordFor(t, accepted, at, "email"), accepted)
|
|
if err != nil {
|
|
t.Fatalf("NewContactGrant() error = %v", err)
|
|
}
|
|
if view := grant.CandidateView(true); !view.Readable || view.BlockedBy != nil || view.Status != ContactGrantActive {
|
|
t.Fatalf("visible view = %+v, want readable and not blocked", view)
|
|
}
|
|
view := grant.CandidateView(false)
|
|
if view.Readable || view.BlockedBy == nil || *view.BlockedBy != ContactAccessBlockVisibility {
|
|
t.Fatalf("hidden view = %+v, want blocked by visibility", view)
|
|
}
|
|
|
|
revokedAt := at.Add(time.Hour)
|
|
revoked, err := RestoreContactGrant(StoredContactGrant{
|
|
ID: grant.ID(), OrganizationID: grant.OrganizationID(), CandidateID: grant.CandidateID(), InvitationID: grant.InvitationID(),
|
|
ConsentRecordID: grant.ConsentRecordID(), TermsVersion: grant.TermsVersion(), Fields: []string{"email"},
|
|
ConsentTextVersion: "c4-2026-10-01", GrantedAt: at, RevokedAt: &revokedAt, RevocationReason: string(ContactRevocationConsentRevoked),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("RestoreContactGrant(revoked) error = %v", err)
|
|
}
|
|
closed := revoked.CandidateView(true)
|
|
if closed.Readable || closed.BlockedBy != nil || closed.Status != ContactGrantRevoked || closed.RevokedAt == nil ||
|
|
!closed.RevokedAt.Equal(revokedAt) || closed.RevocationReason == nil || *closed.RevocationReason != ContactRevocationConsentRevoked {
|
|
t.Fatalf("revoked view = %+v, want closed with its reason and no read", closed)
|
|
}
|
|
}
|
|
|
|
func TestRestoreContactGrantRefusesNonCanonicalFieldsAndBrokenRevocation(t *testing.T) {
|
|
accepted, at := acceptedTestInvitation(t)
|
|
valid := StoredContactGrant{
|
|
ID: uuid.Must(uuid.NewV7()), OrganizationID: accepted.OrganizationID(), CandidateID: accepted.CandidateID(),
|
|
InvitationID: accepted.ID(), ConsentRecordID: uuid.Must(uuid.NewV7()), TermsVersion: accepted.TermsVersion(),
|
|
Fields: []string{"email", "phone"}, ConsentTextVersion: "c4-2026-10-01", GrantedAt: at,
|
|
}
|
|
if _, err := RestoreContactGrant(valid); err != nil {
|
|
t.Fatalf("RestoreContactGrant(valid) error = %v", err)
|
|
}
|
|
reversed := valid
|
|
reversed.Fields = []string{"phone", "email"}
|
|
beforeGrant := valid
|
|
beforeGrant.RevokedAt = ptrTime(at.Add(-time.Minute))
|
|
beforeGrant.RevocationReason = string(ContactRevocationVisibilityClosed)
|
|
noReason := valid
|
|
noReason.RevokedAt = ptrTime(at.Add(time.Minute))
|
|
unknownKind := valid
|
|
unknownKind.Fields = []string{"address"}
|
|
for name, stored := range map[string]StoredContactGrant{
|
|
"reversed fields": reversed,
|
|
"unknown kind": unknownKind,
|
|
"revocation before grant": beforeGrant,
|
|
"revocation without a reason": noReason,
|
|
} {
|
|
if _, err := RestoreContactGrant(stored); !errors.Is(err, ErrInvalidContactGrant) && !errors.Is(err, ErrInvalidConsent) {
|
|
t.Errorf("%s: RestoreContactGrant() error = %v, want a refusal", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func ptrTime(value time.Time) *time.Time { return &value }
|
|
|
|
func TestInvitationAcceptsOnlyTheCurrentTermsFromAnAnswerableState(t *testing.T) {
|
|
delivered, _ := deliveredTestInvitation(t)
|
|
at := delivered.UpdatedAt().Add(time.Hour)
|
|
if _, err := delivered.Accept(delivered.TermsVersion()+1, at); !errors.Is(err, ErrInvitationVersionConflict) {
|
|
t.Fatalf("stale terms error = %v, want ErrInvitationVersionConflict", err)
|
|
}
|
|
counter := withState(t, delivered, InvitationCounter, nil)
|
|
if _, err := counter.Accept(counter.TermsVersion(), at); !errors.Is(err, ErrInvitationNotAnswerable) {
|
|
t.Fatalf("counter error = %v, want ErrInvitationNotAnswerable", err)
|
|
}
|
|
if _, err := delivered.Accept(delivered.TermsVersion(), *delivered.Snapshot().ResponseDeadlineAt); !errors.Is(err, ErrInvitationNotAnswerable) {
|
|
t.Fatalf("accept at the deadline error = %v, want ErrInvitationNotAnswerable", err)
|
|
}
|
|
viewed, _, err := delivered.RecordView(at)
|
|
if err != nil {
|
|
t.Fatalf("RecordView() error = %v", err)
|
|
}
|
|
accepted, err := viewed.Accept(viewed.TermsVersion(), at.Add(time.Minute))
|
|
if err != nil {
|
|
t.Fatalf("Accept(viewed) error = %v", err)
|
|
}
|
|
snapshot := accepted.Snapshot()
|
|
if snapshot.State != InvitationAccepted || snapshot.Version != viewed.Version()+1 || snapshot.RespondedAt == nil ||
|
|
!snapshot.ResponseDeadlineAt.Equal(*delivered.Snapshot().ResponseDeadlineAt) {
|
|
t.Fatalf("accepted = %+v, want the next version accepted with the deadline kept", snapshot)
|
|
}
|
|
event, err := accepted.AnswerEvent(delivered.CandidateID())
|
|
if err != nil || event.Kind != InvitationEventAccepted || len(event.ChangedFields) != 0 {
|
|
t.Fatalf("AnswerEvent() = %+v, %v; want an accepted event naming no field", event, err)
|
|
}
|
|
}
|
|
|
|
func TestNewContactConsentOfferRendersTheTextAndListsTheHeldKinds(t *testing.T) {
|
|
offer, err := NewContactConsentOffer(testContactText(t), "Acme", []ContactField{ContactFieldFullName, ContactFieldEmail})
|
|
if err != nil {
|
|
t.Fatalf("NewContactConsentOffer() error = %v", err)
|
|
}
|
|
if !strings.Contains(offer.Text.Text, "Acme") || strings.Contains(offer.Text.Text, ConsentTextOrganizationPlaceholder) ||
|
|
offer.Text.TextVersion != "c4-2026-10-01" || !slices.Equal(offer.AvailableFields, []ContactField{ContactFieldFullName, ContactFieldEmail}) {
|
|
t.Fatalf("offer = %+v, want the rendered text and the kinds in the given order", offer)
|
|
}
|
|
if _, err := NewContactConsentOffer(testContactText(t), "Acme", nil); !errors.Is(err, ErrInvalidContactGrant) {
|
|
t.Fatalf("empty offer error = %v, want ErrInvalidContactGrant", err)
|
|
}
|
|
if _, err := NewContactConsentOffer(testContactText(t), "Acme", []ContactField{ContactFieldEmail, ContactFieldEmail}); !errors.Is(err, ErrInvalidContactGrant) {
|
|
t.Fatalf("repeated kind error = %v, want ErrInvalidContactGrant", err)
|
|
}
|
|
}
|
|
|
|
func TestContactGrantValuesAreRedacted(t *testing.T) {
|
|
accepted, at := acceptedTestInvitation(t)
|
|
grant, err := NewContactGrant(testConsentRecordFor(t, accepted, at, "email"), accepted)
|
|
if err != nil {
|
|
t.Fatalf("NewContactGrant() error = %v", err)
|
|
}
|
|
for _, rendered := range []string{fmt.Sprint(grant), fmt.Sprintf("%+v", grant), fmt.Sprintf("%#v", grant)} {
|
|
if strings.Contains(rendered, accepted.CandidateID().String()) || strings.Contains(rendered, "email") {
|
|
t.Fatalf("rendered grant = %q, want the identifier and the kinds hidden", rendered)
|
|
}
|
|
}
|
|
}
|