diff --git a/pkg/email/email_test.go b/pkg/email/email_test.go index 480bbb9..64ad4c4 100755 --- a/pkg/email/email_test.go +++ b/pkg/email/email_test.go @@ -146,3 +146,39 @@ func TestNewInsecure(t *testing.T) { assert.Error(t, err) }) } + +func TestSecure(t *testing.T) { + cfg := newConfig(".env.test") + + emailService := NewSecure(MailServiceConfig{ + Auth: smtp.PlainAuth("", cfg.MailUser, cfg.MailPassword, cfg.MailHost), + Host: cfg.MailHost, + Port: cfg.MailPort, + From: cfg.MailFrom, + }) + + // Assert that the tls.Config is set up correctly + assert.NotNil(t, emailService.tlsconfig) + assert.True(t, emailService.tlsconfig.InsecureSkipVerify) + assert.Equal(t, cfg.MailHost, emailService.tlsconfig.ServerName) + assert.NotNil(t, emailService.tlsconfig.VerifyConnection) + + t.Run("TestSendEmail", func(t *testing.T) { + // Mock the client and test the StartTLS method + var called bool + mockDialFn := func(hostPort string) (SMTPClientIface, error) { + called = true + return &mockSMTP{}, nil + } + emailService.dial = mockDialFn + + data := EmailMessage{ + To: cfg.MailTo, + Subject: "Mail Sender", + Body: "Hello this is a test email", + } + require.NoError(t, emailService.SendEmail(data)) + assert.Equal(t, true, called) + }) + +}