Skip to content

Commit a021ca2

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

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
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: 39 additions & 7 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,14 +39,29 @@ 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

44-
if tt.Password == "" {
45-
t.Skip("no passwd skip")
46-
}
47-
4865
rsp, err := toolSet.(*emailToolSet).sendMail(context.Background(), &sendMailRequest{
4966
Auth: Auth{
5067
Name: tt.Name,
@@ -58,10 +75,19 @@ func TestMailTool_sendMail(t *testing.T) {
5875
},
5976
},
6077
})
78+
t.Logf("rsp: %+v err:%v", rsp, err)
79+
if tt.Password == "" {
80+
t.Logf("password is empty, skip")
81+
continue
82+
}
6183
if err != nil {
62-
t.Errorf("send mail err: %v", err)
84+
if tt.wantErr == true {
85+
t.Errorf("send mail err: %v", err)
86+
}
87+
} else if tt.wantErr == false {
88+
t.Errorf("should err but not")
6389
}
64-
t.Logf("rsp: %+v", rsp)
90+
6591
}
6692
}
6793

@@ -147,6 +173,12 @@ func Test_checkMailBoxType(t *testing.T) {
147173
want: MAIL_GMAIL,
148174
wantErr: false,
149175
},
176+
{
177+
name: "not valid email",
178+
args: args{email: "UserGmAiL.cOm"},
179+
want: MAIL_GMAIL,
180+
wantErr: true,
181+
},
150182
}
151183
for _, tt := range tests {
152184
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)