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