From e9a3301dc9be9f2ab9cd5970d9eeb64a83e32234 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 13 Sep 2019 21:19:41 +0800 Subject: [PATCH 1/7] fix team user api --- go.mod | 2 +- integrations/api_team_user_test.go | 36 ++++++++++++++++++++++++++++++ routers/api/v1/org/team.go | 9 ++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 integrations/api_team_user_test.go diff --git a/go.mod b/go.mod index 41e2297934f46..f5d49294d4361 100644 --- a/go.mod +++ b/go.mod @@ -68,8 +68,8 @@ require ( github.com/lib/pq v1.2.0 github.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96 github.com/lunny/levelqueue v0.0.0-20190217115915-02b525a4418e - github.com/markbates/goth v1.56.0 github.com/mailru/easyjson v0.7.0 // indirect + github.com/markbates/goth v1.56.0 github.com/mattn/go-isatty v0.0.7 github.com/mattn/go-oci8 v0.0.0-20190320171441-14ba190cf52d // indirect github.com/mattn/go-sqlite3 v1.11.0 diff --git a/integrations/api_team_user_test.go b/integrations/api_team_user_test.go new file mode 100644 index 0000000000000..1cb6a4862a472 --- /dev/null +++ b/integrations/api_team_user_test.go @@ -0,0 +1,36 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integrations + +import ( + "net/http" + "testing" + + "code.gitea.io/gitea/models" + api "code.gitea.io/gitea/modules/structs" + "github.com/stretchr/testify/assert" +) + +func TestAPITeamUser(t *testing.T) { + prepareTestEnv(t) + + normalUsername := "user2" + session := loginUser(t, normalUsername) + token := getTokenForLoggedInUser(t, session) + req := NewRequest(t, "GET", "/api/v1/teams/1/members/user1?token="+token) + session.MakeRequest(t, req, http.StatusNotFound) + + req = NewRequest(t, "GET", "/api/v1/teams/1/members/user2?token="+token) + resp := session.MakeRequest(t, req, http.StatusOK) + var user2 *api.User + DecodeJSON(t, resp, &user2) + user := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) + + assert.Equal(t, &api.User{ + ID: user.ID, + Name: user.Name, + FullName: user.FullName, + }, user2) +} diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index 3357c072517de..7cd52eb3beb48 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -287,6 +287,15 @@ func GetTeamMember(ctx *context.APIContext) { if ctx.Written() { return } + teamID := ctx.ParamsInt64("id") + isTeamMember, err := models.IsUserInTeams(u.ID, []int64{teamID}) + if err != nil { + ctx.Error(500, "IsUserInTeams", err) + return + } else if !isTeamMember { + ctx.NotFound() + return + } ctx.JSON(200, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin)) } From 5f675f81fc288097dfa45e2f7d6b543b59a98d54 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 13 Sep 2019 21:29:27 +0800 Subject: [PATCH 2/7] fix tests --- integrations/api_team_user_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integrations/api_team_user_test.go b/integrations/api_team_user_test.go index 1cb6a4862a472..00e7d7c3557ff 100644 --- a/integrations/api_team_user_test.go +++ b/integrations/api_team_user_test.go @@ -30,7 +30,7 @@ func TestAPITeamUser(t *testing.T) { assert.Equal(t, &api.User{ ID: user.ID, - Name: user.Name, + UserName: user.Name, FullName: user.FullName, }, user2) } From 0a823f52c997ca47464dd38f334ed780af5f4e63 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 13 Sep 2019 23:08:00 +0800 Subject: [PATCH 3/7] fix api --- routers/api/v1/org/team.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index 7cd52eb3beb48..7b8fd12fba9a5 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -287,7 +287,7 @@ func GetTeamMember(ctx *context.APIContext) { if ctx.Written() { return } - teamID := ctx.ParamsInt64("id") + teamID := ctx.ParamsInt64("teamid") isTeamMember, err := models.IsUserInTeams(u.ID, []int64{teamID}) if err != nil { ctx.Error(500, "IsUserInTeams", err) From 31737a0dbdcd4b7572a836446ea09c81cc88ff22 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 14 Sep 2019 10:16:24 +0800 Subject: [PATCH 4/7] fix team user api --- integrations/api_team_user_test.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/integrations/api_team_user_test.go b/integrations/api_team_user_test.go index 00e7d7c3557ff..c4148b5149b56 100644 --- a/integrations/api_team_user_test.go +++ b/integrations/api_team_user_test.go @@ -28,9 +28,5 @@ func TestAPITeamUser(t *testing.T) { DecodeJSON(t, resp, &user2) user := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) - assert.Equal(t, &api.User{ - ID: user.ID, - UserName: user.Name, - FullName: user.FullName, - }, user2) + assert.Equal(t, user.APIFormat(), user2) } From b5aeb98c7293f6c5044de34b856990e588b84046 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 14 Sep 2019 11:02:41 +0800 Subject: [PATCH 5/7] change user convert --- integrations/api_team_user_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/integrations/api_team_user_test.go b/integrations/api_team_user_test.go index c4148b5149b56..1ca75ffff7129 100644 --- a/integrations/api_team_user_test.go +++ b/integrations/api_team_user_test.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/routers/api/v1/convert" "github.com/stretchr/testify/assert" ) @@ -28,5 +29,5 @@ func TestAPITeamUser(t *testing.T) { DecodeJSON(t, resp, &user2) user := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) - assert.Equal(t, user.APIFormat(), user2) + assert.Equal(t, convert.ToUser(user, true, true), user2) } From 99addc1115f2e126f1f64f3582d68451e1ffa241 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 14 Sep 2019 14:22:33 +0800 Subject: [PATCH 6/7] fix tests --- integrations/api_team_user_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integrations/api_team_user_test.go b/integrations/api_team_user_test.go index 1ca75ffff7129..5b9c753019111 100644 --- a/integrations/api_team_user_test.go +++ b/integrations/api_team_user_test.go @@ -29,5 +29,5 @@ func TestAPITeamUser(t *testing.T) { DecodeJSON(t, resp, &user2) user := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) - assert.Equal(t, convert.ToUser(user, true, true), user2) + assert.Equal(t, convert.ToUser(user, true, false), user2) } From 4d700c036f9285bb396e5965c5f733c8868bd2d1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 15 Sep 2019 15:32:18 +0800 Subject: [PATCH 7/7] fix tests --- integrations/api_team_user_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/integrations/api_team_user_test.go b/integrations/api_team_user_test.go index 5b9c753019111..70d52c13601d6 100644 --- a/integrations/api_team_user_test.go +++ b/integrations/api_team_user_test.go @@ -7,6 +7,7 @@ package integrations import ( "net/http" "testing" + "time" "code.gitea.io/gitea/models" api "code.gitea.io/gitea/modules/structs" @@ -27,6 +28,8 @@ func TestAPITeamUser(t *testing.T) { resp := session.MakeRequest(t, req, http.StatusOK) var user2 *api.User DecodeJSON(t, resp, &user2) + user2.Created = user2.Created.In(time.Local) + user2.LastLogin = user2.LastLogin.In(time.Local) user := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) assert.Equal(t, convert.ToUser(user, true, false), user2)