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
@@ -30,6 +30,7 @@ import (
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/inbound"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
"lincodex/backend/internal/usecase/consent"
|
||||
"lincodex/backend/internal/usecase/notification"
|
||||
"lincodex/backend/internal/usecase/notification/mailtemplate"
|
||||
"lincodex/backend/internal/worker/mailsender"
|
||||
@@ -321,6 +322,85 @@ func TestServiceMailSending(t *testing.T) {
|
||||
harness.assertLogsHoldNoSecrets(t, user.Email.Address(), raw.Reveal(), "verify-email")
|
||||
})
|
||||
|
||||
t.Run("a password reset email for an account without C2 is accepted by the local Resend fixture", func(t *testing.T) {
|
||||
reset(t)
|
||||
calls := len(fixture.Requests())
|
||||
user := harness.createUser(ctx, t, true)
|
||||
raw, token, deliveryID := harness.enqueuePasswordReset(ctx, t, user, mailSenderTokenLifetime)
|
||||
assertRowCount(ctx, t, pool, pgx.Identifier{"consent_records"}, 0)
|
||||
|
||||
stored := harness.delivery(ctx, t, deliveryID)
|
||||
if stored.Status() != domain.DeliveryStatusPending || stored.Kind() != domain.MailKindPasswordReset || stored.EmailTokenID() != token.ID {
|
||||
t.Fatalf("enqueued delivery is %s of kind %s bound to %v", stored.Status(), stored.Kind(), stored.EmailTokenID())
|
||||
}
|
||||
|
||||
sender := harness.newSender(t, harness.transactor, 100, 3000)
|
||||
result := harness.pass(ctx, t, sender)
|
||||
if result.Outcome != inbound.MailPassAccepted || result.DeliveryID != deliveryID {
|
||||
t.Fatalf("pass = %+v, want accepted for the enqueued delivery", result)
|
||||
}
|
||||
requests := fixture.Requests()[calls:]
|
||||
if len(requests) != 1 {
|
||||
t.Fatalf("the fixture received %d calls, want 1", len(requests))
|
||||
}
|
||||
request := requests[0]
|
||||
link := harness.publicURL + "/reset-password#token=" + raw.Reveal()
|
||||
switch {
|
||||
case len(request.To) != 1 || request.To[0] != user.Email.Address():
|
||||
t.Fatal("the message was not addressed to the account")
|
||||
case !strings.Contains(request.Text, link) || !strings.Contains(request.HTML, link):
|
||||
t.Fatal("the message does not carry the reset link with its token in the fragment")
|
||||
case strings.Contains(request.Text, "?token=") || strings.Contains(request.HTML, "?token="):
|
||||
t.Fatal("the token travels in a query")
|
||||
}
|
||||
accepted := harness.delivery(ctx, t, deliveryID)
|
||||
if accepted.Status() != domain.DeliveryStatusAccepted || accepted.AttemptCount() != 1 {
|
||||
t.Fatalf("delivery is %s after %d attempts, want accepted after one attempt", accepted.Status(), accepted.AttemptCount())
|
||||
}
|
||||
harness.assertLogsHoldNoSecrets(t, user.Email.Address(), raw.Reveal(), "reset-password")
|
||||
})
|
||||
|
||||
t.Run("a verification mail goes out after the account's C2 was granted and revoked before the worker runs", func(t *testing.T) {
|
||||
reset(t)
|
||||
calls := len(fixture.Requests())
|
||||
user := harness.createUser(ctx, t, false)
|
||||
_, _, deliveryID := harness.enqueueVerification(ctx, t, user, mailSenderTokenLifetime)
|
||||
|
||||
// C2 is granted and revoked while the mail waits. The publication
|
||||
// history must not hold the mail back: only the account and the token
|
||||
// decide, as ServiceMailState reads nothing else.
|
||||
c2Text, err := postgresadapter.NewConsentStore(pool).ConsentTexts().Current(ctx, domain.ConsentPurposePublication, harness.clock.Now())
|
||||
if err != nil {
|
||||
t.Fatalf("read the current C2 text: %v", err)
|
||||
}
|
||||
consentService := consent.NewService(harness.transactor, harness.clock.Now)
|
||||
granted, err := consentService.GrantInSettingsInNewTransaction(ctx, consent.SettingsGrantInput{
|
||||
SubjectID: user.ID, Purpose: domain.ConsentPurposePublication, TextVersion: c2Text.Version(),
|
||||
})
|
||||
if err != nil || !granted.Created {
|
||||
t.Fatalf("grant C2 = created %t, %v; want a new record", granted.Created, err)
|
||||
}
|
||||
revocation, err := consentService.RevokeInSettingsInNewTransaction(ctx, consent.SettingsRevocationInput{
|
||||
SubjectID: user.ID, Purpose: domain.ConsentPurposePublication,
|
||||
})
|
||||
if err != nil || len(revocation.Records) != 1 {
|
||||
t.Fatalf("revoke C2 = %d records, %v; want one revoked record", len(revocation.Records), err)
|
||||
}
|
||||
if _, revoked := revocation.Records[0].RevokedAt(); !revoked {
|
||||
t.Fatal("the C2 record is not revoked")
|
||||
}
|
||||
|
||||
sender := harness.newSender(t, harness.transactor, 100, 3000)
|
||||
result := harness.pass(ctx, t, sender)
|
||||
if result.Outcome != inbound.MailPassAccepted || result.DeliveryID != deliveryID {
|
||||
t.Fatalf("pass = %+v, want the verification mail accepted after C2 was revoked", result)
|
||||
}
|
||||
requests := fixture.Requests()[calls:]
|
||||
if len(requests) != 1 || len(requests[0].To) != 1 || requests[0].To[0] != user.Email.Address() {
|
||||
t.Fatalf("the fixture received %d calls, want one to the account", len(requests))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("an accepted request with a lost response retries under the same key within 24 hours", func(t *testing.T) {
|
||||
reset(t)
|
||||
calls := len(fixture.Requests())
|
||||
@@ -637,6 +717,39 @@ func TestServiceMailSending(t *testing.T) {
|
||||
harness.assertQuotaUsage(ctx, t, 0)
|
||||
})
|
||||
|
||||
t.Run("a verification mail revoked before its send is cancelled, keeps its account, and stays out of the logs", func(t *testing.T) {
|
||||
reset(t)
|
||||
calls := len(fixture.Requests())
|
||||
user := harness.createUser(ctx, t, false)
|
||||
raw, _, deliveryID := harness.enqueueVerification(ctx, t, user, mailSenderTokenLifetime)
|
||||
if invalidated, err := harness.identity.EmailTokens().InvalidateActive(ctx, user.ID, domain.EmailTokenPurposeEmailVerification, harness.clock.Now()); err != nil || invalidated != 1 {
|
||||
t.Fatalf("InvalidateActive() = %d, %v; want 1", invalidated, err)
|
||||
}
|
||||
|
||||
sender := harness.newSender(t, harness.transactor, 100, 3000)
|
||||
if result := harness.pass(ctx, t, sender); result.Outcome != inbound.MailPassCancelled || result.CancelReason != domain.DeliveryCancelTokenRevoked {
|
||||
t.Fatalf("pass = %+v, want cancelled with reason %s", result, domain.DeliveryCancelTokenRevoked)
|
||||
}
|
||||
if again := harness.pass(ctx, t, sender); again.Outcome != inbound.MailPassIdle {
|
||||
t.Fatalf("pass after the cancellation = %+v, want idle", again)
|
||||
}
|
||||
if got := len(fixture.Requests()[calls:]); got != 0 {
|
||||
t.Fatalf("the provider received %d calls, want none for a revoked mail", got)
|
||||
}
|
||||
|
||||
cancelled := harness.delivery(ctx, t, deliveryID)
|
||||
switch {
|
||||
case cancelled.Status() != domain.DeliveryStatusCancelled || cancelled.CancelReason() != domain.DeliveryCancelTokenRevoked:
|
||||
t.Fatalf("delivery is %s (%s), want cancelled by a revoked token", cancelled.Status(), cancelled.CancelReason())
|
||||
case cancelled.ProviderAccount() != harness.account || cancelled.AttemptCount() != 0 || cancelled.ProviderMessageID() != "":
|
||||
t.Fatal("the cancellation changed the transport account or recorded an attempt")
|
||||
case !cancelled.Content().Redacted() || cancelled.Content().Text() != "":
|
||||
t.Fatal("the body of the cancelled mail is still stored")
|
||||
}
|
||||
// The address, the token, the link, and the account ID never reach the logs.
|
||||
harness.assertLogsHoldNoSecrets(t, user.Email.Address(), raw.Reveal(), user.ID.String(), "verify-email")
|
||||
})
|
||||
|
||||
t.Run("a delivery that cannot be restored is cancelled and does not block the queue", func(t *testing.T) {
|
||||
reset(t)
|
||||
calls := len(fixture.Requests())
|
||||
@@ -1695,13 +1808,34 @@ func (harness *senderHarness) createUser(ctx context.Context, t *testing.T, veri
|
||||
// enqueueVerification issues a token and stores its mail in one transaction,
|
||||
// as the registration flow will.
|
||||
func (harness *senderHarness) enqueueVerification(ctx context.Context, t *testing.T, user domain.User, lifetime time.Duration) (domain.SecretToken, domain.EmailToken, uuid.UUID) {
|
||||
t.Helper()
|
||||
return harness.enqueueToken(ctx, t, user, domain.EmailTokenPurposeEmailVerification, lifetime, harness.mailer.EnqueueVerification)
|
||||
}
|
||||
|
||||
// enqueuePasswordReset issues a password reset token and stores its mail in
|
||||
// one transaction, as the reset flow will.
|
||||
func (harness *senderHarness) enqueuePasswordReset(ctx context.Context, t *testing.T, user domain.User, lifetime time.Duration) (domain.SecretToken, domain.EmailToken, uuid.UUID) {
|
||||
t.Helper()
|
||||
return harness.enqueueToken(ctx, t, user, domain.EmailTokenPurposePasswordReset, lifetime, harness.mailer.EnqueuePasswordReset)
|
||||
}
|
||||
|
||||
// enqueueToken issues a token of purpose and stores the mail that enqueue
|
||||
// builds for it, in the same transaction as the token.
|
||||
func (harness *senderHarness) enqueueToken(
|
||||
ctx context.Context,
|
||||
t *testing.T,
|
||||
user domain.User,
|
||||
purpose domain.EmailTokenPurpose,
|
||||
lifetime time.Duration,
|
||||
enqueue func(context.Context, outbound.UnitOfWork, notification.TokenMailRequest) (uuid.UUID, error),
|
||||
) (domain.SecretToken, domain.EmailToken, uuid.UUID) {
|
||||
t.Helper()
|
||||
raw, err := domain.NewSecretToken()
|
||||
if err != nil {
|
||||
t.Fatalf("NewSecretToken() error = %v", err)
|
||||
}
|
||||
token, err := domain.NewEmailToken(domain.NewEmailTokenInput{
|
||||
UserID: user.ID, Purpose: domain.EmailTokenPurposeEmailVerification, TokenHash: raw.Hash(),
|
||||
UserID: user.ID, Purpose: purpose, TokenHash: raw.Hash(),
|
||||
RegistrationVersion: user.RegistrationVersion, IssuedAt: harness.clock.Now(), Lifetime: lifetime,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -1712,13 +1846,13 @@ func (harness *senderHarness) enqueueVerification(ctx context.Context, t *testin
|
||||
if err := unit.EmailTokens().Insert(ctx, token); err != nil {
|
||||
return err
|
||||
}
|
||||
deliveryID, err = harness.mailer.EnqueueVerification(ctx, unit, notification.TokenMailRequest{
|
||||
deliveryID, err = enqueue(ctx, unit, notification.TokenMailRequest{
|
||||
Recipient: user.Email, Token: token, RawToken: raw, Lifetime: domain.Hours(lifetime / time.Hour),
|
||||
})
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("enqueue the verification mail: %v", err)
|
||||
t.Fatalf("enqueue the %s mail: %v", purpose, err)
|
||||
}
|
||||
return raw, token, deliveryID
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user