More tests. Also testing GH's behaviour on ENV variables for testing

This commit is contained in:
Winni Neessen 2022-03-16 21:02:31 +01:00
parent ec162e7836
commit 1c699da955
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
2 changed files with 89 additions and 0 deletions

View file

@ -245,6 +245,11 @@ func (c *Client) SetTLSPolicy(p TLSPolicy) {
c.tlspolicy = p
}
// SetSSL tells the Client wether to use SSL or not
func (c *Client) SetSSL(s bool) {
c.ssl = s
}
// SetTLSConfig overrides the current *tls.Config with the given *tls.Config value
func (c *Client) SetTLSConfig(co *tls.Config) error {
if co == nil {
@ -431,6 +436,9 @@ func (c *Client) checkConn() error {
// tls tries to make sure that the STARTTLS requirements are satisfied
func (c *Client) tls() error {
if c.co == nil {
return ErrNoActiveConnection
}
if !c.ssl && c.tlspolicy != NoTLS {
est := false
st, _ := c.sc.Extension("STARTTLS")

View file

@ -1,10 +1,12 @@
package mail
import (
"context"
"crypto/tls"
"fmt"
"github.com/wneessen/go-mail/auth"
"net/smtp"
"os"
"testing"
"time"
)
@ -255,6 +257,30 @@ func TestSetTLSConfig(t *testing.T) {
}
}
// TestSetSSL tests the SetSSL() method for the Client object
func TestSetSSL(t *testing.T) {
tests := []struct {
name string
value bool
}{
{"SSL: on", true},
{"SSL: off", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := NewClient(DefaultHost)
if err != nil {
t.Errorf("failed to create new client: %s", err)
return
}
c.SetSSL(tt.value)
if c.ssl != tt.value {
t.Errorf("failed to set SSL setting. Got: %t, want: %t", c.ssl, tt.value)
}
})
}
}
// TestSetUsername tests the SetUsername method for the Client object
func TestSetUsername(t *testing.T) {
tests := []struct {
@ -368,3 +394,58 @@ func TestSetSMTPAuthCustom(t *testing.T) {
})
}
}
// Test_tls tests the tls method for the Client object
func Test_tls(t *testing.T) {
tests := []struct {
name string
policy TLSPolicy
}{
{"Mandatory", TLSMandatory},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := getTestConnection(true)
if err != nil {
t.Skipf("failed to create test client: %s. Skipping tests", err)
}
if err := c.tls(); err != nil {
t.Errorf("failed to set TLS/SSL config in client: %s", err)
}
})
}
}
// getTestConnection takes environment variables to establish a connection to a real
// SMTP server to test all functionality that requires a connection
func getTestConnection(auth bool) (*Client, error) {
th := os.Getenv("TEST_HOST")
if th == "" {
return nil, fmt.Errorf("no TEST_HOST set")
}
c, err := NewClient(th)
if err != nil {
return c, err
}
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)
}
}
if err := c.DialWithContext(context.Background()); err != nil {
return c, fmt.Errorf("connection to test server failed: %s", err)
}
if err := c.Close(); err != nil {
return c, fmt.Errorf("disconnect from test server failed: %s", err)
}
return c, nil
}