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.

org_test.go 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. package integrations
  5. import (
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestOrgRepos(t *testing.T) {
  14. defer prepareTestEnv(t)()
  15. var (
  16. users = []string{"user1", "user2"}
  17. cases = map[string][]string{
  18. "alphabetically": {"repo21", "repo3", "repo5"},
  19. "reversealphabetically": {"repo5", "repo3", "repo21"},
  20. }
  21. )
  22. for _, user := range users {
  23. t.Run(user, func(t *testing.T) {
  24. session := loginUser(t, user)
  25. for sortBy, repos := range cases {
  26. req := NewRequest(t, "GET", "/user3?sort="+sortBy)
  27. resp := session.MakeRequest(t, req, http.StatusOK)
  28. htmlDoc := NewHTMLParser(t, resp.Body)
  29. sel := htmlDoc.doc.Find("a.name")
  30. assert.Len(t, repos, len(sel.Nodes))
  31. for i := 0; i < len(repos); i++ {
  32. assert.EqualValues(t, repos[i], strings.TrimSpace(sel.Eq(i).Text()))
  33. }
  34. }
  35. })
  36. }
  37. }
  38. func TestLimitedOrg(t *testing.T) {
  39. defer prepareTestEnv(t)()
  40. // not logged in user
  41. req := NewRequest(t, "GET", "/limited_org")
  42. MakeRequest(t, req, http.StatusNotFound)
  43. req = NewRequest(t, "GET", "/limited_org/public_repo_on_limited_org")
  44. MakeRequest(t, req, http.StatusNotFound)
  45. req = NewRequest(t, "GET", "/limited_org/private_repo_on_limited_org")
  46. MakeRequest(t, req, http.StatusNotFound)
  47. // login non-org member user
  48. session := loginUser(t, "user2")
  49. req = NewRequest(t, "GET", "/limited_org")
  50. session.MakeRequest(t, req, http.StatusOK)
  51. req = NewRequest(t, "GET", "/limited_org/public_repo_on_limited_org")
  52. session.MakeRequest(t, req, http.StatusOK)
  53. req = NewRequest(t, "GET", "/limited_org/private_repo_on_limited_org")
  54. session.MakeRequest(t, req, http.StatusNotFound)
  55. // site admin
  56. session = loginUser(t, "user1")
  57. req = NewRequest(t, "GET", "/limited_org")
  58. session.MakeRequest(t, req, http.StatusOK)
  59. req = NewRequest(t, "GET", "/limited_org/public_repo_on_limited_org")
  60. session.MakeRequest(t, req, http.StatusOK)
  61. req = NewRequest(t, "GET", "/limited_org/private_repo_on_limited_org")
  62. session.MakeRequest(t, req, http.StatusOK)
  63. }
  64. func TestPrivateOrg(t *testing.T) {
  65. defer prepareTestEnv(t)()
  66. // not logged in user
  67. req := NewRequest(t, "GET", "/privated_org")
  68. MakeRequest(t, req, http.StatusNotFound)
  69. req = NewRequest(t, "GET", "/privated_org/public_repo_on_private_org")
  70. MakeRequest(t, req, http.StatusNotFound)
  71. req = NewRequest(t, "GET", "/privated_org/private_repo_on_private_org")
  72. MakeRequest(t, req, http.StatusNotFound)
  73. // login non-org member user
  74. session := loginUser(t, "user2")
  75. req = NewRequest(t, "GET", "/privated_org")
  76. session.MakeRequest(t, req, http.StatusNotFound)
  77. req = NewRequest(t, "GET", "/privated_org/public_repo_on_private_org")
  78. session.MakeRequest(t, req, http.StatusNotFound)
  79. req = NewRequest(t, "GET", "/privated_org/private_repo_on_private_org")
  80. session.MakeRequest(t, req, http.StatusNotFound)
  81. // non-org member who is collaborator on repo in private org
  82. session = loginUser(t, "user4")
  83. req = NewRequest(t, "GET", "/privated_org")
  84. session.MakeRequest(t, req, http.StatusNotFound)
  85. req = NewRequest(t, "GET", "/privated_org/public_repo_on_private_org") // colab of this repo
  86. session.MakeRequest(t, req, http.StatusOK)
  87. req = NewRequest(t, "GET", "/privated_org/private_repo_on_private_org")
  88. session.MakeRequest(t, req, http.StatusNotFound)
  89. // site admin
  90. session = loginUser(t, "user1")
  91. req = NewRequest(t, "GET", "/privated_org")
  92. session.MakeRequest(t, req, http.StatusOK)
  93. req = NewRequest(t, "GET", "/privated_org/public_repo_on_private_org")
  94. session.MakeRequest(t, req, http.StatusOK)
  95. req = NewRequest(t, "GET", "/privated_org/private_repo_on_private_org")
  96. session.MakeRequest(t, req, http.StatusOK)
  97. }
  98. func TestOrgRestrictedUser(t *testing.T) {
  99. defer prepareTestEnv(t)()
  100. // privated_org is a private org who has id 23
  101. orgName := "privated_org"
  102. // public_repo_on_private_org is a public repo on privated_org
  103. repoName := "public_repo_on_private_org"
  104. // user29 is a restricted user who is not a member of the organization
  105. restrictedUser := "user29"
  106. // #17003 reports a bug whereby adding a restricted user to a read-only team doesn't work
  107. // assert restrictedUser cannot see the org or the public repo
  108. restrictedSession := loginUser(t, restrictedUser)
  109. req := NewRequest(t, "GET", fmt.Sprintf("/%s", orgName))
  110. restrictedSession.MakeRequest(t, req, http.StatusNotFound)
  111. req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s", orgName, repoName))
  112. restrictedSession.MakeRequest(t, req, http.StatusNotFound)
  113. // Therefore create a read-only team
  114. adminSession := loginUser(t, "user1")
  115. token := getTokenForLoggedInUser(t, adminSession)
  116. teamToCreate := &api.CreateTeamOption{
  117. Name: "codereader",
  118. Description: "Code Reader",
  119. IncludesAllRepositories: true,
  120. Permission: "read",
  121. Units: []string{"repo.code"},
  122. }
  123. req = NewRequestWithJSON(t, "POST",
  124. fmt.Sprintf("/api/v1/orgs/%s/teams?token=%s", orgName, token), teamToCreate)
  125. var apiTeam api.Team
  126. resp := adminSession.MakeRequest(t, req, http.StatusCreated)
  127. DecodeJSON(t, resp, &apiTeam)
  128. checkTeamResponse(t, &apiTeam, teamToCreate.Name, teamToCreate.Description, teamToCreate.IncludesAllRepositories,
  129. teamToCreate.Permission, teamToCreate.Units)
  130. checkTeamBean(t, apiTeam.ID, teamToCreate.Name, teamToCreate.Description, teamToCreate.IncludesAllRepositories,
  131. teamToCreate.Permission, teamToCreate.Units)
  132. //teamID := apiTeam.ID
  133. // Now we need to add the restricted user to the team
  134. req = NewRequest(t, "PUT",
  135. fmt.Sprintf("/api/v1/teams/%d/members/%s?token=%s", apiTeam.ID, restrictedUser, token))
  136. _ = adminSession.MakeRequest(t, req, http.StatusNoContent)
  137. // Now we need to check if the restrictedUser can access the repo
  138. req = NewRequest(t, "GET", fmt.Sprintf("/%s", orgName))
  139. restrictedSession.MakeRequest(t, req, http.StatusOK)
  140. req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s", orgName, repoName))
  141. restrictedSession.MakeRequest(t, req, http.StatusOK)
  142. }