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
Этот коммит содержится в:
Carlos Tadeu Panato Junior
2016-12-08 16:21:43 +01:00
коммит произвёл Harrison Healey
родитель b57c9fec89
Коммит d402b1d010
5 изменённых файлов: 128 добавлений и 0 удалений

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

@@ -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 {

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

@@ -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")
}
}

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

@@ -1769,6 +1769,18 @@ func (c *Client) GetTeamStats(teamId string) (*Result, *AppError) {
}
}
// GetTeamStats will return a team stats object containing the number of users on the team
// based on the team id provided. Must be authenticated.
func (c *Client) GetTeamByName(teamName string) (*Result, *AppError) {
if r, err := c.DoApiGet(fmt.Sprintf("/teams/name/%v", teamName), "", ""); err != nil {
return nil, err
} else {
defer closeBody(r)
return &Result{r.Header.Get(HEADER_REQUEST_ID),
r.Header.Get(HEADER_ETAG_SERVER), TeamStatsFromJson(r.Body)}, nil
}
}
// GetTeamMembersByIds will return team member objects as an array based on the
// team id and a list of user ids provided. Must be authenticated.
func (c *Client) GetTeamMembersByIds(teamId string, userIds []string) (*Result, *AppError) {

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

@@ -520,6 +520,15 @@ export default class Client {
end(this.handleResponse.bind(this, 'findTeamByName', success, error));
}
getTeamByName(teamName, success, error) {
request.
get(`${this.getTeamsRoute()}/name/${teamName}`).
set(this.defaultHeaders).
type('application/json').
accept('application/json').
end(this.handleResponse.bind(this, 'getTeamByName', success, error));
}
createTeam(team, success, error) {
request.
post(`${this.getTeamsRoute()}/create`).

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

@@ -328,5 +328,21 @@ describe('Client.Team', function() {
);
});
});
it('getTeamByName', function(done) {
TestHelper.initBasic(() => {
TestHelper.basicClient().getTeamByName(
TestHelper.basicTeam().name,
function(data) {
console.log(data);
assert.equal(data.name, TestHelper.basicTeam().name);
done();
},
function(err) {
done(new Error(err.message));
}
);
});
});
});