Skip to content

Commit da5a0b8

Browse files
authored
add tests for api user orgs (#5494)
* add tests for api user orgs * add permission for admin to list user's orgs even he is a private user of org
1 parent 4b4453c commit da5a0b8

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

integrations/api_user_orgs_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2018 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.package models
4+
5+
package integrations
6+
7+
import (
8+
"fmt"
9+
"net/http"
10+
"testing"
11+
12+
api "code.gitea.io/sdk/gitea"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestUserOrgs(t *testing.T) {
17+
prepareTestEnv(t)
18+
adminUsername := "user1"
19+
normalUsername := "user2"
20+
session := loginUser(t, adminUsername)
21+
token := getTokenForLoggedInUser(t, session)
22+
urlStr := fmt.Sprintf("/api/v1/users/%s/orgs?token=%s", normalUsername, token)
23+
req := NewRequest(t, "GET", urlStr)
24+
resp := session.MakeRequest(t, req, http.StatusOK)
25+
var orgs []*api.Organization
26+
DecodeJSON(t, resp, &orgs)
27+
28+
assert.Equal(t, []*api.Organization{
29+
{
30+
ID: 3,
31+
UserName: "user3",
32+
FullName: "User Three",
33+
AvatarURL: "https://secure.gravatar.com/avatar/97d6d9441ff85fdc730e02a6068d267b?d=identicon",
34+
Description: "",
35+
Website: "",
36+
Location: "",
37+
},
38+
}, orgs)
39+
}
40+
41+
func TestMyOrgs(t *testing.T) {
42+
prepareTestEnv(t)
43+
44+
normalUsername := "user2"
45+
session := loginUser(t, normalUsername)
46+
token := getTokenForLoggedInUser(t, session)
47+
req := NewRequest(t, "GET", "/api/v1/user/orgs?token="+token)
48+
resp := session.MakeRequest(t, req, http.StatusOK)
49+
var orgs []*api.Organization
50+
DecodeJSON(t, resp, &orgs)
51+
52+
assert.Equal(t, []*api.Organization{
53+
{
54+
ID: 3,
55+
UserName: "user3",
56+
FullName: "User Three",
57+
AvatarURL: "https://secure.gravatar.com/avatar/97d6d9441ff85fdc730e02a6068d267b?d=identicon",
58+
Description: "",
59+
Website: "",
60+
Location: "",
61+
},
62+
}, orgs)
63+
}

routers/api/v1/org/org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func ListUserOrgs(ctx *context.APIContext) {
6060
if ctx.Written() {
6161
return
6262
}
63-
listUserOrgs(ctx, u, false)
63+
listUserOrgs(ctx, u, ctx.User.IsAdmin)
6464
}
6565

6666
// Create api for create organization

0 commit comments

Comments
 (0)