Files
team/server/channels/app/notification_email_test.go
9ed85b6850 Cherry-pick Go 1.25.8 upgrade and FIPS password fixes to release-11.4 (#36123)
* ci: shard server Postgres tests into 4 parallel runners (#35739)

Cherry-pick of 00296f9b49 from master.

* updated go to version 1.25.8 (#35817)

Cherry-pick of 32a10bac62 from master.

* Move password hashers from server/v8 to server/public (#35805)

Cherry-pick of cca3f08574 from master.

* Generate instead of hard-coding test passwords, enforce new minimum for FIPS, shard CI, fix FIPS builds (#35905)

Cherry-pick of 1bdd985b41 from master.

* upgrade golangci-lint (#35845)

Cherry-pick of 157e0400b2 from master.

On release-11.4 (unlike 11.5), UpdatePostsChannelTypeByChannelId and
BackfillPostsChannelType exist in the elasticsearch/opensearch files,
so the imports they require (fmt, updatebyquery, scriptlanguage) must
be added — they were not in the cherry-pick diff since they already
existed on master.

Also updates html2text/tablewriter/olekukonko deps to fix a workspace
conflict where enterprise's Go 1.25.8 bump pulled in a newer html2text
that requires tablewriter ≥ 1.1 (was pinned to 0.0.5 via a replace
directive). Matches the subset of 866f759e4b (Mm 66662 bump deps)
relevant to this incompatibility.

* Fix FIPS-incompatible passwords and config in e2e test suites (#36001)

Cherry-pick of b51b4a8937 from master.

release-11.4 differences vs release-11.5:
- demo_plugin_installation.spec.ts also deleted (modify/delete conflict, git rm)
- authentication_2_spec.ts conflict: HEAD missing check() call; took
  incoming (check + FIPS-length passwords)
- member_invitation_ui_spec.ts: HEAD has 'Create Account' (capital A)
  matching release-11.4 button text; kept HEAD's button text with
  incoming's newTestPassword()

* fix(cypress): demo plugin (#36056)

Cherry-pick of 39f1979aa5 from master.

demo_plugin_installation.spec.ts was deleted in step 10 (modify/delete);
re-created here with both the FIPS-password change and the URL fix.

* test: clean up channel store data after TestChannelStore (#36066)

TestChannelStore sub-tests create channels, members, and team members
using fake TeamIds and UserIds (model.NewId() for non-existent rows).
These records are left in the database and cause integrity tests
(TestCheck*) running in the same binary to fail their full-table scans.

Register a t.Cleanup on TestChannelStore that purges the affected
tables entirely. A blanket purge is safe: the schema enforces no FK
constraints, and every test suite creates its own data independently.

* Fix command injection in server-test-template workflow (#36080)

Cherry-pick of 9223935f16 from master.

* fix(ci): restore testname format in sharded gotestsum runs (#36078)

run-shard-tests.sh called gotestsum directly without --format, so it
fell back to gotestsum's default (pkgname) instead of the testname
format set by the Makefile. Pass --format "${GOTESTSUM_FORMAT:-testname}"
to match the Makefile default.

Co-authored-by: Mattermost Build <[email protected]>

* fixup: drop demo_plugin playwright specs — master-only APIs

demo_plugin/helpers.ts and demo_plugin_installation.spec.ts use
installAndEnablePlugin and isPluginActive which don't exist on
release-11.4's PlaywrightExtended type. These files were re-created
by the step-11 cherry-pick of 39f1979aa5, but they don't compile on
this branch (same class of problem as actions.spec.ts in step 10).

* fixup: add missing await in PluginLinkTooltip test

The test added in 39f1979aa5 is missing an await on renderWithContext,
causing the pointer-events assertion to race. Fix from 0c7d3f27c6
(on a feature branch, not yet on master).

* fixup: drop pointer-events test incompatible with jsdom@20

The 'should not block interaction with elements outside the tooltip' test
checks getComputedStyle for pointer-events: none, which is set only in SCSS.
release-11.4 uses jest-environment-jsdom@29 (jsdom@20), which does not apply
stylesheets, so getComputedStyle always returns "". The test passes on
release-11.5+ only because they use jest-environment-jsdom@30 (jsdom@25+).

Drop the test rather than backport the jsdom upgrade.

* fixup: add missing i18n keys for elasticsearch/opensearch functions

BackfillPostsChannelType and UpdatePostsChannelTypeByChannelId exist on
release-11.4 (unlike release-11.5+). The golangci-lint cherry-pick added
new error strings to those functions referencing i18n keys that weren't
in en.json.

* fixup: run make i18n-extract to reorder keys correctly

* fixup: drop terms-and-privacy checkbox from MM-T1774 on release-11.4

The #signup-body-card-form-check-terms-and-privacy element did not exist
on release-11.4's signup page. It was added to master after this branch
was cut, so the check() call from the incoming cherry-pick causes the test
to fail with "element never found".

* fixup: remove UpdatePostsChannelTypeByChannelId/BackfillPostsChannelType

These functions were introduced by MM-67540 (allow searching public channel
messages without channel membership), which never landed on release-11.4.
The golangci-lint cherry-pick only modernizes existing code; the rerere
resolution incorrectly added the full function bodies from master.

Remove both functions and their now-unused imports (fmt, updatebyquery,
scriptlanguage) from elasticsearch.go and opensearch.go. Also remove the
two i18n keys added to support them.

Same treatment as release-11.5, where these functions were also absent.

---------

Co-authored-by: Mattermost Build <[email protected]>
2026-04-16 12:10:03 -03:00

1226 lines
46 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"bytes"
"fmt"
"html/template"
"regexp"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/i18n"
"github.com/mattermost/mattermost/server/public/shared/timezones"
"github.com/mattermost/mattermost/server/v8/channels/store/storetest/mocks"
"github.com/mattermost/mattermost/server/v8/channels/testlib"
"github.com/mattermost/mattermost/server/v8/channels/utils"
)
// Helper function to create PostNotification for testing
func buildTestPostNotification(post *model.Post, channel *model.Channel, sender *model.User) *PostNotification {
return &PostNotification{
Channel: channel,
Post: post,
Sender: sender,
ProfileMap: make(map[string]*model.User),
}
}
// Helper function to create test user
func buildTestUser(id, username, displayName string, useMilitaryTime bool) *model.User {
return &model.User{
Id: id,
Username: username,
Nickname: displayName,
Locale: "en",
}
}
// Helper function to create test team
func buildTestTeam(id, name, displayName string) *model.Team {
return &model.Team{
Id: id,
Name: name,
DisplayName: displayName,
}
}
// Helper function to set up preference mocks
func setupPreferenceMocks(th *TestHelper, userId string, useMilitaryTime bool) {
preferenceStoreMock := mocks.PreferenceStore{}
if useMilitaryTime {
preferenceStoreMock.On("Get", userId, model.PreferenceCategoryDisplaySettings, model.PreferenceNameUseMilitaryTime).Return(&model.Preference{Value: "true"}, nil)
} else {
preferenceStoreMock.On("Get", userId, model.PreferenceCategoryDisplaySettings, model.PreferenceNameUseMilitaryTime).Return(&model.Preference{Value: "false"}, nil)
}
// Mock the name format preference as well
preferenceStoreMock.On("Get", userId, model.PreferenceCategoryDisplaySettings, model.PreferenceNameNameFormat).Return(&model.Preference{Value: model.ShowUsername}, nil)
th.App.Srv().Store().(*mocks.Store).On("Preference").Return(&preferenceStoreMock)
}
func TestGetDirectMessageNotificationEmailSubject(t *testing.T) {
mainHelper.Parallel(t)
expectedPrefix := "[http://localhost:8065] New Direct Message from @sender on"
user := &model.User{}
post := &model.Post{
CreateAt: 1501804801000,
}
translateFunc := i18n.GetUserTranslations("en")
subject := getDirectMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "@sender", true)
require.Regexp(t, regexp.MustCompile("^"+regexp.QuoteMeta(expectedPrefix)), subject, fmt.Sprintf("Expected subject line prefix '%s', got %s", expectedPrefix, subject))
}
func TestGetGroupMessageNotificationEmailSubjectFull(t *testing.T) {
mainHelper.Parallel(t)
expectedPrefix := "[http://localhost:8065] New Group Message in sender on"
user := &model.User{}
post := &model.Post{
CreateAt: 1501804801000,
}
translateFunc := i18n.GetUserTranslations("en")
emailNotificationContentsType := model.EmailNotificationContentsFull
subject := getGroupMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "sender", emailNotificationContentsType, true)
require.Regexp(t, regexp.MustCompile("^"+regexp.QuoteMeta(expectedPrefix)), subject, fmt.Sprintf("Expected subject line prefix '%s', got %s", expectedPrefix, subject))
}
func TestGetGroupMessageNotificationEmailSubjectGeneric(t *testing.T) {
mainHelper.Parallel(t)
expectedPrefix := "[http://localhost:8065] New Group Message on"
user := &model.User{}
post := &model.Post{
CreateAt: 1501804801000,
}
translateFunc := i18n.GetUserTranslations("en")
emailNotificationContentsType := model.EmailNotificationContentsGeneric
subject := getGroupMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "sender", emailNotificationContentsType, true)
require.Regexp(t, regexp.MustCompile("^"+regexp.QuoteMeta(expectedPrefix)), subject, fmt.Sprintf("Expected subject line prefix '%s', got %s", expectedPrefix, subject))
}
func TestGetNotificationEmailSubject(t *testing.T) {
mainHelper.Parallel(t)
expectedPrefix := "[http://localhost:8065] Notification in team on"
user := &model.User{}
post := &model.Post{
CreateAt: 1501804801000,
}
translateFunc := i18n.GetUserTranslations("en")
subject := getNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "team", true)
require.Regexp(t, regexp.MustCompile("^"+regexp.QuoteMeta(expectedPrefix)), subject, fmt.Sprintf("Expected subject line prefix '%s', got %s", expectedPrefix, subject))
}
func TestGetNotificationEmailBodyFullNotificationPublicChannel(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
post := &model.Post{
Id: "test-post-id",
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeOpen,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "mentioned you in a message", fmt.Sprintf("Expected email text 'mentioned you in a message. Got %s", body))
require.Contains(t, body, post.Message, fmt.Sprintf("Expected email text '%s'. Got %s", post.Message, body))
require.Contains(t, body, team.Name, fmt.Sprintf("Expected email text '%s'. Got %s", team.Name, body))
}
func TestGetNotificationEmailBodyFullNotificationGroupChannel(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
post := &model.Post{
Id: "test-post-id",
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeGroup,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "sent you a new message", fmt.Sprintf("Expected email text 'sent you a new message. Got %s", body))
require.Contains(t, body, post.Message, fmt.Sprintf("Expected email text '%s'. Got %s", post.Message, body))
require.Contains(t, body, team.Name, fmt.Sprintf("Expected email text '%s'. Got %s", team.Name, body))
}
func TestGetNotificationEmailBodyFullNotificationPrivateChannel(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
post := &model.Post{
Id: "test-post-id",
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypePrivate,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "mentioned you in a message", fmt.Sprintf("Expected email text 'mentioned you in a message. Got %s", body))
require.Contains(t, body, post.Message, fmt.Sprintf("Expected email text '%s'. Got %s", post.Message, body))
require.Contains(t, body, team.Name, fmt.Sprintf("Expected email text '%s'. Got %s", team.Name, body))
}
func TestGetNotificationEmailBodyFullNotificationDirectChannel(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
post := &model.Post{
Id: "test-post-id",
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeDirect,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "sent you a new message", fmt.Sprintf("Expected email text 'sent you a new message. Got %s", body))
require.Contains(t, body, post.Message, fmt.Sprintf("Expected email text '%s'. Got %s", post.Message, body))
require.Contains(t, body, team.Name, fmt.Sprintf("Expected email text '%s'. Got %s", team.Name, body))
}
func TestGetNotificationEmailBodyFullNotificationLocaleTimeWithTimezone(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := &model.User{
Id: "test-recipient-id",
Username: "recipient",
Nickname: "Recipient User",
Locale: "en",
Timezone: timezones.DefaultUserTimezone(),
}
recipient.Timezone["automaticTimezone"] = "America/New_York"
post := &model.Post{
Id: "test-post-id",
CreateAt: 1524663790000,
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeDirect,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, false)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
r, _ := regexp.Compile("E([S|D]+)T")
zone := r.FindString(body)
require.Contains(t, body, "9:43 AM "+zone, fmt.Sprintf("Expected email text '9:43 AM %s'. Got %s", zone, body))
}
func TestGetNotificationEmailBodyFullNotificationLocaleTimeNoTimezone(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := &model.User{
Id: "test-recipient-id",
Username: "recipient",
Nickname: "Recipient User",
Locale: "en",
Timezone: timezones.DefaultUserTimezone(),
}
post := &model.Post{
Id: "test-post-id",
CreateAt: 1524681000000,
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeDirect,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
tm := time.Unix(post.CreateAt/1000, 0)
zone, _ := tm.Zone()
formattedTime := utils.FormattedPostTime{
Hour: fmt.Sprintf("%02d", tm.Hour()),
Minute: fmt.Sprintf("%02d", tm.Minute()),
TimeZone: zone,
}
tmp, err := template.New("foo").Parse(`{{.}}`)
require.NoError(t, err)
var text bytes.Buffer
err = tmp.Execute(&text, fmt.Sprintf("%s:%s %s", formattedTime.Hour, formattedTime.Minute, formattedTime.TimeZone))
require.NoError(t, err)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
postTimeLine := text.String()
require.Contains(t, body, postTimeLine, fmt.Sprintf("Expected email text '%s'. Got %s", postTimeLine, body))
}
func TestGetNotificationEmailBodyFullNotificationLocaleTime12Hour(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := &model.User{
Id: "test-recipient-id",
Username: "recipient",
Nickname: "Recipient User",
Locale: "en",
Timezone: timezones.DefaultUserTimezone(),
}
recipient.Timezone["automaticTimezone"] = "America/New_York"
post := &model.Post{
Id: "test-post-id",
CreateAt: 1524681000000, // 1524681000 // 1524681000000
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeDirect,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, false)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "2:30 PM", fmt.Sprintf("Expected email text '2:30 PM'. Got %s", body))
}
func TestGetNotificationEmailBodyFullNotificationLocaleTime24Hour(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := &model.User{
Id: "test-recipient-id",
Username: "recipient",
Nickname: "Recipient User",
Locale: "en",
Timezone: timezones.DefaultUserTimezone(),
}
recipient.Timezone["automaticTimezone"] = "America/New_York"
post := &model.Post{
Id: "test-post-id",
CreateAt: 1524681000000,
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeDirect,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "14:30", fmt.Sprintf("Expected email text '14:30'. Got %s", body))
}
func TestGetNotificationEmailBodyWithUserPreference(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := &model.User{
Id: "test-recipient-id",
Username: "recipient",
Nickname: "Recipient User",
Locale: "en",
Timezone: timezones.DefaultUserTimezone(),
}
recipient.Timezone["automaticTimezone"] = "America/New_York"
post := &model.Post{
Id: "test-post-id",
CreateAt: 1524681000000,
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeDirect,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
// Test 12-hour format
is24HourFormat := false
expectedTimeFormat := "2:30 PM"
if is24HourFormat {
expectedTimeFormat = "14:30"
}
setupPreferenceMocks(th, recipient.Id, is24HourFormat)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, expectedTimeFormat, fmt.Sprintf("Expected email text '%s'. Got %s", expectedTimeFormat, body))
}
func TestGetNotificationEmailBodyFullNotificationWithSlackAttachments(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
post := &model.Post{
Id: "test-post-id",
Message: "This is the message",
}
messageAttachments := []*model.SlackAttachment{
{
Color: "#FF0000",
Pretext: "message attachment 1 pretext",
AuthorName: "author name",
AuthorLink: "https://example.com/slack_attachment_1/author_link",
AuthorIcon: "https://example.com/slack_attachment_1/author_icon",
Title: "message attachment 1 title",
TitleLink: "https://example.com/slack_attachment_1/title_link",
Text: "message attachment 1 text",
ImageURL: "https://example.com/slack_attachment_1/image",
ThumbURL: "https://example.com/slack_attachment_1/thumb",
Fields: []*model.SlackAttachmentField{
{
Short: true,
Title: "message attachment 1 field 1 title",
Value: "message attachment 1 field 1 value",
},
{
Short: false,
Title: "message attachment 1 field 2 title",
Value: "message attachment 1 field 2 value",
},
{
Short: true,
Title: "message attachment 1 field 3 title",
Value: "message attachment 1 field 3 value",
},
{
Short: true,
Title: "message attachment 1 field 4 title",
Value: "message attachment 1 field 4 value",
},
},
},
{
Color: "#FF0000",
Pretext: "message attachment 2 pretext",
AuthorName: "author name 2",
Text: "message attachment 2 text",
},
}
model.ParseSlackAttachment(post, messageAttachments)
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeOpen,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "#FF0000")
require.Contains(t, body, "message attachment 1 pretext")
require.Contains(t, body, "author name")
require.Contains(t, body, "https://example.com/slack_attachment_1/author_link")
require.Contains(t, body, "https://example.com/slack_attachment_1/author_icon")
require.Contains(t, body, "message attachment 1 title")
require.Contains(t, body, "https://example.com/slack_attachment_1/title_link")
require.Contains(t, body, "message attachment 1 text")
require.Contains(t, body, "https://example.com/slack_attachment_1/image")
require.Contains(t, body, "https://example.com/slack_attachment_1/thumb")
require.Contains(t, body, "message attachment 1 field 1 title")
require.Contains(t, body, "message attachment 1 field 1 value")
require.Contains(t, body, "message attachment 1 field 2 title")
require.Contains(t, body, "message attachment 1 field 2 value")
require.Contains(t, body, "message attachment 1 field 3 title")
require.Contains(t, body, "message attachment 1 field 3 value")
require.Contains(t, body, "message attachment 1 field 4 title")
require.Contains(t, body, "message attachment 1 field 4 value")
require.Contains(t, body, "https://example.com/slack_attachment_1/thumb")
require.Contains(t, body, "message attachment 2 pretext")
require.Contains(t, body, "author name 2")
require.Contains(t, body, "message attachment 2 text")
}
// from here
func TestGetNotificationEmailBodyGenericNotificationPublicChannel(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
post := &model.Post{
Id: "test-post-id",
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeOpen,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "mentioned you in a message", fmt.Sprintf("Expected email text 'mentioned you in a message. Got %s", body))
require.Contains(t, body, team.Name, fmt.Sprintf("Expected email text '%s'. Got %s", team.Name, body))
}
func TestGetNotificationEmailBodyGenericNotificationGroupChannel(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
post := &model.Post{
Id: "test-post-id",
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeGroup,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "sent you a new message", fmt.Sprintf("Expected email text 'sent you a new message. Got %s", body))
require.Contains(t, body, team.Name, fmt.Sprintf("Expected email text '%s'. Got %s", team.Name, body))
}
func TestGetNotificationEmailBodyGenericNotificationPrivateChannel(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
post := &model.Post{
Id: "test-post-id",
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypePrivate,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "mentioned you in a message", fmt.Sprintf("Expected email text 'mentioned you in a message. Got %s", body))
require.Contains(t, body, team.Name, fmt.Sprintf("Expected email text '%s'. Got %s", team.Name, body))
}
func TestGetNotificationEmailBodyGenericNotificationDirectChannel(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
post := &model.Post{
Id: "test-post-id",
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeDirect,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "sent you a new message", fmt.Sprintf("Expected email text 'sent you a new message. Got %s", body))
require.Contains(t, body, team.Name, fmt.Sprintf("Expected email text '%s'. Got %s", team.Name, body))
}
func TestGetNotificationEmailEscapingChars(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
message := "<b>Bold Test</b>"
post := &model.Post{
Id: "test-post-id",
Message: message,
}
ch := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeOpen,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, ch, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
assert.NotContains(t, body, message)
}
func TestGetNotificationEmailBodyPublicChannelMention(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
ch := &model.Channel{
Id: "test-channel-id",
Name: "channelname",
DisplayName: "ChannelName",
Type: model.ChannelTypeOpen,
}
id := model.NewId()
recipient := &model.User{
Id: "test-recipient-id",
Email: "success+" + id + "@simulator.amazonses.com",
Username: "un_" + id,
Nickname: "nn_" + id,
Password: model.NewTestPassword(),
EmailVerified: true,
Locale: "en",
}
post := &model.Post{
Id: "test-post-id",
Message: "This is the message ~" + ch.Name,
}
sender := buildTestUser("test-sender-id", "user1", "user1", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
teamURL := th.App.GetSiteURL() + "/landing#" + "/testteam"
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
channelStoreMock := mocks.ChannelStore{}
channelStoreMock.On("GetByNames", "test", []string{ch.Name}, true).Return([]*model.Channel{ch}, nil)
storeMock.On("Channel").Return(&channelStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
th.App.Srv().EmailService.SetStore(storeMock)
notification := buildTestPostNotification(post, ch, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
channelURL := teamURL + "/channels/" + ch.Name
mention := "~" + ch.Name
assert.Contains(t, body, "<a href='"+channelURL+"'>"+mention+"</a>")
}
func TestGetNotificationEmailBodyMultiPublicChannelMention(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
ch := &model.Channel{
Id: model.NewId(),
Name: "channelnameone",
DisplayName: "ChannelName",
Type: model.ChannelTypeOpen,
}
mention := "~" + ch.Name
ch2 := &model.Channel{
Id: model.NewId(),
Name: "channelnametwo",
DisplayName: "ChannelName2",
Type: model.ChannelTypeOpen,
}
mention2 := "~" + ch2.Name
ch3 := &model.Channel{
Id: model.NewId(),
Name: "channelnamethree",
DisplayName: "ChannelName3",
Type: model.ChannelTypeOpen,
}
mention3 := "~" + ch3.Name
message := fmt.Sprintf("This is the message Channel1: %s; Channel2: %s;"+
" Channel3: %s", mention, mention2, mention3)
post := &model.Post{
Message: message,
}
teamURL := th.App.GetSiteURL() + "/landing#" + "/testteam"
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
channelStoreMock := mocks.ChannelStore{}
channelStoreMock.On("GetByNames", "test", []string{ch.Name, ch2.Name, ch3.Name}, true).Return([]*model.Channel{ch, ch2, ch3}, nil)
storeMock.On("Channel").Return(&channelStoreMock)
th.App.Srv().EmailService.SetStore(storeMock)
channelURL := teamURL + "/channels/" + ch.Name
channelURL2 := teamURL + "/channels/" + ch2.Name
channelURL3 := teamURL + "/channels/" + ch3.Name
expMessage := fmt.Sprintf("This is the message Channel1: <a href='%s'>%s</a>;"+
" Channel2: <a href='%s'>%s</a>; Channel3: <a href='%s'>%s</a>",
channelURL, mention, channelURL2, mention2, channelURL3, mention3)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
sender := buildTestUser("test-sender-id", "user1", "user1", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, ch, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
assert.Contains(t, body, expMessage)
}
func TestGetNotificationEmailBodyPrivateChannelMention(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
ch := &model.Channel{
Id: "test-channel-id",
Name: "channelname",
DisplayName: "ChannelName",
Type: model.ChannelTypePrivate,
}
id := model.NewId()
recipient := &model.User{
Id: "test-recipient-id",
Email: "success+" + id + "@simulator.amazonses.com",
Username: "un_" + id,
Nickname: "nn_" + id,
Password: model.NewTestPassword(),
EmailVerified: true,
Locale: "en",
}
post := &model.Post{
Id: "test-post-id",
Message: "This is the message ~" + ch.Name,
}
sender := buildTestUser("test-sender-id", "user1", "user1", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
teamURL := "http://localhost:8065/testteam"
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
channelStoreMock := mocks.ChannelStore{}
channelStoreMock.On("GetByNames", "test", []string{ch.Name}, true).Return([]*model.Channel{ch}, nil)
storeMock.On("Channel").Return(&channelStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
th.App.Srv().EmailService.SetStore(storeMock)
notification := buildTestPostNotification(post, ch, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
channelURL := teamURL + "/channels/" + ch.Name
mention := "~" + ch.Name
assert.NotContains(t, body, "<a href='"+channelURL+"'>"+mention+"</a>")
}
func TestGenerateHyperlinkForChannelsPublic(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
ch := &model.Channel{
Name: "channelname",
DisplayName: "ChannelName",
Type: model.ChannelTypeOpen,
}
message := "This is the message "
mention := "~" + ch.Name
teamName := "testteam"
teamURL := "http://localhost:8065/testteam"
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
channelStoreMock := mocks.ChannelStore{}
channelStoreMock.On("GetByNames", "test", []string{ch.Name}, true).Return([]*model.Channel{ch}, nil)
storeMock.On("Channel").Return(&channelStoreMock)
th.App.Srv().EmailService.SetStore(storeMock)
outMessage, err := th.App.Srv().EmailService.GenerateHyperlinkForChannels(message+mention, teamName, teamURL)
require.NoError(t, err)
channelURL := teamURL + "/channels/" + ch.Name
assert.Equal(t, message+"<a href='"+channelURL+"'>"+mention+"</a>", outMessage)
}
func TestGenerateHyperlinkForChannelsMultiPublic(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
// TODO: Fix the case where the first channel name contains the other channel names (for example here channelnameone)"
ch := &model.Channel{
Id: model.NewId(),
Name: "channelnameone",
DisplayName: "ChannelName",
Type: model.ChannelTypeOpen,
}
mention := "~" + ch.Name
ch2 := &model.Channel{
Id: model.NewId(),
Name: "channelnametwo",
DisplayName: "ChannelName2",
Type: model.ChannelTypeOpen,
}
mention2 := "~" + ch2.Name
ch3 := &model.Channel{
Id: model.NewId(),
Name: "channelnamethree",
DisplayName: "ChannelName3",
Type: model.ChannelTypeOpen,
}
mention3 := "~" + ch3.Name
message := fmt.Sprintf("This is the message Channel1: %s; Channel2: %s;"+
" Channel3: %s", mention, mention2, mention3)
teamName := "testteam"
teamURL := "http://localhost:8065/testteam"
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
channelStoreMock := mocks.ChannelStore{}
channelStoreMock.On("GetByNames", "test", []string{ch.Name, ch2.Name, ch3.Name}, true).Return([]*model.Channel{ch, ch2, ch3}, nil)
storeMock.On("Channel").Return(&channelStoreMock)
th.App.Srv().EmailService.SetStore(storeMock)
outMessage, err := th.App.Srv().EmailService.GenerateHyperlinkForChannels(message, teamName, teamURL)
require.NoError(t, err)
channelURL := teamURL + "/channels/" + ch.Name
channelURL2 := teamURL + "/channels/" + ch2.Name
channelURL3 := teamURL + "/channels/" + ch3.Name
expMessage := fmt.Sprintf("This is the message Channel1: <a href='%s'>%s</a>;"+
" Channel2: <a href='%s'>%s</a>; Channel3: <a href='%s'>%s</a>",
channelURL, mention, channelURL2, mention2, channelURL3, mention3)
assert.Equal(t, expMessage, outMessage)
}
func TestGenerateHyperlinkForChannelsPrivate(t *testing.T) {
mainHelper.Parallel(t)
th := SetupWithStoreMock(t)
ch := &model.Channel{
Name: "channelname",
DisplayName: "ChannelName",
Type: model.ChannelTypePrivate,
}
message := "This is the message ~" + ch.Name
teamName := "testteam"
teamURL := "http://localhost:8065/testteam"
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Id: "test", Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
channelStoreMock := mocks.ChannelStore{}
channelStoreMock.On("GetByNames", "test", []string{ch.Name}, true).Return([]*model.Channel{ch}, nil)
storeMock.On("Channel").Return(&channelStoreMock)
th.App.Srv().EmailService.SetStore(storeMock)
outMessage, err := th.App.Srv().EmailService.GenerateHyperlinkForChannels(message, teamName, teamURL)
require.NoError(t, err)
assert.Equal(t, message, outMessage)
}
func TestLandingLink(t *testing.T) {
mainHelper.Parallel(t)
// Create a minimal helper that sets the site URL
mockStore := testlib.GetMockStoreForSetupFunctions()
th := setupTestHelper(mockStore, mainHelper.GetSQLStore(), mainHelper.GetSQLSettings(), mainHelper.GetSearchEngine(), false, false,
func(cfg *model.Config) {
cfg.ServiceSettings.SiteURL = model.NewPointer("http://localhost:8065")
}, nil, t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
post := &model.Post{
Id: "test-post-id",
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeOpen,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
teamURL := "http://localhost:8065/landing#/testteam"
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, teamURL, fmt.Sprintf("Expected email text '%s'. Got %s", teamURL, body))
}
func TestLandingLinkPermalink(t *testing.T) {
mainHelper.Parallel(t)
// Create a minimal helper that sets the site URL
mockStore := testlib.GetMockStoreForSetupFunctions()
th := setupTestHelper(mockStore, mainHelper.GetSQLStore(), mainHelper.GetSQLSettings(), mainHelper.GetSearchEngine(), false, false,
func(cfg *model.Config) {
cfg.ServiceSettings.SiteURL = model.NewPointer("http://localhost:8065")
}, nil, t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
post := &model.Post{
Id: "Test_id",
Message: "This is the message",
}
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeOpen,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
setupPreferenceMocks(th, recipient.Id, true)
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
body, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, body, "/pl/"+post.Id, fmt.Sprintf("Expected email text to contain permalink '/pl/%s'. Got %s", post.Id, body))
}
func TestMarkdownConversion(t *testing.T) {
mainHelper.Parallel(t)
tests := []struct {
name string
args string
want string
}{
{
name: "markdown: escape string test",
args: "<b>not bold</b>",
want: "&lt;b&gt;not bold&lt;/b&gt;",
},
{
name: "markdown: strong",
args: "This is **Mattermost**",
want: "This is <strong>Mattermost</strong>",
},
{
name: "markdown: blockquote",
args: "Below is blockquote\n" +
"> This is Mattermost blockquote\n" +
"> on multiple lines!",
want: "<blockquote>\n" +
"<p>This is Mattermost blockquote\n" +
"on multiple lines!</p>\n" +
"</blockquote>",
},
{
name: "markdown: emphasis",
args: "This is *Mattermost*",
want: "This is <em>Mattermost</em>",
},
{
name: "markdown: links",
args: "This is [Mattermost](https://mattermost.com)",
want: "This is <a href=\"https://mattermost.com\">Mattermost</a>",
},
{
name: "markdown: strikethrough",
args: "This is ~~Mattermost~~",
want: "This is <del>Mattermost</del>",
},
{
name: "markdown: table",
args: "| Tables | Are | Cool |\n" +
"| ------------- |:-------------:| -----:|\n" +
"| col 3 is | right-aligned | $1600 |\n" +
"| col 2 is | centered | $12 |\n" +
"| zebra stripes | are neat | $1 |\n",
want: "<table>\n" +
"<thead>\n" +
"<tr>\n" +
"<th>Tables</th>\n" +
"<th style=\"text-align:center\">Are</th>\n" +
"<th style=\"text-align:right\">Cool</th>\n" +
"</tr>\n" +
"</thead>\n" +
"<tbody>\n" +
"<tr>\n" +
"<td>col 3 is</td>\n" +
"<td style=\"text-align:center\">right-aligned</td>\n" +
"<td style=\"text-align:right\">$1600</td>\n" +
"</tr>\n" +
"<tr>\n" +
"<td>col 2 is</td>\n" +
"<td style=\"text-align:center\">centered</td>\n" +
"<td style=\"text-align:right\">$12</td>\n" +
"</tr>\n" +
"<tr>\n" +
"<td>zebra stripes</td>\n" +
"<td style=\"text-align:center\">are neat</td>\n" +
"<td style=\"text-align:right\">$1</td>\n" +
"</tr>\n" +
"</tbody>\n" +
"</table>",
},
{
name: "markdown: multiline with header and links",
args: "###### H6 header\n[link 1](https://mattermost.com) - [link 2](https://mattermost.com)",
want: "<h6>H6 header</h6>\n" +
"<p><a href=\"https://mattermost.com\">link 1</a> - <a href=\"https://mattermost.com\">link 2</a></p>",
},
}
th := SetupWithStoreMock(t)
recipient := buildTestUser("test-recipient-id", "recipient", "Recipient User", true)
storeMock := th.App.Srv().Store().(*mocks.Store)
teamStoreMock := mocks.TeamStore{}
teamStoreMock.On("GetByName", "testteam").Return(&model.Team{Name: "testteam"}, nil)
storeMock.On("Team").Return(&teamStoreMock)
channel := &model.Channel{
Id: "test-channel-id",
Name: "testchannel",
DisplayName: "ChannelName",
Type: model.ChannelTypeOpen,
}
sender := buildTestUser("test-sender-id", "sender", "sender", true)
team := buildTestTeam("test-team-id", "testteam", "testteam")
setupPreferenceMocks(th, recipient.Id, true)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
post := &model.Post{
Id: "Test_id",
Message: tt.args,
}
notification := buildTestPostNotification(post, channel, sender)
emailNotification := th.App.buildEmailNotification(th.Context, notification, recipient, team)
got, err := th.App.getNotificationEmailBodyFromEmailNotification(th.Context, recipient, emailNotification, post, "user-avatar.png")
require.NoError(t, err)
require.Contains(t, got, tt.want)
})
}
}