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.

187 lines
6.1 KiB

  1. // Copyright 2017 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. "testing"
  9. "code.gitea.io/gitea/models"
  10. api "code.gitea.io/sdk/gitea"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestAPIUserReposNotLogin(t *testing.T) {
  14. prepareTestEnv(t)
  15. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  16. req := NewRequestf(t, "GET", "/api/v1/users/%s/repos", user.Name)
  17. resp := MakeRequest(t, req, http.StatusOK)
  18. var apiRepos []api.Repository
  19. DecodeJSON(t, resp, &apiRepos)
  20. expectedLen := models.GetCount(t, models.Repository{OwnerID: user.ID},
  21. models.Cond("is_private = ?", false))
  22. assert.Len(t, apiRepos, expectedLen)
  23. for _, repo := range apiRepos {
  24. assert.EqualValues(t, user.ID, repo.Owner.ID)
  25. assert.False(t, repo.Private)
  26. }
  27. }
  28. func TestAPISearchRepo(t *testing.T) {
  29. prepareTestEnv(t)
  30. const keyword = "test"
  31. req := NewRequestf(t, "GET", "/api/v1/repos/search?q=%s", keyword)
  32. resp := MakeRequest(t, req, http.StatusOK)
  33. var body api.SearchResults
  34. DecodeJSON(t, resp, &body)
  35. assert.NotEmpty(t, body.Data)
  36. for _, repo := range body.Data {
  37. assert.Contains(t, repo.Name, keyword)
  38. assert.False(t, repo.Private)
  39. }
  40. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 15}).(*models.User)
  41. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 16}).(*models.User)
  42. orgUser := models.AssertExistsAndLoadBean(t, &models.User{ID: 17}).(*models.User)
  43. // Map of expected results, where key is user for login
  44. type expectedResults map[*models.User]struct {
  45. count int
  46. repoOwnerID int64
  47. repoName string
  48. includesPrivate bool
  49. }
  50. testCases := []struct {
  51. name, requestURL string
  52. expectedResults
  53. }{
  54. {name: "RepositoriesMax50", requestURL: "/api/v1/repos/search?limit=50", expectedResults: expectedResults{
  55. nil: {count: 12},
  56. user: {count: 12},
  57. user2: {count: 12}},
  58. },
  59. {name: "RepositoriesMax10", requestURL: "/api/v1/repos/search?limit=10", expectedResults: expectedResults{
  60. nil: {count: 10},
  61. user: {count: 10},
  62. user2: {count: 10}},
  63. },
  64. {name: "RepositoriesDefaultMax10", requestURL: "/api/v1/repos/search", expectedResults: expectedResults{
  65. nil: {count: 10},
  66. user: {count: 10},
  67. user2: {count: 10}},
  68. },
  69. {name: "RepositoriesByName", requestURL: fmt.Sprintf("/api/v1/repos/search?q=%s", "big_test_"), expectedResults: expectedResults{
  70. nil: {count: 4, repoName: "big_test_"},
  71. user: {count: 4, repoName: "big_test_"},
  72. user2: {count: 4, repoName: "big_test_"}},
  73. },
  74. {name: "RepositoriesAccessibleAndRelatedToUser", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", user.ID), expectedResults: expectedResults{
  75. // FIXME: Should return 4 (all public repositories related to "another" user = owned + collaborative), now returns only public repositories directly owned by user
  76. nil: {count: 2},
  77. user: {count: 8, includesPrivate: true},
  78. // FIXME: Should return 4 (all public repositories related to "another" user = owned + collaborative), now returns only public repositories directly owned by user
  79. user2: {count: 2}},
  80. },
  81. {name: "RepositoriesAccessibleAndRelatedToUser2", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", user2.ID), expectedResults: expectedResults{
  82. nil: {count: 1},
  83. user: {count: 1},
  84. user2: {count: 2, includesPrivate: true}},
  85. },
  86. {name: "RepositoriesOwnedByOrganization", requestURL: fmt.Sprintf("/api/v1/repos/search?uid=%d", orgUser.ID), expectedResults: expectedResults{
  87. nil: {count: 1, repoOwnerID: orgUser.ID},
  88. user: {count: 2, repoOwnerID: orgUser.ID, includesPrivate: true},
  89. user2: {count: 1, repoOwnerID: orgUser.ID}},
  90. },
  91. }
  92. for _, testCase := range testCases {
  93. t.Run(testCase.name, func(t *testing.T) {
  94. for userToLogin, expected := range testCase.expectedResults {
  95. var session *TestSession
  96. var testName string
  97. if userToLogin != nil && userToLogin.ID > 0 {
  98. testName = fmt.Sprintf("LoggedUser%d", userToLogin.ID)
  99. session = loginUser(t, userToLogin.Name)
  100. } else {
  101. testName = "AnonymousUser"
  102. session = emptyTestSession(t)
  103. }
  104. t.Run(testName, func(t *testing.T) {
  105. request := NewRequest(t, "GET", testCase.requestURL)
  106. response := session.MakeRequest(t, request, http.StatusOK)
  107. var body api.SearchResults
  108. DecodeJSON(t, response, &body)
  109. assert.Len(t, body.Data, expected.count)
  110. for _, repo := range body.Data {
  111. assert.NotEmpty(t, repo.Name)
  112. if len(expected.repoName) > 0 {
  113. assert.Contains(t, repo.Name, expected.repoName)
  114. }
  115. if expected.repoOwnerID > 0 {
  116. assert.Equal(t, expected.repoOwnerID, repo.Owner.ID)
  117. }
  118. if !expected.includesPrivate {
  119. assert.False(t, repo.Private)
  120. }
  121. }
  122. })
  123. }
  124. })
  125. }
  126. }
  127. func TestAPIViewRepo(t *testing.T) {
  128. prepareTestEnv(t)
  129. req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1")
  130. resp := MakeRequest(t, req, http.StatusOK)
  131. var repo api.Repository
  132. DecodeJSON(t, resp, &repo)
  133. assert.EqualValues(t, 1, repo.ID)
  134. assert.EqualValues(t, "repo1", repo.Name)
  135. }
  136. func TestAPIOrgRepos(t *testing.T) {
  137. prepareTestEnv(t)
  138. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  139. // User3 is an Org. Check their repos.
  140. sourceOrg := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User)
  141. // Login as User2.
  142. session := loginUser(t, user.Name)
  143. req := NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", sourceOrg.Name)
  144. resp := session.MakeRequest(t, req, http.StatusOK)
  145. var apiRepos []*api.Repository
  146. DecodeJSON(t, resp, &apiRepos)
  147. expectedLen := models.GetCount(t, models.Repository{OwnerID: sourceOrg.ID},
  148. models.Cond("is_private = ?", false))
  149. assert.Len(t, apiRepos, expectedLen)
  150. for _, repo := range apiRepos {
  151. assert.False(t, repo.Private)
  152. }
  153. }
  154. func TestAPIGetRepoByIDUnauthorized(t *testing.T) {
  155. prepareTestEnv(t)
  156. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User)
  157. sess := loginUser(t, user.Name)
  158. req := NewRequestf(t, "GET", "/api/v1/repositories/2")
  159. sess.MakeRequest(t, req, http.StatusNotFound)
  160. }