Skip to content

Commit f82be7f

Browse files
committed
tests: email tool add more test case
1 parent cbd3b39 commit f82be7f

File tree

2 files changed

+100
-7
lines changed

2 files changed

+100
-7
lines changed

tool/email/sendmail.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ type sendMailResponse struct {
5252
}
5353

5454
// sendMail performs the send mail operation.
55-
// go smtp not support context, one send one mail, can't stop
5655
func (e *emailToolSet) sendMail(ctx context.Context, req *sendMailRequest) (rsp *sendMailResponse, err error) {
5756
rsp = &sendMailResponse{}
5857

@@ -81,7 +80,7 @@ func (e *emailToolSet) sendMail(ctx context.Context, req *sendMailRequest) (rsp
8180
opts...,
8281
)
8382
if err != nil {
84-
rsp.Message = fmt.Sprintf("the address or password is incorrect,please check: %v", err)
83+
rsp.Message = fmt.Sprintf("the server address err: %v", err)
8584
return rsp, nil
8685
}
8786

tool/email/sendmail_test.go

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10-
func TestMailTool_sendMail(t *testing.T) {
10+
func Test_emailToolSet_sendMail(t *testing.T) {
1111
toolSet, err := NewToolSet(
1212
WithSendEmailEnabled(true),
1313
)
@@ -21,6 +21,7 @@ func TestMailTool_sendMail(t *testing.T) {
2121
ToEmail string
2222
Subject string
2323
Content string
24+
wantErr bool
2425
}{
2526

2627
{
@@ -29,6 +30,7 @@ func TestMailTool_sendMail(t *testing.T) {
2930
ToEmail: "[email protected]",
3031
Subject: "test",
3132
Content: "test",
33+
wantErr: false,
3234
},
3335

3436
{
@@ -37,13 +39,94 @@ func TestMailTool_sendMail(t *testing.T) {
3739
ToEmail: "[email protected]",
3840
Subject: "test",
3941
Content: "test",
42+
wantErr: false,
43+
},
44+
// error case
45+
{
46+
Name: "18503@[email protected]",
47+
Password: "",
48+
ToEmail: "[email protected]",
49+
Subject: "test",
50+
Content: "test",
51+
wantErr: true,
52+
},
53+
// error case
54+
{
55+
56+
Password: "",
57+
ToEmail: "185039@[email protected]",
58+
Subject: "test",
59+
Content: "test",
60+
wantErr: true,
4061
},
4162
}
4263
for _, tt := range tests {
4364

65+
rsp, err := toolSet.(*emailToolSet).sendMail(context.Background(), &sendMailRequest{
66+
Auth: Auth{
67+
Name: tt.Name,
68+
Password: tt.Password,
69+
},
70+
MailList: []*Mail{
71+
{
72+
ToEmail: tt.ToEmail,
73+
Subject: tt.Subject,
74+
Content: tt.Content,
75+
},
76+
},
77+
})
78+
t.Logf("rsp: %+v err:%v", rsp, err)
4479
if tt.Password == "" {
45-
t.Skip("no passwd skip")
80+
t.Logf("password is empty, skip")
81+
continue
4682
}
83+
if rsp.Message != "" {
84+
if tt.wantErr == false {
85+
t.Errorf("send mail err: %s", rsp.Message)
86+
}
87+
} else if tt.wantErr == true {
88+
t.Errorf("should err but not")
89+
}
90+
91+
}
92+
}
93+
94+
func Test_emailToolSet_sendMail2(t *testing.T) {
95+
toolSet, err := NewToolSet(
96+
WithSendEmailEnabled(true),
97+
)
98+
if err != nil {
99+
t.Errorf("NewToolSet failed, err: %v", err)
100+
}
101+
102+
tests := []struct {
103+
Name string
104+
Password string
105+
ToEmail string
106+
Subject string
107+
Content string
108+
wantErr bool
109+
}{
110+
// error case
111+
{
112+
Name: "18503@[email protected]",
113+
Password: "",
114+
ToEmail: "[email protected]",
115+
Subject: "test",
116+
Content: "test",
117+
wantErr: true,
118+
},
119+
// error case
120+
{
121+
122+
Password: "",
123+
ToEmail: "185039@[email protected]",
124+
Subject: "test",
125+
Content: "test",
126+
wantErr: true,
127+
},
128+
}
129+
for _, tt := range tests {
47130

48131
rsp, err := toolSet.(*emailToolSet).sendMail(context.Background(), &sendMailRequest{
49132
Auth: Auth{
@@ -58,10 +141,15 @@ func TestMailTool_sendMail(t *testing.T) {
58141
},
59142
},
60143
})
61-
if err != nil {
62-
t.Errorf("send mail err: %v", err)
144+
t.Logf("rsp: %+v err:%v", rsp, err)
145+
if rsp.Message != "" {
146+
if tt.wantErr == false {
147+
t.Errorf("send mail err: %s", rsp.Message)
148+
}
149+
} else if tt.wantErr == true {
150+
t.Errorf("should err but not")
63151
}
64-
t.Logf("rsp: %+v", rsp)
152+
65153
}
66154
}
67155

@@ -147,6 +235,12 @@ func Test_checkMailBoxType(t *testing.T) {
147235
want: MAIL_GMAIL,
148236
wantErr: false,
149237
},
238+
{
239+
name: "not valid email",
240+
args: args{email: "UserGmAiL.cOm"},
241+
want: MAIL_GMAIL,
242+
wantErr: true,
243+
},
150244
}
151245
for _, tt := range tests {
152246
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)