github.com/fretkak/mattermost-mattermost-server@v5.11.1+incompatible/app/notification_email_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"fmt"
     8  	"regexp"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/mattermost/mattermost-server/model"
    14  	"github.com/mattermost/mattermost-server/services/timezones"
    15  	"github.com/mattermost/mattermost-server/utils"
    16  )
    17  
    18  func TestGetDirectMessageNotificationEmailSubject(t *testing.T) {
    19  	th := Setup(t)
    20  	defer th.TearDown()
    21  
    22  	expectedPrefix := "[http://localhost:8065] New Direct Message from @sender on"
    23  	user := &model.User{}
    24  	post := &model.Post{
    25  		CreateAt: 1501804801000,
    26  	}
    27  	translateFunc := utils.GetUserTranslations("en")
    28  	subject := getDirectMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "sender", true)
    29  	if !strings.HasPrefix(subject, expectedPrefix) {
    30  		t.Fatal("Expected subject line prefix '" + expectedPrefix + "', got " + subject)
    31  	}
    32  }
    33  
    34  func TestGetGroupMessageNotificationEmailSubjectFull(t *testing.T) {
    35  	th := Setup(t)
    36  	defer th.TearDown()
    37  
    38  	expectedPrefix := "[http://localhost:8065] New Group Message in sender on"
    39  	user := &model.User{}
    40  	post := &model.Post{
    41  		CreateAt: 1501804801000,
    42  	}
    43  	translateFunc := utils.GetUserTranslations("en")
    44  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
    45  	subject := getGroupMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "sender", emailNotificationContentsType, true)
    46  	if !strings.HasPrefix(subject, expectedPrefix) {
    47  		t.Fatal("Expected subject line prefix '" + expectedPrefix + "', got " + subject)
    48  	}
    49  }
    50  
    51  func TestGetGroupMessageNotificationEmailSubjectGeneric(t *testing.T) {
    52  	th := Setup(t)
    53  	defer th.TearDown()
    54  
    55  	expectedPrefix := "[http://localhost:8065] New Group Message on"
    56  	user := &model.User{}
    57  	post := &model.Post{
    58  		CreateAt: 1501804801000,
    59  	}
    60  	translateFunc := utils.GetUserTranslations("en")
    61  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC
    62  	subject := getGroupMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "sender", emailNotificationContentsType, true)
    63  	if !strings.HasPrefix(subject, expectedPrefix) {
    64  		t.Fatal("Expected subject line prefix '" + expectedPrefix + "', got " + subject)
    65  	}
    66  }
    67  
    68  func TestGetNotificationEmailSubject(t *testing.T) {
    69  	th := Setup(t)
    70  	defer th.TearDown()
    71  
    72  	expectedPrefix := "[http://localhost:8065] Notification in team on"
    73  	user := &model.User{}
    74  	post := &model.Post{
    75  		CreateAt: 1501804801000,
    76  	}
    77  	translateFunc := utils.GetUserTranslations("en")
    78  	subject := getNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "team", true)
    79  	if !strings.HasPrefix(subject, expectedPrefix) {
    80  		t.Fatal("Expected subject line prefix '" + expectedPrefix + "', got " + subject)
    81  	}
    82  }
    83  
    84  func TestGetNotificationEmailBodyFullNotificationPublicChannel(t *testing.T) {
    85  	th := Setup(t)
    86  	defer th.TearDown()
    87  
    88  	recipient := &model.User{}
    89  	post := &model.Post{
    90  		Message: "This is the message",
    91  	}
    92  	channel := &model.Channel{
    93  		DisplayName: "ChannelName",
    94  		Type:        model.CHANNEL_OPEN,
    95  	}
    96  	channelName := "ChannelName"
    97  	senderName := "sender"
    98  	teamName := "team"
    99  	teamURL := "http://localhost:8065/" + teamName
   100  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
   101  	translateFunc := utils.GetUserTranslations("en")
   102  
   103  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
   104  	if !strings.Contains(body, "You have a new notification.") {
   105  		t.Fatal("Expected email text 'You have a new notification. Got " + body)
   106  	}
   107  	if !strings.Contains(body, "Channel: "+channel.DisplayName) {
   108  		t.Fatal("Expected email text 'Channel: " + channel.DisplayName + "'. Got " + body)
   109  	}
   110  	if !strings.Contains(body, "@"+senderName+" - ") {
   111  		t.Fatal("Expected email text '@" + senderName + " - '. Got " + body)
   112  	}
   113  	if !strings.Contains(body, post.Message) {
   114  		t.Fatal("Expected email text '" + post.Message + "'. Got " + body)
   115  	}
   116  	if !strings.Contains(body, teamURL) {
   117  		t.Fatal("Expected email text '" + teamURL + "'. Got " + body)
   118  	}
   119  }
   120  
   121  func TestGetNotificationEmailBodyFullNotificationGroupChannel(t *testing.T) {
   122  	th := Setup(t)
   123  	defer th.TearDown()
   124  
   125  	recipient := &model.User{}
   126  	post := &model.Post{
   127  		Message: "This is the message",
   128  	}
   129  	channel := &model.Channel{
   130  		DisplayName: "ChannelName",
   131  		Type:        model.CHANNEL_GROUP,
   132  	}
   133  	channelName := "ChannelName"
   134  	senderName := "sender"
   135  	teamName := "team"
   136  	teamURL := "http://localhost:8065/" + teamName
   137  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
   138  	translateFunc := utils.GetUserTranslations("en")
   139  
   140  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
   141  	if !strings.Contains(body, "You have a new Group Message.") {
   142  		t.Fatal("Expected email text 'You have a new Group Message. Got " + body)
   143  	}
   144  	if !strings.Contains(body, "Channel: ChannelName") {
   145  		t.Fatal("Expected email text 'Channel: ChannelName'. Got " + body)
   146  	}
   147  	if !strings.Contains(body, "@"+senderName+" - ") {
   148  		t.Fatal("Expected email text '@" + senderName + " - '. Got " + body)
   149  	}
   150  	if !strings.Contains(body, post.Message) {
   151  		t.Fatal("Expected email text '" + post.Message + "'. Got " + body)
   152  	}
   153  	if !strings.Contains(body, teamURL) {
   154  		t.Fatal("Expected email text '" + teamURL + "'. Got " + body)
   155  	}
   156  }
   157  
   158  func TestGetNotificationEmailBodyFullNotificationPrivateChannel(t *testing.T) {
   159  	th := Setup(t)
   160  	defer th.TearDown()
   161  
   162  	recipient := &model.User{}
   163  	post := &model.Post{
   164  		Message: "This is the message",
   165  	}
   166  	channel := &model.Channel{
   167  		DisplayName: "ChannelName",
   168  		Type:        model.CHANNEL_PRIVATE,
   169  	}
   170  	channelName := "ChannelName"
   171  	senderName := "sender"
   172  	teamName := "team"
   173  	teamURL := "http://localhost:8065/" + teamName
   174  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
   175  	translateFunc := utils.GetUserTranslations("en")
   176  
   177  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
   178  	if !strings.Contains(body, "You have a new notification.") {
   179  		t.Fatal("Expected email text 'You have a new notification. Got " + body)
   180  	}
   181  	if !strings.Contains(body, "Channel: "+channel.DisplayName) {
   182  		t.Fatal("Expected email text 'Channel: " + channel.DisplayName + "'. Got " + body)
   183  	}
   184  	if !strings.Contains(body, "@"+senderName+" - ") {
   185  		t.Fatal("Expected email text '@" + senderName + " - '. Got " + body)
   186  	}
   187  	if !strings.Contains(body, post.Message) {
   188  		t.Fatal("Expected email text '" + post.Message + "'. Got " + body)
   189  	}
   190  	if !strings.Contains(body, teamURL) {
   191  		t.Fatal("Expected email text '" + teamURL + "'. Got " + body)
   192  	}
   193  }
   194  
   195  func TestGetNotificationEmailBodyFullNotificationDirectChannel(t *testing.T) {
   196  	th := Setup(t)
   197  	defer th.TearDown()
   198  
   199  	recipient := &model.User{}
   200  	post := &model.Post{
   201  		Message: "This is the message",
   202  	}
   203  	channel := &model.Channel{
   204  		DisplayName: "ChannelName",
   205  		Type:        model.CHANNEL_DIRECT,
   206  	}
   207  	channelName := "ChannelName"
   208  	senderName := "sender"
   209  	teamName := "team"
   210  	teamURL := "http://localhost:8065/" + teamName
   211  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
   212  	translateFunc := utils.GetUserTranslations("en")
   213  
   214  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
   215  	if !strings.Contains(body, "You have a new Direct Message.") {
   216  		t.Fatal("Expected email text 'You have a new Direct Message. Got " + body)
   217  	}
   218  	if !strings.Contains(body, "@"+senderName+" - ") {
   219  		t.Fatal("Expected email text '@" + senderName + " - '. Got " + body)
   220  	}
   221  	if !strings.Contains(body, post.Message) {
   222  		t.Fatal("Expected email text '" + post.Message + "'. Got " + body)
   223  	}
   224  	if !strings.Contains(body, teamURL) {
   225  		t.Fatal("Expected email text '" + teamURL + "'. Got " + body)
   226  	}
   227  }
   228  
   229  func TestGetNotificationEmailBodyFullNotificationLocaleTimeWithTimezone(t *testing.T) {
   230  	th := Setup(t)
   231  	defer th.TearDown()
   232  
   233  	recipient := &model.User{
   234  		Timezone: timezones.DefaultUserTimezone(),
   235  	}
   236  	recipient.Timezone["automaticTimezone"] = "America/New_York"
   237  	post := &model.Post{
   238  		CreateAt: 1524663790000,
   239  		Message:  "This is the message",
   240  	}
   241  	channel := &model.Channel{
   242  		DisplayName: "ChannelName",
   243  		Type:        model.CHANNEL_DIRECT,
   244  	}
   245  	channelName := "ChannelName"
   246  	senderName := "sender"
   247  	teamName := "team"
   248  	teamURL := "http://localhost:8065/" + teamName
   249  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
   250  	translateFunc := utils.GetUserTranslations("en")
   251  
   252  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, false, translateFunc)
   253  	r, _ := regexp.Compile("E([S|D]+)T")
   254  	zone := r.FindString(body)
   255  	if !strings.Contains(body, "sender - 9:43 AM "+zone+", April 25") {
   256  		t.Fatal("Expected email text 'sender - 9:43 AM " + zone + ", April 25'. Got " + body)
   257  	}
   258  }
   259  
   260  func TestGetNotificationEmailBodyFullNotificationLocaleTimeNoTimezone(t *testing.T) {
   261  	th := Setup(t)
   262  	defer th.TearDown()
   263  
   264  	recipient := &model.User{
   265  		Timezone: timezones.DefaultUserTimezone(),
   266  	}
   267  	post := &model.Post{
   268  		CreateAt: 1524681000000,
   269  		Message:  "This is the message",
   270  	}
   271  	channel := &model.Channel{
   272  		DisplayName: "ChannelName",
   273  		Type:        model.CHANNEL_DIRECT,
   274  	}
   275  	channelName := "ChannelName"
   276  	senderName := "sender"
   277  	teamName := "team"
   278  	teamURL := "http://localhost:8065/" + teamName
   279  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
   280  	translateFunc := utils.GetUserTranslations("en")
   281  
   282  	tm := time.Unix(post.CreateAt/1000, 0)
   283  	zone, _ := tm.Zone()
   284  
   285  	formattedTime := formattedPostTime{
   286  		Time:     tm,
   287  		Year:     fmt.Sprintf("%d", tm.Year()),
   288  		Month:    translateFunc(tm.Month().String()),
   289  		Day:      fmt.Sprintf("%d", tm.Day()),
   290  		Hour:     fmt.Sprintf("%02d", tm.Hour()),
   291  		Minute:   fmt.Sprintf("%02d", tm.Minute()),
   292  		TimeZone: zone,
   293  	}
   294  
   295  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
   296  	postTimeLine := fmt.Sprintf("sender - %s:%s %s, %s %s", formattedTime.Hour, formattedTime.Minute, formattedTime.TimeZone, formattedTime.Month, formattedTime.Day)
   297  	if !strings.Contains(body, postTimeLine) {
   298  		t.Fatal("Expected email text '" + postTimeLine + " '. Got " + body)
   299  	}
   300  }
   301  
   302  func TestGetNotificationEmailBodyFullNotificationLocaleTime12Hour(t *testing.T) {
   303  	th := Setup(t)
   304  	defer th.TearDown()
   305  
   306  	recipient := &model.User{
   307  		Timezone: timezones.DefaultUserTimezone(),
   308  	}
   309  	recipient.Timezone["automaticTimezone"] = "America/New_York"
   310  	post := &model.Post{
   311  		CreateAt: 1524681000000, // 1524681000 // 1524681000000
   312  		Message:  "This is the message",
   313  	}
   314  	channel := &model.Channel{
   315  		DisplayName: "ChannelName",
   316  		Type:        model.CHANNEL_DIRECT,
   317  	}
   318  	channelName := "ChannelName"
   319  	senderName := "sender"
   320  	teamName := "team"
   321  	teamURL := "http://localhost:8065/" + teamName
   322  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
   323  	translateFunc := utils.GetUserTranslations("en")
   324  
   325  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, false, translateFunc)
   326  	if !strings.Contains(body, "sender - 2:30 PM") {
   327  		t.Fatal("Expected email text 'sender - 2:30 PM'. Got " + body)
   328  	}
   329  	if !strings.Contains(body, "April 25") {
   330  		t.Fatal("Expected email text 'April 25'. Got " + body)
   331  	}
   332  }
   333  
   334  func TestGetNotificationEmailBodyFullNotificationLocaleTime24Hour(t *testing.T) {
   335  	th := Setup(t)
   336  	defer th.TearDown()
   337  
   338  	recipient := &model.User{
   339  		Timezone: timezones.DefaultUserTimezone(),
   340  	}
   341  	recipient.Timezone["automaticTimezone"] = "America/New_York"
   342  	post := &model.Post{
   343  		CreateAt: 1524681000000,
   344  		Message:  "This is the message",
   345  	}
   346  	channel := &model.Channel{
   347  		DisplayName: "ChannelName",
   348  		Type:        model.CHANNEL_DIRECT,
   349  	}
   350  	channelName := "ChannelName"
   351  	senderName := "sender"
   352  	teamName := "team"
   353  	teamURL := "http://localhost:8065/" + teamName
   354  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
   355  	translateFunc := utils.GetUserTranslations("en")
   356  
   357  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
   358  	if !strings.Contains(body, "sender - 14:30") {
   359  		t.Fatal("Expected email text 'sender - 14:30'. Got " + body)
   360  	}
   361  	if !strings.Contains(body, "April 25") {
   362  		t.Fatal("Expected email text 'April 25'. Got " + body)
   363  	}
   364  }
   365  
   366  // from here
   367  func TestGetNotificationEmailBodyGenericNotificationPublicChannel(t *testing.T) {
   368  	th := Setup(t)
   369  	defer th.TearDown()
   370  
   371  	recipient := &model.User{}
   372  	post := &model.Post{
   373  		Message: "This is the message",
   374  	}
   375  	channel := &model.Channel{
   376  		DisplayName: "ChannelName",
   377  		Type:        model.CHANNEL_OPEN,
   378  	}
   379  	channelName := "ChannelName"
   380  	senderName := "sender"
   381  	teamName := "team"
   382  	teamURL := "http://localhost:8065/" + teamName
   383  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC
   384  	translateFunc := utils.GetUserTranslations("en")
   385  
   386  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
   387  	if !strings.Contains(body, "You have a new notification from @"+senderName) {
   388  		t.Fatal("Expected email text 'You have a new notification from @" + senderName + "'. Got " + body)
   389  	}
   390  	if strings.Contains(body, "Channel: "+channel.DisplayName) {
   391  		t.Fatal("Did not expect email text 'Channel: " + channel.DisplayName + "'. Got " + body)
   392  	}
   393  	if strings.Contains(body, post.Message) {
   394  		t.Fatal("Did not expect email text '" + post.Message + "'. Got " + body)
   395  	}
   396  	if !strings.Contains(body, teamURL) {
   397  		t.Fatal("Expected email text '" + teamURL + "'. Got " + body)
   398  	}
   399  }
   400  
   401  func TestGetNotificationEmailBodyGenericNotificationGroupChannel(t *testing.T) {
   402  	th := Setup(t)
   403  	defer th.TearDown()
   404  
   405  	recipient := &model.User{}
   406  	post := &model.Post{
   407  		Message: "This is the message",
   408  	}
   409  	channel := &model.Channel{
   410  		DisplayName: "ChannelName",
   411  		Type:        model.CHANNEL_GROUP,
   412  	}
   413  	channelName := "ChannelName"
   414  	senderName := "sender"
   415  	teamName := "team"
   416  	teamURL := "http://localhost:8065/" + teamName
   417  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC
   418  	translateFunc := utils.GetUserTranslations("en")
   419  
   420  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
   421  	if !strings.Contains(body, "You have a new Group Message from @"+senderName) {
   422  		t.Fatal("Expected email text 'You have a new Group Message from @" + senderName + "'. Got " + body)
   423  	}
   424  	if strings.Contains(body, "CHANNEL: "+channel.DisplayName) {
   425  		t.Fatal("Did not expect email text 'CHANNEL: " + channel.DisplayName + "'. Got " + body)
   426  	}
   427  	if strings.Contains(body, post.Message) {
   428  		t.Fatal("Did not expect email text '" + post.Message + "'. Got " + body)
   429  	}
   430  	if !strings.Contains(body, teamURL) {
   431  		t.Fatal("Expected email text '" + teamURL + "'. Got " + body)
   432  	}
   433  }
   434  
   435  func TestGetNotificationEmailBodyGenericNotificationPrivateChannel(t *testing.T) {
   436  	th := Setup(t)
   437  	defer th.TearDown()
   438  
   439  	recipient := &model.User{}
   440  	post := &model.Post{
   441  		Message: "This is the message",
   442  	}
   443  	channel := &model.Channel{
   444  		DisplayName: "ChannelName",
   445  		Type:        model.CHANNEL_PRIVATE,
   446  	}
   447  	channelName := "ChannelName"
   448  	senderName := "sender"
   449  	teamName := "team"
   450  	teamURL := "http://localhost:8065/" + teamName
   451  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC
   452  	translateFunc := utils.GetUserTranslations("en")
   453  
   454  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
   455  	if !strings.Contains(body, "You have a new notification from @"+senderName) {
   456  		t.Fatal("Expected email text 'You have a new notification from @" + senderName + "'. Got " + body)
   457  	}
   458  	if strings.Contains(body, "CHANNEL: "+channel.DisplayName) {
   459  		t.Fatal("Did not expect email text 'CHANNEL: " + channel.DisplayName + "'. Got " + body)
   460  	}
   461  	if strings.Contains(body, post.Message) {
   462  		t.Fatal("Did not expect email text '" + post.Message + "'. Got " + body)
   463  	}
   464  	if !strings.Contains(body, teamURL) {
   465  		t.Fatal("Expected email text '" + teamURL + "'. Got " + body)
   466  	}
   467  }
   468  
   469  func TestGetNotificationEmailBodyGenericNotificationDirectChannel(t *testing.T) {
   470  	th := Setup(t)
   471  	defer th.TearDown()
   472  
   473  	recipient := &model.User{}
   474  	post := &model.Post{
   475  		Message: "This is the message",
   476  	}
   477  	channel := &model.Channel{
   478  		DisplayName: "ChannelName",
   479  		Type:        model.CHANNEL_DIRECT,
   480  	}
   481  	channelName := "ChannelName"
   482  	senderName := "sender"
   483  	teamName := "team"
   484  	teamURL := "http://localhost:8065/" + teamName
   485  	emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC
   486  	translateFunc := utils.GetUserTranslations("en")
   487  
   488  	body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
   489  	if !strings.Contains(body, "You have a new Direct Message from @"+senderName) {
   490  		t.Fatal("Expected email text 'You have a new Direct Message from @" + senderName + "'. Got " + body)
   491  	}
   492  	if strings.Contains(body, "CHANNEL: "+channel.DisplayName) {
   493  		t.Fatal("Did not expect email text 'CHANNEL: " + channel.DisplayName + "'. Got " + body)
   494  	}
   495  	if strings.Contains(body, post.Message) {
   496  		t.Fatal("Did not expect email text '" + post.Message + "'. Got " + body)
   497  	}
   498  	if !strings.Contains(body, teamURL) {
   499  		t.Fatal("Expected email text '" + teamURL + "'. Got " + body)
   500  	}
   501  }