123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- // 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)
- }
- }
- }
|