feat(mail): send service identity mail through a leased worker
This commit is contained in:
1 parent
204c6bae62
commit
d21fd49a42
58 files changed
+7577
-164
No files matched your search
@@ -0,0 +1,138 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/usecase/notification/mailtemplate"
|
||||
)
|
||||
|
||||
type serviceMailHarness struct {
|
||||
mailer *ServiceMailer
|
||||
unit fakeUnit
|
||||
user uuid.UUID
|
||||
}
|
||||
|
||||
func newServiceMailHarness(t *testing.T) *serviceMailHarness {
|
||||
t.Helper()
|
||||
renderer, err := mailtemplate.NewRenderer(testOrigin)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRenderer() error = %v", err)
|
||||
}
|
||||
return &serviceMailHarness{
|
||||
mailer: NewServiceMailer(NewTemplateRenderer(renderer), testAccount(t, "resend"), func() time.Time { return testNow }),
|
||||
unit: fakeUnit{deliveries: &fakeDeliveries{stored: make(map[uuid.UUID]domain.Delivery)}},
|
||||
user: uuid.MustParse("01926f3a-7c00-7000-8000-0000000000bb"),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *serviceMailHarness) request(t *testing.T, purpose domain.EmailTokenPurpose) TokenMailRequest {
|
||||
t.Helper()
|
||||
raw, err := domain.NewSecretToken()
|
||||
if err != nil {
|
||||
t.Fatalf("NewSecretToken() error = %v", err)
|
||||
}
|
||||
token, err := domain.NewEmailToken(domain.NewEmailTokenInput{
|
||||
UserID: h.user, Purpose: purpose, TokenHash: raw.Hash(), RegistrationVersion: 1, IssuedAt: testNow, Lifetime: 24 * time.Hour,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewEmailToken() error = %v", err)
|
||||
}
|
||||
recipient, err := domain.NewEmailAddress(testRecipientAddress)
|
||||
if err != nil {
|
||||
t.Fatalf("NewEmailAddress() error = %v", err)
|
||||
}
|
||||
return TokenMailRequest{Recipient: recipient, Token: token, RawToken: raw, Lifetime: 24}
|
||||
}
|
||||
|
||||
func TestServiceMailerStoresATokenBoundDeliveryUnderTheTokenID(t *testing.T) {
|
||||
h := newServiceMailHarness(t)
|
||||
for name, test := range map[string]struct {
|
||||
purpose domain.EmailTokenPurpose
|
||||
enqueue func(*ServiceMailer, TokenMailRequest) (uuid.UUID, error)
|
||||
kind domain.MailKind
|
||||
path string
|
||||
}{
|
||||
"verification": {domain.EmailTokenPurposeEmailVerification, func(mailer *ServiceMailer, request TokenMailRequest) (uuid.UUID, error) {
|
||||
return mailer.EnqueueVerification(t.Context(), h.unit, request)
|
||||
}, domain.MailKindVerification, "/verify-email"},
|
||||
"password reset": {domain.EmailTokenPurposePasswordReset, func(mailer *ServiceMailer, request TokenMailRequest) (uuid.UUID, error) {
|
||||
return mailer.EnqueuePasswordReset(t.Context(), h.unit, request)
|
||||
}, domain.MailKindPasswordReset, "/reset-password"},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
request := h.request(t, test.purpose)
|
||||
id, err := test.enqueue(h.mailer, request)
|
||||
if err != nil {
|
||||
t.Fatalf("enqueue error = %v", err)
|
||||
}
|
||||
if id != request.Token.ID {
|
||||
t.Fatalf("delivery ID = %s, want the token ID %s", id, request.Token.ID)
|
||||
}
|
||||
stored := h.unit.deliveries.stored[id]
|
||||
switch {
|
||||
case stored.Kind() != test.kind || stored.EmailTokenID() != request.Token.ID:
|
||||
t.Fatalf("delivery is %s bound to %s", stored.Kind(), stored.EmailTokenID())
|
||||
case stored.Status() != domain.DeliveryStatusPending || !stored.NextAttemptAt().Equal(testNow):
|
||||
t.Fatalf("delivery is %s due %s, want pending and due now", stored.Status(), stored.NextAttemptAt())
|
||||
case stored.RecipientUserID() != h.user || stored.ProviderAccount() != testAccount(t, "resend"):
|
||||
t.Fatal("the delivery is not addressed to the token's account on the configured provider account")
|
||||
}
|
||||
// The token travels in the fragment of the link, never in the
|
||||
// path or the query, where an access log would record it.
|
||||
link := testOrigin + test.path + "#token=" + request.RawToken.Reveal()
|
||||
if !strings.Contains(stored.Content().Text(), link) || !strings.Contains(stored.Content().HTML(), link) {
|
||||
t.Fatal("the stored message lacks the link with the token in its fragment")
|
||||
}
|
||||
if strings.Contains(stored.Content().Text(), "?token=") || strings.Contains(stored.Content().Text(), test.path+"/") {
|
||||
t.Fatal("the token is outside the fragment")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceMailerRepeatsAreHarmless(t *testing.T) {
|
||||
h := newServiceMailHarness(t)
|
||||
request := h.request(t, domain.EmailTokenPurposeEmailVerification)
|
||||
first, err := h.mailer.EnqueueVerification(t.Context(), h.unit, request)
|
||||
if err != nil {
|
||||
t.Fatalf("first EnqueueVerification() error = %v", err)
|
||||
}
|
||||
second, err := h.mailer.EnqueueVerification(t.Context(), h.unit, request)
|
||||
if err != nil || second != first || len(h.unit.deliveries.stored) != 1 {
|
||||
t.Fatalf("second EnqueueVerification() = %s, %v with %d deliveries; want the same delivery", second, err, len(h.unit.deliveries.stored))
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceMailerRefusesAWrongTokenBeforeRendering(t *testing.T) {
|
||||
h := newServiceMailHarness(t)
|
||||
reset := h.request(t, domain.EmailTokenPurposePasswordReset)
|
||||
if _, err := h.mailer.EnqueueVerification(t.Context(), h.unit, reset); !errors.Is(err, domain.ErrInvalidEmailToken) {
|
||||
t.Fatalf("EnqueueVerification(reset token) error = %v, want ErrInvalidEmailToken", err)
|
||||
}
|
||||
verification := h.request(t, domain.EmailTokenPurposeEmailVerification)
|
||||
if _, err := h.mailer.EnqueuePasswordReset(t.Context(), h.unit, verification); !errors.Is(err, domain.ErrInvalidEmailToken) {
|
||||
t.Fatalf("EnqueuePasswordReset(verification token) error = %v, want ErrInvalidEmailToken", err)
|
||||
}
|
||||
for name, change := range map[string]func(*TokenMailRequest){
|
||||
"no token ID": func(request *TokenMailRequest) { request.Token = domain.EmailToken{} },
|
||||
"no raw token": func(request *TokenMailRequest) { request.RawToken = domain.SecretToken{} },
|
||||
"no recipient": func(request *TokenMailRequest) { request.Recipient = domain.EmailAddress{} },
|
||||
"zero lifetime": func(request *TokenMailRequest) { request.Lifetime = 0 },
|
||||
"lifetime >7d": func(request *TokenMailRequest) { request.Lifetime = 24 * 8 },
|
||||
} {
|
||||
request := h.request(t, domain.EmailTokenPurposeEmailVerification)
|
||||
change(&request)
|
||||
if _, err := h.mailer.EnqueueVerification(t.Context(), h.unit, request); err == nil {
|
||||
t.Fatalf("%s: EnqueueVerification() accepted the request", name)
|
||||
}
|
||||
}
|
||||
if len(h.unit.deliveries.stored) != 0 {
|
||||
t.Fatal("a refused request stored a delivery")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user