-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
145 lines (117 loc) Β· 2.83 KB
/
client_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package wail
import (
"os"
"testing"
"time"
)
func testClientNoConfig() *SmtpClient {
return NewClient(nil)
}
func testConfigWithAuth() *SmtpClient {
cfg := &SmtpConfig{
Server: ServerConfig{
Host: "smtp.mail.ru",
Port: 465,
NeedAuth: true,
ConnectTimeout: 10 * time.Second,
},
Sender: SenderConfig{
Name: "Test",
Login: os.Getenv("SENDER_LOGIN"),
Password: os.Getenv("SENDER_PWD"),
},
}
return NewClient(cfg)
}
func testConfigEmptyLoginPassword() *SmtpClient {
cfg := &SmtpConfig{
Server: ServerConfig{
Host: "smtp.mail.ru",
Port: 465,
NeedAuth: true,
},
Sender: SenderConfig{
Name: "Alex",
},
}
return NewClient(cfg)
}
func testConfigNoAuth() *SmtpClient {
cfg := &SmtpConfig{
Server: ServerConfig{
Host: "smtp.mail.ru",
Port: 465,
NeedAuth: false,
},
}
return NewClient(cfg)
}
func testConfigNoEncrypt() *SmtpClient {
cfg := &SmtpConfig{
Server: ServerConfig{
Host: "smtp.mail.ru",
Port: 465,
NeedAuth: false,
EncryptType: EncryptNone,
ConnectTimeout: 10 * time.Second,
},
}
return NewClient(cfg)
}
func testEmptyConfig() *SmtpClient {
return NewClient(&SmtpConfig{})
}
func TestDial(t *testing.T) {
if err := testClientNoConfig().Dial(); err == nil {
t.Error("smtp config should be provided")
}
if err := testConfigWithAuth().Dial(); err != nil {
t.Errorf("testConfigWithAuth test failed: %v", err)
}
if err := testConfigEmptyLoginPassword().Dial(); err == nil {
t.Error("can't do Auth with an empty sender login and password")
}
if err := testConfigNoAuth().Dial(); err != nil {
t.Errorf("testConfigNoAuth test failed: %v", err)
}
// testConfigNoEncrypt should fails because
// the specified server demands an encrypt connection
if err := testConfigNoEncrypt().Dial(); err == nil {
t.Error("server expects an encrypt connection")
}
if err := testEmptyConfig().Dial(); err == nil {
t.Error("config should not be empty")
}
}
func TestClose(t *testing.T) {
// Do Close() before Dial()
if err := testClientNoConfig().Close(); err == nil {
t.Error("can't do Close() before Dial()")
}
}
func TestSend(t *testing.T) {
// Do Send() before Dial()
if err := testClientNoConfig().Send(nil); err == nil {
t.Error("can't do Send() before Dial()")
}
// Do Send() a nil mail
c := testConfigWithAuth()
c.Dial()
defer c.Close()
if err := c.Send(nil); err == nil {
t.Error("can't send a nil mail")
}
// Do Send() -> Close() -> Send()
// This test checks whether reconnection to the server is being performed
mail := NewMail(nil)
mail.SetSubject("ΡΠ΅ΠΌΠ°")
mail.To("example@example.com")
mt := NewTextMessage()
mt.Set(TextPlain, []byte("Hello, World"))
mail.SetMessage(&mt)
c.Send(mail)
c.Close()
if err := c.Send(mail); err != nil {
t.Error(err)
}
}