mirror of
https://github.com/wneessen/go-mail.git
synced 2024-12-22 18:50:37 +01:00
Fix tests to not log authentication data
We don't want to expose SMTP authentication details in tests, therefore the tests have been adjusted a bit
This commit is contained in:
parent
34b432a985
commit
e36511e90e
4 changed files with 63 additions and 9 deletions
|
@ -410,8 +410,11 @@ func (c *Client) SetSSL(s bool) {
|
|||
}
|
||||
|
||||
// SetDebugLog tells the Client whether debug logging is enabled or not
|
||||
func (c *Client) SetDebugLog(s bool) {
|
||||
c.dl = s
|
||||
func (c *Client) SetDebugLog(v bool) {
|
||||
c.dl = v
|
||||
if c.sc != nil {
|
||||
c.sc.SetDebugLog(v)
|
||||
}
|
||||
}
|
||||
|
||||
// SetTLSConfig overrides the current *tls.Config with the given *tls.Config value
|
||||
|
@ -479,7 +482,7 @@ func (c *Client) DialWithContext(pc context.Context) error {
|
|||
return err
|
||||
}
|
||||
if c.dl {
|
||||
c.sc.SetDebugLog()
|
||||
c.sc.SetDebugLog(true)
|
||||
}
|
||||
if err := c.sc.Hello(c.helo); err != nil {
|
||||
return err
|
||||
|
|
|
@ -546,11 +546,10 @@ func TestClient_DialWithContext(t *testing.T) {
|
|||
// TestClient_DialWithContext_Debug tests the DialWithContext method for the Client object with debug
|
||||
// logging enabled on the SMTP client
|
||||
func TestClient_DialWithContext_Debug(t *testing.T) {
|
||||
c, err := getTestConnection(true)
|
||||
c, err := getTestClient(true)
|
||||
if err != nil {
|
||||
t.Skipf("failed to create test client: %s. Skipping tests", err)
|
||||
}
|
||||
c.SetDebugLog(true)
|
||||
ctx := context.Background()
|
||||
if err := c.DialWithContext(ctx); err != nil {
|
||||
t.Errorf("failed to dial with context: %s", err)
|
||||
|
@ -562,6 +561,7 @@ func TestClient_DialWithContext_Debug(t *testing.T) {
|
|||
if c.sc == nil {
|
||||
t.Errorf("DialWithContext didn't fail but no SMTP client found.")
|
||||
}
|
||||
c.SetDebugLog(true)
|
||||
if err := c.Close(); err != nil {
|
||||
t.Errorf("failed to close connection: %s", err)
|
||||
}
|
||||
|
@ -1141,6 +1141,8 @@ func getTestConnection(auth bool) (*Client, error) {
|
|||
if p != "" {
|
||||
c.SetPassword(p)
|
||||
}
|
||||
// We don't want to log authentication data in tests
|
||||
c.SetDebugLog(false)
|
||||
}
|
||||
if err := c.DialWithContext(context.Background()); err != nil {
|
||||
return c, fmt.Errorf("connection to test server failed: %w", err)
|
||||
|
@ -1151,6 +1153,51 @@ func getTestConnection(auth bool) (*Client, error) {
|
|||
return c, nil
|
||||
}
|
||||
|
||||
// getTestClient takes environment variables to establish a client without connecting
|
||||
// to the SMTP server
|
||||
func getTestClient(auth bool) (*Client, error) {
|
||||
if os.Getenv("TEST_SKIP_ONLINE") != "" {
|
||||
return nil, fmt.Errorf("env variable TEST_SKIP_ONLINE is set. Skipping online tests")
|
||||
}
|
||||
th := os.Getenv("TEST_HOST")
|
||||
if th == "" {
|
||||
return nil, fmt.Errorf("no TEST_HOST set")
|
||||
}
|
||||
tp := 25
|
||||
if tps := os.Getenv("TEST_PORT"); tps != "" {
|
||||
tpi, err := strconv.Atoi(tps)
|
||||
if err == nil {
|
||||
tp = tpi
|
||||
}
|
||||
}
|
||||
sv := false
|
||||
if sve := os.Getenv("TEST_TLS_SKIP_VERIFY"); sve != "" {
|
||||
sv = true
|
||||
}
|
||||
c, err := NewClient(th, WithPort(tp))
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
c.tlsconfig.InsecureSkipVerify = sv
|
||||
if auth {
|
||||
st := os.Getenv("TEST_SMTPAUTH_TYPE")
|
||||
if st != "" {
|
||||
c.SetSMTPAuth(SMTPAuthType(st))
|
||||
}
|
||||
u := os.Getenv("TEST_SMTPAUTH_USER")
|
||||
if u != "" {
|
||||
c.SetUsername(u)
|
||||
}
|
||||
p := os.Getenv("TEST_SMTPAUTH_PASS")
|
||||
if p != "" {
|
||||
c.SetPassword(p)
|
||||
}
|
||||
// We don't want to log authentication data in tests
|
||||
c.SetDebugLog(false)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// getTestConnectionWithDSN takes environment variables to establish a connection to a real
|
||||
// SMTP server to test all functionality that requires a connection. It also enables DSN
|
||||
func getTestConnectionWithDSN(auth bool) (*Client, error) {
|
||||
|
|
10
smtp/smtp.go
10
smtp/smtp.go
|
@ -432,9 +432,13 @@ func (c *Client) Quit() error {
|
|||
}
|
||||
|
||||
// SetDebugLog enables the debug logging for incoming and outgoing SMTP messages
|
||||
func (c *Client) SetDebugLog() {
|
||||
c.debug = true
|
||||
c.logger = log.New(os.Stderr, "[DEBUG] ", log.LstdFlags|log.Lmsgprefix)
|
||||
func (c *Client) SetDebugLog(v bool) {
|
||||
c.debug = v
|
||||
if v {
|
||||
c.logger = log.New(os.Stderr, "[DEBUG] ", log.LstdFlags|log.Lmsgprefix)
|
||||
return
|
||||
}
|
||||
c.logger = nil
|
||||
}
|
||||
|
||||
// debugLog checks if the debug flag is set and if so logs the provided message to StdErr
|
||||
|
|
|
@ -655,7 +655,7 @@ func TestClient_SetDebugLog(t *testing.T) {
|
|||
defer func() {
|
||||
_ = c.Close()
|
||||
}()
|
||||
c.SetDebugLog()
|
||||
c.SetDebugLog(true)
|
||||
if !c.debug {
|
||||
t.Errorf("Expected DebugLog flag to be true but received false")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue