Skip to content

Commit 270d9ee

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

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-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: 81 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,76 @@ func TestMailTool_sendMail(t *testing.T) {
3739
ToEmail: "[email protected]",
3840
Subject: "test",
3941
Content: "test",
42+
wantErr: false,
4043
},
4144
}
4245
for _, tt := range tests {
4346

47+
rsp, err := toolSet.(*emailToolSet).sendMail(context.Background(), &sendMailRequest{
48+
Auth: Auth{
49+
Name: tt.Name,
50+
Password: tt.Password,
51+
},
52+
MailList: []*Mail{
53+
{
54+
ToEmail: tt.ToEmail,
55+
Subject: tt.Subject,
56+
Content: tt.Content,
57+
},
58+
},
59+
})
60+
t.Logf("rsp: %+v err:%v", rsp, err)
4461
if tt.Password == "" {
45-
t.Skip("no passwd skip")
62+
t.Logf("password is empty, skip")
63+
continue
4664
}
65+
if rsp.Message != "" {
66+
if tt.wantErr == false {
67+
t.Errorf("send mail err: %s", rsp.Message)
68+
}
69+
} else if tt.wantErr == true {
70+
t.Errorf("should err but not")
71+
}
72+
73+
}
74+
}
75+
76+
func Test_emailToolSet_sendMail2(t *testing.T) {
77+
toolSet, err := NewToolSet(
78+
WithSendEmailEnabled(true),
79+
)
80+
if err != nil {
81+
t.Errorf("NewToolSet failed, err: %v", err)
82+
}
83+
84+
tests := []struct {
85+
Name string
86+
Password string
87+
ToEmail string
88+
Subject string
89+
Content string
90+
wantErr bool
91+
}{
92+
// error case
93+
{
94+
Name: "18503@[email protected]",
95+
Password: "",
96+
ToEmail: "[email protected]",
97+
Subject: "test",
98+
Content: "test",
99+
wantErr: true,
100+
},
101+
// error case
102+
{
103+
104+
Password: "",
105+
ToEmail: "185039@[email protected]",
106+
Subject: "test",
107+
Content: "test",
108+
wantErr: true,
109+
},
110+
}
111+
for _, tt := range tests {
47112

48113
rsp, err := toolSet.(*emailToolSet).sendMail(context.Background(), &sendMailRequest{
49114
Auth: Auth{
@@ -58,10 +123,15 @@ func TestMailTool_sendMail(t *testing.T) {
58123
},
59124
},
60125
})
61-
if err != nil {
62-
t.Errorf("send mail err: %v", err)
126+
t.Logf("rsp: %+v err:%v", rsp, err)
127+
if rsp.Message != "" {
128+
if tt.wantErr == false {
129+
t.Errorf("send mail err: %s", rsp.Message)
130+
}
131+
} else if tt.wantErr == true {
132+
t.Errorf("should err but not")
63133
}
64-
t.Logf("rsp: %+v", rsp)
134+
65135
}
66136
}
67137

@@ -147,6 +217,12 @@ func Test_checkMailBoxType(t *testing.T) {
147217
want: MAIL_GMAIL,
148218
wantErr: false,
149219
},
220+
{
221+
name: "not valid email",
222+
args: args{email: "UserGmAiL.cOm"},
223+
want: MAIL_UNKNOWN,
224+
wantErr: true,
225+
},
150226
}
151227
for _, tt := range tests {
152228
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)