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 6.9KB

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