github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/utils/mail_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package utils 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestMailConnection(t *testing.T) { 14 cfg, _, err := LoadConfig("config.json") 15 require.Nil(t, err) 16 17 if conn, err := connectToSMTPServer(cfg); err != nil { 18 t.Log(err) 19 t.Fatal("Should connect to the STMP Server") 20 } else { 21 if _, err1 := newSMTPClient(conn, cfg); err1 != nil { 22 t.Log(err) 23 t.Fatal("Should get new smtp client") 24 } 25 } 26 27 cfg.EmailSettings.SMTPServer = "wrongServer" 28 cfg.EmailSettings.SMTPPort = "553" 29 30 if _, err := connectToSMTPServer(cfg); err == nil { 31 t.Log(err) 32 t.Fatal("Should not to the STMP Server") 33 } 34 35 } 36 37 func TestSendMailUsingConfig(t *testing.T) { 38 cfg, _, err := LoadConfig("config.json") 39 require.Nil(t, err) 40 T = GetUserTranslations("en") 41 42 var emailTo = "test@example.com" 43 var emailSubject = "Testing this email" 44 var emailBody = "This is a test from autobot" 45 46 //Delete all the messages before check the sample email 47 DeleteMailBox(emailTo) 48 49 if err := SendMailUsingConfig(emailTo, emailSubject, emailBody, cfg, true); err != nil { 50 t.Log(err) 51 t.Fatal("Should connect to the STMP Server") 52 } else { 53 //Check if the email was send to the right email address 54 var resultsMailbox JSONMessageHeaderInbucket 55 err := RetryInbucket(5, func() error { 56 var err error 57 resultsMailbox, err = GetMailBox(emailTo) 58 return err 59 }) 60 if err != nil { 61 t.Log(err) 62 t.Log("No email was received, maybe due load on the server. Disabling this verification") 63 } 64 if err == nil && len(resultsMailbox) > 0 { 65 if !strings.ContainsAny(resultsMailbox[0].To[0], emailTo) { 66 t.Fatal("Wrong To recipient") 67 } else { 68 if resultsEmail, err := GetMessageFromMailbox(emailTo, resultsMailbox[0].ID); err == nil { 69 if !strings.Contains(resultsEmail.Body.Text, emailBody) { 70 t.Log(resultsEmail.Body.Text) 71 t.Fatal("Received message") 72 } 73 } 74 } 75 } 76 } 77 } 78 79 /*func TestSendMailUsingConfigAdvanced(t *testing.T) { 80 cfg, _, err := LoadConfig("config.json") 81 require.Nil(t, err) 82 T = GetUserTranslations("en") 83 84 var mimeTo = "test@example.com" 85 var smtpTo = "test2@example.com" 86 var from = mail.Address{Name: "Nobody", Address: "nobody@mattermost.com"} 87 var emailSubject = "Testing this email" 88 var emailBody = "This is a test from autobot" 89 90 //Delete all the messages before check the sample email 91 DeleteMailBox(smtpTo) 92 93 fileBackend, err := NewFileBackend(&cfg.FileSettings, true) 94 assert.Nil(t, err) 95 96 // create two files with the same name that will both be attached to the email 97 fileName := "file.txt" 98 filePath1 := fmt.Sprintf("test1/%s", fileName) 99 filePath2 := fmt.Sprintf("test2/%s", fileName) 100 fileContents1 := []byte("hello world") 101 fileContents2 := []byte("foo bar") 102 assert.Nil(t, fileBackend.WriteFile(fileContents1, filePath1)) 103 assert.Nil(t, fileBackend.WriteFile(fileContents2, filePath2)) 104 defer fileBackend.RemoveFile(filePath1) 105 defer fileBackend.RemoveFile(filePath2) 106 107 attachments := make([]*model.FileInfo, 2) 108 attachments[0] = &model.FileInfo{ 109 Name: fileName, 110 Path: filePath1, 111 } 112 attachments[1] = &model.FileInfo{ 113 Name: fileName, 114 Path: filePath2, 115 } 116 117 headers := make(map[string]string) 118 headers["TestHeader"] = "TestValue" 119 120 if err := SendMailUsingConfigAdvanced(mimeTo, smtpTo, from, emailSubject, emailBody, attachments, headers, cfg, true); err != nil { 121 t.Log(err) 122 t.Fatal("Should connect to the STMP Server") 123 } else { 124 //Check if the email was send to the right email address 125 var resultsMailbox JSONMessageHeaderInbucket 126 err := RetryInbucket(5, func() error { 127 var err error 128 resultsMailbox, err = GetMailBox(smtpTo) 129 return err 130 }) 131 if err != nil { 132 t.Log(err) 133 t.Fatal("No emails found for address " + smtpTo) 134 } 135 if err == nil && len(resultsMailbox) > 0 { 136 if !strings.ContainsAny(resultsMailbox[0].To[0], smtpTo) { 137 t.Fatal("Wrong To recipient") 138 } else { 139 if resultsEmail, err := GetMessageFromMailbox(smtpTo, resultsMailbox[0].ID); err == nil { 140 if !strings.Contains(resultsEmail.Body.Text, emailBody) { 141 t.Log(resultsEmail.Body.Text) 142 t.Fatal("Received message") 143 } 144 145 // 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 146 assert.Equal(t, mimeTo, resultsEmail.Header["To"][0]) 147 148 // verify that the MIME from address is correct - unfortunately, we can't verify the SMTP from address 149 assert.Equal(t, from.String(), resultsEmail.Header["From"][0]) 150 151 // check that the custom mime headers came through - header case seems to get mutated 152 assert.Equal(t, "TestValue", resultsEmail.Header["Testheader"][0]) 153 154 // ensure that the attachments were successfully sent 155 assert.Len(t, resultsEmail.Attachments, 2) 156 assert.Equal(t, fileName, resultsEmail.Attachments[0].Filename) 157 assert.Equal(t, fileName, resultsEmail.Attachments[1].Filename) 158 attachment1 := string(resultsEmail.Attachments[0].Bytes) 159 attachment2 := string(resultsEmail.Attachments[1].Bytes) 160 if attachment1 == string(fileContents1) { 161 assert.Equal(t, attachment2, string(fileContents2)) 162 } else if attachment1 == string(fileContents2) { 163 assert.Equal(t, attachment2, string(fileContents1)) 164 } else { 165 assert.Fail(t, "Unrecognized attachment contents") 166 } 167 } 168 } 169 } 170 } 171 }*/