57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package test
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/mail"
|
|
"RenewCMS/internal/infrastructure/useCases"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
type recordedMail struct {
|
|
Address string
|
|
Template string
|
|
Data any
|
|
}
|
|
|
|
type mockMailRepo struct {
|
|
SentEmails []recordedMail
|
|
fail failSwitch
|
|
}
|
|
|
|
func newMockMailRepo() *mockMailRepo {
|
|
return &mockMailRepo{}
|
|
}
|
|
|
|
func (m *mockMailRepo) Send(receiverAddress string, templateName string, data any) error {
|
|
if err := m.fail.trigger("Send"); err != nil {
|
|
return err
|
|
}
|
|
m.SentEmails = append(m.SentEmails, recordedMail{receiverAddress, templateName, data})
|
|
return nil
|
|
}
|
|
|
|
var _ mail.Repository = &mockMailRepo{}
|
|
|
|
func TestSendMail(t *testing.T) {
|
|
repo := newMockMailRepo()
|
|
uc := useCases.NewSendMailUseCase(repo)
|
|
|
|
t.Run("Successful Email Send", func(t *testing.T) {
|
|
err := uc.SendMail("user@example.com", "welcome", "User123")
|
|
if err != nil {
|
|
t.Fatalf("Expected no error, got %v", err)
|
|
}
|
|
if len(repo.SentEmails) != 1 {
|
|
t.Errorf("Expected 1 email to be sent, got %d", len(repo.SentEmails))
|
|
}
|
|
})
|
|
|
|
t.Run("Failed Email Send", func(t *testing.T) {
|
|
repo.fail.failNext("Send")
|
|
err := uc.SendMail("fail@example.com", "reset", nil)
|
|
if !errors.Is(err, errRepositoryFailure) {
|
|
t.Errorf("Expected repository failure, got %v", err)
|
|
}
|
|
})
|
|
}
|