226 lines
11 KiB
Go
226 lines
11 KiB
Go
package domain
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/google/uuid"
|
||
)
|
||
|
||
// deliveredTestInvitation returns a draft delivered an hour after it was
|
||
// created, with the default seven-day deadline.
|
||
func deliveredTestInvitation(t *testing.T) (Invitation, NewInvitationDraftInput) {
|
||
t.Helper()
|
||
input := validInvitationDraftInput()
|
||
draft, err := NewInvitationDraft(input)
|
||
if err != nil {
|
||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||
}
|
||
delivered, err := draft.Deliver(input.At.Add(time.Hour))
|
||
if err != nil {
|
||
t.Fatalf("Deliver() error = %v", err)
|
||
}
|
||
return delivered, input
|
||
}
|
||
|
||
// withState restores the invitation in another state, as a later operation
|
||
// would store it.
|
||
func withState(t *testing.T, invitation Invitation, state InvitationState, change func(*StoredInvitation)) Invitation {
|
||
t.Helper()
|
||
stored := invitation.Stored()
|
||
stored.State = string(state)
|
||
stored.Version++
|
||
if change != nil {
|
||
change(&stored)
|
||
}
|
||
restored, err := RestoreInvitation(stored)
|
||
if err != nil {
|
||
t.Fatalf("RestoreInvitation(%s) error = %v", state, err)
|
||
}
|
||
return restored
|
||
}
|
||
|
||
func TestRecordViewMovesOnlyADeliveredInvitationBeforeItsDeadline(t *testing.T) {
|
||
delivered, input := deliveredTestInvitation(t)
|
||
deadline := *delivered.Snapshot().ResponseDeadlineAt
|
||
viewedAt := deadline.Add(-time.Microsecond)
|
||
viewed, changed, err := delivered.RecordView(viewedAt)
|
||
if err != nil || !changed {
|
||
t.Fatalf("RecordView() = %v, %v; want a change", changed, err)
|
||
}
|
||
snapshot := viewed.Snapshot()
|
||
if snapshot.State != InvitationViewed || snapshot.Version != delivered.Version()+1 || snapshot.TermsVersion != delivered.TermsVersion() ||
|
||
!snapshot.ViewedAt.Equal(viewedAt) || !snapshot.ResponseDeadlineAt.Equal(deadline) || !snapshot.UpdatedAt.Equal(viewedAt) {
|
||
t.Fatalf("viewed = %+v, want the next version viewed with the same deadline", snapshot)
|
||
}
|
||
event, err := viewed.AnswerEvent(input.ActorID)
|
||
if err != nil || event.Kind != InvitationEventViewed || event.Version != snapshot.Version || len(event.ChangedFields) != 0 || !event.OccurredAt.Equal(viewedAt) {
|
||
t.Fatalf("AnswerEvent() = %+v, %v", event, err)
|
||
}
|
||
|
||
// A repeat, a late view, and a view of another state change nothing.
|
||
for name, test := range map[string]struct {
|
||
invitation Invitation
|
||
at time.Time
|
||
}{
|
||
"viewed": {viewed, viewedAt},
|
||
"at the deadline": {delivered, deadline},
|
||
"after it": {delivered, deadline.Add(time.Hour)},
|
||
"declined": {withState(t, delivered, InvitationDeclined, func(s *StoredInvitation) { s.RespondedAt = &viewedAt }), deadline.Add(-time.Second)},
|
||
"withdrawn": {withState(t, delivered, InvitationWithdrawn, nil), viewedAt},
|
||
"planned counter": {withState(t, viewed, InvitationCounter, nil), viewedAt},
|
||
"expired by timing": {withState(t, delivered, InvitationExpired, func(s *StoredInvitation) { reason := string(InvitationExpiryTimeout); s.ExpiryReason = &reason }), viewedAt},
|
||
} {
|
||
same, changed, err := test.invitation.RecordView(test.at)
|
||
if err != nil || changed || same.Version() != test.invitation.Version() || same.State() != test.invitation.State() {
|
||
t.Errorf("RecordView(%s) = %s v%d, %v, %v; want no change", name, same.State(), same.Version(), changed, err)
|
||
}
|
||
}
|
||
if _, _, err := delivered.RecordView(delivered.UpdatedAt().Add(-time.Second)); !errors.Is(err, ErrInvalidInvitation) {
|
||
t.Fatalf("RecordView(before the last change) error = %v, want ErrInvalidInvitation", err)
|
||
}
|
||
if _, err := delivered.AnswerEvent(input.ActorID); !errors.Is(err, ErrInvalidInvitation) {
|
||
t.Fatalf("AnswerEvent(delivered) error = %v, want ErrInvalidInvitation", err)
|
||
}
|
||
}
|
||
|
||
func TestDeclineIsAllowedFromDeliveredViewedAndCounterBeforeTheDeadline(t *testing.T) {
|
||
delivered, input := deliveredTestInvitation(t)
|
||
deadline := *delivered.Snapshot().ResponseDeadlineAt
|
||
viewed, _, err := delivered.RecordView(delivered.UpdatedAt().Add(time.Minute))
|
||
if err != nil {
|
||
t.Fatalf("RecordView() error = %v", err)
|
||
}
|
||
counter := withState(t, viewed, InvitationCounter, nil)
|
||
choice := InvitationDeclineChoice{Reason: InvitationDeclineOther}
|
||
for name, invitation := range map[string]Invitation{"delivered": delivered, "viewed": viewed, "counter": counter} {
|
||
at := deadline.Add(-time.Microsecond)
|
||
declined, decline, err := invitation.Decline(choice, at)
|
||
if err != nil {
|
||
t.Fatalf("Decline(%s) error = %v", name, err)
|
||
}
|
||
snapshot := declined.Snapshot()
|
||
if snapshot.State != InvitationDeclined || snapshot.Version != invitation.Version()+1 || !snapshot.RespondedAt.Equal(at) ||
|
||
!snapshot.ResponseDeadlineAt.Equal(deadline) || !declined.IsTerminal() {
|
||
t.Fatalf("declined %s = %+v", name, snapshot)
|
||
}
|
||
if decline.InvitationID != invitation.ID() || decline.OrganizationID != invitation.OrganizationID() ||
|
||
decline.Choice.Reason != InvitationDeclineOther || decline.Choice.Comment != nil || !decline.DeclinedAt.Equal(at) {
|
||
t.Fatalf("decline of %s = %+v", name, decline)
|
||
}
|
||
event, err := declined.AnswerEvent(input.CandidateID)
|
||
if err != nil || event.Kind != InvitationEventDeclined || event.ActorID != input.CandidateID || len(event.ChangedFields) != 0 {
|
||
t.Fatalf("AnswerEvent(%s) = %+v, %v", name, event, err)
|
||
}
|
||
view, err := declined.CandidateView(InvitationOrganizationRef{ID: input.OrganizationID, Name: "Org"}, &decline)
|
||
if err != nil || view.Decline == nil || view.Decline.Reason != InvitationDeclineOther || view.Decline.Comment != nil {
|
||
t.Fatalf("CandidateView(%s) = %+v, %v", name, view, err)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestDeclineRefusesTerminalAndLateInvitations(t *testing.T) {
|
||
delivered, _ := deliveredTestInvitation(t)
|
||
deadline := *delivered.Snapshot().ResponseDeadlineAt
|
||
responded := delivered.UpdatedAt().Add(time.Minute)
|
||
choice := InvitationDeclineChoice{Reason: InvitationDeclineSalary}
|
||
draft, err := NewInvitationDraft(validInvitationDraftInput())
|
||
if err != nil {
|
||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||
}
|
||
for name, test := range map[string]struct {
|
||
invitation Invitation
|
||
at time.Time
|
||
}{
|
||
"at the deadline": {delivered, deadline},
|
||
"draft": {draft, draft.UpdatedAt()},
|
||
"declined": {withState(t, delivered, InvitationDeclined, func(s *StoredInvitation) { s.RespondedAt = &responded }), responded},
|
||
"accepted": {withState(t, delivered, InvitationAccepted, func(s *StoredInvitation) { s.RespondedAt = &responded }), responded},
|
||
"withdrawn": {withState(t, delivered, InvitationWithdrawn, nil), responded},
|
||
"cancelled": {withState(t, delivered, InvitationCancelledByCandidate, nil), responded},
|
||
} {
|
||
if _, _, err := test.invitation.Decline(choice, test.at); !errors.Is(err, ErrInvitationNotAnswerable) {
|
||
t.Errorf("Decline(%s) error = %v, want ErrInvitationNotAnswerable", name, err)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestDeclineChoiceValidatesTheReasonAndTheComment(t *testing.T) {
|
||
text := func(value string) *string { return &value }
|
||
for name, comment := range map[string]*string{
|
||
"absent": nil, "line breaks": text("Не подходит\nстек"), "500 characters": text(strings.Repeat("я", 500)),
|
||
"leading space": text(" ok"),
|
||
} {
|
||
if err := (InvitationDeclineChoice{Reason: InvitationDeclineOther, Comment: comment}).Validate(); err != nil {
|
||
t.Errorf("Validate(%s) error = %v", name, err)
|
||
}
|
||
}
|
||
for name, test := range map[string]struct {
|
||
choice InvitationDeclineChoice
|
||
pointer string
|
||
}{
|
||
"unknown reason": {InvitationDeclineChoice{Reason: "rude"}, "/reason"},
|
||
"empty": {InvitationDeclineChoice{Reason: InvitationDeclineOther, Comment: text("")}, "/comment"},
|
||
"white space": {InvitationDeclineChoice{Reason: InvitationDeclineOther, Comment: text(" \n ")}, "/comment"},
|
||
"501 characters": {InvitationDeclineChoice{Reason: InvitationDeclineOther, Comment: text(strings.Repeat("я", 501))}, "/comment"},
|
||
"NUL": {InvitationDeclineChoice{Reason: InvitationDeclineOther, Comment: text("a\x00b")}, "/comment"},
|
||
"invalid UTF-8": {InvitationDeclineChoice{Reason: InvitationDeclineOther, Comment: text("a\xffb")}, "/comment"},
|
||
"escape characters": {InvitationDeclineChoice{Reason: InvitationDeclineOther, Comment: text("a\x1bb")}, "/comment"},
|
||
} {
|
||
err := test.choice.Validate()
|
||
field, ok := errors.AsType[*InvitationFieldError](err)
|
||
if !ok || field.Pointer != test.pointer {
|
||
t.Errorf("Validate(%s) error = %v, want a field error at %s", name, err, test.pointer)
|
||
}
|
||
}
|
||
same := InvitationDeclineChoice{Reason: InvitationDeclineOther, Comment: text("x")}
|
||
if !same.Equal(InvitationDeclineChoice{Reason: InvitationDeclineOther, Comment: text("x")}) ||
|
||
same.Equal(InvitationDeclineChoice{Reason: InvitationDeclineOther}) ||
|
||
same.Equal(InvitationDeclineChoice{Reason: InvitationDeclineStack, Comment: text("x")}) {
|
||
t.Fatal("Equal does not compare the reason and the comment")
|
||
}
|
||
}
|
||
|
||
func TestStoredAnswersMustMatchTheState(t *testing.T) {
|
||
delivered, _ := deliveredTestInvitation(t)
|
||
before := delivered.Snapshot().DeliveredAt.Add(-time.Second)
|
||
after := delivered.Snapshot().DeliveredAt.Add(time.Second)
|
||
for name, change := range map[string]func(*StoredInvitation){
|
||
"delivered with a view": func(s *StoredInvitation) { s.ViewedAt = &after },
|
||
"viewed without a view": func(s *StoredInvitation) { s.State = string(InvitationViewed) },
|
||
"declined without time": func(s *StoredInvitation) { s.State = string(InvitationDeclined) },
|
||
"view before delivery": func(s *StoredInvitation) { s.State = string(InvitationViewed); s.ViewedAt = &before },
|
||
"answer before delivery": func(s *StoredInvitation) { s.State = string(InvitationDeclined); s.RespondedAt = &before },
|
||
} {
|
||
stored := delivered.Stored()
|
||
change(&stored)
|
||
if _, err := RestoreInvitation(stored); !errors.Is(err, ErrInvalidInvitation) {
|
||
t.Errorf("RestoreInvitation(%s) error = %v, want ErrInvalidInvitation", name, err)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestDeclineValuesAreRedacted(t *testing.T) {
|
||
comment := "private-decline-comment"
|
||
decline := InvitationDecline{
|
||
InvitationID: uuid.New(), OrganizationID: uuid.New(), DeclinedAt: time.Now(),
|
||
Choice: InvitationDeclineChoice{Reason: InvitationDeclineSalary, Comment: &comment},
|
||
}
|
||
delivered, input := deliveredTestInvitation(t)
|
||
view, err := delivered.CandidateView(InvitationOrganizationRef{ID: input.OrganizationID, Name: "Org"}, nil)
|
||
if err != nil {
|
||
t.Fatalf("CandidateView() error = %v", err)
|
||
}
|
||
for _, value := range []any{decline, decline.Choice, view} {
|
||
for _, format := range []string{"%v", "%+v", "%#v", "%s"} {
|
||
if output := fmt.Sprintf(format, value); strings.Contains(output, comment) || strings.Contains(output, string(InvitationDeclineSalary)) ||
|
||
strings.Contains(output, input.CandidateID.String()) {
|
||
t.Errorf("%s of %T discloses a private value: %s", format, value, output)
|
||
}
|
||
}
|
||
}
|
||
}
|