summaryrefslogtreecommitdiffstats
path: root/utils/mail_test.go
blob: 207fe32a53f66617f1bb5c5794766a5c144e7307 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package utils

import (
	"strings"
	"testing"

	"net/mail"

	"github.com/mattermost/mattermost-server/model"
	"github.com/stretchr/testify/assert"
	"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 = "test@example.com"
	var emailSubject = "Testing this email"
	var emailBody = "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 right 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")
					}
				}
			}
		}
	}
}

func TestSendMailUsingConfigAdvanced(t *testing.T) {
	cfg, _, err := LoadConfig("config.json")
	require.Nil(t, err)
	T = GetUserTranslations("en")

	var mimeTo = "test@example.com"
	var smtpTo = "test2@example.com"
	var from = mail.Address{Name: "Nobody", Address: "nobody@mattermost.com"}
	var emailSubject = "Testing this email"
	var emailBody = "This is a test from autobot"

	//Delete all the messages before check the sample email
	DeleteMailBox(smtpTo)

	// create a file that will be attached to the email
	fileBackend, err := NewFileBackend(&cfg.FileSettings)
	assert.Nil(t, err)
	fileContents := []byte("hello world")
	fileName := "file.txt"
	assert.Nil(t, fileBackend.WriteFile(fileContents, fileName))
	defer fileBackend.RemoveFile(fileName)

	attachments := make([]*model.FileInfo, 1)
	attachments[0] = &model.FileInfo{
		Name: fileName,
		Path: fileName,
	}

	headers := make(map[string]string)
	headers["TestHeader"] = "TestValue"

	if err := SendMailUsingConfigAdvanced(mimeTo, smtpTo, from, emailSubject, emailBody, attachments, headers, cfg); err != nil {
		t.Log(err)
		t.Fatal("Should connect to the STMP Server")
	} else {
		//Check if the email was send to the right email address
		var resultsMailbox JSONMessageHeaderInbucket
		err := RetryInbucket(5, func() error {
			var err error
			resultsMailbox, err = GetMailBox(smtpTo)
			return err
		})
		if err != nil {
			t.Log(err)
			t.Fatal("No emails found for address " + smtpTo)
		}
		if err == nil && len(resultsMailbox) > 0 {
			if !strings.ContainsAny(resultsMailbox[0].To[0], smtpTo) {
				t.Fatal("Wrong To recipient")
			} else {
				if resultsEmail, err := GetMessageFromMailbox(smtpTo, resultsMailbox[0].ID); err == nil {
					if !strings.Contains(resultsEmail.Body.Text, emailBody) {
						t.Log(resultsEmail.Body.Text)
						t.Fatal("Received message")
					}

					// verify that the To header of the email message is set to the MIME recipient, even though we got it out of the SMTP recipient's email inbox
					assert.Equal(t, mimeTo, resultsEmail.Header["To"][0])

					// verify that the MIME from address is correct - unfortunately, we can't verify the SMTP from address
					assert.Equal(t, from.String(), resultsEmail.Header["From"][0])

					// check that the custom mime headers came through - header case seems to get mutated
					assert.Equal(t, "TestValue", resultsEmail.Header["Testheader"][0])

					// ensure that the attachment was successfully sent
					assert.Len(t, resultsEmail.Attachments, 1)
					assert.Equal(t, fileName, resultsEmail.Attachments[0].Filename)
					assert.Equal(t, fileContents, resultsEmail.Attachments[0].Bytes)
				}
			}
		}
	}
}