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() produces the verify-new-email email's subject and link", func(t *testing.T) { repo := mailer.NewMailRepository(config.SmtpSettings{}) subject, body, err := repo.Render("verify-new-email", useCases.MailVerifyNewEmailData{ Host: "https://cms.example.com", Username: "marta", VerificationCode: "abc", }) if err != nil { t.Fatalf("expected no error, got %v", err) } if subject != "Confirm your new RenewCMS email address" { t.Errorf("unexpected subject %q", subject) } if !strings.Contains(body, "https://cms.example.com/account/email/confirm?c=abc") || strings.Contains(body, "{{") { t.Errorf("expected the confirmation link and no unreplaced action, got %q", body) } }) t.Run("render() produces the email-change-notice email's subject, new address and profile link", func(t *testing.T) { repo := mailer.NewMailRepository(config.SmtpSettings{}) subject, body, err := repo.Render("email-change-notice", useCases.MailEmailChangeNoticeData{ Host: "https://cms.example.com", Username: "marta", NewEmail: "new@renew.example", }) if err != nil { t.Fatalf("expected no error, got %v", err) } if subject != "Your RenewCMS email address change was requested" { t.Errorf("unexpected subject %q", subject) } if !strings.Contains(body, "new@renew.example") || !strings.Contains(body, "https://cms.example.com/profile") || strings.Contains(body, "{{") { t.Errorf("expected the new address, the profile link and no unreplaced action, got %q", body) } }) t.Run("render() produces the account-deleted email without personal data", func(t *testing.T) { repo := mailer.NewMailRepository(config.SmtpSettings{}) subject, body, err := repo.Render("account-deleted", useCases.MailAccountDeletedData{Host: "https://cms.example.com"}) if err != nil { t.Fatalf("expected no error, got %v", err) } if subject != "Your RenewCMS account has been deleted" { t.Errorf("unexpected subject %q", subject) } if !strings.Contains(body, "https://cms.example.com") || strings.Contains(body, "{{") || strings.Contains(body, "@renew") || strings.Contains(body, "