feat: new emails design
This commit is contained in:
1 parent
4e41d60142
commit
30a4df47b1
15 files changed
+498
-68
No files matched your search
@@ -90,8 +90,8 @@ func TestRegistrationGateAndVerificationEmailHttp(t *testing.T) {
|
||||
if sent.Address != "alice@example.com" {
|
||||
t.Errorf("expected email sent to alice@example.com, got %q", sent.Address)
|
||||
}
|
||||
if sent.Template != "mailValidation" {
|
||||
t.Errorf("expected template mailValidation, got %q", sent.Template)
|
||||
if sent.Template != "verification" {
|
||||
t.Errorf("expected template verification, got %q", sent.Template)
|
||||
}
|
||||
|
||||
storedUser, err := userRepo.GetByUsername("alice")
|
||||
@@ -260,8 +260,8 @@ func TestRegistrationGateAndVerificationEmailUseCase(t *testing.T) {
|
||||
if mailRepo.SentEmails[0].Address != "carol@example.com" {
|
||||
t.Errorf("expected email sent to carol@example.com, got %q", mailRepo.SentEmails[0].Address)
|
||||
}
|
||||
if mailRepo.SentEmails[0].Template != "mailValidation" {
|
||||
t.Errorf("expected template mailValidation, got %q", mailRepo.SentEmails[0].Template)
|
||||
if mailRepo.SentEmails[0].Template != "verification" {
|
||||
t.Errorf("expected template verification, got %q", mailRepo.SentEmails[0].Template)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -65,8 +65,8 @@ func TestRequestPasswordResetHTTP(t *testing.T) {
|
||||
if mailRepo.SentEmails[0].Address != "alice@example.com" {
|
||||
t.Errorf("expected email to alice@example.com, got %s", mailRepo.SentEmails[0].Address)
|
||||
}
|
||||
if mailRepo.SentEmails[0].Template != "passwordReset" {
|
||||
t.Errorf("expected template passwordReset, got %s", mailRepo.SentEmails[0].Template)
|
||||
if mailRepo.SentEmails[0].Template != "password-reset" {
|
||||
t.Errorf("expected template password-reset, got %s", mailRepo.SentEmails[0].Template)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -204,8 +204,8 @@ func TestRequestPasswordResetHTTP(t *testing.T) {
|
||||
if mailRepo.SentEmails[0].Address != "bob@example.com" {
|
||||
t.Errorf("expected email to bob@example.com, got %s", mailRepo.SentEmails[0].Address)
|
||||
}
|
||||
if mailRepo.SentEmails[0].Template != "passwordReset" {
|
||||
t.Errorf("expected template passwordReset, got %s", mailRepo.SentEmails[0].Template)
|
||||
if mailRepo.SentEmails[0].Template != "password-reset" {
|
||||
t.Errorf("expected template password-reset, got %s", mailRepo.SentEmails[0].Template)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -54,8 +54,8 @@ func TestRequestPasswordResetUseCase(t *testing.T) {
|
||||
if sent.Address != "alice@example.com" {
|
||||
t.Errorf("expected email to alice@example.com, got %s", sent.Address)
|
||||
}
|
||||
if sent.Template != "passwordReset" {
|
||||
t.Errorf("expected template passwordReset, got %s", sent.Template)
|
||||
if sent.Template != "password-reset" {
|
||||
t.Errorf("expected template password-reset, got %s", sent.Template)
|
||||
}
|
||||
data, ok := sent.Data.(useCases.MailPasswordResetData)
|
||||
if !ok {
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/infrastructure/config"
|
||||
"RenewCMS/internal/infrastructure/mailer"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTransactionalEmailTemplates(t *testing.T) {
|
||||
t.Run("render() produces the verification email's subject, link and copy", func(t *testing.T) {
|
||||
repo := mailer.NewMailRepository(config.SmtpSettings{})
|
||||
|
||||
subject, body, err := repo.Render("verification", useCases.MailValidationData{
|
||||
Host: "https://cms.example.com",
|
||||
VerificationCode: "abc123",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if subject != "Verify your RenewCMS account" {
|
||||
t.Errorf("expected subject %q, got %q", "Verify your RenewCMS account", subject)
|
||||
}
|
||||
if !strings.Contains(body, "https://cms.example.com/register/validate?c=abc123") {
|
||||
t.Errorf("expected body to contain the verification link, got %q", body)
|
||||
}
|
||||
if !strings.Contains(body, "Verify my account") {
|
||||
t.Errorf("expected body to contain the button text, got %q", body)
|
||||
}
|
||||
if !strings.Contains(body, "This link expires in 2 hours.") {
|
||||
t.Errorf("expected body to contain the expiry copy, got %q", body)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("render() produces the password-reset email's subject, link and copy", func(t *testing.T) {
|
||||
repo := mailer.NewMailRepository(config.SmtpSettings{})
|
||||
|
||||
subject, body, err := repo.Render("password-reset", useCases.MailPasswordResetData{
|
||||
Host: "https://cms.example.com",
|
||||
VerificationCode: "abc123",
|
||||
Email: "alice@example.com",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if subject != "Reset your RenewCMS password" {
|
||||
t.Errorf("expected subject %q, got %q", "Reset your RenewCMS password", subject)
|
||||
}
|
||||
if !strings.Contains(body, "https://cms.example.com/register/reset/validate?c=abc123&email=alice%40example.com") {
|
||||
t.Errorf("expected body to contain the password-reset link, got %q", body)
|
||||
}
|
||||
if !strings.Contains(body, "Choose a new password") {
|
||||
t.Errorf("expected body to contain the button text, got %q", body)
|
||||
}
|
||||
if !strings.Contains(body, "This link expires in 1 hour and works once.") {
|
||||
t.Errorf("expected body to contain the expiry copy, got %q", body)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("render() falls back to the old default subject for an unrecognized template name", func(t *testing.T) {
|
||||
repo := mailer.NewMailRepository(config.SmtpSettings{})
|
||||
|
||||
subject, body, err := repo.Render("does-not-exist", nil)
|
||||
|
||||
if subject != "RenewCMS | Action required" {
|
||||
t.Errorf("expected fallback subject, got %q", subject)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("expected an error because templates/does-not-exist.html cannot be found")
|
||||
}
|
||||
if body != "" {
|
||||
t.Errorf("expected empty body, got %q", body)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("A template execution failure is returned without sending", func(t *testing.T) {
|
||||
repo := mailer.NewMailRepository(config.SmtpSettings{})
|
||||
|
||||
type incompatibleData struct {
|
||||
Unrelated int
|
||||
}
|
||||
|
||||
_, body, err := repo.Render("verification", incompatibleData{Unrelated: 1})
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("expected an error from a failed template execution")
|
||||
}
|
||||
if body != "" {
|
||||
t.Errorf("expected empty body, got %q", body)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Send() returns a render error without dialing SMTP", func(t *testing.T) {
|
||||
repo := mailer.NewMailRepository(config.SmtpSettings{})
|
||||
|
||||
_, _, renderErr := repo.Render("does-not-exist", nil)
|
||||
err := repo.Send("someone@example.com", "does-not-exist", nil)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("expected an error from a failed render, got nil")
|
||||
}
|
||||
if err.Error() != renderErr.Error() {
|
||||
t.Errorf("expected Send to return the render error %q, got %q", renderErr, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("The old template files no longer exist under their previous names", func(t *testing.T) {
|
||||
for _, oldFile := range []string{"mailValidation.html", "passwordReset.html"} {
|
||||
path := "../internal/infrastructure/mailer/templates/" + oldFile
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
t.Errorf("expected %s to not exist, got err=%v", path, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user