feat(notifications): send product notifications with a rights check right before each send
This commit is contained in:
1 parent
a856066316
commit
623d41b969
59 files changed
+4125
-1126
No files matched your search
@@ -0,0 +1,173 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var (
|
||||
testInvitationID = uuid.MustParse("01926f3a-7c00-7000-8000-0000000000c1")
|
||||
testSenderOrganizationID = uuid.MustParse("01926f3a-7c00-7000-8000-0000000000c3")
|
||||
)
|
||||
|
||||
// newInvitationNotice is a notification to the test recipient that names an
|
||||
// invitation of the test organization.
|
||||
func newInvitationNotice(t *testing.T) Delivery {
|
||||
t.Helper()
|
||||
content, err := NewMailContent(MailKindNotification, "notification.v1", "Новое приглашение от работодателя", "Работодатель отправил вам приглашение\n", "")
|
||||
if err != nil {
|
||||
t.Fatalf("NewMailContent() error = %v", err)
|
||||
}
|
||||
subject, err := NewInvitationMailSubject(testInvitationID, testSenderOrganizationID)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationMailSubject() error = %v", err)
|
||||
}
|
||||
input := testDeliveryInput(t)
|
||||
input.Content = content
|
||||
input.Subject = subject
|
||||
delivery, err := NewDelivery(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewDelivery() error = %v", err)
|
||||
}
|
||||
return delivery
|
||||
}
|
||||
|
||||
// currentInvitationStanding is the standing of a notice that may be sent: the
|
||||
// address is verified and unchanged, the invitation awaits an answer before its
|
||||
// deadline, and the candidate neither hid the organization nor withdrew
|
||||
// publication.
|
||||
func currentInvitationStanding(delivery Delivery) InvitationNoticeStanding {
|
||||
return InvitationNoticeStanding{
|
||||
RecipientFound: true,
|
||||
RecipientEmailVerified: true,
|
||||
RecipientEmailKey: delivery.Recipient().Normalized(),
|
||||
InvitationFound: true,
|
||||
InvitationOrganizationID: testSenderOrganizationID,
|
||||
InvitationState: InvitationDelivered,
|
||||
ResponseDeadlineAt: after(7 * 24 * 60),
|
||||
PublicationPublished: true,
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckInvitationNoticeAllowsAnInvitationThatStillAwaitsAnswer(t *testing.T) {
|
||||
delivery := newInvitationNotice(t)
|
||||
for _, state := range []InvitationState{InvitationDelivered, InvitationViewed} {
|
||||
standing := currentInvitationStanding(delivery)
|
||||
standing.InvitationState = state
|
||||
if reason := delivery.CheckInvitationNotice(standing, after(1)); reason != "" {
|
||||
t.Fatalf("CheckInvitationNotice(%s) = %q for an open invitation, want it sendable", state, reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The first failed rule decides, so each row changes one fact from the current
|
||||
// standing and names the reason that fact must give.
|
||||
func TestCheckInvitationNoticeNamesTheReasonOfTheFirstFailedRule(t *testing.T) {
|
||||
delivery := newInvitationNotice(t)
|
||||
deadline := currentInvitationStanding(delivery).ResponseDeadlineAt
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
change func(*InvitationNoticeStanding)
|
||||
at int
|
||||
want DeliveryCancelReason
|
||||
}{
|
||||
{"the account is gone", func(s *InvitationNoticeStanding) { s.RecipientFound = false }, 1, DeliveryCancelRecipientIneligible},
|
||||
{"the address is not verified", func(s *InvitationNoticeStanding) { s.RecipientEmailVerified = false }, 1, DeliveryCancelRecipientIneligible},
|
||||
{"the address changed", func(s *InvitationNoticeStanding) { s.RecipientEmailKey = "other@example.com" }, 1, DeliveryCancelRecipientMismatch},
|
||||
{"the invitation is gone", func(s *InvitationNoticeStanding) { s.InvitationFound = false }, 1, DeliveryCancelSourceClosed},
|
||||
{"the invitation belongs to another organization", func(s *InvitationNoticeStanding) {
|
||||
s.InvitationOrganizationID = uuid.MustParse("01926f3a-7c00-7000-8000-0000000000c9")
|
||||
}, 1, DeliveryCancelSourceClosed},
|
||||
{"the candidate declined", func(s *InvitationNoticeStanding) { s.InvitationState = InvitationDeclined }, 1, DeliveryCancelRightsRevoked},
|
||||
{"the candidate accepted", func(s *InvitationNoticeStanding) { s.InvitationState = InvitationAccepted }, 1, DeliveryCancelRightsRevoked},
|
||||
{"the invitation expired", func(s *InvitationNoticeStanding) { s.InvitationState = InvitationExpired }, 1, DeliveryCancelRightsRevoked},
|
||||
{"the deadline has passed", func(*InvitationNoticeStanding) {}, 7*24*60 + 1, DeliveryCancelRightsRevoked},
|
||||
{"the deadline is reached", func(*InvitationNoticeStanding) {}, 7 * 24 * 60, DeliveryCancelRightsRevoked},
|
||||
{"the invitation was never delivered", func(s *InvitationNoticeStanding) {
|
||||
s.ResponseDeadlineAt = deadline
|
||||
s.InvitationState = InvitationDraft
|
||||
}, 1, DeliveryCancelRightsRevoked},
|
||||
{"the candidate hid the organization", func(s *InvitationNoticeStanding) { s.OrganizationHidden = true }, 1, DeliveryCancelRightsRevoked},
|
||||
{"the candidate withdrew publication", func(s *InvitationNoticeStanding) { s.PublicationPublished = false }, 1, DeliveryCancelRightsRevoked},
|
||||
{"an unverified address outranks a hidden organization", func(s *InvitationNoticeStanding) {
|
||||
s.RecipientEmailVerified = false
|
||||
s.OrganizationHidden = true
|
||||
}, 1, DeliveryCancelRecipientIneligible},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
standing := currentInvitationStanding(delivery)
|
||||
test.change(&standing)
|
||||
if got := delivery.CheckInvitationNotice(standing, after(test.at)); got != test.want {
|
||||
t.Fatalf("CheckInvitationNotice() = %q, want %q", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckInvitationNoticeRefusesAMailThatNamesNoInvitation(t *testing.T) {
|
||||
content, err := NewMailContent(MailKindNotification, "notification.v1", "Уведомление", "Текст\n", "")
|
||||
if err != nil {
|
||||
t.Fatalf("NewMailContent() error = %v", err)
|
||||
}
|
||||
input := testDeliveryInput(t)
|
||||
input.Content = content
|
||||
delivery, err := NewDelivery(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewDelivery() error = %v", err)
|
||||
}
|
||||
if reason := delivery.CheckInvitationNotice(currentInvitationStanding(delivery), after(1)); reason != DeliveryCancelSourceClosed {
|
||||
t.Fatalf("CheckInvitationNotice() = %q for a notice without a subject, want source_closed", reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewMailSubjectNamesOnlyKnownRecords(t *testing.T) {
|
||||
if _, err := NewInvitationMailSubject(uuid.Nil, testSenderOrganizationID); !errors.Is(err, ErrInvalidMailSubject) {
|
||||
t.Fatalf("NewInvitationMailSubject(nil invitation) error = %v, want ErrInvalidMailSubject", err)
|
||||
}
|
||||
if _, err := NewInvitationMailSubject(testInvitationID, uuid.Nil); !errors.Is(err, ErrInvalidMailSubject) {
|
||||
t.Fatalf("NewInvitationMailSubject(nil organization) error = %v, want ErrInvalidMailSubject", err)
|
||||
}
|
||||
if subject, err := RestoreMailSubject("", uuid.Nil, uuid.Nil); err != nil || !subject.IsZero() {
|
||||
t.Fatalf("RestoreMailSubject(empty) = %v, %v; want the zero subject", subject, err)
|
||||
}
|
||||
if _, err := RestoreMailSubject("contract", testInvitationID, testSenderOrganizationID); !errors.Is(err, ErrInvalidMailSubject) {
|
||||
t.Fatalf("RestoreMailSubject(unknown kind) error = %v, want ErrInvalidMailSubject", err)
|
||||
}
|
||||
subject, err := NewInvitationMailSubject(testInvitationID, testSenderOrganizationID)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationMailSubject() error = %v", err)
|
||||
}
|
||||
if text := subject.String(); text != "[mail subject invitation]" {
|
||||
t.Fatalf("String() = %q; the identifiers must stay out of printed values", text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnlyANotificationNamesASubject(t *testing.T) {
|
||||
subject, err := NewInvitationMailSubject(testInvitationID, testSenderOrganizationID)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationMailSubject() error = %v", err)
|
||||
}
|
||||
input := testDeliveryInput(t)
|
||||
input.Subject = subject
|
||||
if _, err := NewDelivery(input); !errors.Is(err, ErrInvalidDelivery) {
|
||||
t.Fatalf("NewDelivery(verification with subject) error = %v, want ErrInvalidDelivery", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRightsCancelReasonsAreKnownAndExposed(t *testing.T) {
|
||||
for _, value := range []string{"rights_revoked", "recipient_ineligible", "source_closed"} {
|
||||
reason, err := ParseDeliveryCancelReason(value)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseDeliveryCancelReason(%q) error = %v", value, err)
|
||||
}
|
||||
listed := false
|
||||
for _, known := range DeliveryCancelReasons() {
|
||||
listed = listed || known == reason
|
||||
}
|
||||
if !listed {
|
||||
t.Fatalf("%q is parsed but missing from DeliveryCancelReasons, so its metric series would not exist at zero", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user