-
Notifications
You must be signed in to change notification settings - Fork 1
/
gomail_test.go
119 lines (93 loc) · 3.39 KB
/
gomail_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package gomail
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type MockEmailSender struct{}
func (m *MockEmailSender) SendEmail(message *EmailMessage) error {
return nil
}
func TestNewEmailMessage(t *testing.T) {
from := "sender@example.com"
to := []string{"recipient@example.com"}
subject := "Test Subject"
body := "This is a test email body."
emailMessage := NewEmailMessage(from, to, subject, body)
assert.Equal(t, from, emailMessage.GetFrom())
assert.Equal(t, to, emailMessage.GetTo())
assert.Equal(t, subject, emailMessage.GetSubject())
assert.Equal(t, body, emailMessage.GetText())
assert.Empty(t, emailMessage.GetHTML())
}
func TestNewFullEmailMessage(t *testing.T) {
from := "sender@example.com"
to := []string{"recipient@example.com"}
cc := []string{"cc@example.com"}
bcc := []string{"bcc@example.com"}
replyTo := "replyto@example.com"
subject := "Test Subject"
textBody := "This is a test text body."
htmlBody := "<p>This is a test HTML body.</p>"
attachments := []Attachment{
*NewAttachment("test.txt", []byte("test content")),
}
emailMessage := NewFullEmailMessage(from, to, subject, cc, bcc, replyTo, textBody, htmlBody, attachments)
assert.Equal(t, from, emailMessage.GetFrom())
assert.Equal(t, to, emailMessage.GetTo())
assert.Equal(t, cc, emailMessage.GetCC())
assert.Equal(t, bcc, emailMessage.GetBCC())
assert.Equal(t, replyTo, emailMessage.GetReplyTo())
assert.Equal(t, subject, emailMessage.GetSubject())
assert.Equal(t, textBody, emailMessage.GetText())
assert.Equal(t, htmlBody, emailMessage.GetHTML())
assert.Equal(t, attachments, emailMessage.GetAttachments())
}
func TestNewAttachment(t *testing.T) {
filename := "test.txt"
content := []byte("test content")
attachment := NewAttachment(filename, content)
assert.Equal(t, filename, attachment.GetFilename())
assert.Equal(t, content, attachment.GetRawContent())
}
func TestNewAttachmentFromFile(t *testing.T) {
filePath := "testdata/testfile.txt"
content, err := os.ReadFile(filePath)
assert.NoError(t, err)
attachment, err := NewAttachmentFromFile(filePath)
assert.NoError(t, err)
assert.Equal(t, "testfile.txt", attachment.GetFilename())
assert.Equal(t, content, attachment.GetRawContent())
}
func TestBuildMimeMessage(t *testing.T) {
from := "sender@example.com"
to := []string{"recipient@example.com"}
subject := "Test Subject"
body := "This is a test email body."
emailMessage := NewEmailMessage(from, to, subject, body)
mimeMessage, err := BuildMimeMessage(emailMessage)
assert.NoError(t, err)
assert.NotEmpty(t, mimeMessage)
}
func TestValidateEmail(t *testing.T) {
validEmail := "valid@example.com"
invalidEmail := "invalid-email"
assert.Equal(t, validEmail, ValidateEmail(validEmail))
assert.Empty(t, ValidateEmail(invalidEmail))
}
func TestValidateEmailSlice(t *testing.T) {
emails := []string{"valid@example.com", "invalid-email"}
validEmails := ValidateEmailSlice(emails)
assert.Len(t, validEmails, 1)
assert.Equal(t, "valid@example.com", validEmails[0])
}
func TestGetMimeType(t *testing.T) {
assert.Contains(t, GetMimeType("test.txt"), "text/plain")
assert.Contains(t, GetMimeType("tarFile.tar"), "application/x-tar")
}
func TestSendEmail(t *testing.T) {
sender := &MockEmailSender{}
message := NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Subject", "This is a test email body.")
err := sender.SendEmail(message)
assert.NoError(t, err)
}