From 9728b71f0839a6418f33df7a8188a801902a3a8e Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Thu, 26 Jan 2017 17:48:23 +0100 Subject: Add tests for app/email.go (#5187) * Add tests for app/email.go * fix issue related with inbucket * applied go fmt --- app/email_test.go | 419 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 419 insertions(+) create mode 100644 app/email_test.go (limited to 'app') diff --git a/app/email_test.go b/app/email_test.go new file mode 100644 index 000000000..ecaa389bf --- /dev/null +++ b/app/email_test.go @@ -0,0 +1,419 @@ +// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package app + +import ( + "strings" + "testing" + + "github.com/mattermost/platform/utils" +) + +func TestSendChangeUsernameEmail(t *testing.T) { + Setup() + + var emailTo string = "test@example.com" + var oldUsername string = "myoldusername" + var newUsername string = "fancyusername" + var locale string = "en" + var siteURL string = "" + var expectedPartialMessage string = "Your username for Mattermost has been changed to " + newUsername + "." + var expectedSubject string = "[" + utils.Cfg.TeamSettings.SiteName + "] Your username has changed for Mattermost" + + //Delete all the messages before check the sample email + utils.DeleteMailBox(emailTo) + + if err := SendChangeUsernameEmail(oldUsername, newUsername, emailTo, locale, siteURL); err != nil { + t.Log(err) + t.Fatal("Should send change username email") + } else { + //Check if the email was send to the rigth email address + if resultsMailbox, err := utils.GetMailBox(emailTo); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], emailTo) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(emailTo, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + } + } + } +} + +func TestSendEmailChangeVerifyEmail(t *testing.T) { + Setup() + utils.LoadConfig("config.json") + + var userId string = "5349853498543jdfvndf9834" + var newUserEmail string = "newtest@example.com" + var locale string = "en" + var siteURL string = "" + var expectedPartialMessage string = "You updated your email" + var expectedSubject string = "[" + utils.Cfg.TeamSettings.SiteName + "] Verify new email address for Mattermost" + + //Delete all the messages before check the sample email + utils.DeleteMailBox(newUserEmail) + + if err := SendEmailChangeVerifyEmail(userId, newUserEmail, locale, siteURL); err != nil { + t.Log(err) + t.Fatal("Should send change username email") + } else { + //Check if the email was send to the rigth email address + if resultsMailbox, err := utils.GetMailBox(newUserEmail); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], newUserEmail) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(newUserEmail, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + if !strings.Contains(resultsEmail.Body.Text, utils.UrlEncode(newUserEmail)) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong new email in the message") + } + } + } + } +} + +func TestSendEmailChangeEmail(t *testing.T) { + Setup() + utils.LoadConfig("config.json") + + var oldEmail string = "test@example.com" + var newUserEmail string = "newtest@example.com" + var locale string = "en" + var siteURL string = "" + var expectedPartialMessage string = "Your email address for Mattermost has been changed to " + newUserEmail + var expectedSubject string = "[" + utils.Cfg.TeamSettings.SiteName + "] Your email address has changed for Mattermost" + + //Delete all the messages before check the sample email + utils.DeleteMailBox(oldEmail) + + if err := SendEmailChangeEmail(oldEmail, newUserEmail, locale, siteURL); err != nil { + t.Log(err) + t.Fatal("Should send change username email") + } else { + //Check if the email was send to the rigth email address + if resultsMailbox, err := utils.GetMailBox(oldEmail); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], oldEmail) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(oldEmail, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + } + } + } +} + +func TestSendVerifyEmail(t *testing.T) { + Setup() + utils.LoadConfig("config.json") + + var userId string = "5349853498543jdfvndf9834" + var userEmail string = "test@example.com" + var locale string = "en" + var siteURL string = "" + var expectedPartialMessage string = "Please verify your email address by clicking below" + var expectedSubject string = "[" + utils.Cfg.TeamSettings.SiteName + "] Email Verification" + + //Delete all the messages before check the sample email + utils.DeleteMailBox(userEmail) + + if err := SendVerifyEmail(userId, userEmail, locale, siteURL); err != nil { + t.Log(err) + t.Fatal("Should send change username email") + } else { + //Check if the email was send to the rigth email address + if resultsMailbox, err := utils.GetMailBox(userEmail); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], userEmail) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(userEmail, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + if !strings.Contains(resultsEmail.Body.Text, utils.UrlEncode(userEmail)) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong new email in the message") + } + } + } + } +} + +func TestSendSignInChangeEmail(t *testing.T) { + Setup() + utils.LoadConfig("config.json") + + var email string = "test@example.com" + var locale string = "en" + var siteURL string = "" + var method string = "AD/LDAP" + var expectedPartialMessage string = "You updated your sign-in method on Mattermost to " + method + "." + var expectedSubject string = "You updated your sign-in method on Mattermost" + + //Delete all the messages before check the sample email + utils.DeleteMailBox(email) + + if err := SendSignInChangeEmail(email, method, locale, siteURL); err != nil { + t.Log(err) + t.Fatal("Should send change username email") + } else { + //Check if the email was send to the rigth email address + if resultsMailbox, err := utils.GetMailBox(email); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], email) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(email, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + } + } + } +} + +func TestSendWelcomeEmail(t *testing.T) { + Setup() + utils.LoadConfig("config.json") + + var userId string = "32432nkjnijn432uj32" + var email string = "test@example.com" + var locale string = "en" + var siteURL string = "http://test.mattermost.io" + var verified bool = true + var expectedPartialMessage string = "Mattermost lets you share messages and files from your PC or phone, with instant search and archiving" + var expectedSubject string = "You joined test.mattermost.io" + + //Delete all the messages before check the sample email + utils.DeleteMailBox(email) + + if err := SendWelcomeEmail(userId, email, verified, locale, siteURL); err != nil { + t.Log(err) + t.Fatal("Should send change username email") + } else { + //Check if the email was send to the rigth email address + if resultsMailbox, err := utils.GetMailBox(email); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], email) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(email, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + } + } + } + + utils.DeleteMailBox(email) + verified = false + var expectedVerifyEmail string = "Please verify your email address by clicking below." + + if err := SendWelcomeEmail(userId, email, verified, locale, siteURL); err != nil { + t.Log(err) + t.Fatal("Should send change username email") + } else { + //Check if the email was send to the rigth email address + if resultsMailbox, err := utils.GetMailBox(email); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], email) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(email, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + if !strings.Contains(resultsEmail.Body.Text, expectedVerifyEmail) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + if !strings.Contains(resultsEmail.Body.Text, utils.UrlEncode(email)) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong email in the message") + } + } + } + } +} + +func TestSendPasswordChangeEmail(t *testing.T) { + Setup() + utils.LoadConfig("config.json") + + var email string = "test@example.com" + var locale string = "en" + var siteURL string = "http://test.mattermost.io" + var method string = "using a reset password link" + var expectedPartialMessage string = "Your password has been updated for " + utils.Cfg.TeamSettings.SiteName + " on " + siteURL + " by " + method + var expectedSubject string = "Your password has been updated for " + utils.Cfg.TeamSettings.SiteName + " on " + utils.Cfg.TeamSettings.SiteName + + //Delete all the messages before check the sample email + utils.DeleteMailBox(email) + + if err := SendPasswordChangeEmail(email, method, locale, siteURL); err != nil { + t.Log(err) + t.Fatal("Should send change username email") + } else { + //Check if the email was send to the rigth email address + if resultsMailbox, err := utils.GetMailBox(email); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], email) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(email, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + } + } + } +} + +func TestSendMfaChangeEmail(t *testing.T) { + Setup() + utils.LoadConfig("config.json") + + var email string = "test@example.com" + var locale string = "en" + var siteURL string = "http://test.mattermost.io" + var activated bool = true + var expectedPartialMessage string = "Multi-factor authentication has been added to your account on " + siteURL + "." + var expectedSubject string = "Your MFA has been updated on " + utils.Cfg.TeamSettings.SiteName + + //Delete all the messages before check the sample email + utils.DeleteMailBox(email) + + if err := SendMfaChangeEmail(email, activated, locale, siteURL); err != nil { + t.Log(err) + t.Fatal("Should send change username email") + } else { + //Check if the email was send to the rigth email address + if resultsMailbox, err := utils.GetMailBox(email); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], email) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(email, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + } + } + } + + activated = false + expectedPartialMessage = "Multi-factor authentication has been removed from your account on " + siteURL + "." + utils.DeleteMailBox(email) + + if err := SendMfaChangeEmail(email, activated, locale, siteURL); err != nil { + t.Log(err) + t.Fatal("Should send change username email") + } else { + //Check if the email was send to the rigth email address + if resultsMailbox, err := utils.GetMailBox(email); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], email) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(email, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + } + } + } +} + +func TestSendInviteEmails(t *testing.T) { + th := Setup().InitBasic() + utils.LoadConfig("config.json") + + var email1 string = "test1@example.com" + var email2 string = "test2@example.com" + var senderName string = "TheBoss" + var siteURL string = "http://test.mattermost.io" + invites := []string{email1, email2} + var expectedPartialMessage string = "The team member *" + senderName + "* , has invited you to join *" + th.BasicTeam.DisplayName + "*" + var expectedSubject string = senderName + " invited you to join " + th.BasicTeam.DisplayName + " Team on " + utils.Cfg.TeamSettings.SiteName + + //Delete all the messages before check the sample email + utils.DeleteMailBox(email1) + utils.DeleteMailBox(email2) + + SendInviteEmails(th.BasicTeam, senderName, invites, siteURL) + + //Check if the email was send to the rigth email address to email1 + if resultsMailbox, err := utils.GetMailBox(email1); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], email1) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(email1, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Log(expectedSubject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + } + } + + //Check if the email was send to the rigth email address to email2 + if resultsMailbox, err := utils.GetMailBox(email2); err != nil && !strings.ContainsAny(resultsMailbox[0].To[0], email2) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(email2, resultsMailbox[0].ID); err == nil { + if !strings.Contains(resultsEmail.Subject, expectedSubject) { + t.Log(resultsEmail.Subject) + t.Log(expectedSubject) + t.Fatal("Wrong Subject") + } + if !strings.Contains(resultsEmail.Body.Text, expectedPartialMessage) { + t.Log(resultsEmail.Body.Text) + t.Fatal("Wrong Body message") + } + } + } +} -- cgit v1.2.3-1-g7c22