Skip to content

Commit 267fbbf

Browse files
authored
Fix team user api (#8172) (#8188)
* fix team user api * fix tests * fix api * fix team user api * change user convert * fix tests * fix tests
1 parent 9e8df4b commit 267fbbf

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

integrations/api_team_user_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"testing"
10+
"time"
11+
12+
"code.gitea.io/gitea/models"
13+
api "code.gitea.io/gitea/modules/structs"
14+
"code.gitea.io/gitea/routers/api/v1/convert"
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func TestAPITeamUser(t *testing.T) {
19+
prepareTestEnv(t)
20+
21+
normalUsername := "user2"
22+
session := loginUser(t, normalUsername)
23+
token := getTokenForLoggedInUser(t, session)
24+
req := NewRequest(t, "GET", "/api/v1/teams/1/members/user1?token="+token)
25+
session.MakeRequest(t, req, http.StatusNotFound)
26+
27+
req = NewRequest(t, "GET", "/api/v1/teams/1/members/user2?token="+token)
28+
resp := session.MakeRequest(t, req, http.StatusOK)
29+
var user2 *api.User
30+
DecodeJSON(t, resp, &user2)
31+
user2.Created = user2.Created.In(time.Local)
32+
user2.LastLogin = user2.LastLogin.In(time.Local)
33+
user := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User)
34+
35+
assert.Equal(t, convert.ToUser(user, true, false), user2)
36+
}

routers/api/v1/org/team.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@ func GetTeamMember(ctx *context.APIContext) {
288288
if ctx.Written() {
289289
return
290290
}
291+
teamID := ctx.ParamsInt64("teamid")
292+
isTeamMember, err := models.IsUserInTeams(u.ID, []int64{teamID})
293+
if err != nil {
294+
ctx.Error(500, "IsUserInTeams", err)
295+
return
296+
} else if !isTeamMember {
297+
ctx.NotFound()
298+
return
299+
}
291300
ctx.JSON(200, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
292301
}
293302

0 commit comments

Comments
 (0)