From 595d5f3d95991751c2a42fa2b844d34d51ec53db Mon Sep 17 00:00:00 2001 From: Brabem <69128477+luhaoling@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:56:24 +0800 Subject: [PATCH] feat: add send email test (#254) * Support email registration * add the email test * send email test * send email test * add the email test --- pkg/email/mail.go | 1 + pkg/email/mail_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 pkg/email/mail_test.go 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 new file mode 100644 index 000000000..daf40c674 --- /dev/null +++ b/pkg/email/mail_test.go @@ -0,0 +1,62 @@ +package email + +import ( + "context" + "errors" + "github.com/OpenIMSDK/chat/pkg/common/config" + "gopkg.in/yaml.v3" + "io/ioutil" + "testing" +) + +func TestEmail(T *testing.T) { + if err := InitConfig(); err != nil { + panic(err) + } + tests := []struct { + name string + ctx context.Context + mail string + code string + want error + }{ + { + name: "success send email", + ctx: context.Background(), + mail: "test@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."), + }, + } + mail, err := NewMail() + if err != nil { + T.Errorf("Init mail failed,%v", err) + } + + 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") + if err != nil { + return err + } + err = yaml.Unmarshal(yam, &config.Config) + if err != nil { + return err + } + return nil +}