diff options
Diffstat (limited to 'api/post_test.go')
-rw-r--r-- | api/post_test.go | 347 |
1 files changed, 19 insertions, 328 deletions
diff --git a/api/post_test.go b/api/post_test.go index 6101d2c8d..95f753da7 100644 --- a/api/post_test.go +++ b/api/post_test.go @@ -14,6 +14,7 @@ import ( "testing" "time" + "github.com/mattermost/platform/app" "github.com/mattermost/platform/model" "github.com/mattermost/platform/store" "github.com/mattermost/platform/utils" @@ -134,7 +135,7 @@ func TestCreatePost(t *testing.T) { } else if rpost9 := resp.Data.(*model.Post); len(rpost9.FileIds) != 3 { t.Fatal("post should have 3 files") } else { - infos := store.Must(Srv.Store.FileInfo().GetForPost(rpost9.Id)).([]*model.FileInfo) + infos := store.Must(app.Srv.Store.FileInfo().GetForPost(rpost9.Id)).([]*model.FileInfo) if len(infos) != 3 { t.Fatal("should've attached all 3 files to post") @@ -904,7 +905,9 @@ func TestMakeDirectChannelVisible(t *testing.T) { channel := Client.Must(Client.CreateDirectChannel(user2.Id)).Data.(*model.Channel) - makeDirectChannelVisible(channel.Id) + if err := app.MakeDirectChannelVisible(channel.Id); err != nil { + t.Fatal(err) + } if result, err := Client.GetPreference(model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, user2.Id); err != nil { t.Fatal("Errored trying to set direct channel to be visible for user1") @@ -913,281 +916,6 @@ func TestMakeDirectChannelVisible(t *testing.T) { } } -func TestGetMentionKeywords(t *testing.T) { - // user with username or custom mentions enabled - user1 := &model.User{ - Id: model.NewId(), - FirstName: "First", - Username: "User", - NotifyProps: map[string]string{ - "mention_keys": "User,@User,MENTION", - }, - } - - profiles := map[string]*model.User{user1.Id: user1} - mentions := getMentionKeywordsInChannel(profiles) - if len(mentions) != 3 { - t.Fatal("should've returned three mention keywords") - } else if ids, ok := mentions["user"]; !ok || ids[0] != user1.Id { - t.Fatal("should've returned mention key of user") - } else if ids, ok := mentions["@user"]; !ok || ids[0] != user1.Id { - t.Fatal("should've returned mention key of @user") - } else if ids, ok := mentions["mention"]; !ok || ids[0] != user1.Id { - t.Fatal("should've returned mention key of mention") - } - - // user with first name mention enabled - user2 := &model.User{ - Id: model.NewId(), - FirstName: "First", - Username: "User", - NotifyProps: map[string]string{ - "first_name": "true", - }, - } - - profiles = map[string]*model.User{user2.Id: user2} - mentions = getMentionKeywordsInChannel(profiles) - if len(mentions) != 2 { - t.Fatal("should've returned two mention keyword") - } else if ids, ok := mentions["First"]; !ok || ids[0] != user2.Id { - t.Fatal("should've returned mention key of First") - } - - // user with @channel/@all mentions enabled - user3 := &model.User{ - Id: model.NewId(), - FirstName: "First", - Username: "User", - NotifyProps: map[string]string{ - "channel": "true", - }, - } - - profiles = map[string]*model.User{user3.Id: user3} - mentions = getMentionKeywordsInChannel(profiles) - if len(mentions) != 3 { - t.Fatal("should've returned three mention keywords") - } else if ids, ok := mentions["@channel"]; !ok || ids[0] != user3.Id { - t.Fatal("should've returned mention key of @channel") - } else if ids, ok := mentions["@all"]; !ok || ids[0] != user3.Id { - t.Fatal("should've returned mention key of @all") - } - - // user with all types of mentions enabled - user4 := &model.User{ - Id: model.NewId(), - FirstName: "First", - Username: "User", - NotifyProps: map[string]string{ - "mention_keys": "User,@User,MENTION", - "first_name": "true", - "channel": "true", - }, - } - - profiles = map[string]*model.User{user4.Id: user4} - mentions = getMentionKeywordsInChannel(profiles) - if len(mentions) != 6 { - t.Fatal("should've returned six mention keywords") - } else if ids, ok := mentions["user"]; !ok || ids[0] != user4.Id { - t.Fatal("should've returned mention key of user") - } else if ids, ok := mentions["@user"]; !ok || ids[0] != user4.Id { - t.Fatal("should've returned mention key of @user") - } else if ids, ok := mentions["mention"]; !ok || ids[0] != user4.Id { - t.Fatal("should've returned mention key of mention") - } else if ids, ok := mentions["First"]; !ok || ids[0] != user4.Id { - t.Fatal("should've returned mention key of First") - } else if ids, ok := mentions["@channel"]; !ok || ids[0] != user4.Id { - t.Fatal("should've returned mention key of @channel") - } else if ids, ok := mentions["@all"]; !ok || ids[0] != user4.Id { - t.Fatal("should've returned mention key of @all") - } - - dup_count := func(list []string) map[string]int { - - duplicate_frequency := make(map[string]int) - - for _, item := range list { - // check if the item/element exist in the duplicate_frequency map - - _, exist := duplicate_frequency[item] - - if exist { - duplicate_frequency[item] += 1 // increase counter by 1 if already in the map - } else { - duplicate_frequency[item] = 1 // else start counting from 1 - } - } - return duplicate_frequency - } - - // multiple users - profiles = map[string]*model.User{ - user1.Id: user1, - user2.Id: user2, - user3.Id: user3, - user4.Id: user4, - } - mentions = getMentionKeywordsInChannel(profiles) - if len(mentions) != 6 { - t.Fatal("should've returned six mention keywords") - } else if ids, ok := mentions["user"]; !ok || len(ids) != 2 || (ids[0] != user1.Id && ids[1] != user1.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { - t.Fatal("should've mentioned user1 and user4 with user") - } else if ids := dup_count(mentions["@user"]); len(ids) != 4 || (ids[user1.Id] != 2) || (ids[user4.Id] != 2) { - t.Fatal("should've mentioned user1 and user4 with @user") - } else if ids, ok := mentions["mention"]; !ok || len(ids) != 2 || (ids[0] != user1.Id && ids[1] != user1.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { - t.Fatal("should've mentioned user1 and user4 with mention") - } else if ids, ok := mentions["First"]; !ok || len(ids) != 2 || (ids[0] != user2.Id && ids[1] != user2.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { - t.Fatal("should've mentioned user2 and user4 with mention") - } else if ids, ok := mentions["@channel"]; !ok || len(ids) != 2 || (ids[0] != user3.Id && ids[1] != user3.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { - t.Fatal("should've mentioned user3 and user4 with @channel") - } else if ids, ok := mentions["@all"]; !ok || len(ids) != 2 || (ids[0] != user3.Id && ids[1] != user3.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { - t.Fatal("should've mentioned user3 and user4 with @all") - } -} - -func TestGetExplicitMentionsAtHere(t *testing.T) { - // test all the boundary cases that we know can break up terms (and those that we know won't) - cases := map[string]bool{ - "": false, - "here": false, - "@here": true, - " @here ": true, - "\t@here\t": true, - "\n@here\n": true, - // "!@here!": true, - // "@@here@": true, - // "#@here#": true, - // "$@here$": true, - // "%@here%": true, - // "^@here^": true, - // "&@here&": true, - // "*@here*": true, - "(@here(": true, - ")@here)": true, - // "-@here-": true, - // "_@here_": true, - // "=@here=": true, - "+@here+": true, - "[@here[": true, - "{@here{": true, - "]@here]": true, - "}@here}": true, - "\\@here\\": true, - // "|@here|": true, - ";@here;": true, - ":@here:": true, - // "'@here'": true, - // "\"@here\"": true, - ",@here,": true, - "<@here<": true, - ".@here.": true, - ">@here>": true, - "/@here/": true, - "?@here?": true, - // "`@here`": true, - // "~@here~": true, - } - - for message, shouldMention := range cases { - if _, _, hereMentioned, _, _ := getExplicitMentions(message, nil); hereMentioned && !shouldMention { - t.Fatalf("shouldn't have mentioned @here with \"%v\"", message) - } else if !hereMentioned && shouldMention { - t.Fatalf("should've have mentioned @here with \"%v\"", message) - } - } - - // mentioning @here and someone - id := model.NewId() - if mentions, potential, hereMentioned, _, _ := getExplicitMentions("@here @user @potential", map[string][]string{"@user": {id}}); !hereMentioned { - t.Fatal("should've mentioned @here with \"@here @user\"") - } else if len(mentions) != 1 || !mentions[id] { - t.Fatal("should've mentioned @user with \"@here @user\"") - } else if len(potential) > 1 { - t.Fatal("should've potential mentions for @potential") - } -} - -func TestGetExplicitMentions(t *testing.T) { - id1 := model.NewId() - id2 := model.NewId() - - // not mentioning anybody - message := "this is a message" - keywords := map[string][]string{} - if mentions, potential, _, _, _ := getExplicitMentions(message, keywords); len(mentions) != 0 || len(potential) != 0 { - t.Fatal("shouldn't have mentioned anybody or have any potencial mentions") - } - - // mentioning a user that doesn't exist - message = "this is a message for @user" - if mentions, _, _, _, _ := getExplicitMentions(message, keywords); len(mentions) != 0 { - t.Fatal("shouldn't have mentioned user that doesn't exist") - } - - // mentioning one person - keywords = map[string][]string{"@user": {id1}} - if mentions, _, _, _, _ := getExplicitMentions(message, keywords); len(mentions) != 1 || !mentions[id1] { - t.Fatal("should've mentioned @user") - } - - // mentioning one person without an @mention - message = "this is a message for @user" - keywords = map[string][]string{"this": {id1}} - if mentions, _, _, _, _ := getExplicitMentions(message, keywords); len(mentions) != 1 || !mentions[id1] { - t.Fatal("should've mentioned this") - } - - // mentioning multiple people with one word - message = "this is a message for @user" - keywords = map[string][]string{"@user": {id1, id2}} - if mentions, _, _, _, _ := getExplicitMentions(message, keywords); len(mentions) != 2 || !mentions[id1] || !mentions[id2] { - t.Fatal("should've mentioned two users with @user") - } - - // mentioning only one of multiple people - keywords = map[string][]string{"@user": {id1}, "@mention": {id2}} - if mentions, _, _, _, _ := getExplicitMentions(message, keywords); len(mentions) != 1 || !mentions[id1] || mentions[id2] { - t.Fatal("should've mentioned @user and not @mention") - } - - // mentioning multiple people with multiple words - message = "this is an @mention for @user" - keywords = map[string][]string{"@user": {id1}, "@mention": {id2}} - if mentions, _, _, _, _ := getExplicitMentions(message, keywords); len(mentions) != 2 || !mentions[id1] || !mentions[id2] { - t.Fatal("should've mentioned two users with @user and @mention") - } - - // mentioning @channel (not a special case, but it's good to double check) - message = "this is an message for @channel" - keywords = map[string][]string{"@channel": {id1, id2}} - if mentions, _, _, _, _ := getExplicitMentions(message, keywords); len(mentions) != 2 || !mentions[id1] || !mentions[id2] { - t.Fatal("should've mentioned two users with @channel") - } - - // mentioning @all (not a special case, but it's good to double check) - message = "this is an message for @all" - keywords = map[string][]string{"@all": {id1, id2}} - if mentions, _, _, _, _ := getExplicitMentions(message, keywords); len(mentions) != 2 || !mentions[id1] || !mentions[id2] { - t.Fatal("should've mentioned two users with @all") - } - - // mentioning user.period without mentioning user (PLT-3222) - message = "user.period doesn't complicate things at all by including periods in their username" - keywords = map[string][]string{"user.period": {id1}, "user": {id2}} - if mentions, _, _, _, _ := getExplicitMentions(message, keywords); len(mentions) != 1 || !mentions[id1] || mentions[id2] { - t.Fatal("should've mentioned user.period and not user") - } - - // mentioning a potential out of channel user - message = "this is an message for @potential and @user" - keywords = map[string][]string{"@user": {id1}} - if mentions, potential, _, _, _ := getExplicitMentions(message, keywords); len(mentions) != 1 || !mentions[id1] || len(potential) != 1 { - t.Fatal("should've mentioned user and have a potential not in channel") - } -} - func TestGetFlaggedPosts(t *testing.T) { th := Setup().InitBasic() Client := th.BasicClient @@ -1226,28 +954,28 @@ func TestGetFlaggedPosts(t *testing.T) { func TestGetMessageForNotification(t *testing.T) { Setup().InitBasic() - testPng := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{ + testPng := store.Must(app.Srv.Store.FileInfo().Save(&model.FileInfo{ CreatorId: model.NewId(), Path: "test1.png", Name: "test1.png", MimeType: "image/png", })).(*model.FileInfo) - testJpg1 := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{ + testJpg1 := store.Must(app.Srv.Store.FileInfo().Save(&model.FileInfo{ CreatorId: model.NewId(), Path: "test2.jpg", Name: "test2.jpg", MimeType: "image/jpeg", })).(*model.FileInfo) - testFile := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{ + testFile := store.Must(app.Srv.Store.FileInfo().Save(&model.FileInfo{ CreatorId: model.NewId(), Path: "test1.go", Name: "test1.go", MimeType: "text/plain", })).(*model.FileInfo) - testJpg2 := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{ + testJpg2 := store.Must(app.Srv.Store.FileInfo().Save(&model.FileInfo{ CreatorId: model.NewId(), Path: "test3.jpg", Name: "test3.jpg", @@ -1261,37 +989,37 @@ func TestGetMessageForNotification(t *testing.T) { Message: "test", } - if getMessageForNotification(post, translateFunc) != "test" { + if app.GetMessageForNotification(post, translateFunc) != "test" { t.Fatal("should've returned message text") } post.FileIds = model.StringArray{testPng.Id} - store.Must(Srv.Store.FileInfo().AttachToPost(testPng.Id, post.Id)) - if getMessageForNotification(post, translateFunc) != "test" { + store.Must(app.Srv.Store.FileInfo().AttachToPost(testPng.Id, post.Id)) + if app.GetMessageForNotification(post, translateFunc) != "test" { t.Fatal("should've returned message text, even with attachments") } post.Message = "" - if message := getMessageForNotification(post, translateFunc); message != "1 image sent: test1.png" { + if message := app.GetMessageForNotification(post, translateFunc); message != "1 image sent: test1.png" { t.Fatal("should've returned number of images:", message) } post.FileIds = model.StringArray{testPng.Id, testJpg1.Id} - store.Must(Srv.Store.FileInfo().AttachToPost(testJpg1.Id, post.Id)) - if message := getMessageForNotification(post, translateFunc); message != "2 images sent: test1.png, test2.jpg" && message != "2 images sent: test2.jpg, test1.png" { + store.Must(app.Srv.Store.FileInfo().AttachToPost(testJpg1.Id, post.Id)) + if message := app.GetMessageForNotification(post, translateFunc); message != "2 images sent: test1.png, test2.jpg" && message != "2 images sent: test2.jpg, test1.png" { t.Fatal("should've returned number of images:", message) } post.Id = model.NewId() post.FileIds = model.StringArray{testFile.Id} - store.Must(Srv.Store.FileInfo().AttachToPost(testFile.Id, post.Id)) - if message := getMessageForNotification(post, translateFunc); message != "1 file sent: test1.go" { + store.Must(app.Srv.Store.FileInfo().AttachToPost(testFile.Id, post.Id)) + if message := app.GetMessageForNotification(post, translateFunc); message != "1 file sent: test1.go" { t.Fatal("should've returned number of files:", message) } - store.Must(Srv.Store.FileInfo().AttachToPost(testJpg2.Id, post.Id)) + store.Must(app.Srv.Store.FileInfo().AttachToPost(testJpg2.Id, post.Id)) post.FileIds = model.StringArray{testFile.Id, testJpg2.Id} - if message := getMessageForNotification(post, translateFunc); message != "2 files sent: test1.go, test3.jpg" && message != "2 files sent: test3.jpg, test1.go" { + if message := app.GetMessageForNotification(post, translateFunc); message != "2 files sent: test1.go, test3.jpg" && message != "2 files sent: test3.jpg, test1.go" { t.Fatal("should've returned number of mixed files:", message) } } @@ -1334,43 +1062,6 @@ func TestGetFileInfosForPost(t *testing.T) { } } -func TestSendNotifications(t *testing.T) { - th := Setup().InitBasic() - Client := th.BasicClient - - AddUserToChannel(th.BasicUser2, th.BasicChannel) - - mockSession := model.Session{ - UserId: th.BasicUser.Id, - TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, UserId: th.BasicUser.Id}}, - IsOAuth: false, - } - - newContext := &Context{ - Session: mockSession, - RequestId: model.NewId(), - IpAddress: "", - Path: "fake", - Err: nil, - siteURL: *utils.Cfg.ServiceSettings.SiteURL, - TeamId: th.BasicTeam.Id, - } - - post1 := Client.Must(Client.CreatePost(&model.Post{ - ChannelId: th.BasicChannel.Id, - Message: "@" + th.BasicUser2.Username, - })).Data.(*model.Post) - - mentions := sendNotifications(newContext, post1, th.BasicTeam, th.BasicChannel) - if mentions == nil { - t.Log(mentions) - t.Fatal("user should have been mentioned") - } else if mentions[0] != th.BasicUser2.Id { - t.Log(mentions) - t.Fatal("user should have been mentioned") - } -} - func TestGetPostById(t *testing.T) { th := Setup().InitBasic() Client := th.BasicClient |