Log redacted email provider errors and diagnose sender configuration (#57)
This commit is contained in:
1 parent
c1d28e705e
commit
3e4e496037
8 files changed
+327
-23
No files matched your search
@@ -2,18 +2,23 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.sendico.io/tech/sxchnge/notifications/email"
|
||||
"git.sendico.io/tech/sxchnge/notifications/model"
|
||||
merrors "git.sendico.io/tech/sxchnge/shared/errors"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zaptest/observer"
|
||||
)
|
||||
|
||||
type workerRepo struct {
|
||||
Repository
|
||||
job model.Notification
|
||||
saved *model.Notification
|
||||
job model.Notification
|
||||
saved *model.Notification
|
||||
finishErr error
|
||||
}
|
||||
|
||||
func (r *workerRepo) Recover(context.Context, time.Time) error { return nil }
|
||||
@@ -21,7 +26,10 @@ func (r *workerRepo) Claim(context.Context, string, time.Time) (*model.Notificat
|
||||
n := r.job
|
||||
return &n, nil
|
||||
}
|
||||
func (r *workerRepo) Finish(_ context.Context, n *model.Notification) error { r.saved = n; return nil }
|
||||
func (r *workerRepo) Finish(_ context.Context, n *model.Notification) error {
|
||||
r.saved = n
|
||||
return r.finishErr
|
||||
}
|
||||
|
||||
type workerSender struct {
|
||||
calls int
|
||||
@@ -62,3 +70,36 @@ func TestWorkerTerminalAndRetryOutcomes(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkerLogsProviderFailureOnce(t *testing.T) {
|
||||
for _, failPersistence := range []bool{false, true} {
|
||||
t.Run(fmt.Sprint(failPersistence), func(t *testing.T) {
|
||||
core, logs := observer.New(zap.WarnLevel)
|
||||
repo := &workerRepo{job: model.Notification{Driver: "resend", ExpiresAt: time.Now().Add(time.Hour), Attempts: 1, Email: model.Email{Address: "private@example.com", Subject: "Private subject", Text: "Private text"}}}
|
||||
if failPersistence {
|
||||
repo.finishErr = merrors.ServiceUnavailable("storage offline")
|
||||
}
|
||||
sender := &workerSender{outcome: email.Outcome{State: model.Failed, Code: "provider_authentication_failed", HTTPStatus: 403, ProviderError: "validation_error", ProviderMessage: "The sxchnge.com domain is not verified"}}
|
||||
svc := New(repo, nil, sender, "resend", zap.New(core))
|
||||
if _, err := svc.Dispatch(context.Background(), "id"); !errors.Is(err, repo.finishErr) {
|
||||
t.Fatal("unexpected persistence error", err)
|
||||
}
|
||||
entries := logs.All()
|
||||
if len(entries) != 1 || entries[0].Message != "Email delivery attempt finished" {
|
||||
t.Fatal("expected one provider outcome log", entries)
|
||||
}
|
||||
fields := entries[0].ContextMap()
|
||||
if fields["provider_status"] != int64(403) || fields["provider_error"] != "validation_error" || fields["provider_message"] != "The sxchnge.com domain is not verified" {
|
||||
t.Fatal("provider diagnostic missing", fields)
|
||||
}
|
||||
if repo.saved.FailureCode != "provider_authentication_failed" {
|
||||
t.Fatal("public failure code changed", repo.saved.FailureCode)
|
||||
}
|
||||
for _, field := range []string{"address", "email", "subject", "text", "html", "parameters"} {
|
||||
if _, ok := fields[field]; ok {
|
||||
t.Fatalf("private notification field %s logged", field)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user