From 18ee37586027e5672446a6f23c05a8ccb2b35896 Mon Sep 17 00:00:00 2001 From: Saturnino Abril Date: Sat, 21 Oct 2017 01:38:26 +0800 Subject: [PLT-7362] Option to add user to channel if mentioned user is not currently in the channel (#7619) * Option to add user to channel if mentioned user is not currently in the channel * instead of link from server, just add component on client side to add channel member * change implementation using post.props * do clean up and add test * sanitize post.props['add_channel_member'] on post creation * move sanitize to app.CreatePost and also apply to app.UpdatePost --- api4/post_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) (limited to 'api4/post_test.go') diff --git a/api4/post_test.go b/api4/post_test.go index 3a410f2b4..05c5c3d97 100644 --- a/api4/post_test.go +++ b/api4/post_test.go @@ -24,7 +24,7 @@ func TestCreatePost(t *testing.T) { defer th.TearDown() Client := th.Client - post := &model.Post{ChannelId: th.BasicChannel.Id, Message: "#hashtag a" + model.NewId() + "a"} + post := &model.Post{ChannelId: th.BasicChannel.Id, Message: "#hashtag a" + model.NewId() + "a", Props: model.StringInterface{model.PROPS_ADD_CHANNEL_MEMBER: "no good"}} rpost, resp := Client.CreatePost(post) CheckNoError(t, resp) CheckCreatedStatus(t, resp) @@ -45,6 +45,10 @@ func TestCreatePost(t *testing.T) { t.Fatal("newly created post shouldn't have EditAt set") } + if rpost.Props[model.PROPS_ADD_CHANNEL_MEMBER] != nil { + t.Fatal("newly created post shouldn't have Props['add_channel_member'] set") + } + post.RootId = rpost.Id post.ParentId = rpost.Id _, resp = Client.CreatePost(post) @@ -371,6 +375,73 @@ func TestCreatePostAll(t *testing.T) { CheckForbiddenStatus(t, resp) } +func TestCreatePostSendOutOfChannelMentions(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer th.TearDown() + Client := th.Client + + WebSocketClient, err := th.CreateWebSocketClient() + if err != nil { + t.Fatal(err) + } + WebSocketClient.Listen() + + inChannelUser := th.CreateUser() + th.LinkUserToTeam(inChannelUser, th.BasicTeam) + th.App.AddUserToChannel(inChannelUser, th.BasicChannel) + + post1 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "@" + inChannelUser.Username} + _, resp := Client.CreatePost(post1) + CheckNoError(t, resp) + CheckCreatedStatus(t, resp) + + timeout := time.After(300 * time.Millisecond) + waiting := true + for waiting { + select { + case event := <-WebSocketClient.EventChannel: + if event.Event == model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE { + t.Fatal("should not have ephemeral message event") + } + + case <-timeout: + waiting = false + } + } + + outOfChannelUser := th.CreateUser() + th.LinkUserToTeam(outOfChannelUser, th.BasicTeam) + + post2 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "@" + outOfChannelUser.Username} + _, resp = Client.CreatePost(post2) + CheckNoError(t, resp) + CheckCreatedStatus(t, resp) + + timeout = time.After(300 * time.Millisecond) + waiting = true + for waiting { + select { + case event := <-WebSocketClient.EventChannel: + if event.Event != model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE { + // Ignore any other events + continue + } + + wpost := model.PostFromJson(strings.NewReader(event.Data["post"].(string))) + if acm, ok := wpost.Props[model.PROPS_ADD_CHANNEL_MEMBER].(map[string]interface{}); !ok { + t.Fatal("should have received ephemeral post with 'add_channel_member' in props") + } else { + if acm["post_id"] == nil || acm["user_ids"] == nil || acm["usernames"] == nil { + t.Fatal("should not be nil") + } + } + waiting = false + case <-timeout: + t.Fatal("timed out waiting for ephemeral message event") + } + } +} + func TestUpdatePost(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer th.TearDown() @@ -421,6 +492,7 @@ func TestUpdatePost(t *testing.T) { msg1 := "#hashtag a" + model.NewId() + " update post again" rpost.Message = msg1 + rpost.Props[model.PROPS_ADD_CHANNEL_MEMBER] = "no good" rrupost, resp := Client.UpdatePost(rpost.Id, rpost) CheckNoError(t, resp) @@ -428,6 +500,10 @@ func TestUpdatePost(t *testing.T) { t.Fatal("failed to updates") } + if rrupost.Props[model.PROPS_ADD_CHANNEL_MEMBER] != nil { + t.Fatal("failed to sanitize Props['add_channel_member'], should be nil") + } + rpost2, err := th.App.CreatePost(&model.Post{ChannelId: channel.Id, Message: "zz" + model.NewId() + "a", Type: model.POST_JOIN_LEAVE, UserId: th.BasicUser.Id}, channel, false) if err != nil { t.Fatal(err) -- cgit v1.2.3-1-g7c22