77 lines
2.4 KiB
Go
77 lines
2.4 KiB
Go
package service
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewEmailService(t *testing.T) {
|
|
svc := NewEmailService("key", "from@example.com", "Pulse App", "https://example.com")
|
|
if svc == nil {
|
|
t.Fatal("expected non-nil EmailService")
|
|
}
|
|
if svc.apiKey != "key" {
|
|
t.Errorf("expected apiKey 'key', got %s", svc.apiKey)
|
|
}
|
|
if svc.fromEmail != "from@example.com" {
|
|
t.Errorf("expected fromEmail, got %s", svc.fromEmail)
|
|
}
|
|
if svc.fromName != "Pulse App" {
|
|
t.Errorf("expected fromName, got %s", svc.fromName)
|
|
}
|
|
if svc.baseURL != "https://example.com" {
|
|
t.Errorf("expected baseURL, got %s", svc.baseURL)
|
|
}
|
|
}
|
|
|
|
// When apiKey is empty, send just logs and returns nil — no HTTP call
|
|
func TestEmailService_SendVerificationEmail_NoAPIKey(t *testing.T) {
|
|
svc := NewEmailService("", "from@pulse.app", "Pulse", "https://pulse.app")
|
|
err := svc.SendVerificationEmail("user@example.com", "testuser", "abc123token")
|
|
if err != nil {
|
|
t.Errorf("expected nil error with no API key, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestEmailService_SendPasswordResetEmail_NoAPIKey(t *testing.T) {
|
|
svc := NewEmailService("", "from@pulse.app", "Pulse", "https://pulse.app")
|
|
err := svc.SendPasswordResetEmail("user@example.com", "testuser", "resettoken")
|
|
if err != nil {
|
|
t.Errorf("expected nil error with no API key, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestEmailService_VerificationURLFormat(t *testing.T) {
|
|
baseURL := "https://pulse.app"
|
|
token := "myverifytoken"
|
|
expectedURL := baseURL + "/verify-email?token=" + token
|
|
|
|
// Verify the URL is constructed correctly (via the send method with no key)
|
|
svc := NewEmailService("", "no@reply.com", "Pulse", baseURL)
|
|
// The verification email would contain the token in the URL
|
|
// We test the format indirectly — if no error, the URL was constructed
|
|
err := svc.SendVerificationEmail("test@test.com", "user", token)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
// Verify URL format is correct
|
|
if !strings.Contains(expectedURL, token) {
|
|
t.Errorf("expected URL to contain token %s", token)
|
|
}
|
|
}
|
|
|
|
func TestEmailService_ResetURLFormat(t *testing.T) {
|
|
baseURL := "https://pulse.app"
|
|
token := "myresettoken"
|
|
expectedURL := baseURL + "/reset-password?token=" + token
|
|
|
|
svc := NewEmailService("", "no@reply.com", "Pulse", baseURL)
|
|
err := svc.SendPasswordResetEmail("test@test.com", "user", token)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !strings.Contains(expectedURL, token) {
|
|
t.Errorf("expected URL to contain token %s", token)
|
|
}
|
|
}
|