From 5efcd2d9d3fb91ae7475918b807b7947b533da9b Mon Sep 17 00:00:00 2001 From: Robin Naundorf Date: Tue, 9 May 2017 14:52:46 +0200 Subject: Add API Endpoint for deleted Channels (#5889) --- api4/channel.go | 21 +++++++++++++++++++++ api4/channel_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) (limited to 'api4') diff --git a/api4/channel.go b/api4/channel.go index 493b012a0..69cc0953e 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -21,6 +21,7 @@ func InitChannel() { BaseRoutes.Channels.Handle("/members/{user_id:[A-Za-z0-9]+}/view", ApiSessionRequired(viewChannel)).Methods("POST") BaseRoutes.ChannelsForTeam.Handle("", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET") + BaseRoutes.ChannelsForTeam.Handle("/deleted", ApiSessionRequired(getDeletedChannelsForTeam)).Methods("GET") BaseRoutes.ChannelsForTeam.Handle("/ids", ApiSessionRequired(getPublicChannelsByIdsForTeam)).Methods("POST") BaseRoutes.ChannelsForTeam.Handle("/search", ApiSessionRequired(searchChannelsForTeam)).Methods("POST") BaseRoutes.User.Handle("/teams/{team_id:[A-Za-z0-9]+}/channels", ApiSessionRequired(getChannelsForTeamForUser)).Methods("GET") @@ -385,6 +386,26 @@ func getPublicChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Request } } +func getDeletedChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireTeamId() + if c.Err != nil { + return + } + + if !app.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_MANAGE_TEAM) { + c.SetPermissionError(model.PERMISSION_MANAGE_TEAM) + return + } + + if channels, err := app.GetDeletedChannels(c.Params.TeamId, c.Params.Page * c.Params.PerPage, c.Params.PerPage); err != nil { + c.Err = err + return + } else { + w.Write([]byte(channels.ToJson())) + return + } +} + func getPublicChannelsByIdsForTeam(c *Context, w http.ResponseWriter, r *http.Request) { c.RequireTeamId() if c.Err != nil { diff --git a/api4/channel_test.go b/api4/channel_test.go index 345bbefaf..10cb272f3 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -489,6 +489,55 @@ func TestGetChannel(t *testing.T) { CheckNotFoundStatus(t, resp) } +func TestGetDeletedChannelsForTeam(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + team := th.BasicTeam + + channels, resp := Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") + CheckForbiddenStatus(t, resp) + + th.LoginTeamAdmin() + + channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") + CheckNoError(t, resp) + if len(*channels) != 0 { + t.Fatal("should be no deleted channels") + } + + // create and delete public channel + publicChannel1 := th.CreatePublicChannel() + Client.DeleteChannel(publicChannel1.Id) + + channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") + CheckNoError(t, resp) + if len(*channels) != 1 { + t.Fatal("should be 1 deleted channel") + } + + publicChannel2 := th.CreatePublicChannel() + Client.DeleteChannel(publicChannel2.Id) + + channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") + CheckNoError(t, resp) + if len(*channels) != 2 { + t.Fatal("should be 2 deleted channels") + } + + channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 1, "") + CheckNoError(t, resp) + if len(*channels) != 1 { + t.Fatal("should be one channel per page") + } + + channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 1, 1, "") + CheckNoError(t, resp) + if len(*channels) != 1 { + t.Fatal("should be one channel per page") + } +} + func TestGetPublicChannelsForTeam(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() -- cgit v1.2.3-1-g7c22