Implementation endpoint for APIv4: GET /teams/name/{name} (#5473)

* APIv4: GET /teams/name/{name}

Signed-off-by: Saturnino Abril <saturnino.abril@gmail.com>

* reorganized test with SystemAdminClient

Signed-off-by: Saturnino Abril <saturnino.abril@gmail.com>
Этот коммит содержится в:
Saturnino Abril
2017-02-21 01:27:57 +09:00
коммит произвёл enahum
родитель 22118fafc8
Коммит 623560481b
4 изменённых файлов: 63 добавлений и 0 удалений

Просмотреть файл

@@ -21,6 +21,7 @@ func InitTeam() {
BaseRoutes.Team.Handle("", ApiSessionRequired(getTeam)).Methods("GET")
BaseRoutes.Team.Handle("/stats", ApiHandler(getTeamStats)).Methods("GET")
BaseRoutes.TeamByName.Handle("", ApiSessionRequired(getTeamByName)).Methods("GET")
BaseRoutes.TeamMember.Handle("", ApiSessionRequired(getTeamMember)).Methods("GET")
}
@@ -66,6 +67,21 @@ func getTeam(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
func getTeamByName(c *Context, w http.ResponseWriter, r *http.Request) {
if team, err := app.GetTeamByName(c.Params.TeamName); err != nil {
c.Err = err
return
} else {
if team.Type != model.TEAM_OPEN && !app.SessionHasPermissionToTeam(c.Session, team.Id, model.PERMISSION_VIEW_TEAM) {
c.SetPermissionError(model.PERMISSION_VIEW_TEAM)
return
}
w.Write([]byte(team.ToJson()))
return
}
}
func getTeamsForUser(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireUserId()
if c.Err != nil {

Просмотреть файл

@@ -114,6 +114,42 @@ func TestGetTeam(t *testing.T) {
CheckNoError(t, resp)
}
func TestGetTeamByName(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Client := th.Client
team := th.BasicTeam
rteam, resp := Client.GetTeamByName(team.Name, "")
CheckNoError(t, resp)
if rteam.Name != team.Name {
t.Fatal("wrong team")
}
_, resp = Client.GetTeamByName("junk", "")
CheckNotFoundStatus(t, resp)
_, resp = Client.GetTeamByName("", "")
CheckNotFoundStatus(t, resp)
Client.Logout()
_, resp = Client.GetTeamByName(team.Name, "")
CheckUnauthorizedStatus(t, resp)
_, resp = th.SystemAdminClient.GetTeamByName(team.Name, "")
CheckNoError(t, resp)
th.LoginTeamAdmin()
team2 := &model.Team{DisplayName: "Name", Name: GenerateTestTeamName(), Email: GenerateTestEmail(), Type: model.TEAM_INVITE}
rteam2, _ := Client.CreateTeam(team2)
th.LoginBasic()
_, resp = Client.GetTeamByName(rteam2.Name, "")
CheckForbiddenStatus(t, resp)
}
func TestGetTeamsForUser(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()

Просмотреть файл

@@ -304,6 +304,7 @@ func GetTeam(teamId string) (*model.Team, *model.AppError) {
func GetTeamByName(name string) (*model.Team, *model.AppError) {
if result := <-Srv.Store.Team().GetByName(name); result.Err != nil {
result.Err.StatusCode = http.StatusNotFound
return nil, result.Err
} else {
return result.Data.(*model.Team), nil

Просмотреть файл

@@ -481,6 +481,16 @@ func (c *Client4) GetTeam(teamId, etag string) (*Team, *Response) {
}
}
// GetTeamByName returns a team based on the provided team name string.
func (c *Client4) GetTeamByName(name, etag string) (*Team, *Response) {
if r, err := c.DoApiGet(c.GetTeamByNameRoute(name), etag); err != nil {
return nil, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return TeamFromJson(r.Body), BuildResponse(r)
}
}
// GetTeamsForUser returns a list of teams a user is on. Must be logged in as the user
// or be a system administrator.
func (c *Client4) GetTeamsForUser(userId, etag string) ([]*Team, *Response) {