From d402b1d010a56256f15bb482684c18b10ed4bcc5 Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Thu, 8 Dec 2016 16:21:43 +0100 Subject: Add API call to get a team by its name (#4690) * Add API call to get a team by its name * add tests for client side and update route regex * remove action * add check for permissions and create tests --- api/team.go | 22 ++++++++++++++++++ api/team_test.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) (limited to 'api') diff --git a/api/team.go b/api/team.go index 3fc367ac0..1d4de6b9e 100644 --- a/api/team.go +++ b/api/team.go @@ -31,6 +31,7 @@ func InitTeam() { BaseRoutes.Teams.Handle("/all_team_listings", ApiUserRequired(GetAllTeamListings)).Methods("GET") BaseRoutes.Teams.Handle("/get_invite_info", ApiAppHandler(getInviteInfo)).Methods("POST") BaseRoutes.Teams.Handle("/find_team_by_name", ApiAppHandler(findTeamByName)).Methods("POST") + BaseRoutes.Teams.Handle("/name/{team_name:[A-Za-z0-9\\-]+}", ApiAppHandler(getTeamByName)).Methods("GET") BaseRoutes.NeedTeam.Handle("/me", ApiUserRequired(getMyTeam)).Methods("GET") BaseRoutes.NeedTeam.Handle("/stats", ApiUserRequired(getTeamStats)).Methods("GET") @@ -701,6 +702,27 @@ func findTeamByName(c *Context, w http.ResponseWriter, r *http.Request) { } } +func getTeamByName(c *Context, w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + teamname := params["team_name"] + + if result := <-Srv.Store.Team().GetByName(teamname); result.Err != nil { + c.Err = result.Err + return + } else { + team := result.Data.(*model.Team) + + if team.Type != model.TEAM_OPEN && c.Session.GetTeamByTeamId(team.Id) == nil { + if !HasPermissionToContext(c, model.PERMISSION_MANAGE_SYSTEM) { + return + } + } + + w.Write([]byte(team.ToJson())) + return + } +} + func InviteMembers(team *model.Team, senderName string, invites []string) { for _, invite := range invites { if len(invite) > 0 { diff --git a/api/team_test.go b/api/team_test.go index bb7d19e4f..5880ffcff 100644 --- a/api/team_test.go +++ b/api/team_test.go @@ -797,3 +797,72 @@ func TestUpdateTeamDescription(t *testing.T) { t.Fatal(err) } } + +func TestGetTeamByName(t *testing.T) { + th := Setup().InitSystemAdmin().InitBasic() + th.BasicClient.Logout() + Client := th.BasicClient + + team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "success+" + model.NewId() + "@simulator.amazonses.com", Type: model.TEAM_INVITE} + team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team) + + team2 := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "success+" + model.NewId() + "@simulator.amazonses.com", Type: model.TEAM_OPEN} + team2 = Client.Must(Client.CreateTeam(team2)).Data.(*model.Team) + + user := &model.User{Email: team.Email, Nickname: "My Testing", Password: "passwd1"} + user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User) + LinkUserToTeam(user, team) + store.Must(Srv.Store.User().VerifyEmail(user.Id)) + + Client.Login(user.Email, "passwd1") + if _, err := Client.GetTeamByName(team.Name); err != nil { + t.Fatal("Failed to get team") + } + + if _, err := Client.GetTeamByName("InvalidTeamName"); err == nil { + t.Fatal("Should not exist this team") + } + + if _, err := Client.GetTeamByName(team2.Name); err != nil { + t.Fatal("Failed to get team") + } + + Client.Must(Client.Logout()) + + user2 := &model.User{Email: "success+" + model.NewId() + "@simulator.amazonses.com", Nickname: "Jabba the Hutt", Password: "passwd1"} + user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User) + store.Must(Srv.Store.User().VerifyEmail(user2.Id)) + + Client.Login(user2.Email, "passwd1") + + // TEAM_INVITE and user is not part of the team + if _, err := Client.GetTeamByName(team.Name); err == nil { + t.Fatal("Should not fail dont have permissions to get the team") + } + + if _, err := Client.GetTeamByName("InvalidTeamName"); err == nil { + t.Fatal("Should not exist this team") + } + + // TEAM_OPEN and user is not part of the team + if _, err := Client.GetTeamByName(team2.Name); err != nil { + t.Fatal("Should not fail team is open") + } + + Client.Must(Client.Logout()) + th.BasicClient.Logout() + th.LoginSystemAdmin() + + if _, err := th.SystemAdminClient.GetTeamByName(team.Name); err != nil { + t.Fatal("Should not failed to get team the user is admin") + } + + if _, err := th.SystemAdminClient.GetTeamByName(team2.Name); err != nil { + t.Fatal("Should not failed to get team the user is admin and team is open") + } + + if _, err := Client.GetTeamByName("InvalidTeamName"); err == nil { + t.Fatal("Should not exist this team") + } + +} -- cgit v1.2.3-1-g7c22