summaryrefslogtreecommitdiffstats
path: root/api4/post_test.go
diff options
context:
space:
mode:
authorSaturnino Abril <saturnino.abril@gmail.com>2017-10-21 01:38:26 +0800
committerGitHub <noreply@github.com>2017-10-21 01:38:26 +0800
commit18ee37586027e5672446a6f23c05a8ccb2b35896 (patch)
treed915646e73a91b0082ecdf89b7a290291c0c9451 /api4/post_test.go
parentcbb18479e9a4c410664535f6e0f5032d4261dfff (diff)
downloadchat-18ee37586027e5672446a6f23c05a8ccb2b35896.tar.gz
chat-18ee37586027e5672446a6f23c05a8ccb2b35896.tar.bz2
chat-18ee37586027e5672446a6f23c05a8ccb2b35896.zip
[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
Diffstat (limited to 'api4/post_test.go')
-rw-r--r--api4/post_test.go78
1 files changed, 77 insertions, 1 deletions
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)