From 1cf98b67c5162afa7556783f227408e35ab2b6b5 Mon Sep 17 00:00:00 2001 From: luhaoling <2198702716@qq.com> Date: Thu, 2 Nov 2023 18:24:50 +0800 Subject: [PATCH 1/3] add the email test --- pkg/email/mail_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 pkg/email/mail_test.go diff --git a/pkg/email/mail_test.go b/pkg/email/mail_test.go new file mode 100644 index 000000000..6b6744e88 --- /dev/null +++ b/pkg/email/mail_test.go @@ -0,0 +1,39 @@ +package email + +import ( + "context" + "fmt" + "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) + } + err = mail.SendMail(context.Background(), "text@gmail.com", "code") + if err != nil { + log.Fatal(err) + } + fmt.Println("Send Successful") + +} + +func InitConfig() error { + yam, err := ioutil.ReadFile("config/config.yaml") + if err != nil { + return err + } + err = yaml.Unmarshal(yam, &config.Config) + if err != nil { + return err + } + return nil +} From f480f25fecb7906a6b9c79aca6e0c73c3d075e57 Mon Sep 17 00:00:00 2001 From: luhaoling <2198702716@qq.com> Date: Fri, 3 Nov 2023 10:29:09 +0800 Subject: [PATCH 2/3] send email test --- config/config.yaml | 4 ++-- pkg/email/mail.go | 1 + pkg/email/mail_test.go | 41 ++++++++++++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index 729504517..0a6063603 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -82,8 +82,8 @@ verifyCode: verificationCodeTemplateCode: "" mail: # 根据对应的发件邮箱更改 sendMail、senderAuthorizationCode、smtpAddr、smtpPort 即可 title: "" - senderMail: "" # 发送者 - senderAuthorizationCode: "" # 授权码 + senderMail: "2198702716@qq.com" # 发送者 + senderAuthorizationCode: "lvxhehnmlcfrebab" # 授权码 smtpAddr: "smtp.qq.com" # smtp 服务器地址 smtpPort: 25 # smtp 服务器邮件发送端口 testDepartMentID: 001 diff --git a/pkg/email/mail.go b/pkg/email/mail.go index b240c18ee..73178a413 100644 --- a/pkg/email/mail.go +++ b/pkg/email/mail.go @@ -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 } diff --git a/pkg/email/mail_test.go b/pkg/email/mail_test.go index 6b6744e88..13ab55992 100644 --- a/pkg/email/mail_test.go +++ b/pkg/email/mail_test.go @@ -2,11 +2,10 @@ package email import ( "context" - "fmt" + "errors" "github.com/OpenIMSDK/chat/pkg/common/config" "gopkg.in/yaml.v3" "io/ioutil" - "log" "testing" ) @@ -14,20 +13,44 @@ 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: "lmf91248@gmail.com", + 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(), "text@gmail.com", "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 } From ff39fd27d6aefcba4e7242f0281b43a9e48c3105 Mon Sep 17 00:00:00 2001 From: luhaoling <2198702716@qq.com> Date: Fri, 3 Nov 2023 10:31:54 +0800 Subject: [PATCH 3/3] send email test --- config/config.yaml | 4 ++-- pkg/email/mail_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index 0a6063603..729504517 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -82,8 +82,8 @@ verifyCode: verificationCodeTemplateCode: "" mail: # 根据对应的发件邮箱更改 sendMail、senderAuthorizationCode、smtpAddr、smtpPort 即可 title: "" - senderMail: "2198702716@qq.com" # 发送者 - senderAuthorizationCode: "lvxhehnmlcfrebab" # 授权码 + senderMail: "" # 发送者 + senderAuthorizationCode: "" # 授权码 smtpAddr: "smtp.qq.com" # smtp 服务器地址 smtpPort: 25 # smtp 服务器邮件发送端口 testDepartMentID: 001 diff --git a/pkg/email/mail_test.go b/pkg/email/mail_test.go index 13ab55992..daf40c674 100644 --- a/pkg/email/mail_test.go +++ b/pkg/email/mail_test.go @@ -23,7 +23,7 @@ func TestEmail(T *testing.T) { { name: "success send email", ctx: context.Background(), - mail: "lmf91248@gmail.com", + mail: "test@gmail.com", code: "5555", want: errors.New("nil"), },