@@ -6,43 +6,87 @@ package integrations
6
6
7
7
import (
8
8
"net/http"
9
+ "net/url"
9
10
"strings"
10
11
"testing"
11
12
12
13
"code.gitea.io/gitea/models"
14
+ "code.gitea.io/gitea/modules/setting"
13
15
api "code.gitea.io/sdk/gitea"
14
16
15
17
"github.com/stretchr/testify/assert"
16
18
)
17
19
18
20
func TestAPIOrg (t * testing.T ) {
19
- prepareTestEnv (t )
20
-
21
- session := loginUser (t , "user1" )
22
-
23
- token := getTokenForLoggedInUser (t , session )
24
- var org = api.CreateOrgOption {
25
- UserName : "user1_org" ,
26
- FullName : "User1's organization" ,
27
- Description : "This organization created by user1" ,
28
- Website : "https://try.gitea.io" ,
29
- Location : "Shanghai" ,
30
- }
31
- req := NewRequestWithJSON (t , "POST" , "/api/v1/orgs?token=" + token , & org )
32
- resp := session .MakeRequest (t , req , http .StatusCreated )
33
-
34
- var apiOrg api.Organization
35
- DecodeJSON (t , resp , & apiOrg )
36
-
37
- assert .Equal (t , org .UserName , apiOrg .UserName )
38
- assert .Equal (t , org .FullName , apiOrg .FullName )
39
- assert .Equal (t , org .Description , apiOrg .Description )
40
- assert .Equal (t , org .Website , apiOrg .Website )
41
- assert .Equal (t , org .Location , apiOrg .Location )
42
-
43
- models .AssertExistsAndLoadBean (t , & models.User {
44
- Name : org .UserName ,
45
- LowerName : strings .ToLower (org .UserName ),
46
- FullName : org .FullName ,
21
+ onGiteaRun (t , func (* testing.T , * url.URL ) {
22
+ session := loginUser (t , "user1" )
23
+
24
+ token := getTokenForLoggedInUser (t , session )
25
+ var org = api.CreateOrgOption {
26
+ UserName : "user1_org" ,
27
+ FullName : "User1's organization" ,
28
+ Description : "This organization created by user1" ,
29
+ Website : "https://try.gitea.io" ,
30
+ Location : "Shanghai" ,
31
+ }
32
+ req := NewRequestWithJSON (t , "POST" , "/api/v1/orgs?token=" + token , & org )
33
+ resp := session .MakeRequest (t , req , http .StatusCreated )
34
+
35
+ var apiOrg api.Organization
36
+ DecodeJSON (t , resp , & apiOrg )
37
+
38
+ assert .Equal (t , org .UserName , apiOrg .UserName )
39
+ assert .Equal (t , org .FullName , apiOrg .FullName )
40
+ assert .Equal (t , org .Description , apiOrg .Description )
41
+ assert .Equal (t , org .Website , apiOrg .Website )
42
+ assert .Equal (t , org .Location , apiOrg .Location )
43
+
44
+ models .AssertExistsAndLoadBean (t , & models.User {
45
+ Name : org .UserName ,
46
+ LowerName : strings .ToLower (org .UserName ),
47
+ FullName : org .FullName ,
48
+ })
49
+
50
+ req = NewRequestf (t , "GET" , "/api/v1/orgs/%s" , org .UserName )
51
+ resp = session .MakeRequest (t , req , http .StatusOK )
52
+ DecodeJSON (t , resp , & apiOrg )
53
+ assert .EqualValues (t , org .UserName , apiOrg .UserName )
54
+
55
+ req = NewRequestf (t , "GET" , "/api/v1/orgs/%s/repos" , org .UserName )
56
+ resp = session .MakeRequest (t , req , http .StatusOK )
57
+
58
+ var repos []* api.Repository
59
+ DecodeJSON (t , resp , & repos )
60
+ for _ , repo := range repos {
61
+ assert .False (t , repo .Private )
62
+ }
63
+
64
+ req = NewRequestf (t , "GET" , "/api/v1/orgs/%s/members" , org .UserName )
65
+ resp = session .MakeRequest (t , req , http .StatusOK )
66
+
67
+ // user1 on this org is public
68
+ var users []* api.User
69
+ DecodeJSON (t , resp , & users )
70
+ assert .EqualValues (t , 1 , len (users ))
71
+ assert .EqualValues (t , "user1" , users [0 ].UserName )
72
+ })
73
+ }
74
+
75
+ func TestAPIOrgDeny (t * testing.T ) {
76
+ onGiteaRun (t , func (* testing.T , * url.URL ) {
77
+ setting .Service .RequireSignInView = true
78
+ defer func () {
79
+ setting .Service .RequireSignInView = false
80
+ }()
81
+
82
+ var orgName = "user1_org"
83
+ req := NewRequestf (t , "GET" , "/api/v1/orgs/%s" , orgName )
84
+ MakeRequest (t , req , http .StatusNotFound )
85
+
86
+ req = NewRequestf (t , "GET" , "/api/v1/orgs/%s/repos" , orgName )
87
+ MakeRequest (t , req , http .StatusNotFound )
88
+
89
+ req = NewRequestf (t , "GET" , "/api/v1/orgs/%s/members" , orgName )
90
+ MakeRequest (t , req , http .StatusNotFound )
47
91
})
48
92
}
0 commit comments