From b7d045844c4d00d8268da50f2388be1d12c91829 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Sat, 30 Jan 2016 13:44:39 -0500 Subject: Added serverside check for a post that @mentions someone who isn't in the channel --- api/post_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'api/post_test.go') diff --git a/api/post_test.go b/api/post_test.go index 1a9fd2579..1b05bd39f 100644 --- a/api/post_test.go +++ b/api/post_test.go @@ -857,3 +857,71 @@ func TestMakeDirectChannelVisible(t *testing.T) { t.Fatal("Failed to set direct channel to be visible for user2") } } + +func TestGetOutOfChannelMentions(t *testing.T) { + Setup() + + team1 := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN} + team1 = Client.Must(Client.CreateTeam(team1)).Data.(*model.Team) + + user1 := &model.User{TeamId: team1.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user1"} + user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User) + store.Must(Srv.Store.User().VerifyEmail(user1.Id)) + + user2 := &model.User{TeamId: team1.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user2"} + user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User) + store.Must(Srv.Store.User().VerifyEmail(user2.Id)) + + user3 := &model.User{TeamId: team1.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user3"} + user3 = Client.Must(Client.CreateUser(user3, "")).Data.(*model.User) + store.Must(Srv.Store.User().VerifyEmail(user3.Id)) + + Client.Must(Client.LoginByEmail(team1.Name, user1.Email, "pwd")) + + channel1 := &model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team1.Id} + channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel) + + // test a post that doesn't @mention anybody + post1 := &model.Post{ChannelId: channel1.Id, Message: "user1 user2 user3"} + if mentioned := getOutOfChannelMentions(post1, team1.Id); len(mentioned) != 0 { + t.Fatalf("getOutOfChannelMentions returned %v when no users were mentioned", mentioned) + } + + // test a post that @mentions someone in the channel + post2 := &model.Post{ChannelId: channel1.Id, Message: "@user1 is user1"} + if mentioned := getOutOfChannelMentions(post2, team1.Id); len(mentioned) != 0 { + t.Fatalf("getOutOfChannelMentions returned %v when only users in the channel were mentioned", mentioned) + } + + // test a post that @mentions someone not in the channel + post3 := &model.Post{ChannelId: channel1.Id, Message: "@user2 and @user3 aren't in the channel"} + if mentioned := getOutOfChannelMentions(post3, team1.Id); len(mentioned) != 2 || (mentioned[0].Id != user2.Id && mentioned[0].Id != user3.Id) || (mentioned[1].Id != user2.Id && mentioned[1].Id != user3.Id) { + t.Fatalf("getOutOfChannelMentions returned %v when two users outside the channel were mentioned", mentioned) + } + + // test a post that @mentions someone not in the channel as well as someone in the channel + post4 := &model.Post{ChannelId: channel1.Id, Message: "@user2 and @user1 might be in the channel"} + if mentioned := getOutOfChannelMentions(post4, team1.Id); len(mentioned) != 1 || mentioned[0].Id != user2.Id { + t.Fatalf("getOutOfChannelMentions returned %v when someone in the channel and someone outside the channel were mentioned", mentioned) + } + + Client.Must(Client.Logout()) + + team2 := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN} + team2 = Client.Must(Client.CreateTeam(team2)).Data.(*model.Team) + + user4 := &model.User{TeamId: team2.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user4"} + user4 = Client.Must(Client.CreateUser(user4, "")).Data.(*model.User) + store.Must(Srv.Store.User().VerifyEmail(user4.Id)) + + Client.Must(Client.LoginByEmail(team2.Name, user4.Email, "pwd")) + + channel2 := &model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team2.Id} + channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel) + + // test a post that @mentions someone on a different team + post5 := &model.Post{ChannelId: channel2.Id, Message: "@user2 and @user3 might be in the channel"} + if mentioned := getOutOfChannelMentions(post5, team2.Id); len(mentioned) != 0 { + t.Fatalf("getOutOfChannelMentions returned %v when two users on a different team were mentioned", mentioned) + } +} -- cgit v1.2.3-1-g7c22 From f8005744a26b7196f8b652e47bd7c094193dbb30 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Tue, 2 Feb 2016 11:02:13 -0500 Subject: Changed out of channel mention tests to use proper email addresses --- api/post_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'api/post_test.go') diff --git a/api/post_test.go b/api/post_test.go index 1b05bd39f..86e9292d6 100644 --- a/api/post_test.go +++ b/api/post_test.go @@ -8,6 +8,7 @@ import ( "github.com/mattermost/platform/store" "github.com/mattermost/platform/utils" "net/http" + "strings" "testing" "time" ) @@ -861,18 +862,18 @@ func TestMakeDirectChannelVisible(t *testing.T) { func TestGetOutOfChannelMentions(t *testing.T) { Setup() - team1 := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN} + team1 := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: strings.ToLower(model.NewId()) + "success+test@simulator.amazonses.com", Type: model.TEAM_OPEN} team1 = Client.Must(Client.CreateTeam(team1)).Data.(*model.Team) - user1 := &model.User{TeamId: team1.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user1"} + user1 := &model.User{TeamId: team1.Id, Email: strings.ToLower(model.NewId()) + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user1"} user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User) store.Must(Srv.Store.User().VerifyEmail(user1.Id)) - user2 := &model.User{TeamId: team1.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user2"} + user2 := &model.User{TeamId: team1.Id, Email: strings.ToLower(model.NewId()) + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user2"} user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User) store.Must(Srv.Store.User().VerifyEmail(user2.Id)) - user3 := &model.User{TeamId: team1.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user3"} + user3 := &model.User{TeamId: team1.Id, Email: strings.ToLower(model.NewId()) + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user3"} user3 = Client.Must(Client.CreateUser(user3, "")).Data.(*model.User) store.Must(Srv.Store.User().VerifyEmail(user3.Id)) @@ -907,10 +908,10 @@ func TestGetOutOfChannelMentions(t *testing.T) { Client.Must(Client.Logout()) - team2 := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN} + team2 := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: strings.ToLower(model.NewId()) + "success+test@simulator.amazonses.com", Type: model.TEAM_OPEN} team2 = Client.Must(Client.CreateTeam(team2)).Data.(*model.Team) - user4 := &model.User{TeamId: team2.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user4"} + user4 := &model.User{TeamId: team2.Id, Email: strings.ToLower(model.NewId()) + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "pwd", Username: "user4"} user4 = Client.Must(Client.CreateUser(user4, "")).Data.(*model.User) store.Must(Srv.Store.User().VerifyEmail(user4.Id)) -- cgit v1.2.3-1-g7c22 From 994358c31a93296a225f7d34942bbedfeac025c4 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Wed, 3 Feb 2016 10:56:47 -0500 Subject: Reworked the code for sending notifications and checking for out of channel mentions to share some data --- api/post_test.go | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'api/post_test.go') diff --git a/api/post_test.go b/api/post_test.go index 86e9292d6..027043766 100644 --- a/api/post_test.go +++ b/api/post_test.go @@ -882,27 +882,41 @@ func TestGetOutOfChannelMentions(t *testing.T) { channel1 := &model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team1.Id} channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel) + var allProfiles map[string]*model.User + if result := <-Srv.Store.User().GetProfiles(team1.Id); result.Err != nil { + t.Fatal(result.Err) + } else { + allProfiles = result.Data.(map[string]*model.User) + } + + var members []model.ChannelMember + if result := <-Srv.Store.Channel().GetMembers(channel1.Id); result.Err != nil { + t.Fatal(result.Err) + } else { + members = result.Data.([]model.ChannelMember) + } + // test a post that doesn't @mention anybody post1 := &model.Post{ChannelId: channel1.Id, Message: "user1 user2 user3"} - if mentioned := getOutOfChannelMentions(post1, team1.Id); len(mentioned) != 0 { + if mentioned := getOutOfChannelMentions(post1, allProfiles, members); len(mentioned) != 0 { t.Fatalf("getOutOfChannelMentions returned %v when no users were mentioned", mentioned) } // test a post that @mentions someone in the channel post2 := &model.Post{ChannelId: channel1.Id, Message: "@user1 is user1"} - if mentioned := getOutOfChannelMentions(post2, team1.Id); len(mentioned) != 0 { + if mentioned := getOutOfChannelMentions(post2, allProfiles, members); len(mentioned) != 0 { t.Fatalf("getOutOfChannelMentions returned %v when only users in the channel were mentioned", mentioned) } // test a post that @mentions someone not in the channel post3 := &model.Post{ChannelId: channel1.Id, Message: "@user2 and @user3 aren't in the channel"} - if mentioned := getOutOfChannelMentions(post3, team1.Id); len(mentioned) != 2 || (mentioned[0].Id != user2.Id && mentioned[0].Id != user3.Id) || (mentioned[1].Id != user2.Id && mentioned[1].Id != user3.Id) { + if mentioned := getOutOfChannelMentions(post3, allProfiles, members); len(mentioned) != 2 || (mentioned[0].Id != user2.Id && mentioned[0].Id != user3.Id) || (mentioned[1].Id != user2.Id && mentioned[1].Id != user3.Id) { t.Fatalf("getOutOfChannelMentions returned %v when two users outside the channel were mentioned", mentioned) } // test a post that @mentions someone not in the channel as well as someone in the channel post4 := &model.Post{ChannelId: channel1.Id, Message: "@user2 and @user1 might be in the channel"} - if mentioned := getOutOfChannelMentions(post4, team1.Id); len(mentioned) != 1 || mentioned[0].Id != user2.Id { + if mentioned := getOutOfChannelMentions(post4, allProfiles, members); len(mentioned) != 1 || mentioned[0].Id != user2.Id { t.Fatalf("getOutOfChannelMentions returned %v when someone in the channel and someone outside the channel were mentioned", mentioned) } @@ -920,9 +934,21 @@ func TestGetOutOfChannelMentions(t *testing.T) { channel2 := &model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team2.Id} channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel) + if result := <-Srv.Store.User().GetProfiles(team2.Id); result.Err != nil { + t.Fatal(result.Err) + } else { + allProfiles = result.Data.(map[string]*model.User) + } + + if result := <-Srv.Store.Channel().GetMembers(channel2.Id); result.Err != nil { + t.Fatal(result.Err) + } else { + members = result.Data.([]model.ChannelMember) + } + // test a post that @mentions someone on a different team post5 := &model.Post{ChannelId: channel2.Id, Message: "@user2 and @user3 might be in the channel"} - if mentioned := getOutOfChannelMentions(post5, team2.Id); len(mentioned) != 0 { + if mentioned := getOutOfChannelMentions(post5, allProfiles, members); len(mentioned) != 0 { t.Fatalf("getOutOfChannelMentions returned %v when two users on a different team were mentioned", mentioned) } } -- cgit v1.2.3-1-g7c22