feat(invitations): send invitations into the candidate inbox atomically
This commit is contained in:
1 parent
fc35e27af5
commit
1a067221f7
46 files changed
+3276
-113
No files matched your search
@@ -0,0 +1,92 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestDeliverStartsTheDeadlineAtTheDeliveryForEveryPeriod(t *testing.T) {
|
||||
for _, period := range []CalendarDays{3, 7, 14} {
|
||||
input := validInvitationDraftInput()
|
||||
input.Content.ResponsePeriod = period
|
||||
draft, err := NewInvitationDraft(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
deliveredAt := input.At.Add(time.Hour)
|
||||
delivered, err := draft.Deliver(deliveredAt)
|
||||
if err != nil {
|
||||
t.Fatalf("Deliver(%d days) error = %v", period, err)
|
||||
}
|
||||
snapshot := delivered.Snapshot()
|
||||
if snapshot.State != InvitationDelivered || snapshot.Version != draft.Version()+1 || snapshot.TermsVersion != draft.TermsVersion() ||
|
||||
!snapshot.DeliveredAt.Equal(deliveredAt) || !snapshot.ResponseDeadlineAt.Equal(deliveredAt.Add(time.Duration(period)*24*time.Hour)) ||
|
||||
!snapshot.UpdatedAt.Equal(deliveredAt) || !delivered.IsDelivered() || draft.IsDelivered() {
|
||||
t.Fatalf("delivered %d-day invitation = %+v", period, snapshot)
|
||||
}
|
||||
event, err := delivered.DeliveryEvent(input.ActorID)
|
||||
if err != nil {
|
||||
t.Fatalf("DeliveryEvent() error = %v", err)
|
||||
}
|
||||
if event.Kind != InvitationEventDelivered || event.Version != snapshot.Version || len(event.ChangedFields) != 0 ||
|
||||
!event.OccurredAt.Equal(deliveredAt) {
|
||||
t.Fatalf("delivery event = %+v", event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliverRefusesAnythingButAnUnsentDraft(t *testing.T) {
|
||||
input := validInvitationDraftInput()
|
||||
draft, err := NewInvitationDraft(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
if _, err := draft.Deliver(input.At.Add(-time.Second)); !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("Deliver(before the last change) error = %v, want ErrInvalidInvitation", err)
|
||||
}
|
||||
delivered, err := draft.Deliver(input.At)
|
||||
if err != nil {
|
||||
t.Fatalf("Deliver() error = %v", err)
|
||||
}
|
||||
if _, err := delivered.Deliver(input.At.Add(time.Hour)); !errors.Is(err, ErrInvitationNotDraft) {
|
||||
t.Fatalf("Deliver(delivered) error = %v, want ErrInvitationNotDraft", err)
|
||||
}
|
||||
if _, err := draft.DeliveryEvent(input.ActorID); !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("DeliveryEvent(draft) error = %v, want ErrInvalidInvitation", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCandidateSummaryCarriesNoCandidateIdentifier(t *testing.T) {
|
||||
input := validInvitationDraftInput()
|
||||
draft, err := NewInvitationDraft(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
if _, err := draft.CandidateSummary(InvitationOrganizationRef{ID: input.OrganizationID, Name: "Sender"}); !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("CandidateSummary(draft) error = %v, want ErrInvalidInvitation", err)
|
||||
}
|
||||
delivered, err := draft.Deliver(input.At)
|
||||
if err != nil {
|
||||
t.Fatalf("Deliver() error = %v", err)
|
||||
}
|
||||
if _, err := delivered.CandidateSummary(InvitationOrganizationRef{ID: uuid.New(), Name: "Other"}); !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("CandidateSummary(other organization) error = %v, want ErrInvalidInvitation", err)
|
||||
}
|
||||
summary, err := delivered.CandidateSummary(InvitationOrganizationRef{ID: input.OrganizationID, Name: "Sender"})
|
||||
if err != nil {
|
||||
t.Fatalf("CandidateSummary() error = %v", err)
|
||||
}
|
||||
if summary.State != InvitationDelivered || summary.RoleTitle != input.Content.Terms.RoleTitle || !summary.DeliveredAt.Equal(input.At) {
|
||||
t.Fatalf("summary = %+v", summary)
|
||||
}
|
||||
for _, text := range []string{fmt.Sprint(summary), fmt.Sprintf("%+v", summary), fmt.Sprintf("%#v", summary)} {
|
||||
if strings.Contains(text, input.CandidateID.String()) || strings.Contains(text, summary.RoleTitle) {
|
||||
t.Fatalf("formatted summary leaks its content: %s", text)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user