Skip to content

Commit

Permalink
send email test
Browse files Browse the repository at this point in the history
  • Loading branch information
luhaoling committed Nov 3, 2023
1 parent 1cf98b6 commit f480f25
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
4 changes: 2 additions & 2 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ verifyCode:
verificationCodeTemplateCode: ""
mail: # 根据对应的发件邮箱更改 sendMail、senderAuthorizationCode、smtpAddr、smtpPort 即可
title: ""
senderMail: "" # 发送者
senderAuthorizationCode: "" # 授权码
senderMail: "[email protected]" # 发送者
senderAuthorizationCode: "lvxhehnmlcfrebab" # 授权码
smtpAddr: "smtp.qq.com" # smtp 服务器地址
smtpPort: 25 # smtp 服务器邮件发送端口
testDepartMentID: 001
Expand Down
1 change: 1 addition & 0 deletions pkg/email/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewMail() (Mail, error) {
config.Config.VerifyCode.Mail.SmtpPort,
config.Config.VerifyCode.Mail.SenderMail,
config.Config.VerifyCode.Mail.SenderAuthorizationCode)

return &mail{dail: dail}, nil
}

Expand Down
41 changes: 32 additions & 9 deletions pkg/email/mail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,55 @@ package email

import (
"context"
"fmt"
"errors"
"github.com/OpenIMSDK/chat/pkg/common/config"
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"testing"
)

func TestEmail(T *testing.T) {
if err := InitConfig(); err != nil {
panic(err)
}
mail, err := NewMail()
if err != nil {
log.Fatal(err)
tests := []struct {
name string
ctx context.Context
mail string
code string
want error
}{
{
name: "success send email",
ctx: context.Background(),
mail: "[email protected]",
code: "5555",
want: errors.New("nil"),
},
{
name: "fail send email",
ctx: context.Background(),
mail: "",
code: "5555",
want: errors.New("dial tcp :0: connectex: The requested address is not valid in its context."),
},
}
err = mail.SendMail(context.Background(), "[email protected]", "code")
mail, err := NewMail()
if err != nil {
log.Fatal(err)
T.Errorf("Init mail failed,%v", err)
}
fmt.Println("Send Successful")

for _, tt := range tests {
T.Run(tt.name, func(t *testing.T) {
if got := mail.SendMail(tt.ctx, tt.mail, tt.code); errors.Is(got, tt.want) {
t.Errorf("%v have a err,%v", tt.name, tt.want)
}
})
}
}

func InitConfig() error {
yam, err := ioutil.ReadFile("config/config.yaml")
yam, err := ioutil.ReadFile("../../config/config.yaml")
if err != nil {
return err
}
Expand Down

0 comments on commit f480f25

Please sign in to comment.