github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/app/notification_email_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package app 5 6 import ( 7 "bytes" 8 "fmt" 9 "html/template" 10 "regexp" 11 "strings" 12 "testing" 13 "time" 14 15 "github.com/stretchr/testify/assert" 16 "github.com/stretchr/testify/require" 17 18 "github.com/mattermost/mattermost-server/v5/model" 19 "github.com/mattermost/mattermost-server/v5/services/timezones" 20 "github.com/mattermost/mattermost-server/v5/store/storetest/mocks" 21 "github.com/mattermost/mattermost-server/v5/utils" 22 ) 23 24 func TestGetDirectMessageNotificationEmailSubject(t *testing.T) { 25 expectedPrefix := "[http://localhost:8065] New Direct Message from @sender on" 26 user := &model.User{} 27 post := &model.Post{ 28 CreateAt: 1501804801000, 29 } 30 translateFunc := utils.GetUserTranslations("en") 31 subject := getDirectMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "@sender", true) 32 require.Regexp(t, regexp.MustCompile("^"+regexp.QuoteMeta(expectedPrefix)), subject, fmt.Sprintf("Expected subject line prefix '%s', got %s", expectedPrefix, subject)) 33 } 34 35 func TestGetGroupMessageNotificationEmailSubjectFull(t *testing.T) { 36 expectedPrefix := "[http://localhost:8065] New Group Message in sender on" 37 user := &model.User{} 38 post := &model.Post{ 39 CreateAt: 1501804801000, 40 } 41 translateFunc := utils.GetUserTranslations("en") 42 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 43 subject := getGroupMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "sender", emailNotificationContentsType, true) 44 require.Regexp(t, regexp.MustCompile("^"+regexp.QuoteMeta(expectedPrefix)), subject, fmt.Sprintf("Expected subject line prefix '%s', got %s", expectedPrefix, subject)) 45 } 46 47 func TestGetGroupMessageNotificationEmailSubjectGeneric(t *testing.T) { 48 expectedPrefix := "[http://localhost:8065] New Group Message on" 49 user := &model.User{} 50 post := &model.Post{ 51 CreateAt: 1501804801000, 52 } 53 translateFunc := utils.GetUserTranslations("en") 54 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC 55 subject := getGroupMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "sender", emailNotificationContentsType, true) 56 require.Regexp(t, regexp.MustCompile("^"+regexp.QuoteMeta(expectedPrefix)), subject, fmt.Sprintf("Expected subject line prefix '%s', got %s", expectedPrefix, subject)) 57 } 58 59 func TestGetNotificationEmailSubject(t *testing.T) { 60 expectedPrefix := "[http://localhost:8065] Notification in team on" 61 user := &model.User{} 62 post := &model.Post{ 63 CreateAt: 1501804801000, 64 } 65 translateFunc := utils.GetUserTranslations("en") 66 subject := getNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "team", true) 67 require.Regexp(t, regexp.MustCompile("^"+regexp.QuoteMeta(expectedPrefix)), subject, fmt.Sprintf("Expected subject line prefix '%s', got %s", expectedPrefix, subject)) 68 } 69 70 func TestGetNotificationEmailBodyFullNotificationPublicChannel(t *testing.T) { 71 th := SetupWithStoreMock(t) 72 defer th.TearDown() 73 74 recipient := &model.User{} 75 post := &model.Post{ 76 Message: "This is the message", 77 } 78 channel := &model.Channel{ 79 DisplayName: "ChannelName", 80 Type: model.CHANNEL_OPEN, 81 } 82 channelName := "ChannelName" 83 senderName := "sender" 84 teamName := "testteam" 85 teamURL := "http://localhost:8065/testteam" 86 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 87 translateFunc := utils.GetUserTranslations("en") 88 89 storeMock := th.App.Srv().Store.(*mocks.Store) 90 teamStoreMock := mocks.TeamStore{} 91 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 92 storeMock.On("Team").Return(&teamStoreMock) 93 94 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 95 require.Contains(t, body, "You have a new notification.", fmt.Sprintf("Expected email text 'You have a new notification. Got %s", body)) 96 require.Contains(t, body, "Channel: "+channel.DisplayName, "Expected email text 'Channel: %s'. Got %s", channel.DisplayName, body) 97 require.Contains(t, body, senderName+" - ", fmt.Sprintf("Expected email text '%s - '. Got %s", senderName, body)) 98 require.Contains(t, body, post.Message, fmt.Sprintf("Expected email text '%s'. Got %s", post.Message, body)) 99 require.Contains(t, body, teamURL, fmt.Sprintf("Expected email text '%s'. Got %s", teamURL, body)) 100 } 101 102 func TestGetNotificationEmailBodyFullNotificationGroupChannel(t *testing.T) { 103 th := SetupWithStoreMock(t) 104 defer th.TearDown() 105 106 recipient := &model.User{} 107 post := &model.Post{ 108 Message: "This is the message", 109 } 110 channel := &model.Channel{ 111 DisplayName: "ChannelName", 112 Type: model.CHANNEL_GROUP, 113 } 114 channelName := "ChannelName" 115 senderName := "sender" 116 teamName := "testteam" 117 teamURL := "http://localhost:8065/testteam" 118 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 119 translateFunc := utils.GetUserTranslations("en") 120 121 storeMock := th.App.Srv().Store.(*mocks.Store) 122 teamStoreMock := mocks.TeamStore{} 123 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 124 storeMock.On("Team").Return(&teamStoreMock) 125 126 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 127 require.Contains(t, body, "You have a new Group Message.", fmt.Sprintf("Expected email text 'You have a new Group Message. Got "+body)) 128 require.Contains(t, body, "Channel: ChannelName", fmt.Sprintf("Expected email text 'Channel: ChannelName'. Got %s", body)) 129 require.Contains(t, body, senderName+" - ", fmt.Sprintf("Expected email text '%s - '. Got %s", senderName, body)) 130 require.Contains(t, body, post.Message, fmt.Sprintf("Expected email text '%s'. Got %s", post.Message, body)) 131 require.Contains(t, body, teamURL, fmt.Sprintf("Expected email text '%s'. Got %s", teamURL, body)) 132 } 133 134 func TestGetNotificationEmailBodyFullNotificationPrivateChannel(t *testing.T) { 135 th := SetupWithStoreMock(t) 136 defer th.TearDown() 137 138 recipient := &model.User{} 139 post := &model.Post{ 140 Message: "This is the message", 141 } 142 channel := &model.Channel{ 143 DisplayName: "ChannelName", 144 Type: model.CHANNEL_PRIVATE, 145 } 146 channelName := "ChannelName" 147 senderName := "sender" 148 teamName := "testteam" 149 teamURL := "http://localhost:8065/testteam" 150 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 151 translateFunc := utils.GetUserTranslations("en") 152 153 storeMock := th.App.Srv().Store.(*mocks.Store) 154 teamStoreMock := mocks.TeamStore{} 155 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 156 storeMock.On("Team").Return(&teamStoreMock) 157 158 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 159 require.Contains(t, body, "You have a new notification.", fmt.Sprintf("Expected email text 'You have a new notification. Got "+body)) 160 require.Contains(t, body, "Channel: "+channel.DisplayName, fmt.Sprintf("Expected email text 'Channel: "+channel.DisplayName+"'. Got "+body)) 161 require.Contains(t, body, senderName+" - ", fmt.Sprintf("Expected email text '%s - '. Got %s", senderName, body)) 162 require.Contains(t, body, post.Message, fmt.Sprintf("Expected email text '%s'. Got %s", post.Message, body)) 163 require.Contains(t, body, teamURL, fmt.Sprintf("Expected email text '%s'. Got %s", teamURL, body)) 164 } 165 166 func TestGetNotificationEmailBodyFullNotificationDirectChannel(t *testing.T) { 167 th := SetupWithStoreMock(t) 168 defer th.TearDown() 169 170 recipient := &model.User{} 171 post := &model.Post{ 172 Message: "This is the message", 173 } 174 channel := &model.Channel{ 175 DisplayName: "ChannelName", 176 Type: model.CHANNEL_DIRECT, 177 } 178 channelName := "ChannelName" 179 senderName := "sender" 180 teamName := "testteam" 181 teamURL := "http://localhost:8065/testteam" 182 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 183 translateFunc := utils.GetUserTranslations("en") 184 185 storeMock := th.App.Srv().Store.(*mocks.Store) 186 teamStoreMock := mocks.TeamStore{} 187 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 188 storeMock.On("Team").Return(&teamStoreMock) 189 190 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 191 require.Contains(t, body, "You have a new Direct Message.", fmt.Sprintf("Expected email text 'You have a new Direct Message. Got "+body)) 192 require.Contains(t, body, senderName+" - ", fmt.Sprintf("Expected email text '%s - '. Got %s", senderName, body)) 193 require.Contains(t, body, post.Message, fmt.Sprintf("Expected email text '%s'. Got %s", post.Message, body)) 194 require.Contains(t, body, teamURL, fmt.Sprintf("Expected email text '%s'. Got %s", teamURL, body)) 195 } 196 197 func TestGetNotificationEmailBodyFullNotificationLocaleTimeWithTimezone(t *testing.T) { 198 th := SetupWithStoreMock(t) 199 defer th.TearDown() 200 201 recipient := &model.User{ 202 Timezone: timezones.DefaultUserTimezone(), 203 } 204 recipient.Timezone["automaticTimezone"] = "America/New_York" 205 post := &model.Post{ 206 CreateAt: 1524663790000, 207 Message: "This is the message", 208 } 209 channel := &model.Channel{ 210 DisplayName: "ChannelName", 211 Type: model.CHANNEL_DIRECT, 212 } 213 channelName := "ChannelName" 214 senderName := "sender" 215 teamName := "testteam" 216 teamURL := "http://localhost:8065/testteam" 217 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 218 translateFunc := utils.GetUserTranslations("en") 219 220 storeMock := th.App.Srv().Store.(*mocks.Store) 221 teamStoreMock := mocks.TeamStore{} 222 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 223 storeMock.On("Team").Return(&teamStoreMock) 224 225 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, false, translateFunc) 226 r, _ := regexp.Compile("E([S|D]+)T") 227 zone := r.FindString(body) 228 require.Contains(t, body, "sender - 9:43 AM "+zone+", April 25", fmt.Sprintf("Expected email text 'sender - 9:43 AM %s, April 25'. Got %s", zone, body)) 229 } 230 231 func TestGetNotificationEmailBodyFullNotificationLocaleTimeNoTimezone(t *testing.T) { 232 th := SetupWithStoreMock(t) 233 defer th.TearDown() 234 235 recipient := &model.User{ 236 Timezone: timezones.DefaultUserTimezone(), 237 } 238 post := &model.Post{ 239 CreateAt: 1524681000000, 240 Message: "This is the message", 241 } 242 channel := &model.Channel{ 243 DisplayName: "ChannelName", 244 Type: model.CHANNEL_DIRECT, 245 } 246 channelName := "ChannelName" 247 senderName := "sender" 248 teamName := "testteam" 249 teamURL := "http://localhost:8065/testteam" 250 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 251 translateFunc := utils.GetUserTranslations("en") 252 253 storeMock := th.App.Srv().Store.(*mocks.Store) 254 teamStoreMock := mocks.TeamStore{} 255 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 256 storeMock.On("Team").Return(&teamStoreMock) 257 258 tm := time.Unix(post.CreateAt/1000, 0) 259 zone, _ := tm.Zone() 260 261 formattedTime := formattedPostTime{ 262 Time: tm, 263 Year: fmt.Sprintf("%d", tm.Year()), 264 Month: translateFunc(tm.Month().String()), 265 Day: fmt.Sprintf("%d", tm.Day()), 266 Hour: fmt.Sprintf("%02d", tm.Hour()), 267 Minute: fmt.Sprintf("%02d", tm.Minute()), 268 TimeZone: zone, 269 } 270 271 tmp, err := template.New("foo").Parse(`{{.}}`) 272 require.NoError(t, err) 273 var text bytes.Buffer 274 err = tmp.Execute(&text, fmt.Sprintf("sender - %s:%s %s, %s %s", formattedTime.Hour, formattedTime.Minute, formattedTime.TimeZone, formattedTime.Month, formattedTime.Day)) 275 require.NoError(t, err) 276 277 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 278 postTimeLine := text.String() 279 require.Contains(t, body, postTimeLine, fmt.Sprintf("Expected email text '%s'. Got %s", postTimeLine, body)) 280 } 281 282 func TestGetNotificationEmailBodyFullNotificationLocaleTime12Hour(t *testing.T) { 283 th := SetupWithStoreMock(t) 284 defer th.TearDown() 285 286 recipient := &model.User{ 287 Timezone: timezones.DefaultUserTimezone(), 288 } 289 recipient.Timezone["automaticTimezone"] = "America/New_York" 290 post := &model.Post{ 291 CreateAt: 1524681000000, // 1524681000 // 1524681000000 292 Message: "This is the message", 293 } 294 channel := &model.Channel{ 295 DisplayName: "ChannelName", 296 Type: model.CHANNEL_DIRECT, 297 } 298 channelName := "ChannelName" 299 senderName := "sender" 300 teamName := "testteam" 301 teamURL := "http://localhost:8065/testteam" 302 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 303 translateFunc := utils.GetUserTranslations("en") 304 305 storeMock := th.App.Srv().Store.(*mocks.Store) 306 teamStoreMock := mocks.TeamStore{} 307 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 308 storeMock.On("Team").Return(&teamStoreMock) 309 310 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, false, translateFunc) 311 require.Contains(t, body, "sender - 2:30 PM", fmt.Sprintf("Expected email text 'sender - 2:30 PM'. Got %s", body)) 312 require.Contains(t, body, "April 25", fmt.Sprintf("Expected email text 'April 25'. Got %s", body)) 313 } 314 315 func TestGetNotificationEmailBodyFullNotificationLocaleTime24Hour(t *testing.T) { 316 th := SetupWithStoreMock(t) 317 defer th.TearDown() 318 319 recipient := &model.User{ 320 Timezone: timezones.DefaultUserTimezone(), 321 } 322 recipient.Timezone["automaticTimezone"] = "America/New_York" 323 post := &model.Post{ 324 CreateAt: 1524681000000, 325 Message: "This is the message", 326 } 327 channel := &model.Channel{ 328 DisplayName: "ChannelName", 329 Type: model.CHANNEL_DIRECT, 330 } 331 channelName := "ChannelName" 332 senderName := "sender" 333 teamName := "testteam" 334 teamURL := "http://localhost:8065/testteam" 335 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 336 translateFunc := utils.GetUserTranslations("en") 337 338 storeMock := th.App.Srv().Store.(*mocks.Store) 339 teamStoreMock := mocks.TeamStore{} 340 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 341 storeMock.On("Team").Return(&teamStoreMock) 342 343 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 344 require.Contains(t, body, "sender - 14:30", fmt.Sprintf("Expected email text 'sender - 14:30'. Got %s", body)) 345 require.Contains(t, body, "April 25", fmt.Sprintf("Expected email text 'April 25'. Got %s", body)) 346 } 347 348 // from here 349 func TestGetNotificationEmailBodyGenericNotificationPublicChannel(t *testing.T) { 350 th := SetupWithStoreMock(t) 351 defer th.TearDown() 352 353 recipient := &model.User{} 354 post := &model.Post{ 355 Message: "This is the message", 356 } 357 channel := &model.Channel{ 358 DisplayName: "ChannelName", 359 Type: model.CHANNEL_OPEN, 360 } 361 channelName := "ChannelName" 362 senderName := "sender" 363 teamName := "testteam" 364 teamURL := "http://localhost:8065/testteam" 365 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC 366 translateFunc := utils.GetUserTranslations("en") 367 368 storeMock := th.App.Srv().Store.(*mocks.Store) 369 teamStoreMock := mocks.TeamStore{} 370 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 371 storeMock.On("Team").Return(&teamStoreMock) 372 373 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 374 require.Contains(t, body, "You have a new notification from "+senderName, fmt.Sprintf("Expected email text 'You have a new notification from %s'. Got %s", senderName, body)) 375 require.False(t, strings.Contains(body, "Channel: "+channel.DisplayName), fmt.Sprintf("Did not expect email text 'CHANNEL: %s'. Got %s", channel.DisplayName, body)) 376 require.False(t, strings.Contains(body, post.Message), fmt.Sprintf("Did not expect email text '%s'. Got %s", post.Message, body)) 377 require.Contains(t, body, teamURL, fmt.Sprintf("Expected email text '%s'. Got %s", teamURL, body)) 378 } 379 380 func TestGetNotificationEmailBodyGenericNotificationGroupChannel(t *testing.T) { 381 th := SetupWithStoreMock(t) 382 defer th.TearDown() 383 384 recipient := &model.User{} 385 post := &model.Post{ 386 Message: "This is the message", 387 } 388 channel := &model.Channel{ 389 DisplayName: "ChannelName", 390 Type: model.CHANNEL_GROUP, 391 } 392 channelName := "ChannelName" 393 senderName := "sender" 394 teamName := "testteam" 395 teamURL := "http://localhost:8065/testteam" 396 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC 397 translateFunc := utils.GetUserTranslations("en") 398 399 storeMock := th.App.Srv().Store.(*mocks.Store) 400 teamStoreMock := mocks.TeamStore{} 401 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 402 storeMock.On("Team").Return(&teamStoreMock) 403 404 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 405 require.Contains(t, body, "You have a new Group Message from "+senderName, fmt.Sprintf("Expected email text 'You have a new Group Message from %s'. Got %s", senderName, body)) 406 require.False(t, strings.Contains(body, "CHANNEL: "+channel.DisplayName), fmt.Sprintf("Did not expect email text 'CHANNEL: %s'. Got %s", channel.DisplayName, body)) 407 require.False(t, strings.Contains(body, post.Message), fmt.Sprintf("Did not expect email text '%s'. Got %s", post.Message, body)) 408 require.Contains(t, body, teamURL, fmt.Sprintf("Expected email text '%s'. Got %s", teamURL, body)) 409 } 410 411 func TestGetNotificationEmailBodyGenericNotificationPrivateChannel(t *testing.T) { 412 th := SetupWithStoreMock(t) 413 defer th.TearDown() 414 415 recipient := &model.User{} 416 post := &model.Post{ 417 Message: "This is the message", 418 } 419 channel := &model.Channel{ 420 DisplayName: "ChannelName", 421 Type: model.CHANNEL_PRIVATE, 422 } 423 channelName := "ChannelName" 424 senderName := "sender" 425 teamName := "testteam" 426 teamURL := "http://localhost:8065/testteam" 427 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC 428 translateFunc := utils.GetUserTranslations("en") 429 430 storeMock := th.App.Srv().Store.(*mocks.Store) 431 teamStoreMock := mocks.TeamStore{} 432 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 433 storeMock.On("Team").Return(&teamStoreMock) 434 435 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 436 require.Contains(t, body, "You have a new notification from "+senderName, fmt.Sprintf("Expected email text 'You have a new notification from %s'. Got %s", senderName, body)) 437 require.False(t, strings.Contains(body, "CHANNEL: "+channel.DisplayName), fmt.Sprintf("Did not expect email text 'CHANNEL: %s'. Got %s", channel.DisplayName, body)) 438 require.False(t, strings.Contains(body, post.Message), fmt.Sprintf("Did not expect email text '%s'. Got %s", post.Message, body)) 439 require.Contains(t, body, teamURL, fmt.Sprintf("Expected email text '%s'. Got %s", teamURL, body)) 440 } 441 442 func TestGetNotificationEmailBodyGenericNotificationDirectChannel(t *testing.T) { 443 th := SetupWithStoreMock(t) 444 defer th.TearDown() 445 446 recipient := &model.User{} 447 post := &model.Post{ 448 Message: "This is the message", 449 } 450 channel := &model.Channel{ 451 DisplayName: "ChannelName", 452 Type: model.CHANNEL_DIRECT, 453 } 454 channelName := "ChannelName" 455 senderName := "sender" 456 teamName := "testteam" 457 teamURL := "http://localhost:8065/testteam" 458 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC 459 translateFunc := utils.GetUserTranslations("en") 460 461 storeMock := th.App.Srv().Store.(*mocks.Store) 462 teamStoreMock := mocks.TeamStore{} 463 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 464 storeMock.On("Team").Return(&teamStoreMock) 465 466 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 467 require.Contains(t, body, "You have a new Direct Message from "+senderName, fmt.Sprintf("Expected email text 'You have a new Direct Message from "+senderName+"'. Got "+body)) 468 require.False(t, strings.Contains(body, "CHANNEL: "+channel.DisplayName), fmt.Sprintf("Did not expect email text 'CHANNEL: %s'. Got %s", channel.DisplayName, body)) 469 require.False(t, strings.Contains(body, post.Message), fmt.Sprintf("Did not expect email text '%s'. Got %s", post.Message, body)) 470 require.Contains(t, body, teamURL, fmt.Sprintf("Expected email text '%s'. Got %s", teamURL, body)) 471 } 472 473 func TestGetNotificationEmailEscapingChars(t *testing.T) { 474 th := SetupWithStoreMock(t) 475 defer th.TearDown() 476 477 ch := &model.Channel{ 478 DisplayName: "ChannelName", 479 Type: model.CHANNEL_OPEN, 480 } 481 channelName := "ChannelName" 482 recipient := &model.User{} 483 message := "<b>Bold Test</b>" 484 post := &model.Post{ 485 Message: message, 486 } 487 488 senderName := "sender" 489 teamName := "testteam" 490 teamURL := "http://localhost:8065/testteam" 491 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 492 translateFunc := utils.GetUserTranslations("en") 493 494 storeMock := th.App.Srv().Store.(*mocks.Store) 495 teamStoreMock := mocks.TeamStore{} 496 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 497 storeMock.On("Team").Return(&teamStoreMock) 498 499 body := th.App.getNotificationEmailBody(recipient, post, ch, 500 channelName, senderName, teamName, teamURL, 501 emailNotificationContentsType, true, translateFunc) 502 503 assert.NotContains(t, body, message) 504 } 505 506 func TestGetNotificationEmailBodyPublicChannelMention(t *testing.T) { 507 th := SetupWithStoreMock(t) 508 defer th.TearDown() 509 510 ch := &model.Channel{ 511 Name: "channelname", 512 DisplayName: "ChannelName", 513 Type: model.CHANNEL_OPEN, 514 } 515 id := model.NewId() 516 recipient := &model.User{ 517 Email: "success+" + id + "@simulator.amazonses.com", 518 Username: "un_" + id, 519 Nickname: "nn_" + id, 520 Password: "Password1", 521 EmailVerified: true, 522 } 523 post := &model.Post{ 524 Message: "This is the message ~" + ch.Name, 525 } 526 527 senderName := "user1" 528 teamName := "testteam" 529 teamURL := "http://localhost:8065/testteam" 530 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 531 translateFunc := utils.GetUserTranslations("en") 532 533 storeMock := th.App.Srv().Store.(*mocks.Store) 534 teamStoreMock := mocks.TeamStore{} 535 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil) 536 storeMock.On("Team").Return(&teamStoreMock) 537 538 channelStoreMock := mocks.ChannelStore{} 539 channelStoreMock.On("GetByNames", "test", []string{ch.Name}, true).Return([]*model.Channel{ch}, nil) 540 storeMock.On("Channel").Return(&channelStoreMock) 541 542 body := th.App.getNotificationEmailBody(recipient, post, ch, 543 ch.Name, senderName, teamName, teamURL, 544 emailNotificationContentsType, true, translateFunc) 545 channelURL := teamURL + "/channels/" + ch.Name 546 mention := "~" + ch.Name 547 assert.Contains(t, body, "<a href='"+channelURL+"'>"+mention+"</a>") 548 } 549 550 func TestGetNotificationEmailBodyMultiPublicChannelMention(t *testing.T) { 551 th := SetupWithStoreMock(t) 552 defer th.TearDown() 553 554 ch := &model.Channel{ 555 Id: model.NewId(), 556 Name: "channelnameone", 557 DisplayName: "ChannelName", 558 Type: model.CHANNEL_OPEN, 559 } 560 mention := "~" + ch.Name 561 562 ch2 := &model.Channel{ 563 Id: model.NewId(), 564 Name: "channelnametwo", 565 DisplayName: "ChannelName2", 566 Type: model.CHANNEL_OPEN, 567 } 568 mention2 := "~" + ch2.Name 569 570 ch3 := &model.Channel{ 571 Id: model.NewId(), 572 Name: "channelnamethree", 573 DisplayName: "ChannelName3", 574 Type: model.CHANNEL_OPEN, 575 } 576 mention3 := "~" + ch3.Name 577 578 message := fmt.Sprintf("This is the message Channel1: %s; Channel2: %s;"+ 579 " Channel3: %s", mention, mention2, mention3) 580 id := model.NewId() 581 recipient := &model.User{ 582 Email: "success+" + id + "@simulator.amazonses.com", 583 Username: "un_" + id, 584 Nickname: "nn_" + id, 585 Password: "Password1", 586 EmailVerified: true, 587 } 588 post := &model.Post{ 589 Message: message, 590 } 591 592 senderName := "user1" 593 teamName := "testteam" 594 teamURL := "http://localhost:8065/testteam" 595 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 596 translateFunc := utils.GetUserTranslations("en") 597 598 storeMock := th.App.Srv().Store.(*mocks.Store) 599 teamStoreMock := mocks.TeamStore{} 600 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil) 601 storeMock.On("Team").Return(&teamStoreMock) 602 603 channelStoreMock := mocks.ChannelStore{} 604 channelStoreMock.On("GetByNames", "test", []string{ch.Name, ch2.Name, ch3.Name}, true).Return([]*model.Channel{ch, ch2, ch3}, nil) 605 storeMock.On("Channel").Return(&channelStoreMock) 606 607 body := th.App.getNotificationEmailBody(recipient, post, ch, 608 ch.Name, senderName, teamName, teamURL, 609 emailNotificationContentsType, true, translateFunc) 610 channelURL := teamURL + "/channels/" + ch.Name 611 channelURL2 := teamURL + "/channels/" + ch2.Name 612 channelURL3 := teamURL + "/channels/" + ch3.Name 613 expMessage := fmt.Sprintf("This is the message Channel1: <a href='%s'>%s</a>;"+ 614 " Channel2: <a href='%s'>%s</a>; Channel3: <a href='%s'>%s</a>", 615 channelURL, mention, channelURL2, mention2, channelURL3, mention3) 616 assert.Contains(t, body, expMessage) 617 } 618 619 func TestGetNotificationEmailBodyPrivateChannelMention(t *testing.T) { 620 th := SetupWithStoreMock(t) 621 defer th.TearDown() 622 623 ch := &model.Channel{ 624 Name: "channelname", 625 DisplayName: "ChannelName", 626 Type: model.CHANNEL_PRIVATE, 627 } 628 id := model.NewId() 629 recipient := &model.User{ 630 Email: "success+" + id + "@simulator.amazonses.com", 631 Username: "un_" + id, 632 Nickname: "nn_" + id, 633 Password: "Password1", 634 EmailVerified: true, 635 } 636 post := &model.Post{ 637 Message: "This is the message ~" + ch.Name, 638 } 639 640 senderName := "user1" 641 teamName := "testteam" 642 teamURL := "http://localhost:8065/testteam" 643 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 644 translateFunc := utils.GetUserTranslations("en") 645 646 storeMock := th.App.Srv().Store.(*mocks.Store) 647 teamStoreMock := mocks.TeamStore{} 648 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil) 649 storeMock.On("Team").Return(&teamStoreMock) 650 651 channelStoreMock := mocks.ChannelStore{} 652 channelStoreMock.On("GetByNames", "test", []string{ch.Name}, true).Return([]*model.Channel{ch}, nil) 653 storeMock.On("Channel").Return(&channelStoreMock) 654 655 body := th.App.getNotificationEmailBody(recipient, post, ch, 656 ch.Name, senderName, teamName, teamURL, 657 emailNotificationContentsType, true, translateFunc) 658 channelURL := teamURL + "/channels/" + ch.Name 659 mention := "~" + ch.Name 660 assert.NotContains(t, body, "<a href='"+channelURL+"'>"+mention+"</a>") 661 } 662 663 func TestGenerateHyperlinkForChannelsPublic(t *testing.T) { 664 th := SetupWithStoreMock(t) 665 defer th.TearDown() 666 667 ch := &model.Channel{ 668 Name: "channelname", 669 DisplayName: "ChannelName", 670 Type: model.CHANNEL_OPEN, 671 } 672 message := "This is the message " 673 mention := "~" + ch.Name 674 675 teamName := "testteam" 676 teamURL := "http://localhost:8065/testteam" 677 678 storeMock := th.App.Srv().Store.(*mocks.Store) 679 teamStoreMock := mocks.TeamStore{} 680 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil) 681 storeMock.On("Team").Return(&teamStoreMock) 682 683 channelStoreMock := mocks.ChannelStore{} 684 channelStoreMock.On("GetByNames", "test", []string{ch.Name}, true).Return([]*model.Channel{ch}, nil) 685 storeMock.On("Channel").Return(&channelStoreMock) 686 687 outMessage, err := th.App.generateHyperlinkForChannels(message+mention, teamName, teamURL) 688 require.Nil(t, err) 689 channelURL := teamURL + "/channels/" + ch.Name 690 assert.Equal(t, message+"<a href='"+channelURL+"'>"+mention+"</a>", outMessage) 691 } 692 693 func TestGenerateHyperlinkForChannelsMultiPublic(t *testing.T) { 694 th := SetupWithStoreMock(t) 695 defer th.TearDown() 696 697 // TODO: Fix the case where the first channel name contains the other channel names (for example here channelnameone)" 698 ch := &model.Channel{ 699 Id: model.NewId(), 700 Name: "channelnameone", 701 DisplayName: "ChannelName", 702 Type: model.CHANNEL_OPEN, 703 } 704 mention := "~" + ch.Name 705 706 ch2 := &model.Channel{ 707 Id: model.NewId(), 708 Name: "channelnametwo", 709 DisplayName: "ChannelName2", 710 Type: model.CHANNEL_OPEN, 711 } 712 mention2 := "~" + ch2.Name 713 714 ch3 := &model.Channel{ 715 Id: model.NewId(), 716 Name: "channelnamethree", 717 DisplayName: "ChannelName3", 718 Type: model.CHANNEL_OPEN, 719 } 720 mention3 := "~" + ch3.Name 721 722 message := fmt.Sprintf("This is the message Channel1: %s; Channel2: %s;"+ 723 " Channel3: %s", mention, mention2, mention3) 724 725 teamName := "testteam" 726 teamURL := "http://localhost:8065/testteam" 727 728 storeMock := th.App.Srv().Store.(*mocks.Store) 729 teamStoreMock := mocks.TeamStore{} 730 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil) 731 storeMock.On("Team").Return(&teamStoreMock) 732 733 channelStoreMock := mocks.ChannelStore{} 734 channelStoreMock.On("GetByNames", "test", []string{ch.Name, ch2.Name, ch3.Name}, true).Return([]*model.Channel{ch, ch2, ch3}, nil) 735 storeMock.On("Channel").Return(&channelStoreMock) 736 737 outMessage, err := th.App.generateHyperlinkForChannels(message, teamName, teamURL) 738 require.Nil(t, err) 739 channelURL := teamURL + "/channels/" + ch.Name 740 channelURL2 := teamURL + "/channels/" + ch2.Name 741 channelURL3 := teamURL + "/channels/" + ch3.Name 742 expMessage := fmt.Sprintf("This is the message Channel1: <a href='%s'>%s</a>;"+ 743 " Channel2: <a href='%s'>%s</a>; Channel3: <a href='%s'>%s</a>", 744 channelURL, mention, channelURL2, mention2, channelURL3, mention3) 745 assert.Equal(t, expMessage, outMessage) 746 } 747 748 func TestGenerateHyperlinkForChannelsPrivate(t *testing.T) { 749 th := SetupWithStoreMock(t) 750 defer th.TearDown() 751 752 ch := &model.Channel{ 753 Name: "channelname", 754 DisplayName: "ChannelName", 755 Type: model.CHANNEL_PRIVATE, 756 } 757 message := "This is the message ~" + ch.Name 758 759 teamName := "testteam" 760 teamURL := "http://localhost:8065/testteam" 761 762 storeMock := th.App.Srv().Store.(*mocks.Store) 763 teamStoreMock := mocks.TeamStore{} 764 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil) 765 storeMock.On("Team").Return(&teamStoreMock) 766 767 channelStoreMock := mocks.ChannelStore{} 768 channelStoreMock.On("GetByNames", "test", []string{ch.Name}, true).Return([]*model.Channel{ch}, nil) 769 storeMock.On("Channel").Return(&channelStoreMock) 770 771 outMessage, err := th.App.generateHyperlinkForChannels(message, teamName, teamURL) 772 require.Nil(t, err) 773 assert.Equal(t, message, outMessage) 774 } 775 776 func TestLandingLink(t *testing.T) { 777 th := SetupWithStoreMock(t) 778 defer th.TearDown() 779 780 recipient := &model.User{} 781 post := &model.Post{ 782 Message: "This is the message", 783 } 784 channel := &model.Channel{ 785 DisplayName: "ChannelName", 786 Type: model.CHANNEL_OPEN, 787 } 788 channelName := "ChannelName" 789 senderName := "sender" 790 teamName := "testteam" 791 teamURL := "http://localhost:8065/landing#/testteam" 792 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 793 translateFunc := utils.GetUserTranslations("en") 794 795 storeMock := th.App.Srv().Store.(*mocks.Store) 796 teamStoreMock := mocks.TeamStore{} 797 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 798 storeMock.On("Team").Return(&teamStoreMock) 799 800 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 801 require.Contains(t, body, teamURL, fmt.Sprintf("Expected email text '%s'. Got %s", teamURL, body)) 802 } 803 804 func TestLandingLinkPermalink(t *testing.T) { 805 th := SetupWithStoreMock(t) 806 defer th.TearDown() 807 808 recipient := &model.User{} 809 post := &model.Post{ 810 Id: "Test_id", 811 Message: "This is the message", 812 } 813 channel := &model.Channel{ 814 DisplayName: "ChannelName", 815 Type: model.CHANNEL_OPEN, 816 } 817 channelName := "ChannelName" 818 senderName := "sender" 819 teamName := "testteam" 820 teamURL := "http://localhost:8065/landing#/testteam" 821 emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL 822 translateFunc := utils.GetUserTranslations("en") 823 824 storeMock := th.App.Srv().Store.(*mocks.Store) 825 teamStoreMock := mocks.TeamStore{} 826 teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil) 827 storeMock.On("Team").Return(&teamStoreMock) 828 829 body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc) 830 require.Contains(t, body, teamURL+"/pl/"+post.Id, fmt.Sprintf("Expected email text '%s'. Got %s", teamURL, body)) 831 }