-
Notifications
You must be signed in to change notification settings - Fork 0
/
newman_test.go
117 lines (91 loc) · 3.37 KB
/
newman_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
package newman
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 := "[email protected]"
to := []string{"[email protected]"}
subject := "Look sister, go get yourself a cup of coffee or something"
body := "And three times a week I shall require a cannoli"
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 TestNewEmailMessageWithOptions(t *testing.T) {
from := "[email protected]"
to := []string{"[email protected]"}
subject := "Look sister, go get yourself a cup of coffee or something"
body := "And three times a week I shall require a cannoli"
html := "<p>And three times a week I shall require a cannoli</p>"
emailMessage := NewEmailMessageWithOptions(
WithFrom(from),
WithTo(to),
WithSubject(subject),
WithText(body),
WithHTML(html),
)
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.Equal(t, html, emailMessage.GetHTML())
assert.Empty(t, emailMessage.GetBCC())
assert.Empty(t, emailMessage.GetCC())
}
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 := "[email protected]"
to := []string{"[email protected]"}
subject := "Look sister, go get yourself a cup of coffee or something"
body := "And three times a week I shall require a cannoli"
emailMessage := NewEmailMessage(from, to, subject, body)
mimeMessage, err := BuildMimeMessage(emailMessage)
assert.NoError(t, err)
assert.NotEmpty(t, mimeMessage)
}
func TestValidateEmail(t *testing.T) {
validEmail := "[email protected]"
invalidEmail := "invalid-email"
assert.Equal(t, validEmail, ValidateEmail(validEmail))
assert.Empty(t, ValidateEmail(invalidEmail))
}
func TestValidateEmailSlice(t *testing.T) {
emails := []string{"[email protected]", "invalid-email"}
validEmails := ValidateEmailSlice(emails)
assert.Len(t, validEmails, 1)
assert.Equal(t, "[email protected]", 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("[email protected]", []string{"[email protected]"}, "Look sister, go get yourself a cup of coffee or something", "And three times a week I shall require a cannoli")
err := sender.SendEmail(message)
assert.NoError(t, err)
}