diff options
author | Saturnino Abril <saturnino.abril@gmail.com> | 2017-02-18 05:50:17 +0900 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2017-02-17 15:50:17 -0500 |
commit | 00d11e961f6a32f7d4880c40fd6359574d665a4e (patch) | |
tree | ac03d114664413c0f2495815883bc7a861a33b1e | |
parent | 91fe8bb2c0d520f13269b2eadc2717a5ec4eea1c (diff) | |
download | chat-00d11e961f6a32f7d4880c40fd6359574d665a4e.tar.gz chat-00d11e961f6a32f7d4880c40fd6359574d665a4e.tar.bz2 chat-00d11e961f6a32f7d4880c40fd6359574d665a4e.zip |
Implement GET teams/{team_id}/stats - apiv4 (#5453)
* api4 GET teams/{team_id}/stats
* api4 GET teams/{team_id}/stats
* moved TeamStats router to team.go
-rw-r--r-- | api4/apitestlib.go | 14 | ||||
-rw-r--r-- | api4/team.go | 22 | ||||
-rw-r--r-- | api4/team_test.go | 51 | ||||
-rw-r--r-- | model/client4.go | 15 |
4 files changed, 101 insertions, 1 deletions
diff --git a/api4/apitestlib.go b/api4/apitestlib.go index 27bf83f10..84f87d494 100644 --- a/api4/apitestlib.go +++ b/api4/apitestlib.go @@ -260,6 +260,20 @@ func (me *TestHelper) LoginSystemAdminWithClient(client *model.Client4) { utils.EnableDebugLogForTest() } +func (me *TestHelper) UpdateActiveUser(user *model.User, active bool) { + utils.DisableDebugLogForTest() + + _, err := app.UpdateActive(user, active) + if err != nil { + l4g.Error(err.Error()) + l4g.Close() + time.Sleep(time.Second) + panic(err) + } + + utils.EnableDebugLogForTest() +} + func LinkUserToTeam(user *model.User, team *model.Team) { utils.DisableDebugLogForTest() diff --git a/api4/team.go b/api4/team.go index 8564029b0..d1db268dc 100644 --- a/api4/team.go +++ b/api4/team.go @@ -19,9 +19,9 @@ func InitTeam() { BaseRoutes.TeamsForUser.Handle("", ApiSessionRequired(getTeamsForUser)).Methods("GET") BaseRoutes.Team.Handle("", ApiSessionRequired(getTeam)).Methods("GET") + BaseRoutes.Team.Handle("/stats", ApiHandler(getTeamStats)).Methods("GET") BaseRoutes.TeamMember.Handle("", ApiSessionRequired(getTeamMember)).Methods("GET") - } func createTeam(c *Context, w http.ResponseWriter, r *http.Request) { @@ -104,3 +104,23 @@ func getTeamMember(c *Context, w http.ResponseWriter, r *http.Request) { return } } + +func getTeamStats(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_VIEW_TEAM) { + c.SetPermissionError(model.PERMISSION_VIEW_TEAM) + return + } + + if stats, err := app.GetTeamStats(c.Params.TeamId); err != nil { + c.Err = err + return + } else { + w.Write([]byte(stats.ToJson())) + return + } +} diff --git a/api4/team_test.go b/api4/team_test.go index 5c6d64ace..5f7bc7b98 100644 --- a/api4/team_test.go +++ b/api4/team_test.go @@ -192,3 +192,54 @@ func TestGetTeamMember(t *testing.T) { _, resp = th.SystemAdminClient.GetTeamMember(team.Id, user.Id, "") CheckNoError(t, resp) } + +func TestGetTeamStats(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + team := th.BasicTeam + + rstats, resp := Client.GetTeamStats(team.Id, "") + CheckNoError(t, resp) + + if rstats.TeamId != team.Id { + t.Fatal("wrong team id") + } + + if rstats.TotalMemberCount != 3 { + t.Fatal("wrong count") + } + + if rstats.ActiveMemberCount != 3 { + t.Fatal("wrong count") + } + + _, resp = Client.GetTeamStats("junk", "") + CheckBadRequestStatus(t, resp) + + _, resp = Client.GetTeamStats(model.NewId(), "") + CheckForbiddenStatus(t, resp) + + _, resp = th.SystemAdminClient.GetTeamStats(team.Id, "") + CheckNoError(t, resp) + + // deactivate BasicUser2 + th.UpdateActiveUser(th.BasicUser2, false) + + rstats, resp = th.SystemAdminClient.GetTeamStats(team.Id, "") + CheckNoError(t, resp) + + if rstats.TotalMemberCount != 3 { + t.Fatal("wrong count") + } + + if rstats.ActiveMemberCount != 2 { + t.Fatal("wrong count") + } + + // login with different user and test if forbidden + user := th.CreateUser() + Client.Login(user.Email, user.Password) + _, resp = Client.GetTeamStats(th.BasicTeam.Id, "") + CheckForbiddenStatus(t, resp) +} diff --git a/model/client4.go b/model/client4.go index 4b072844d..c4acc6f57 100644 --- a/model/client4.go +++ b/model/client4.go @@ -84,6 +84,10 @@ func (c *Client4) GetTeamMemberRoute(teamId, userId string) string { return fmt.Sprintf(c.GetTeamRoute(teamId)+"/members/%v", userId) } +func (c *Client4) GetTeamStatsRoute(teamId string) string { + return fmt.Sprintf(c.GetTeamRoute(teamId) + "/stats") +} + func (c *Client4) GetChannelsRoute() string { return fmt.Sprintf("/channels") } @@ -498,6 +502,17 @@ func (c *Client4) GetTeamMember(teamId, userId, etag string) (*TeamMember, *Resp } } +// GetTeamStats returns a team stats based on the team id string. +// Must be authenticated. +func (c *Client4) GetTeamStats(teamId, etag string) (*TeamStats, *Response) { + if r, err := c.DoApiGet(c.GetTeamStatsRoute(teamId), etag); err != nil { + return nil, &Response{StatusCode: r.StatusCode, Error: err} + } else { + defer closeBody(r) + return TeamStatsFromJson(r.Body), BuildResponse(r) + } +} + // Channel Section // CreateChannel creates a channel based on the provided channel struct. |