mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-14 01:42:54 +01:00
More tests. Also testing GH's behaviour on ENV variables for testing
This commit is contained in:
parent
ec162e7836
commit
1c699da955
2 changed files with 89 additions and 0 deletions
|
@ -245,6 +245,11 @@ func (c *Client) SetTLSPolicy(p TLSPolicy) {
|
||||||
c.tlspolicy = p
|
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
|
// SetTLSConfig overrides the current *tls.Config with the given *tls.Config value
|
||||||
func (c *Client) SetTLSConfig(co *tls.Config) error {
|
func (c *Client) SetTLSConfig(co *tls.Config) error {
|
||||||
if co == nil {
|
if co == nil {
|
||||||
|
@ -431,6 +436,9 @@ func (c *Client) checkConn() error {
|
||||||
|
|
||||||
// tls tries to make sure that the STARTTLS requirements are satisfied
|
// tls tries to make sure that the STARTTLS requirements are satisfied
|
||||||
func (c *Client) tls() error {
|
func (c *Client) tls() error {
|
||||||
|
if c.co == nil {
|
||||||
|
return ErrNoActiveConnection
|
||||||
|
}
|
||||||
if !c.ssl && c.tlspolicy != NoTLS {
|
if !c.ssl && c.tlspolicy != NoTLS {
|
||||||
est := false
|
est := false
|
||||||
st, _ := c.sc.Extension("STARTTLS")
|
st, _ := c.sc.Extension("STARTTLS")
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package mail
|
package mail
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/wneessen/go-mail/auth"
|
"github.com/wneessen/go-mail/auth"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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
|
// TestSetUsername tests the SetUsername method for the Client object
|
||||||
func TestSetUsername(t *testing.T) {
|
func TestSetUsername(t *testing.T) {
|
||||||
tests := []struct {
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue