From 5872bf9c2f9b81c7aad761d40a6970f6267f1424 Mon Sep 17 00:00:00 2001 From: Martin Kraft Date: Mon, 30 Jul 2018 15:06:08 -0400 Subject: Pr 9039 (#9187) * MM-11065: Allow to search and get archived channels from the API * Fixing more tests * Add some unit tests * Add includeDeleted parameter to session permissions check function * More test fixing * Adding archive channels list in channels search * Add restriction for archived channel edition * Reverting permissions checks modification * Changed the query parameter to include_deleted * Enable search archive channels as true by default * Adding tests for verify search on deleted channels * Allowing to override archive channels during the imports * Fixed test * Search in archive channels from the API must be explicitly requested * Removing includeDeleted parameter from GetChannelByName and GetChannelByNameForTeam * Back to ViewArchivedChannels config * Fixing tests * Reverting GetChannelByName parameter * Add include deleted parameter on GetChannel functions in plugins api * Fixing tests --- api4/apitestlib.go | 5 +++++ api4/channel.go | 10 +++++++--- api4/channel_test.go | 28 ++++++++++++++++++++++++---- api4/post.go | 4 +++- api4/post_test.go | 11 ++++++++++- api4/reaction_test.go | 4 ++-- 6 files changed, 51 insertions(+), 11 deletions(-) (limited to 'api4') diff --git a/api4/apitestlib.go b/api4/apitestlib.go index fce44cfa1..652a8f882 100644 --- a/api4/apitestlib.go +++ b/api4/apitestlib.go @@ -44,6 +44,7 @@ type TestHelper struct { BasicTeam *model.Team BasicChannel *model.Channel BasicPrivateChannel *model.Channel + BasicDeletedChannel *model.Channel BasicChannel2 *model.Channel BasicPost *model.Post @@ -250,6 +251,7 @@ func (me *TestHelper) InitBasic() *TestHelper { me.BasicTeam = me.CreateTeam() me.BasicChannel = me.CreatePublicChannel() me.BasicPrivateChannel = me.CreatePrivateChannel() + me.BasicDeletedChannel = me.CreatePublicChannel() me.BasicChannel2 = me.CreatePublicChannel() me.BasicPost = me.CreatePost() me.BasicUser = me.CreateUser() @@ -262,7 +264,10 @@ func (me *TestHelper) InitBasic() *TestHelper { me.App.AddUserToChannel(me.BasicUser2, me.BasicChannel2) me.App.AddUserToChannel(me.BasicUser, me.BasicPrivateChannel) me.App.AddUserToChannel(me.BasicUser2, me.BasicPrivateChannel) + me.App.AddUserToChannel(me.BasicUser, me.BasicDeletedChannel) + me.App.AddUserToChannel(me.BasicUser2, me.BasicDeletedChannel) me.App.UpdateUserRoles(me.BasicUser.Id, model.SYSTEM_USER_ROLE_ID, false) + me.Client.DeleteChannel(me.BasicDeletedChannel.Id) me.LoginBasic() return me diff --git a/api4/channel.go b/api4/channel.go index db34bf03c..0b8470975 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -589,7 +589,7 @@ func getChannelsForTeamForUser(c *Context, w http.ResponseWriter, r *http.Reques return } - channels, err := c.App.GetChannelsForUser(c.Params.TeamId, c.Params.UserId) + channels, err := c.App.GetChannelsForUser(c.Params.TeamId, c.Params.UserId, false) if err != nil { c.Err = err return @@ -709,7 +709,9 @@ func getChannelByName(c *Context, w http.ResponseWriter, r *http.Request) { var channel *model.Channel var err *model.AppError - if channel, err = c.App.GetChannelByName(c.Params.ChannelName, c.Params.TeamId); err != nil { + includeDeleted := r.URL.Query().Get("include_deleted") == "true" + + if channel, err = c.App.GetChannelByName(c.Params.ChannelName, c.Params.TeamId, includeDeleted); err != nil { c.Err = err return } @@ -744,7 +746,9 @@ func getChannelByNameForTeamName(c *Context, w http.ResponseWriter, r *http.Requ var channel *model.Channel var err *model.AppError - if channel, err = c.App.GetChannelByNameForTeamName(c.Params.ChannelName, c.Params.TeamName); err != nil { + includeDeleted := r.URL.Query().Get("include_deleted") == "true" + + if channel, err = c.App.GetChannelByNameForTeamName(c.Params.ChannelName, c.Params.TeamName, includeDeleted); err != nil { c.Err = err return } diff --git a/api4/channel_test.go b/api4/channel_test.go index 0d8fbe4d5..4645a0ab6 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -867,7 +867,7 @@ func TestDeleteChannel(t *testing.T) { CheckNoError(t, resp) // default channel cannot be deleted. - defaultChannel, _ := th.App.GetChannelByName(model.DEFAULT_CHANNEL, team.Id) + defaultChannel, _ := th.App.GetChannelByName(model.DEFAULT_CHANNEL, team.Id, false) pass, resp = Client.DeleteChannel(defaultChannel.Id) CheckBadRequestStatus(t, resp) @@ -998,7 +998,7 @@ func TestConvertChannelToPrivate(t *testing.T) { defer th.TearDown() Client := th.Client - defaultChannel, _ := th.App.GetChannelByName(model.DEFAULT_CHANNEL, th.BasicTeam.Id) + defaultChannel, _ := th.App.GetChannelByName(model.DEFAULT_CHANNEL, th.BasicTeam.Id, false) _, resp := Client.ConvertChannelToPrivate(defaultChannel.Id) CheckForbiddenStatus(t, resp) @@ -1115,6 +1115,16 @@ func TestGetChannelByName(t *testing.T) { _, resp = Client.GetChannelByName(strings.ToUpper(th.BasicPrivateChannel.Name), th.BasicTeam.Id, "") CheckNoError(t, resp) + _, resp = Client.GetChannelByName(th.BasicDeletedChannel.Name, th.BasicTeam.Id, "") + CheckNotFoundStatus(t, resp) + + channel, resp = Client.GetChannelByNameIncludeDeleted(th.BasicDeletedChannel.Name, th.BasicTeam.Id, "") + CheckNoError(t, resp) + + if channel.Name != th.BasicDeletedChannel.Name { + t.Fatal("names did not match") + } + Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) _, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") CheckNoError(t, resp) @@ -1157,6 +1167,16 @@ func TestGetChannelByNameForTeamName(t *testing.T) { _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") CheckNoError(t, resp) + _, resp = Client.GetChannelByNameForTeamName(th.BasicDeletedChannel.Name, th.BasicTeam.Name, "") + CheckNotFoundStatus(t, resp) + + channel, resp = Client.GetChannelByNameForTeamNameIncludeDeleted(th.BasicDeletedChannel.Name, th.BasicTeam.Name, "") + CheckNoError(t, resp) + + if channel.Name != th.BasicDeletedChannel.Name { + t.Fatal("names did not match") + } + _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, model.NewRandomString(15), "") CheckNotFoundStatus(t, resp) @@ -1330,8 +1350,8 @@ func TestGetChannelMembersForUser(t *testing.T) { members, resp := Client.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "") CheckNoError(t, resp) - if len(*members) != 5 { - t.Fatal("should have 5 members on team") + if len(*members) != 6 { + t.Fatal("should have 6 members on team") } _, resp = Client.GetChannelMembersForUser("", th.BasicTeam.Id, "") diff --git a/api4/post.go b/api4/post.go index b76e89964..12664cc24 100644 --- a/api4/post.go +++ b/api4/post.go @@ -330,6 +330,8 @@ func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) { return } + includeDeletedChannels := r.URL.Query().Get("include_deleted_channels") == "true" + props := model.StringInterfaceFromJson(r.Body) terms, ok := props["terms"].(string) if !ok || len(terms) == 0 { @@ -341,7 +343,7 @@ func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) { startTime := time.Now() - results, err := c.App.SearchPostsInTeam(terms, c.Session.UserId, c.Params.TeamId, isOrSearch) + results, err := c.App.SearchPostsInTeam(terms, c.Session.UserId, c.Params.TeamId, isOrSearch, includeDeletedChannels) elapsedTime := float64(time.Since(startTime)) / float64(time.Second) metrics := c.App.Metrics diff --git a/api4/post_test.go b/api4/post_test.go index 720fdc410..036a64fc7 100644 --- a/api4/post_test.go +++ b/api4/post_test.go @@ -1288,6 +1288,10 @@ func TestSearchPosts(t *testing.T) { message = "hashtag for post4" _ = th.CreateMessagePost(message) + archivedChannel := th.CreatePublicChannel() + _ = th.CreateMessagePostWithClient(th.Client, archivedChannel, "#hashtag for post3") + th.Client.DeleteChannel(archivedChannel.Id) + posts, resp := Client.SearchPosts(th.BasicTeam.Id, "search", false) CheckNoError(t, resp) if len(posts.Order) != 3 { @@ -1306,6 +1310,12 @@ func TestSearchPosts(t *testing.T) { t.Fatal("wrong search") } + posts, resp = Client.SearchPostsIncludeDeletedChannels(th.BasicTeam.Id, "#hashtag", false) + CheckNoError(t, resp) + if len(posts.Order) != 2 { + t.Fatal("wrong search") + } + if posts, resp = Client.SearchPosts(th.BasicTeam.Id, "*", false); len(posts.Order) != 0 { t.Fatal("searching for just * shouldn't return any results") } @@ -1328,7 +1338,6 @@ func TestSearchPosts(t *testing.T) { Client.Logout() _, resp = Client.SearchPosts(th.BasicTeam.Id, "#sgtitlereview", false) CheckUnauthorizedStatus(t, resp) - } func TestSearchHashtagPosts(t *testing.T) { diff --git a/api4/reaction_test.go b/api4/reaction_test.go index dda9a578a..ca2c49808 100644 --- a/api4/reaction_test.go +++ b/api4/reaction_test.go @@ -176,7 +176,7 @@ func TestSaveReaction(t *testing.T) { t.Run("unable-to-react-in-read-only-town-square", func(t *testing.T) { th.LoginBasic() - channel, err := th.App.GetChannelByName("town-square", th.BasicTeam.Id) + channel, err := th.App.GetChannelByName("town-square", th.BasicTeam.Id, true) assert.Nil(t, err) post := th.CreatePostWithClient(th.Client, channel) @@ -482,7 +482,7 @@ func TestDeleteReaction(t *testing.T) { t.Run("unable-to-delete-reactions-in-read-only-town-square", func(t *testing.T) { th.LoginBasic() - channel, err := th.App.GetChannelByName("town-square", th.BasicTeam.Id) + channel, err := th.App.GetChannelByName("town-square", th.BasicTeam.Id, true) assert.Nil(t, err) post := th.CreatePostWithClient(th.Client, channel) -- cgit v1.2.3-1-g7c22