mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
MM-394 convert remaining mailservice tests to require/assert (#12813)
This commit is contained in:
committed by
Ben Schumacher
parent
9fa6e80e55
commit
07cd32779d
@@ -9,7 +9,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -29,24 +28,19 @@ func TestMailConnectionFromConfig(t *testing.T) {
|
|||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
cfg := fs.Get()
|
cfg := fs.Get()
|
||||||
|
conn, err := ConnectToSMTPServer(cfg)
|
||||||
|
require.Nil(t, err, "Should connect to the SMTP Server %v", err)
|
||||||
|
|
||||||
if conn, err := ConnectToSMTPServer(cfg); err != nil {
|
_, err = NewSMTPClient(conn, cfg)
|
||||||
t.Log(err)
|
|
||||||
t.Fatal("Should connect to the STMP Server")
|
require.Nil(t, err, "Should get new SMTP client")
|
||||||
} else {
|
|
||||||
if _, err1 := NewSMTPClient(conn, cfg); err1 != nil {
|
|
||||||
t.Log(err)
|
|
||||||
t.Fatal("Should get new smtp client")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*cfg.EmailSettings.SMTPServer = "wrongServer"
|
*cfg.EmailSettings.SMTPServer = "wrongServer"
|
||||||
*cfg.EmailSettings.SMTPPort = "553"
|
*cfg.EmailSettings.SMTPPort = "553"
|
||||||
|
|
||||||
if _, err := ConnectToSMTPServer(cfg); err == nil {
|
_, err = ConnectToSMTPServer(cfg)
|
||||||
t.Log(err)
|
|
||||||
t.Fatal("Should not to the STMP Server")
|
require.NotNil(t, err, "Should not connect to the SMTP Server")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMailConnectionAdvanced(t *testing.T) {
|
func TestMailConnectionAdvanced(t *testing.T) {
|
||||||
@@ -55,7 +49,7 @@ func TestMailConnectionAdvanced(t *testing.T) {
|
|||||||
|
|
||||||
cfg := fs.Get()
|
cfg := fs.Get()
|
||||||
|
|
||||||
if conn, err := ConnectToSMTPServerAdvanced(
|
conn, err := ConnectToSMTPServerAdvanced(
|
||||||
&SmtpConnectionInfo{
|
&SmtpConnectionInfo{
|
||||||
ConnectionSecurity: *cfg.EmailSettings.ConnectionSecurity,
|
ConnectionSecurity: *cfg.EmailSettings.ConnectionSecurity,
|
||||||
SkipCertVerification: *cfg.EmailSettings.SkipServerCertificateVerification,
|
SkipCertVerification: *cfg.EmailSettings.SkipServerCertificateVerification,
|
||||||
@@ -63,30 +57,28 @@ func TestMailConnectionAdvanced(t *testing.T) {
|
|||||||
SmtpServerHost: *cfg.EmailSettings.SMTPServer,
|
SmtpServerHost: *cfg.EmailSettings.SMTPServer,
|
||||||
SmtpPort: *cfg.EmailSettings.SMTPPort,
|
SmtpPort: *cfg.EmailSettings.SMTPPort,
|
||||||
},
|
},
|
||||||
); err != nil {
|
)
|
||||||
t.Log(err)
|
|
||||||
t.Fatal("Should connect to the STMP Server")
|
|
||||||
} else {
|
|
||||||
if _, err1 := NewSMTPClientAdvanced(
|
|
||||||
conn,
|
|
||||||
utils.GetHostnameFromSiteURL(*cfg.ServiceSettings.SiteURL),
|
|
||||||
&SmtpConnectionInfo{
|
|
||||||
ConnectionSecurity: *cfg.EmailSettings.ConnectionSecurity,
|
|
||||||
SkipCertVerification: *cfg.EmailSettings.SkipServerCertificateVerification,
|
|
||||||
SmtpServerName: *cfg.EmailSettings.SMTPServer,
|
|
||||||
SmtpServerHost: *cfg.EmailSettings.SMTPServer,
|
|
||||||
SmtpPort: *cfg.EmailSettings.SMTPPort,
|
|
||||||
Auth: *cfg.EmailSettings.EnableSMTPAuth,
|
|
||||||
SmtpUsername: *cfg.EmailSettings.SMTPUsername,
|
|
||||||
SmtpPassword: *cfg.EmailSettings.SMTPPassword,
|
|
||||||
},
|
|
||||||
); err1 != nil {
|
|
||||||
t.Log(err)
|
|
||||||
t.Fatal("Should get new smtp client")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := ConnectToSMTPServerAdvanced(
|
require.Nil(t, err, "Should connect to the SMTP Server")
|
||||||
|
|
||||||
|
_, err2 := NewSMTPClientAdvanced(
|
||||||
|
conn,
|
||||||
|
utils.GetHostnameFromSiteURL(*cfg.ServiceSettings.SiteURL),
|
||||||
|
&SmtpConnectionInfo{
|
||||||
|
ConnectionSecurity: *cfg.EmailSettings.ConnectionSecurity,
|
||||||
|
SkipCertVerification: *cfg.EmailSettings.SkipServerCertificateVerification,
|
||||||
|
SmtpServerName: *cfg.EmailSettings.SMTPServer,
|
||||||
|
SmtpServerHost: *cfg.EmailSettings.SMTPServer,
|
||||||
|
SmtpPort: *cfg.EmailSettings.SMTPPort,
|
||||||
|
Auth: *cfg.EmailSettings.EnableSMTPAuth,
|
||||||
|
SmtpUsername: *cfg.EmailSettings.SMTPUsername,
|
||||||
|
SmtpPassword: *cfg.EmailSettings.SMTPPassword,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
require.Nil(t, err2, "Should get new SMTP client")
|
||||||
|
|
||||||
|
_, err3 := ConnectToSMTPServerAdvanced(
|
||||||
&SmtpConnectionInfo{
|
&SmtpConnectionInfo{
|
||||||
ConnectionSecurity: *cfg.EmailSettings.ConnectionSecurity,
|
ConnectionSecurity: *cfg.EmailSettings.ConnectionSecurity,
|
||||||
SkipCertVerification: *cfg.EmailSettings.SkipServerCertificateVerification,
|
SkipCertVerification: *cfg.EmailSettings.SkipServerCertificateVerification,
|
||||||
@@ -94,11 +86,8 @@ func TestMailConnectionAdvanced(t *testing.T) {
|
|||||||
SmtpServerHost: "wrongServer",
|
SmtpServerHost: "wrongServer",
|
||||||
SmtpPort: "553",
|
SmtpPort: "553",
|
||||||
},
|
},
|
||||||
); err == nil {
|
)
|
||||||
t.Log(err)
|
require.NotNil(t, err3, "Should not connect to the SMTP Server")
|
||||||
t.Fatal("Should not to the STMP Server")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSendMailUsingConfig(t *testing.T) {
|
func TestSendMailUsingConfig(t *testing.T) {
|
||||||
@@ -116,32 +105,25 @@ func TestSendMailUsingConfig(t *testing.T) {
|
|||||||
//Delete all the messages before check the sample email
|
//Delete all the messages before check the sample email
|
||||||
DeleteMailBox(emailTo)
|
DeleteMailBox(emailTo)
|
||||||
|
|
||||||
if err := SendMailUsingConfig(emailTo, emailSubject, emailBody, cfg, true); err != nil {
|
err2 := SendMailUsingConfig(emailTo, emailSubject, emailBody, cfg, true)
|
||||||
t.Log(err)
|
require.Nil(t, err2, "Should connect to the SMTP Server")
|
||||||
t.Fatal("Should connect to the STMP Server")
|
|
||||||
|
//Check if the email was send to the right email address
|
||||||
|
var resultsMailbox JSONMessageHeaderInbucket
|
||||||
|
err3 := RetryInbucket(5, func() error {
|
||||||
|
var err error
|
||||||
|
resultsMailbox, err = GetMailBox(emailTo)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if err3 != nil {
|
||||||
|
t.Log(err3)
|
||||||
|
t.Log("No email was received, maybe due load on the server. Skipping this verification")
|
||||||
} else {
|
} else {
|
||||||
//Check if the email was send to the right email address
|
if len(resultsMailbox) > 0 {
|
||||||
var resultsMailbox JSONMessageHeaderInbucket
|
require.Contains(t, resultsMailbox[0].To[0], emailTo, "Wrong To: recipient")
|
||||||
err := RetryInbucket(5, func() error {
|
resultsEmail, err := GetMessageFromMailbox(emailTo, resultsMailbox[0].ID)
|
||||||
var err error
|
require.Nil(t, err, "Could not get message from mailbox")
|
||||||
resultsMailbox, err = GetMailBox(emailTo)
|
require.Contains(t, emailBody, resultsEmail.Body.Text, "Wrong received message %s", resultsEmail.Body.Text)
|
||||||
return err
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Log(err)
|
|
||||||
t.Log("No email was received, maybe due load on the server. Disabling this verification")
|
|
||||||
}
|
|
||||||
if err == nil && len(resultsMailbox) > 0 {
|
|
||||||
if !strings.ContainsAny(resultsMailbox[0].To[0], emailTo) {
|
|
||||||
t.Fatal("Wrong To recipient")
|
|
||||||
} else {
|
|
||||||
if resultsEmail, err := GetMessageFromMailbox(emailTo, resultsMailbox[0].ID); err == nil {
|
|
||||||
if !strings.Contains(resultsEmail.Body.Text, emailBody) {
|
|
||||||
t.Log(resultsEmail.Body.Text)
|
|
||||||
t.Fatal("Received message")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -299,9 +281,7 @@ func TestAuthMethods(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
got = err.Error()
|
got = err.Error()
|
||||||
}
|
}
|
||||||
if got != test.err {
|
assert.True(t, got == test.err, "%d. got error = %q; want %q", i, got, test.err)
|
||||||
t.Errorf("%d. got error = %q; want %q", i, got, test.err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user