mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package utils
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMailConnection(t *testing.T) {
|
|
cfg, _, err := LoadConfig("config.json")
|
|
require.Nil(t, err)
|
|
|
|
if conn, err := connectToSMTPServer(cfg); err != nil {
|
|
t.Log(err)
|
|
t.Fatal("Should connect to the STMP Server")
|
|
} else {
|
|
if _, err1 := newSMTPClient(conn, cfg); err1 != nil {
|
|
t.Log(err)
|
|
t.Fatal("Should get new smtp client")
|
|
}
|
|
}
|
|
|
|
cfg.EmailSettings.SMTPServer = "wrongServer"
|
|
cfg.EmailSettings.SMTPPort = "553"
|
|
|
|
if _, err := connectToSMTPServer(cfg); err == nil {
|
|
t.Log(err)
|
|
t.Fatal("Should not to the STMP Server")
|
|
}
|
|
|
|
}
|
|
|
|
func TestSendMailUsingConfig(t *testing.T) {
|
|
cfg, _, err := LoadConfig("config.json")
|
|
require.Nil(t, err)
|
|
T = GetUserTranslations("en")
|
|
|
|
var emailTo string = "test@example.com"
|
|
var emailSubject string = "Testing this email"
|
|
var emailBody string = "This is a test from autobot"
|
|
|
|
//Delete all the messages before check the sample email
|
|
DeleteMailBox(emailTo)
|
|
|
|
if err := SendMailUsingConfig(emailTo, emailSubject, emailBody, cfg); err != nil {
|
|
t.Log(err)
|
|
t.Fatal("Should connect to the STMP Server")
|
|
} else {
|
|
//Check if the email was send to the rigth email address
|
|
var resultsMailbox JSONMessageHeaderInbucket
|
|
err := RetryInbucket(5, func() error {
|
|
var err error
|
|
resultsMailbox, err = GetMailBox(emailTo)
|
|
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")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|