// mailer_test.go package mailer import ( "bytes" "fmt" "html/template" "strings" "testing" "git.linuxforward.com/byom/byom-onboard/internal/platform/config" ) func TestNewMailer(t *testing.T) { cfg := &config.MailerConfig{ Identity: "", Username: "test@example.com", Password: "testpass", Host: "localhost", Port: "2525", } mailer := NewMailer(cfg) if mailer == nil { t.Error("Expected non-nil mailer") } if mailer.serverAddr != "localhost:2525" { t.Errorf("Expected server address 'localhost:2525', got %s", mailer.serverAddr) } if mailer.from != "test@example.com" { t.Errorf("Expected from address 'test@example.com', got %s", mailer.from) } } func TestSendEmail(t *testing.T) { // Create test data data := &EmailData{ Username: "testuser", Password: "testpass", Subdomains: []string{"app1.example.com", "app2.example.com"}, WebAppURL: "https://main.example.com", SetupGuide: "1. Log in\n2. Configure settings", } // Create mailer with mock configuration cfg := &config.MailerConfig{ Identity: "", Username: "test@example.com", Password: "testpass", Host: "localhost", Port: "2525", } mailer := NewMailer(cfg) // Test email sending err := mailer.SendEmail("recipient@example.com", data) if err != nil { t.Errorf("Expected no error, got %v", err) } // Test email content // Note: In a real implementation, you might want to capture the email content // using a mock SMTP server and verify its contents } // TestEmailTemplateRendering tests the template rendering separately func TestEmailTemplateRendering(t *testing.T) { data := &EmailData{ Username: "testuser", Password: "testpass", Subdomains: []string{"app1.example.com", "app2.example.com"}, WebAppURL: "https://main.example.com", SetupGuide: "1. Log in\n2. Configure settings", } tmpl := template.Must(template.New("welcomeEmail").Parse(welcomeEmailTemplate)) var tpl bytes.Buffer err := tmpl.Execute(&tpl, data) if err != nil { t.Errorf("Template execution failed: %v", err) } result := tpl.String() fmt.Println(result) // Verify that all required information is present in the rendered template expectedContents := []string{ data.Username, data.Password, data.Subdomains[0], data.Subdomains[1], data.WebAppURL, data.SetupGuide, } for _, expected := range expectedContents { if !strings.Contains(result, expected) { t.Errorf("Expected email to contain '%s'", expected) } } }