Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

2.8 KB
Raw Blame
package email

import (
	"context"
	"encoding/json"
	"testing"
)

func TestLogSender_Send(t *testing.T) {
	var called bool
	var gotTo, gotSubject string

	sender := NewLogSender(func(to, subject, body string) {
		called = true
		gotTo = to
		gotSubject = subject
		_ = body
	})

	err := sender.Send(context.Background(), Message{
		To:      "[email protected]",
		Subject: "Test Subject",
		Text:    "Hello",
	})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if !called {
		t.Fatal("log function was not called")
	}
	if gotTo != "[email protected]" {
		t.Errorf("expected [email protected], got %s", gotTo)
	}
	if gotSubject != "Test Subject" {
		t.Errorf("expected subject=Test Subject, got %s", gotSubject)
	}
}

func TestResendSender_New(t *testing.T) {
	sender := NewResendSender("re_test_key", "[email protected]")
	if sender == nil {
		t.Fatal("expected non-nil sender")
	}
	if sender.apiKey != "re_test_key" {
		t.Errorf("expected apiKey=re_test_key, got %s", sender.apiKey)
	}
	if sender.defaultReplyTo != "[email protected]" {
		t.Errorf("expected [email protected], got %s", sender.defaultReplyTo)
	}
}

func TestResendSender_RequestUsesDefaultReplyTo(t *testing.T) {
	sender := NewResendSender("re_test_key", " [email protected] ")

	payload := sender.requestForMessage(Message{
		From:    "Pulse <[email protected]>",
		To:      "[email protected]",
		Subject: "Sign in to Pulse",
		Text:    "Open this link.",
	})

	body, err := json.Marshal(payload)
	if err != nil {
		t.Fatalf("Marshal: %v", err)
	}
	if !json.Valid(body) {
		t.Fatalf("payload is not valid JSON: %s", body)
	}
	var got map[string]string
	if err := json.Unmarshal(body, &got); err != nil {
		t.Fatalf("Unmarshal: %v", err)
	}
	if got["reply_to"] != "[email protected]" {
		t.Fatalf("reply_to = %q, want [email protected]", got["reply_to"])
	}
}

func TestResendSender_RequestPrefersMessageReplyTo(t *testing.T) {
	sender := NewResendSender("re_test_key", "[email protected]")

	payload := sender.requestForMessage(Message{
		From:    "Pulse <[email protected]>",
		To:      "[email protected]",
		ReplyTo: " [email protected] ",
		Subject: "Sign in to Pulse",
		Text:    "Open this link.",
	})

	if payload.ReplyTo != "[email protected]" {
		t.Fatalf("ReplyTo = %q, want [email protected]", payload.ReplyTo)
	}
}

func TestMessage_Fields(t *testing.T) {
	msg := Message{
		From:    "[email protected]",
		To:      "[email protected]",
		ReplyTo: "[email protected]",
		Subject: "Hello",
		HTML:    "<h1>Hello</h1>",
		Text:    "Hello",
	}

	if msg.From != "[email protected]" {
		t.Errorf("unexpected From: %s", msg.From)
	}
	if msg.To != "[email protected]" {
		t.Errorf("unexpected To: %s", msg.To)
	}
	if msg.ReplyTo != "[email protected]" {
		t.Errorf("unexpected ReplyTo: %s", msg.ReplyTo)
	}
}