email_test.go (10393B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package email_test 19 20 import ( 21 "testing" 22 23 "github.com/stretchr/testify/suite" 24 "github.com/superseriousbusiness/gotosocial/internal/config" 25 "github.com/superseriousbusiness/gotosocial/internal/email" 26 "github.com/superseriousbusiness/gotosocial/testrig" 27 ) 28 29 type EmailTestSuite struct { 30 suite.Suite 31 32 sender email.Sender 33 34 sentEmails map[string]string 35 } 36 37 func (suite *EmailTestSuite) SetupTest() { 38 testrig.InitTestLog() 39 suite.sentEmails = make(map[string]string) 40 suite.sender = testrig.NewEmailSender("../../web/template/", suite.sentEmails) 41 } 42 43 func (suite *EmailTestSuite) TestTemplateConfirm() { 44 confirmData := email.ConfirmData{ 45 Username: "test", 46 InstanceURL: "https://example.org", 47 InstanceName: "Test Instance", 48 ConfirmLink: "https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa", 49 } 50 51 suite.sender.SendConfirmEmail("user@example.org", confirmData) 52 suite.Len(suite.sentEmails, 1) 53 suite.Equal("To: user@example.org\r\nFrom: test@example.org\r\nSubject: GoToSocial Email Confirmation\r\n\r\nHello test!\r\n\r\nYou are receiving this mail because you've requested an account on https://example.org.\r\n\r\nWe just need to confirm that this is your email address. To confirm your email, paste the following in your browser's address bar:\r\n\r\nhttps://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\r\n\r\nIf you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of https://example.org\r\n\r\n", suite.sentEmails["user@example.org"]) 54 } 55 56 func (suite *EmailTestSuite) TestTemplateReset() { 57 resetData := email.ResetData{ 58 Username: "test", 59 InstanceURL: "https://example.org", 60 InstanceName: "Test Instance", 61 ResetLink: "https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa", 62 } 63 64 suite.sender.SendResetEmail("user@example.org", resetData) 65 suite.Len(suite.sentEmails, 1) 66 suite.Equal("To: user@example.org\r\nFrom: test@example.org\r\nSubject: GoToSocial Password Reset\r\n\r\nHello test!\r\n\r\nYou are receiving this mail because a password reset has been requested for your account on https://example.org.\r\n\r\nTo reset your password, paste the following in your browser's address bar:\r\n\r\nhttps://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\r\n\r\nIf you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of https://example.org.\r\n\r\n", suite.sentEmails["user@example.org"]) 67 } 68 69 func (suite *EmailTestSuite) TestTemplateReportRemoteToLocal() { 70 // Someone from a remote instance has reported one of our users. 71 reportData := email.NewReportData{ 72 InstanceURL: "https://example.org", 73 InstanceName: "Test Instance", 74 ReportURL: "https://example.org/settings/admin/reports/01GVJHN1RTYZCZTCXVPPPKBX6R", 75 ReportDomain: "fossbros-anonymous.io", 76 ReportTargetDomain: "", 77 } 78 79 if err := suite.sender.SendNewReportEmail([]string{"user@example.org"}, reportData); err != nil { 80 suite.FailNow(err.Error()) 81 } 82 suite.Len(suite.sentEmails, 1) 83 suite.Equal("To: user@example.org\r\nFrom: test@example.org\r\nSubject: GoToSocial New Report\r\n\r\nHello moderator of Test Instance (https://example.org)!\r\n\r\nSomeone from fossbros-anonymous.io has reported a user from your instance.\r\n\r\nTo view the report, paste the following link into your browser: https://example.org/settings/admin/reports/01GVJHN1RTYZCZTCXVPPPKBX6R\r\n\r\n", suite.sentEmails["user@example.org"]) 84 } 85 86 func (suite *EmailTestSuite) TestTemplateReportLocalToRemote() { 87 // Someone from our instance has reported a remote user. 88 reportData := email.NewReportData{ 89 InstanceURL: "https://example.org", 90 InstanceName: "Test Instance", 91 ReportURL: "https://example.org/settings/admin/reports/01GVJHN1RTYZCZTCXVPPPKBX6R", 92 ReportDomain: "", 93 ReportTargetDomain: "fossbros-anonymous.io", 94 } 95 96 if err := suite.sender.SendNewReportEmail([]string{"user@example.org"}, reportData); err != nil { 97 suite.FailNow(err.Error()) 98 } 99 suite.Len(suite.sentEmails, 1) 100 suite.Equal("To: user@example.org\r\nFrom: test@example.org\r\nSubject: GoToSocial New Report\r\n\r\nHello moderator of Test Instance (https://example.org)!\r\n\r\nSomeone from your instance has reported a user from fossbros-anonymous.io.\r\n\r\nTo view the report, paste the following link into your browser: https://example.org/settings/admin/reports/01GVJHN1RTYZCZTCXVPPPKBX6R\r\n\r\n", suite.sentEmails["user@example.org"]) 101 } 102 103 func (suite *EmailTestSuite) TestTemplateReportLocalToLocal() { 104 // Someone from our instance has reported another user on our instance. 105 reportData := email.NewReportData{ 106 InstanceURL: "https://example.org", 107 InstanceName: "Test Instance", 108 ReportURL: "https://example.org/settings/admin/reports/01GVJHN1RTYZCZTCXVPPPKBX6R", 109 ReportDomain: "", 110 ReportTargetDomain: "", 111 } 112 113 if err := suite.sender.SendNewReportEmail([]string{"user@example.org"}, reportData); err != nil { 114 suite.FailNow(err.Error()) 115 } 116 suite.Len(suite.sentEmails, 1) 117 suite.Equal("To: user@example.org\r\nFrom: test@example.org\r\nSubject: GoToSocial New Report\r\n\r\nHello moderator of Test Instance (https://example.org)!\r\n\r\nSomeone from your instance has reported another user from your instance.\r\n\r\nTo view the report, paste the following link into your browser: https://example.org/settings/admin/reports/01GVJHN1RTYZCZTCXVPPPKBX6R\r\n\r\n", suite.sentEmails["user@example.org"]) 118 } 119 120 func (suite *EmailTestSuite) TestTemplateReportMoreThanOneModeratorAddress() { 121 reportData := email.NewReportData{ 122 InstanceURL: "https://example.org", 123 InstanceName: "Test Instance", 124 ReportURL: "https://example.org/settings/admin/reports/01GVJHN1RTYZCZTCXVPPPKBX6R", 125 ReportDomain: "fossbros-anonymous.io", 126 ReportTargetDomain: "", 127 } 128 129 // Send the email to multiple addresses 130 if err := suite.sender.SendNewReportEmail([]string{"user@example.org", "admin@example.org"}, reportData); err != nil { 131 suite.FailNow(err.Error()) 132 } 133 suite.Len(suite.sentEmails, 1) 134 suite.Equal("To: Undisclosed Recipients:;\r\nFrom: test@example.org\r\nSubject: GoToSocial New Report\r\n\r\nHello moderator of Test Instance (https://example.org)!\r\n\r\nSomeone from fossbros-anonymous.io has reported a user from your instance.\r\n\r\nTo view the report, paste the following link into your browser: https://example.org/settings/admin/reports/01GVJHN1RTYZCZTCXVPPPKBX6R\r\n\r\n", suite.sentEmails["user@example.org"]) 135 } 136 137 func (suite *EmailTestSuite) TestTemplateReportMoreThanOneModeratorAddressDisclose() { 138 config.SetSMTPDiscloseRecipients(true) 139 140 reportData := email.NewReportData{ 141 InstanceURL: "https://example.org", 142 InstanceName: "Test Instance", 143 ReportURL: "https://example.org/settings/admin/reports/01GVJHN1RTYZCZTCXVPPPKBX6R", 144 ReportDomain: "fossbros-anonymous.io", 145 ReportTargetDomain: "", 146 } 147 148 // Send the email to multiple addresses 149 if err := suite.sender.SendNewReportEmail([]string{"user@example.org", "admin@example.org"}, reportData); err != nil { 150 suite.FailNow(err.Error()) 151 } 152 suite.Len(suite.sentEmails, 1) 153 suite.Equal("To: user@example.org, admin@example.org\r\nFrom: test@example.org\r\nSubject: GoToSocial New Report\r\n\r\nHello moderator of Test Instance (https://example.org)!\r\n\r\nSomeone from fossbros-anonymous.io has reported a user from your instance.\r\n\r\nTo view the report, paste the following link into your browser: https://example.org/settings/admin/reports/01GVJHN1RTYZCZTCXVPPPKBX6R\r\n\r\n", suite.sentEmails["user@example.org"]) 154 } 155 156 func (suite *EmailTestSuite) TestTemplateReportClosedOK() { 157 reportClosedData := email.ReportClosedData{ 158 InstanceURL: "https://example.org", 159 InstanceName: "Test Instance", 160 ReportTargetUsername: "foss_satan", 161 ReportTargetDomain: "fossbros-anonymous.io", 162 ActionTakenComment: "User was yeeted. Thank you for reporting!", 163 } 164 165 if err := suite.sender.SendReportClosedEmail("user@example.org", reportClosedData); err != nil { 166 suite.FailNow(err.Error()) 167 } 168 suite.Len(suite.sentEmails, 1) 169 suite.Equal("To: user@example.org\r\nFrom: test@example.org\r\nSubject: GoToSocial Report Closed\r\n\r\nHello !\r\n\r\nYou recently reported the account @foss_satan@fossbros-anonymous.io to the moderator(s) of Test Instance (https://example.org).\r\n\r\nThe report you submitted has now been closed.\r\n\r\nThe moderator who closed the report left the following comment: User was yeeted. Thank you for reporting!\r\n\r\n", suite.sentEmails["user@example.org"]) 170 } 171 172 func (suite *EmailTestSuite) TestTemplateReportClosedLocalAccountNoComment() { 173 reportClosedData := email.ReportClosedData{ 174 InstanceURL: "https://example.org", 175 InstanceName: "Test Instance", 176 ReportTargetUsername: "1happyturtle", 177 ReportTargetDomain: "", 178 ActionTakenComment: "", 179 } 180 181 if err := suite.sender.SendReportClosedEmail("user@example.org", reportClosedData); err != nil { 182 suite.FailNow(err.Error()) 183 } 184 suite.Len(suite.sentEmails, 1) 185 suite.Equal("To: user@example.org\r\nFrom: test@example.org\r\nSubject: GoToSocial Report Closed\r\n\r\nHello !\r\n\r\nYou recently reported the account @1happyturtle to the moderator(s) of Test Instance (https://example.org).\r\n\r\nThe report you submitted has now been closed.\r\n\r\nThe moderator who closed the report did not leave a comment.\r\n\r\n", suite.sentEmails["user@example.org"]) 186 } 187 188 func TestEmailTestSuite(t *testing.T) { 189 suite.Run(t, new(EmailTestSuite)) 190 }