mailer_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // mailer_test.go
  2. package mailer
  3. import (
  4. "bytes"
  5. "fmt"
  6. "html/template"
  7. "strings"
  8. "testing"
  9. "git.linuxforward.com/byom/byom-onboard/internal/platform/config"
  10. )
  11. func TestNewMailer(t *testing.T) {
  12. cfg := &config.MailerConfig{
  13. Identity: "",
  14. Username: "test@example.com",
  15. Password: "testpass",
  16. Host: "localhost",
  17. Port: "2525",
  18. }
  19. mailer := NewMailer(cfg)
  20. if mailer == nil {
  21. t.Error("Expected non-nil mailer")
  22. }
  23. if mailer.serverAddr != "localhost:2525" {
  24. t.Errorf("Expected server address 'localhost:2525', got %s", mailer.serverAddr)
  25. }
  26. if mailer.from != "test@example.com" {
  27. t.Errorf("Expected from address 'test@example.com', got %s", mailer.from)
  28. }
  29. }
  30. func TestSendEmail(t *testing.T) {
  31. // Create test data
  32. data := &EmailData{
  33. Username: "testuser",
  34. Password: "testpass",
  35. Subdomains: []string{"app1.example.com", "app2.example.com"},
  36. WebAppURL: "https://main.example.com",
  37. SetupGuide: "1. Log in\n2. Configure settings",
  38. }
  39. // Create mailer with mock configuration
  40. cfg := &config.MailerConfig{
  41. Identity: "",
  42. Username: "test@example.com",
  43. Password: "testpass",
  44. Host: "localhost",
  45. Port: "2525",
  46. }
  47. mailer := NewMailer(cfg)
  48. // Test email sending
  49. err := mailer.SendEmail("recipient@example.com", data)
  50. if err != nil {
  51. t.Errorf("Expected no error, got %v", err)
  52. }
  53. // Test email content
  54. // Note: In a real implementation, you might want to capture the email content
  55. // using a mock SMTP server and verify its contents
  56. }
  57. // TestEmailTemplateRendering tests the template rendering separately
  58. func TestEmailTemplateRendering(t *testing.T) {
  59. data := &EmailData{
  60. Username: "testuser",
  61. Password: "testpass",
  62. Subdomains: []string{"app1.example.com", "app2.example.com"},
  63. WebAppURL: "https://main.example.com",
  64. SetupGuide: "1. Log in\n2. Configure settings",
  65. }
  66. tmpl := template.Must(template.New("welcomeEmail").Parse(welcomeEmailTemplate))
  67. var tpl bytes.Buffer
  68. err := tmpl.Execute(&tpl, data)
  69. if err != nil {
  70. t.Errorf("Template execution failed: %v", err)
  71. }
  72. result := tpl.String()
  73. fmt.Println(result)
  74. // Verify that all required information is present in the rendered template
  75. expectedContents := []string{
  76. data.Username,
  77. data.Password,
  78. data.Subdomains[0],
  79. data.Subdomains[1],
  80. data.WebAppURL,
  81. data.SetupGuide,
  82. }
  83. for _, expected := range expectedContents {
  84. if !strings.Contains(result, expected) {
  85. t.Errorf("Expected email to contain '%s'", expected)
  86. }
  87. }
  88. }