diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-03-24 16:45:34 -0400 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2017-03-24 13:45:34 -0700 |
commit | 28a78d76074749a3b7f1ef2a56617b0a1c7fd623 (patch) | |
tree | 4adc9c167991a9cea04fa9cc3c01e247a50293c8 /api4 | |
parent | 22715a31ed6238eb4f8f0dd8125bf23958345e78 (diff) | |
download | chat-28a78d76074749a3b7f1ef2a56617b0a1c7fd623.tar.gz chat-28a78d76074749a3b7f1ef2a56617b0a1c7fd623.tar.bz2 chat-28a78d76074749a3b7f1ef2a56617b0a1c7fd623.zip |
Implement some channel endpoints for APIv4 (#5846)
* Add v4 endpoint for getting the channels on a team for a user
* Implement PUT /channels/{channel_id}/patch endpoint for APIv4
* Implement POST /teams/{team_id}/channels/search endpoint for APIv4
* Update permission check
Diffstat (limited to 'api4')
-rw-r--r-- | api4/channel.go | 91 | ||||
-rw-r--r-- | api4/channel_test.go | 159 |
2 files changed, 250 insertions, 0 deletions
diff --git a/api4/channel.go b/api4/channel.go index fd33eb882..a19f7d858 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -20,9 +20,12 @@ func InitChannel() { BaseRoutes.Channels.Handle("/members/{user_id:[A-Za-z0-9]+}/view", ApiSessionRequired(viewChannel)).Methods("POST") BaseRoutes.Team.Handle("/channels", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET") + BaseRoutes.Team.Handle("/channels/search", ApiSessionRequired(searchChannelsForTeam)).Methods("POST") + BaseRoutes.User.Handle("/teams/{team_id:[A-Za-z0-9]+}/channels", ApiSessionRequired(getChannelsForTeamForUser)).Methods("GET") BaseRoutes.Channel.Handle("", ApiSessionRequired(getChannel)).Methods("GET") BaseRoutes.Channel.Handle("", ApiSessionRequired(updateChannel)).Methods("PUT") + BaseRoutes.Channel.Handle("/patch", ApiSessionRequired(patchChannel)).Methods("PUT") BaseRoutes.Channel.Handle("", ApiSessionRequired(deleteChannel)).Methods("DELETE") BaseRoutes.Channel.Handle("/stats", ApiSessionRequired(getChannelStats)).Methods("GET") @@ -141,6 +144,37 @@ func updateChannel(c *Context, w http.ResponseWriter, r *http.Request) { } } +func patchChannel(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireChannelId() + if c.Err != nil { + return + } + + patch := model.ChannelPatchFromJson(r.Body) + if patch == nil { + c.SetInvalidParam("channel") + return + } + + oldChannel, err := app.GetChannel(c.Params.ChannelId) + if err != nil { + c.Err = err + return + } + + if !CanManageChannel(c, oldChannel) { + return + } + + if rchannel, err := app.PatchChannel(oldChannel, patch); err != nil { + c.Err = err + return + } else { + c.LogAudit("") + w.Write([]byte(rchannel.ToJson())) + } +} + func CanManageChannel(c *Context, channel *model.Channel) bool { if channel.Type == model.CHANNEL_OPEN && !app.SessionHasPermissionToChannel(c.Session, channel.Id, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_PROPERTIES) { c.SetPermissionError(model.PERMISSION_MANAGE_PUBLIC_CHANNEL_PROPERTIES) @@ -288,6 +322,63 @@ func getPublicChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Request } } +func getChannelsForTeamForUser(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireUserId().RequireTeamId() + if c.Err != nil { + return + } + + if !app.SessionHasPermissionToUser(c.Session, c.Params.UserId) { + c.SetPermissionError(model.PERMISSION_EDIT_OTHER_USERS) + return + } + + if !app.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_VIEW_TEAM) { + c.SetPermissionError(model.PERMISSION_VIEW_TEAM) + return + } + + if channels, err := app.GetChannelsForUser(c.Params.TeamId, c.Params.UserId); err != nil { + c.Err = err + return + } else if HandleEtag(channels.Etag(), "Get Channels", w, r) { + return + } else { + w.Header().Set(model.HEADER_ETAG_SERVER, channels.Etag()) + w.Write([]byte(channels.ToJson())) + } +} + +func searchChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireTeamId() + if c.Err != nil { + return + } + + props := model.ChannelSearchFromJson(r.Body) + if props == nil { + c.SetInvalidParam("channel_search") + return + } + + if len(props.Term) == 0 { + c.SetInvalidParam("term") + return + } + + if !app.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_LIST_TEAM_CHANNELS) { + c.SetPermissionError(model.PERMISSION_LIST_TEAM_CHANNELS) + return + } + + if channels, err := app.SearchChannels(c.Params.TeamId, props.Term); err != nil { + c.Err = err + return + } else { + w.Write([]byte(channels.ToJson())) + } +} + func deleteChannel(c *Context, w http.ResponseWriter, r *http.Request) { c.RequireChannelId() if c.Err != nil { diff --git a/api4/channel_test.go b/api4/channel_test.go index ef0d35e4b..a208313df 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -257,6 +257,62 @@ func TestUpdateChannel(t *testing.T) { } +func TestPatchChannel(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + patch := &model.ChannelPatch{ + Name: new(string), + DisplayName: new(string), + Header: new(string), + Purpose: new(string), + } + *patch.Name = model.NewId() + *patch.DisplayName = model.NewId() + *patch.Header = model.NewId() + *patch.Purpose = model.NewId() + + channel, resp := Client.PatchChannel(th.BasicChannel.Id, patch) + CheckNoError(t, resp) + + if *patch.Name != channel.Name { + t.Fatal("do not match") + } else if *patch.DisplayName != channel.DisplayName { + t.Fatal("do not match") + } else if *patch.Header != channel.Header { + t.Fatal("do not match") + } else if *patch.Purpose != channel.Purpose { + t.Fatal("do not match") + } + + patch.Name = nil + oldName := channel.Name + channel, resp = Client.PatchChannel(th.BasicChannel.Id, patch) + CheckNoError(t, resp) + + if channel.Name != oldName { + t.Fatal("should not have updated") + } + + _, resp = Client.PatchChannel("junk", patch) + CheckBadRequestStatus(t, resp) + + _, resp = Client.PatchChannel(model.NewId(), patch) + CheckNotFoundStatus(t, resp) + + user := th.CreateUser() + Client.Login(user.Email, user.Password) + _, resp = Client.PatchChannel(th.BasicChannel.Id, patch) + CheckForbiddenStatus(t, resp) + + _, resp = th.SystemAdminClient.PatchChannel(th.BasicChannel.Id, patch) + CheckNoError(t, resp) + + _, resp = th.SystemAdminClient.PatchChannel(th.BasicPrivateChannel.Id, patch) + CheckNoError(t, resp) +} + func TestCreateDirectChannel(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() @@ -438,6 +494,109 @@ func TestGetPublicChannelsForTeam(t *testing.T) { CheckNoError(t, resp) } +func TestGetChannelsForTeamForUser(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + channels, resp := Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "") + CheckNoError(t, resp) + + found := make([]bool, 3) + for _, c := range *channels { + if c.Id == th.BasicChannel.Id { + found[0] = true + } else if c.Id == th.BasicChannel2.Id { + found[1] = true + } else if c.Id == th.BasicPrivateChannel.Id { + found[2] = true + } + + if c.TeamId != th.BasicTeam.Id && c.TeamId != "" { + t.Fatal("wrong team") + } + } + + for _, f := range found { + if !f { + t.Fatal("missing a channel") + } + } + + channels, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, resp.Etag) + CheckEtag(t, channels, resp) + + _, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, "junk", "") + CheckBadRequestStatus(t, resp) + + _, resp = Client.GetChannelsForTeamForUser("junk", th.BasicUser.Id, "") + CheckBadRequestStatus(t, resp) + + _, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser2.Id, "") + CheckForbiddenStatus(t, resp) + + _, resp = Client.GetChannelsForTeamForUser(model.NewId(), th.BasicUser.Id, "") + CheckForbiddenStatus(t, resp) + + _, resp = th.SystemAdminClient.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "") + CheckNoError(t, resp) +} + +func TestSearchChannels(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + search := &model.ChannelSearch{Term: th.BasicChannel.Name} + + channels, resp := Client.SearchChannels(th.BasicTeam.Id, search) + CheckNoError(t, resp) + + found := false + for _, c := range *channels { + if c.Type != model.CHANNEL_OPEN { + t.Fatal("should only return public channels") + } + + if c.Id == th.BasicChannel.Id { + found = true + } + } + + if !found { + t.Fatal("didn't find channel") + } + + search.Term = th.BasicPrivateChannel.Name + channels, resp = Client.SearchChannels(th.BasicTeam.Id, search) + CheckNoError(t, resp) + + found = false + for _, c := range *channels { + if c.Id == th.BasicPrivateChannel.Id { + found = true + } + } + + if found { + t.Fatal("shouldn't find private channel") + } + + search.Term = "" + _, resp = Client.SearchChannels(th.BasicTeam.Id, search) + CheckBadRequestStatus(t, resp) + + search.Term = th.BasicChannel.Name + _, resp = Client.SearchChannels(model.NewId(), search) + CheckForbiddenStatus(t, resp) + + _, resp = Client.SearchChannels("junk", search) + CheckBadRequestStatus(t, resp) + + _, resp = th.SystemAdminClient.SearchChannels(th.BasicTeam.Id, search) + CheckNoError(t, resp) +} + func TestDeleteChannel(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() |