feat: expose mocks

This commit is contained in:
Urko. 2023-10-21 20:51:20 +02:00
parent fc005a74c3
commit 9eb969a4c3
2 changed files with 42 additions and 36 deletions

View File

@ -3,7 +3,6 @@ package email
import (
"crypto/tls"
"fmt"
"io"
"net/smtp"
"os"
"testing"
@ -40,40 +39,6 @@ func newConfig(envFile string) *config {
return cfg
}
type mockWriter struct{}
func (w *mockWriter) Close() error {
return nil
}
func (w *mockWriter) Write(p []byte) (n int, err error) {
return 10, nil
}
// Mock SMTP Client
type mockSMTP struct{}
func (m *mockSMTP) StartTLS(*tls.Config) error {
return nil
}
func (m *mockSMTP) Auth(a smtp.Auth) error {
return nil
}
func (m *mockSMTP) Close() error {
return nil
}
func (m *mockSMTP) Data() (io.WriteCloser, error) {
return &mockWriter{}, nil
}
func (m *mockSMTP) Mail(from string) error {
return nil
}
func (m *mockSMTP) Quit() error {
return nil
}
func (m *mockSMTP) Rcpt(to string) error {
return nil
}
func TestNewConfig_MissingEnvFile(t *testing.T) {
assert.Panics(t, func() { newConfig(".missing_env_file") })
}
@ -89,7 +54,7 @@ func TestMockSendEmail(t *testing.T) {
ServerName: "",
},
dial: func(hostPort string) (SMTPClientIface, error) {
return &mockSMTP{}, nil
return &MockSMTP{}, nil
},
}

41
pkg/email/mock.go Normal file
View File

@ -0,0 +1,41 @@
package email
import (
"crypto/tls"
"io"
"net/smtp"
)
type MockWriter struct{}
func (w *MockWriter) Close() error {
return nil
}
func (w *MockWriter) Write(p []byte) (n int, err error) {
return 10, nil
}
// Mock SMTP Client
type MockSMTP struct{}
func (m *MockSMTP) StartTLS(*tls.Config) error {
return nil
}
func (m *MockSMTP) Auth(a smtp.Auth) error {
return nil
}
func (m *MockSMTP) Close() error {
return nil
}
func (m *MockSMTP) Data() (io.WriteCloser, error) {
return &MockWriter{}, nil
}
func (m *MockSMTP) Mail(from string) error {
return nil
}
func (m *MockSMTP) Quit() error {
return nil
}
func (m *MockSMTP) Rcpt(to string) error {
return nil
}