More robust team exists api endpoint (#12130)

* More robust team exists api endpoint

* Making the code more concise

* Better handling of errors on GetTeamByName
Этот коммит содержится в:
Jesús Espino
2019-09-30 21:39:21 +02:00
коммит произвёл GitHub
родитель 89b7b2d99b
Коммит 8cea561ba6
5 изменённых файлов: 115 добавлений и 27 удалений

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

@@ -812,14 +812,31 @@ func teamExists(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
resp := make(map[string]bool) team, err := c.App.GetTeamByName(c.Params.TeamName)
if err != nil && err.StatusCode != http.StatusNotFound {
if _, err := c.App.GetTeamByName(c.Params.TeamName); err != nil { c.Err = err
resp["exists"] = false return
} else {
resp["exists"] = true
} }
exists := false
if team != nil {
var teamMember *model.TeamMember
teamMember, err = c.App.GetTeamMember(team.Id, c.App.Session.UserId)
if err != nil && err.StatusCode != http.StatusNotFound {
c.Err = err
return
}
// Verify that the user can see the team (be a member or have the permission to list the team)
if (teamMember != nil && teamMember.DeleteAt == 0) ||
(team.AllowOpenInvite && c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_LIST_PUBLIC_TEAMS)) ||
(!team.AllowOpenInvite && c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_LIST_PRIVATE_TEAMS)) {
exists = true
}
}
resp := map[string]bool{"exists": exists}
w.Write([]byte(model.MapBoolToJson(resp))) w.Write([]byte(model.MapBoolToJson(resp)))
} }

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

@@ -2102,25 +2102,94 @@ func TestTeamExists(t *testing.T) {
th := Setup().InitBasic() th := Setup().InitBasic()
defer th.TearDown() defer th.TearDown()
Client := th.Client Client := th.Client
team := th.BasicTeam public_member_team := th.BasicTeam
err := th.App.UpdateTeamPrivacy(public_member_team.Id, model.TEAM_OPEN, true)
require.Nil(t, err)
th.LoginBasic() public_not_member_team := th.CreateTeamWithClient(th.SystemAdminClient)
err = th.App.UpdateTeamPrivacy(public_not_member_team.Id, model.TEAM_OPEN, true)
require.Nil(t, err)
exists, resp := Client.TeamExists(team.Name, "") private_member_team := th.CreateTeamWithClient(th.SystemAdminClient)
CheckNoError(t, resp) th.LinkUserToTeam(th.BasicUser, private_member_team)
if !exists { err = th.App.UpdateTeamPrivacy(private_member_team.Id, model.TEAM_INVITE, false)
t.Fatal("team should exist") require.Nil(t, err)
}
exists, resp = Client.TeamExists("testingteam", "") private_not_member_team := th.CreateTeamWithClient(th.SystemAdminClient)
CheckNoError(t, resp) err = th.App.UpdateTeamPrivacy(private_not_member_team.Id, model.TEAM_INVITE, false)
if exists { require.Nil(t, err)
t.Fatal("team should not exist")
}
Client.Logout() // Check the appropriate permissions are enforced.
_, resp = Client.TeamExists(team.Name, "") defaultRolePermissions := th.SaveDefaultRolePermissions()
CheckUnauthorizedStatus(t, resp) defer func() {
th.RestoreDefaultRolePermissions(defaultRolePermissions)
}()
th.AddPermissionToRole(model.PERMISSION_LIST_PUBLIC_TEAMS.Id, model.SYSTEM_USER_ROLE_ID)
th.AddPermissionToRole(model.PERMISSION_LIST_PRIVATE_TEAMS.Id, model.SYSTEM_USER_ROLE_ID)
t.Run("Logged user with permissions and valid public team", func(t *testing.T) {
th.LoginBasic()
exists, resp := Client.TeamExists(public_not_member_team.Name, "")
CheckNoError(t, resp)
assert.True(t, exists, "team should exist")
})
t.Run("Logged user with permissions and valid private team", func(t *testing.T) {
th.LoginBasic()
exists, resp := Client.TeamExists(private_not_member_team.Name, "")
CheckNoError(t, resp)
assert.True(t, exists, "team should exist")
})
t.Run("Logged user and invalid team", func(t *testing.T) {
th.LoginBasic()
exists, resp := Client.TeamExists("testingteam", "")
CheckNoError(t, resp)
assert.False(t, exists, "team should not exist")
})
t.Run("Logged out user", func(t *testing.T) {
Client.Logout()
_, resp := Client.TeamExists(public_not_member_team.Name, "")
CheckUnauthorizedStatus(t, resp)
})
t.Run("Logged without LIST_PUBLIC_TEAMS permissions and member public team", func(t *testing.T) {
th.LoginBasic()
th.RemovePermissionFromRole(model.PERMISSION_LIST_PUBLIC_TEAMS.Id, model.SYSTEM_USER_ROLE_ID)
exists, resp := Client.TeamExists(public_member_team.Name, "")
CheckNoError(t, resp)
assert.True(t, exists, "team should be visible")
})
t.Run("Logged without LIST_PUBLIC_TEAMS permissions and not member public team", func(t *testing.T) {
th.LoginBasic()
th.RemovePermissionFromRole(model.PERMISSION_LIST_PUBLIC_TEAMS.Id, model.SYSTEM_USER_ROLE_ID)
exists, resp := Client.TeamExists(public_not_member_team.Name, "")
CheckNoError(t, resp)
assert.False(t, exists, "team should not be visible")
})
t.Run("Logged without LIST_PRIVATE_TEAMS permissions and member private team", func(t *testing.T) {
th.LoginBasic()
th.RemovePermissionFromRole(model.PERMISSION_LIST_PRIVATE_TEAMS.Id, model.SYSTEM_USER_ROLE_ID)
exists, resp := Client.TeamExists(private_member_team.Name, "")
CheckNoError(t, resp)
assert.True(t, exists, "team should be visible")
})
t.Run("Logged without LIST_PRIVATE_TEAMS permissions and not member private team", func(t *testing.T) {
th.LoginBasic()
th.RemovePermissionFromRole(model.PERMISSION_LIST_PRIVATE_TEAMS.Id, model.SYSTEM_USER_ROLE_ID)
exists, resp := Client.TeamExists(private_not_member_team.Name, "")
CheckNoError(t, resp)
assert.False(t, exists, "team should not be visible")
})
} }
func TestImportTeam(t *testing.T) { func TestImportTeam(t *testing.T) {

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

@@ -643,12 +643,7 @@ func (a *App) GetTeam(teamId string) (*model.Team, *model.AppError) {
} }
func (a *App) GetTeamByName(name string) (*model.Team, *model.AppError) { func (a *App) GetTeamByName(name string) (*model.Team, *model.AppError) {
team, err := a.Srv.Store.Team().GetByName(name) return a.Srv.Store.Team().GetByName(name)
if err != nil {
err.StatusCode = http.StatusNotFound
return nil, err
}
return team, nil
} }
func (a *App) GetTeamByInviteId(inviteId string) (*model.Team, *model.AppError) { func (a *App) GetTeamByInviteId(inviteId string) (*model.Team, *model.AppError) {

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

@@ -6770,6 +6770,10 @@
"id": "store.sql_team.get_by_name.app_error", "id": "store.sql_team.get_by_name.app_error",
"translation": "Unable to find the existing team" "translation": "Unable to find the existing team"
}, },
{
"id": "store.sql_team.get_by_name.missing.app_error",
"translation": "Unable to find the existing team"
},
{ {
"id": "store.sql_team.get_by_scheme.app_error", "id": "store.sql_team.get_by_scheme.app_error",
"translation": "Unable to get the channels for the provided scheme" "translation": "Unable to get the channels for the provided scheme"

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

@@ -285,6 +285,9 @@ func (s SqlTeamStore) GetByName(name string) (*model.Team, *model.AppError) {
err := s.GetReplica().SelectOne(&team, "SELECT * FROM Teams WHERE Name = :Name", map[string]interface{}{"Name": name}) err := s.GetReplica().SelectOne(&team, "SELECT * FROM Teams WHERE Name = :Name", map[string]interface{}{"Name": name})
if err != nil { if err != nil {
if err == sql.ErrNoRows {
return nil, model.NewAppError("SqlTeamStore.GetByName", "store.sql_team.get_by_name.missing.app_error", nil, "name="+name+","+err.Error(), http.StatusNotFound)
}
return nil, model.NewAppError("SqlTeamStore.GetByName", "store.sql_team.get_by_name.app_error", nil, "name="+name+", "+err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlTeamStore.GetByName", "store.sql_team.get_by_name.app_error", nil, "name="+name+", "+err.Error(), http.StatusInternalServerError)
} }
return &team, nil return &team, nil