add team exists endpoint for APIv4 (#5517)

Этот коммит содержится в:
Carlos Tadeu Panato Junior
2017-03-08 01:30:33 +01:00
коммит произвёл Joram Wilander
родитель fa75e0e7c4
Коммит 19ec4c4298
5 изменённых файлов: 86 добавлений и 6 удалений

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

@@ -203,7 +203,7 @@ test-server: start-docker prepare-enterprise
echo "mode: count" > cover.out
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=1050s -covermode=count -coverprofile=capi.out ./api || exit 1
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=650s -covermode=count -coverprofile=capi4.out ./api4 || exit 1
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=800s -covermode=count -coverprofile=capi4.out ./api4 || exit 1
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=60s -covermode=count -coverprofile=capp.out ./app || exit 1
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=60s -covermode=count -coverprofile=cmodel.out ./model || exit 1
$(GO) test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=180s -covermode=count -coverprofile=cstore.out ./store || exit 1

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

@@ -26,6 +26,7 @@ func InitTeam() {
BaseRoutes.TeamByName.Handle("", ApiSessionRequired(getTeamByName)).Methods("GET")
BaseRoutes.TeamMember.Handle("", ApiSessionRequired(getTeamMember)).Methods("GET")
BaseRoutes.TeamByName.Handle("/exists", ApiSessionRequired(teamExists)).Methods("GET")
BaseRoutes.TeamMember.Handle("/roles", ApiSessionRequired(updateTeamMemberRoles)).Methods("PUT")
}
@@ -72,6 +73,11 @@ func getTeam(c *Context, w http.ResponseWriter, r *http.Request) {
}
func getTeamByName(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireTeamName()
if c.Err != nil {
return
}
if team, err := app.GetTeamByName(c.Params.TeamName); err != nil {
c.Err = err
return
@@ -232,3 +238,21 @@ func getAllTeams(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(model.TeamListToJson(teams)))
}
func teamExists(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireTeamName()
if c.Err != nil {
return
}
resp := make(map[string]bool)
if _, err := app.GetTeamByName(c.Params.TeamName); err != nil {
resp["exists"] = false
} else {
resp["exists"] = true
}
w.Write([]byte(model.MapBoolToJson(resp)))
return
}

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

@@ -126,7 +126,7 @@ func TestGetAllTeams(t *testing.T) {
rrteams, resp := Client.GetAllTeams("", 1, 1)
CheckNoError(t, resp)
if (len(rrteams) != 1) {
if len(rrteams) != 1 {
t.Fatal("wrong number of teams - should be 1")
}
@@ -139,21 +139,21 @@ func TestGetAllTeams(t *testing.T) {
rrteams1, resp := Client.GetAllTeams("", 1, 0)
CheckNoError(t, resp)
if (len(rrteams1) != 0) {
if len(rrteams1) != 0 {
t.Fatal("wrong number of teams - should be 0")
}
rrteams2, resp := th.SystemAdminClient.GetAllTeams("", 1, 1)
CheckNoError(t, resp)
if (len(rrteams2) != 1) {
if len(rrteams2) != 1 {
t.Fatal("wrong number of teams - should be 1")
}
rrteams2, resp = Client.GetAllTeams("", 1, 0)
CheckNoError(t, resp)
if (len(rrteams2) != 0) {
if len(rrteams2) != 0 {
t.Fatal("wrong number of teams - should be 0")
}
@@ -493,4 +493,29 @@ func TestGetMyTeamsUnread(t *testing.T) {
Client.Logout()
_, resp = Client.GetTeamsUnreadForUser(user.Id, "")
CheckUnauthorizedStatus(t, resp)
}
}
func TestTeamExists(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Client := th.Client
team := th.BasicTeam
th.LoginBasic()
exists, resp := Client.TeamExists(team.Name, "")
CheckNoError(t, resp)
if exists != true {
t.Fatal("team should exist")
}
exists, resp = Client.TeamExists("testingteam", "")
CheckNoError(t, resp)
if exists != false {
t.Fatal("team should not exist")
}
Client.Logout()
_, resp = Client.TeamExists(team.Name, "")
CheckUnauthorizedStatus(t, resp)
}

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

@@ -600,6 +600,16 @@ func (c *Client4) GetTeamByName(name, etag string) (*Team, *Response) {
}
}
// TeamExists returns true or false if the team exist or not.
func (c *Client4) TeamExists(name, etag string) (bool, *Response) {
if r, err := c.DoApiGet(c.GetTeamByNameRoute(name)+"/exists", etag); err != nil {
return false, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return MapBoolFromJson(r.Body)["exists"], 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) {

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

@@ -156,6 +156,15 @@ func MapToJson(objmap map[string]string) string {
}
}
// MapToJson converts a map to a json string
func MapBoolToJson(objmap map[string]bool) string {
if b, err := json.Marshal(objmap); err != nil {
return ""
} else {
return string(b)
}
}
// MapFromJson will decode the key/value pair map
func MapFromJson(data io.Reader) map[string]string {
decoder := json.NewDecoder(data)
@@ -168,6 +177,18 @@ func MapFromJson(data io.Reader) map[string]string {
}
}
// MapFromJson will decode the key/value pair map
func MapBoolFromJson(data io.Reader) map[string]bool {
decoder := json.NewDecoder(data)
var objmap map[string]bool
if err := decoder.Decode(&objmap); err != nil {
return make(map[string]bool)
} else {
return objmap
}
}
func ArrayToJson(objmap []string) string {
if b, err := json.Marshal(objmap); err != nil {
return ""