a7eb3cda81
* Sending mails from password reset now we are really sending the mail * Must set Mail configuration
141 lines
2.8 KiB
Go
141 lines
2.8 KiB
Go
package mail
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"bamort/config"
|
|
)
|
|
|
|
// setupTestEnvironment setzt die Test-Umgebung auf
|
|
func setupTestEnvironment(t *testing.T) {
|
|
original := os.Getenv("ENVIRONMENT")
|
|
os.Setenv("ENVIRONMENT", "test")
|
|
t.Cleanup(func() {
|
|
if original != "" {
|
|
os.Setenv("ENVIRONMENT", original)
|
|
} else {
|
|
os.Unsetenv("ENVIRONMENT")
|
|
}
|
|
})
|
|
// Reload config with test environment
|
|
config.Cfg = config.LoadConfig()
|
|
}
|
|
|
|
func TestNewClient(t *testing.T) {
|
|
setupTestEnvironment(t)
|
|
|
|
// Set test mail config
|
|
os.Setenv("MAIL_HOST", "smtp.example.com")
|
|
os.Setenv("MAIL_PORT", "465")
|
|
os.Setenv("MAIL_USERNAME", "test@example.com")
|
|
os.Setenv("MAIL_PASSWORD", "testpass")
|
|
os.Setenv("MAIL_FROM", "sender@example.com")
|
|
|
|
// Reload config
|
|
config.Cfg = config.LoadConfig()
|
|
|
|
client := NewClient()
|
|
|
|
if client.host != "smtp.example.com" {
|
|
t.Errorf("Expected host 'smtp.example.com', got '%s'", client.host)
|
|
}
|
|
if client.port != 465 {
|
|
t.Errorf("Expected port 465, got %d", client.port)
|
|
}
|
|
if client.username != "test@example.com" {
|
|
t.Errorf("Expected username 'test@example.com', got '%s'", client.username)
|
|
}
|
|
if client.from != "sender@example.com" {
|
|
t.Errorf("Expected from 'sender@example.com', got '%s'", client.from)
|
|
}
|
|
}
|
|
|
|
func TestIsConfigured(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
client *Client
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "Fully configured",
|
|
client: &Client{
|
|
host: "smtp.example.com",
|
|
port: 465,
|
|
from: "test@example.com",
|
|
},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "Missing host",
|
|
client: &Client{
|
|
host: "",
|
|
port: 465,
|
|
from: "test@example.com",
|
|
},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "Missing port",
|
|
client: &Client{
|
|
host: "smtp.example.com",
|
|
port: 0,
|
|
from: "test@example.com",
|
|
},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "Missing from",
|
|
client: &Client{
|
|
host: "smtp.example.com",
|
|
port: 465,
|
|
from: "",
|
|
},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "Empty client",
|
|
client: &Client{
|
|
host: "",
|
|
port: 0,
|
|
from: "",
|
|
},
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := tt.client.IsConfigured()
|
|
if result != tt.expected {
|
|
t.Errorf("Expected IsConfigured() to return %v, got %v", tt.expected, result)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSend_NotConfigured(t *testing.T) {
|
|
setupTestEnvironment(t)
|
|
|
|
// Create client without config
|
|
client := &Client{
|
|
host: "",
|
|
port: 0,
|
|
from: "",
|
|
}
|
|
|
|
msg := Message{
|
|
To: "recipient@example.com",
|
|
Subject: "Test",
|
|
Body: "Test body",
|
|
}
|
|
|
|
err := client.Send(msg)
|
|
if err == nil {
|
|
t.Error("Expected error when sending with unconfigured client, got nil")
|
|
}
|
|
if err.Error() != "SMTP host not configured" {
|
|
t.Errorf("Expected error 'SMTP host not configured', got '%s'", err.Error())
|
|
}
|
|
}
|