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,112 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const redactedContactDisclosure = "[redacted contact disclosure]"
|
||||
|
||||
// ContactDisclosure holds the contact values that one active grant allows an
|
||||
// organization to read now. It is the only domain value that carries contact
|
||||
// values, so it redacts itself in fmt and slog output. It names no candidate
|
||||
// and no organization: the caller already knows both from its own request.
|
||||
type ContactDisclosure struct {
|
||||
invitationID uuid.UUID
|
||||
grantID uuid.UUID
|
||||
fields []ContactField
|
||||
grantedAt time.Time
|
||||
fullName *string
|
||||
email *string
|
||||
phone *string
|
||||
telegram *string
|
||||
}
|
||||
|
||||
// NewContactDisclosure builds the values of an active grant from the profile.
|
||||
// Only the kinds of the grant are read, and a kind yields its value only when
|
||||
// the profile holds it as a confirmed claim, so a suggestion that nobody
|
||||
// confirmed is never disclosed. A kind the profile no longer holds stays nil.
|
||||
func NewContactDisclosure(grant ContactGrant, profile CandidateProfile) (ContactDisclosure, error) {
|
||||
const location = "domain.NewContactDisclosure"
|
||||
if grant.IsZero() || grant.Status() != ContactGrantActive {
|
||||
return ContactDisclosure{}, fmt.Errorf("%s - only an active grant discloses contacts: %w", location, ErrInvalidContactGrant)
|
||||
}
|
||||
content := profile.Content()
|
||||
disclosure := ContactDisclosure{
|
||||
invitationID: grant.InvitationID(), grantID: grant.ID(),
|
||||
fields: grant.Fields().Fields(), grantedAt: grant.GrantedAt(),
|
||||
}
|
||||
for _, field := range disclosure.fields {
|
||||
switch field {
|
||||
case ContactFieldFullName:
|
||||
disclosure.fullName = confirmedContactValue(content.FullName())
|
||||
case ContactFieldEmail:
|
||||
disclosure.email = confirmedContactValue(content.Email())
|
||||
case ContactFieldPhone:
|
||||
disclosure.phone = confirmedContactValue(content.Phone())
|
||||
case ContactFieldTelegram:
|
||||
disclosure.telegram = confirmedContactValue(content.Telegram())
|
||||
}
|
||||
}
|
||||
return disclosure, nil
|
||||
}
|
||||
|
||||
// confirmedContactValue returns the value of a claim the candidate confirmed,
|
||||
// and nil for a missing claim or an unconfirmed suggestion.
|
||||
func confirmedContactValue(claim ProfileClaim[string], present bool) *string {
|
||||
if !present || claim.Status() != ProfileClaimConfirmed {
|
||||
return nil
|
||||
}
|
||||
value := claim.Value()
|
||||
return &value
|
||||
}
|
||||
|
||||
// InvitationID is the invitation whose acceptance created the grant.
|
||||
func (disclosure ContactDisclosure) InvitationID() uuid.UUID { return disclosure.invitationID }
|
||||
|
||||
// GrantID is the grant that allowed the read.
|
||||
func (disclosure ContactDisclosure) GrantID() uuid.UUID { return disclosure.grantID }
|
||||
|
||||
// Fields are the kinds that the grant covers, in canonical order.
|
||||
func (disclosure ContactDisclosure) Fields() []ContactField { return slices.Clone(disclosure.fields) }
|
||||
|
||||
// GrantedAt is the time of the consent that the grant stores.
|
||||
func (disclosure ContactDisclosure) GrantedAt() time.Time { return disclosure.grantedAt }
|
||||
|
||||
// FullName returns the full name and whether it is disclosed.
|
||||
func (disclosure ContactDisclosure) FullName() (string, bool) {
|
||||
return valueOfContact(disclosure.fullName)
|
||||
}
|
||||
|
||||
// Email returns the email address and whether it is disclosed.
|
||||
func (disclosure ContactDisclosure) Email() (string, bool) { return valueOfContact(disclosure.email) }
|
||||
|
||||
// Phone returns the phone number and whether it is disclosed.
|
||||
func (disclosure ContactDisclosure) Phone() (string, bool) { return valueOfContact(disclosure.phone) }
|
||||
|
||||
// Telegram returns the Telegram username and whether it is disclosed.
|
||||
func (disclosure ContactDisclosure) Telegram() (string, bool) {
|
||||
return valueOfContact(disclosure.telegram)
|
||||
}
|
||||
|
||||
func valueOfContact(value *string) (string, bool) {
|
||||
if value == nil {
|
||||
return "", false
|
||||
}
|
||||
return *value, true
|
||||
}
|
||||
|
||||
func (disclosure ContactDisclosure) String() string { return redactedContactDisclosure }
|
||||
func (disclosure ContactDisclosure) GoString() string { return redactedContactDisclosure }
|
||||
func (disclosure ContactDisclosure) Format(state fmt.State, _ rune) {
|
||||
writeRedacted(state, redactedContactDisclosure)
|
||||
}
|
||||
|
||||
// LogValue carries the grant identifier and the number of kinds only.
|
||||
func (disclosure ContactDisclosure) LogValue() slog.Value {
|
||||
return slog.GroupValue(slog.String("grant_id", disclosure.grantID.String()), slog.Int("fields", len(disclosure.fields)))
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -17,7 +17,8 @@ var (
|
||||
)
|
||||
|
||||
// ContactRevocationReason says why a contact grant was revoked. It is shown
|
||||
// only to the candidate. The revocation itself belongs to a later operation.
|
||||
// only to the candidate. The consent revocation and the visibility changes
|
||||
// stamp it in the transaction of the change; a stamp is never cleared.
|
||||
type ContactRevocationReason string
|
||||
|
||||
const (
|
||||
|
||||
@@ -459,6 +459,26 @@ const (
|
||||
InvitationContactAccessClosed InvitationContactAccess = "closed"
|
||||
)
|
||||
|
||||
// InvitationContactAccessView is the contact access of the organization through
|
||||
// one invitation. Fields are the kinds that the contact read returns while the
|
||||
// status is available; the other states list none. It never holds a contact
|
||||
// value, and it names no reason, because the organization is not told one.
|
||||
type InvitationContactAccessView struct {
|
||||
Status InvitationContactAccess `json:"status"`
|
||||
Fields []ContactField `json:"fields"`
|
||||
}
|
||||
|
||||
// NewInvitationContactAccessView builds the view of a status. Fields are kept
|
||||
// only for an available access, and the list is never nil, so it serializes as
|
||||
// an empty array.
|
||||
func NewInvitationContactAccessView(status InvitationContactAccess, fields []ContactField) InvitationContactAccessView {
|
||||
view := InvitationContactAccessView{Status: status, Fields: []ContactField{}}
|
||||
if status == InvitationContactAccessAvailable {
|
||||
view.Fields = append(view.Fields, fields...)
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
// InvitationSnapshot is the serializable part of an invitation that an
|
||||
// organization may read. It has no candidate identifier, so it cannot reveal
|
||||
// who the invitation is for.
|
||||
@@ -500,15 +520,14 @@ func (invitation Invitation) Snapshot() InvitationSnapshot {
|
||||
|
||||
// OrganizationInvitationView is an invitation as its organization reads it:
|
||||
// the snapshot, the organization-scoped alias of the candidate, the coarse
|
||||
// salary indicator, and the state of the contact access without any contact
|
||||
// value. It also serves as the stored original response of an idempotent
|
||||
// create.
|
||||
// salary indicator, and the contact access without any contact value. It also
|
||||
// serves as the stored original response of an idempotent create or send.
|
||||
type OrganizationInvitationView struct {
|
||||
InvitationSnapshot
|
||||
|
||||
CandidateHandle string `json:"candidate_handle"`
|
||||
SalaryFit InvitationSalaryFit `json:"salary_fit"`
|
||||
ContactAccess InvitationContactAccess `json:"contact_access"`
|
||||
CandidateHandle string `json:"candidate_handle"`
|
||||
SalaryFit InvitationSalaryFit `json:"salary_fit"`
|
||||
ContactAccess InvitationContactAccessView `json:"contact_access"`
|
||||
}
|
||||
|
||||
func (invitation Invitation) String() string { return redactedInvitation }
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// InvitationEventExpired is the audit event of an expiry that the server made
|
||||
// at the response deadline. No candidate or organization acted, so the event
|
||||
// names no actor and no field.
|
||||
const InvitationEventExpired InvitationEventKind = "expired"
|
||||
|
||||
// AwaitsAnswer reports the states in which the candidate's answer is still
|
||||
// open: a delivered or a viewed invitation. The planned counter offer is not
|
||||
// among them, because no operation produces it yet.
|
||||
func (invitation Invitation) AwaitsAnswer() bool {
|
||||
return invitation.IsDelivered() && (invitation.State() == InvitationDelivered || invitation.State() == InvitationViewed)
|
||||
}
|
||||
|
||||
// ExpiryDue reports whether the invitation still awaits an answer at the time
|
||||
// and its response deadline has passed. At the deadline or later the answer is
|
||||
// late, so the expiry is due.
|
||||
func (invitation Invitation) ExpiryDue(at time.Time) bool {
|
||||
return invitation.AwaitsAnswer() && invitation.ResponseDeadlinePassed(at)
|
||||
}
|
||||
|
||||
// Expire returns the next version in the terminal state expired with the
|
||||
// timeout reason, when the invitation is due at the time. An invitation that is
|
||||
// not due, because it is answered, expired, or its deadline has not passed yet,
|
||||
// is returned as it is with changed false. The expiry keeps the deadline, the
|
||||
// terms, and the answer fields. It creates no consent and no contact grant, and
|
||||
// it never returns a weekly quota or a pending slot that the organization
|
||||
// spent: the pending limit counts only the invitations that still wait.
|
||||
func (invitation Invitation) Expire(at time.Time) (Invitation, bool, error) {
|
||||
const location = "Invitation.Expire"
|
||||
if !validInvitationTime(at) || at.Before(invitation.record.UpdatedAt) {
|
||||
return Invitation{}, false, fmt.Errorf("%s - expiry time must be set and not before the last change: %w", location, ErrInvalidInvitation)
|
||||
}
|
||||
if !invitation.ExpiryDue(at) {
|
||||
return invitation, false, nil
|
||||
}
|
||||
at = at.UTC()
|
||||
next := invitation.Stored()
|
||||
reason := string(InvitationExpiryTimeout)
|
||||
next.State = string(InvitationExpired)
|
||||
next.ExpiryReason = &reason
|
||||
next.Version++
|
||||
next.UpdatedAt = at
|
||||
expired, err := RestoreInvitation(next)
|
||||
if err != nil {
|
||||
return Invitation{}, false, fmt.Errorf("%s - restore expired invitation: %w", location, err)
|
||||
}
|
||||
return expired, true, nil
|
||||
}
|
||||
|
||||
// ExpiryEvent builds the audit event of the expired version this invitation has
|
||||
// just reached. The event has no actor, because the server made the change.
|
||||
func (invitation Invitation) ExpiryEvent() (InvitationEvent, error) {
|
||||
const location = "Invitation.ExpiryEvent"
|
||||
if invitation.State() != InvitationExpired {
|
||||
return InvitationEvent{}, fmt.Errorf("%s - the invitation must be expired: %w", location, ErrInvalidInvitation)
|
||||
}
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return InvitationEvent{}, fmt.Errorf("%s - generate event ID: %w", location, err)
|
||||
}
|
||||
return InvitationEvent{
|
||||
ID: id, InvitationID: invitation.record.ID, OrganizationID: invitation.record.OrganizationID,
|
||||
Version: invitation.record.Version, TermsVersion: invitation.record.TermsVersion, Kind: InvitationEventExpired,
|
||||
ActorID: uuid.Nil, ChangedFields: []InvitationChangedField{}, OccurredAt: invitation.record.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestExpireMovesADueInvitationToTheTimeoutReasonAndKeepsItsDeadline(t *testing.T) {
|
||||
delivered, _ := deliveredTestInvitation(t)
|
||||
deadline := *delivered.Snapshot().ResponseDeadlineAt
|
||||
viewed, _, err := delivered.RecordView(delivered.UpdatedAt().Add(time.Minute))
|
||||
if err != nil {
|
||||
t.Fatalf("RecordView() error = %v", err)
|
||||
}
|
||||
|
||||
for name, invitation := range map[string]Invitation{"delivered": delivered, "viewed": viewed} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
expired, changed, err := invitation.Expire(deadline)
|
||||
if err != nil || !changed {
|
||||
t.Fatalf("Expire(at the deadline) = %v, %v; want a change", changed, err)
|
||||
}
|
||||
snapshot := expired.Snapshot()
|
||||
if snapshot.State != InvitationExpired || snapshot.ExpiryReason == nil || *snapshot.ExpiryReason != InvitationExpiryTimeout ||
|
||||
snapshot.Version != invitation.Version()+1 || snapshot.TermsVersion != invitation.TermsVersion() ||
|
||||
!snapshot.ResponseDeadlineAt.Equal(deadline) || !snapshot.UpdatedAt.Equal(deadline) ||
|
||||
snapshot.RespondedAt != nil || !snapshot.DeliveredAt.Equal(*invitation.Snapshot().DeliveredAt) {
|
||||
t.Fatalf("expired = %+v, want the next version expired by the timeout with the same deadline and terms", snapshot)
|
||||
}
|
||||
event, err := expired.ExpiryEvent()
|
||||
if err != nil {
|
||||
t.Fatalf("ExpiryEvent() error = %v", err)
|
||||
}
|
||||
if event.Kind != InvitationEventExpired || event.ActorID != uuid.Nil ||
|
||||
event.Version != snapshot.Version || len(event.ChangedFields) != 0 || !event.OccurredAt.Equal(deadline) {
|
||||
t.Fatalf("ExpiryEvent() = %+v, want one system event of the expired version", event)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpireLeavesAnInvitationAloneBeforeItsDeadlineAndAfterAnAnswer(t *testing.T) {
|
||||
delivered, _ := deliveredTestInvitation(t)
|
||||
deadline := *delivered.Snapshot().ResponseDeadlineAt
|
||||
expiring := deadline.Add(-time.Microsecond)
|
||||
declined, _, err := delivered.Decline(InvitationDeclineChoice{Reason: InvitationDeclineOther}, expiring)
|
||||
if err != nil {
|
||||
t.Fatalf("Decline() error = %v", err)
|
||||
}
|
||||
accepted := withState(t, delivered, InvitationAccepted, func(stored *StoredInvitation) {
|
||||
responded := expiring
|
||||
stored.RespondedAt = &responded
|
||||
})
|
||||
|
||||
for name, test := range map[string]struct {
|
||||
invitation Invitation
|
||||
at time.Time
|
||||
}{
|
||||
"one microsecond before the deadline": {delivered, expiring},
|
||||
"declined after the deadline": {declined, deadline.Add(time.Hour)},
|
||||
"accepted after the deadline": {accepted, deadline.Add(time.Hour)},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
unchanged, changed, err := test.invitation.Expire(test.at)
|
||||
if err != nil || changed || unchanged.Version() != test.invitation.Version() || unchanged.State() != test.invitation.State() {
|
||||
t.Fatalf("Expire() = %v, %v, %v; want the invitation unchanged", unchanged.State(), changed, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpireRefusesATimeBeforeTheLastChange(t *testing.T) {
|
||||
delivered, _ := deliveredTestInvitation(t)
|
||||
if _, _, err := delivered.Expire(delivered.UpdatedAt().Add(-time.Second)); !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("Expire(before the last change) error = %v, want ErrInvalidInvitation", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpiryEventRequiresAnExpiredInvitation(t *testing.T) {
|
||||
delivered, _ := deliveredTestInvitation(t)
|
||||
if _, err := delivered.ExpiryEvent(); !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("ExpiryEvent(delivered) error = %v, want ErrInvalidInvitation", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user