You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_org_test.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "testing"
  10. auth_model "code.gitea.io/gitea/models/auth"
  11. "code.gitea.io/gitea/models/db"
  12. org_model "code.gitea.io/gitea/models/organization"
  13. "code.gitea.io/gitea/models/perm"
  14. unit_model "code.gitea.io/gitea/models/unit"
  15. "code.gitea.io/gitea/models/unittest"
  16. user_model "code.gitea.io/gitea/models/user"
  17. "code.gitea.io/gitea/modules/setting"
  18. api "code.gitea.io/gitea/modules/structs"
  19. "code.gitea.io/gitea/tests"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. func TestAPIOrgCreate(t *testing.T) {
  23. onGiteaRun(t, func(*testing.T, *url.URL) {
  24. token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization)
  25. 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. Visibility: "limited",
  32. }
  33. req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &org).
  34. AddTokenAuth(token)
  35. resp := MakeRequest(t, req, http.StatusCreated)
  36. var apiOrg api.Organization
  37. DecodeJSON(t, resp, &apiOrg)
  38. assert.Equal(t, org.UserName, apiOrg.Name)
  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. assert.Equal(t, org.Visibility, apiOrg.Visibility)
  44. unittest.AssertExistsAndLoadBean(t, &user_model.User{
  45. Name: org.UserName,
  46. LowerName: strings.ToLower(org.UserName),
  47. FullName: org.FullName,
  48. })
  49. // Check owner team permission
  50. ownerTeam, _ := org_model.GetOwnerTeam(db.DefaultContext, apiOrg.ID)
  51. for _, ut := range unit_model.AllRepoUnitTypes {
  52. up := perm.AccessModeOwner
  53. if ut == unit_model.TypeExternalTracker || ut == unit_model.TypeExternalWiki {
  54. up = perm.AccessModeRead
  55. }
  56. unittest.AssertExistsAndLoadBean(t, &org_model.TeamUnit{
  57. OrgID: apiOrg.ID,
  58. TeamID: ownerTeam.ID,
  59. Type: ut,
  60. AccessMode: up,
  61. })
  62. }
  63. req = NewRequestf(t, "GET", "/api/v1/orgs/%s", org.UserName).
  64. AddTokenAuth(token)
  65. resp = MakeRequest(t, req, http.StatusOK)
  66. DecodeJSON(t, resp, &apiOrg)
  67. assert.EqualValues(t, org.UserName, apiOrg.Name)
  68. req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", org.UserName).
  69. AddTokenAuth(token)
  70. resp = MakeRequest(t, req, http.StatusOK)
  71. var repos []*api.Repository
  72. DecodeJSON(t, resp, &repos)
  73. for _, repo := range repos {
  74. assert.False(t, repo.Private)
  75. }
  76. req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", org.UserName).
  77. AddTokenAuth(token)
  78. resp = MakeRequest(t, req, http.StatusOK)
  79. // user1 on this org is public
  80. var users []*api.User
  81. DecodeJSON(t, resp, &users)
  82. assert.Len(t, users, 1)
  83. assert.EqualValues(t, "user1", users[0].UserName)
  84. })
  85. }
  86. func TestAPIOrgEdit(t *testing.T) {
  87. onGiteaRun(t, func(*testing.T, *url.URL) {
  88. session := loginUser(t, "user1")
  89. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization)
  90. org := api.EditOrgOption{
  91. FullName: "Org3 organization new full name",
  92. Description: "A new description",
  93. Website: "https://try.gitea.io/new",
  94. Location: "Beijing",
  95. Visibility: "private",
  96. }
  97. req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org).
  98. AddTokenAuth(token)
  99. resp := MakeRequest(t, req, http.StatusOK)
  100. var apiOrg api.Organization
  101. DecodeJSON(t, resp, &apiOrg)
  102. assert.Equal(t, "org3", apiOrg.Name)
  103. assert.Equal(t, org.FullName, apiOrg.FullName)
  104. assert.Equal(t, org.Description, apiOrg.Description)
  105. assert.Equal(t, org.Website, apiOrg.Website)
  106. assert.Equal(t, org.Location, apiOrg.Location)
  107. assert.Equal(t, org.Visibility, apiOrg.Visibility)
  108. })
  109. }
  110. func TestAPIOrgEditBadVisibility(t *testing.T) {
  111. onGiteaRun(t, func(*testing.T, *url.URL) {
  112. session := loginUser(t, "user1")
  113. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization)
  114. org := api.EditOrgOption{
  115. FullName: "Org3 organization new full name",
  116. Description: "A new description",
  117. Website: "https://try.gitea.io/new",
  118. Location: "Beijing",
  119. Visibility: "badvisibility",
  120. }
  121. req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org).
  122. AddTokenAuth(token)
  123. MakeRequest(t, req, http.StatusUnprocessableEntity)
  124. })
  125. }
  126. func TestAPIOrgDeny(t *testing.T) {
  127. onGiteaRun(t, func(*testing.T, *url.URL) {
  128. setting.Service.RequireSignInView = true
  129. defer func() {
  130. setting.Service.RequireSignInView = false
  131. }()
  132. orgName := "user1_org"
  133. req := NewRequestf(t, "GET", "/api/v1/orgs/%s", orgName)
  134. MakeRequest(t, req, http.StatusNotFound)
  135. req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", orgName)
  136. MakeRequest(t, req, http.StatusNotFound)
  137. req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", orgName)
  138. MakeRequest(t, req, http.StatusNotFound)
  139. })
  140. }
  141. func TestAPIGetAll(t *testing.T) {
  142. defer tests.PrepareTestEnv(t)()
  143. token := getUserToken(t, "user1", auth_model.AccessTokenScopeReadOrganization)
  144. // accessing with a token will return all orgs
  145. req := NewRequest(t, "GET", "/api/v1/orgs").
  146. AddTokenAuth(token)
  147. resp := MakeRequest(t, req, http.StatusOK)
  148. var apiOrgList []*api.Organization
  149. DecodeJSON(t, resp, &apiOrgList)
  150. assert.Len(t, apiOrgList, 12)
  151. assert.Equal(t, "Limited Org 36", apiOrgList[1].FullName)
  152. assert.Equal(t, "limited", apiOrgList[1].Visibility)
  153. // accessing without a token will return only public orgs
  154. req = NewRequest(t, "GET", "/api/v1/orgs")
  155. resp = MakeRequest(t, req, http.StatusOK)
  156. DecodeJSON(t, resp, &apiOrgList)
  157. assert.Len(t, apiOrgList, 8)
  158. assert.Equal(t, "org 17", apiOrgList[0].FullName)
  159. assert.Equal(t, "public", apiOrgList[0].Visibility)
  160. }
  161. func TestAPIOrgSearchEmptyTeam(t *testing.T) {
  162. onGiteaRun(t, func(*testing.T, *url.URL) {
  163. token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization)
  164. orgName := "org_with_empty_team"
  165. // create org
  166. req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &api.CreateOrgOption{
  167. UserName: orgName,
  168. }).AddTokenAuth(token)
  169. MakeRequest(t, req, http.StatusCreated)
  170. // create team with no member
  171. req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/orgs/%s/teams", orgName), &api.CreateTeamOption{
  172. Name: "Empty",
  173. IncludesAllRepositories: true,
  174. Permission: "read",
  175. Units: []string{"repo.code", "repo.issues", "repo.ext_issues", "repo.wiki", "repo.pulls"},
  176. }).AddTokenAuth(token)
  177. MakeRequest(t, req, http.StatusCreated)
  178. // case-insensitive search for teams that have no members
  179. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/orgs/%s/teams/search?q=%s", orgName, "empty")).
  180. AddTokenAuth(token)
  181. resp := MakeRequest(t, req, http.StatusOK)
  182. data := struct {
  183. Ok bool
  184. Data []*api.Team
  185. }{}
  186. DecodeJSON(t, resp, &data)
  187. assert.True(t, data.Ok)
  188. if assert.Len(t, data.Data, 1) {
  189. assert.EqualValues(t, "Empty", data.Data[0].Name)
  190. }
  191. })
  192. }