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.

158 lines
6.5 KiB

  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. "net/http"
  7. "net/url"
  8. "path/filepath"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/setting"
  14. api "code.gitea.io/gitea/modules/structs"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func getExpectedContentsListResponseForContents(ref, refType string) []*api.ContentsResponse {
  18. treePath := "README.md"
  19. sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
  20. selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=" + ref
  21. htmlURL := setting.AppURL + "user2/repo1/src/" + refType + "/" + ref + "/" + treePath
  22. gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
  23. downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath
  24. return []*api.ContentsResponse{
  25. {
  26. Name: filepath.Base(treePath),
  27. Path: treePath,
  28. SHA: sha,
  29. Type: "file",
  30. Size: 30,
  31. URL: &selfURL,
  32. HTMLURL: &htmlURL,
  33. GitURL: &gitURL,
  34. DownloadURL: &downloadURL,
  35. Links: &api.FileLinksResponse{
  36. Self: &selfURL,
  37. GitURL: &gitURL,
  38. HTMLURL: &htmlURL,
  39. },
  40. },
  41. }
  42. }
  43. func TestAPIGetContentsList(t *testing.T) {
  44. onGiteaRun(t, testAPIGetContentsList)
  45. }
  46. func testAPIGetContentsList(t *testing.T, u *url.URL) {
  47. /*** SETUP ***/
  48. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  49. user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
  50. user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
  51. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  52. repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
  53. repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
  54. treePath := "" // root dir
  55. // Get user2's token
  56. session := loginUser(t, user2.Name)
  57. token2 := getTokenForLoggedInUser(t, session)
  58. session = emptyTestSession(t)
  59. // Get user4's token
  60. session = loginUser(t, user4.Name)
  61. token4 := getTokenForLoggedInUser(t, session)
  62. session = emptyTestSession(t)
  63. // Make a new branch in repo1
  64. newBranch := "test_branch"
  65. repo1.CreateNewBranch(user2, repo1.DefaultBranch, newBranch)
  66. // Get the commit ID of the default branch
  67. gitRepo, _ := git.OpenRepository(repo1.RepoPath())
  68. defer gitRepo.Close()
  69. commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
  70. // Make a new tag in repo1
  71. newTag := "test_tag"
  72. gitRepo.CreateTag(newTag, commitID)
  73. /*** END SETUP ***/
  74. // ref is default ref
  75. ref := repo1.DefaultBranch
  76. refType := "branch"
  77. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  78. resp := session.MakeRequest(t, req, http.StatusOK)
  79. var contentsListResponse []*api.ContentsResponse
  80. DecodeJSON(t, resp, &contentsListResponse)
  81. assert.NotNil(t, contentsListResponse)
  82. expectedContentsListResponse := getExpectedContentsListResponseForContents(ref, refType)
  83. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  84. // No ref
  85. refType = "branch"
  86. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath)
  87. resp = session.MakeRequest(t, req, http.StatusOK)
  88. DecodeJSON(t, resp, &contentsListResponse)
  89. assert.NotNil(t, contentsListResponse)
  90. expectedContentsListResponse = getExpectedContentsListResponseForContents(repo1.DefaultBranch, refType)
  91. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  92. // ref is the branch we created above in setup
  93. ref = newBranch
  94. refType = "branch"
  95. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  96. resp = session.MakeRequest(t, req, http.StatusOK)
  97. DecodeJSON(t, resp, &contentsListResponse)
  98. assert.NotNil(t, contentsListResponse)
  99. expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType)
  100. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  101. // ref is the new tag we created above in setup
  102. ref = newTag
  103. refType = "tag"
  104. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  105. resp = session.MakeRequest(t, req, http.StatusOK)
  106. DecodeJSON(t, resp, &contentsListResponse)
  107. assert.NotNil(t, contentsListResponse)
  108. expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType)
  109. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  110. // ref is a commit
  111. ref = commitID
  112. refType = "commit"
  113. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  114. resp = session.MakeRequest(t, req, http.StatusOK)
  115. DecodeJSON(t, resp, &contentsListResponse)
  116. assert.NotNil(t, contentsListResponse)
  117. expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType)
  118. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  119. // Test file contents a file with a bad ref
  120. ref = "badref"
  121. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  122. resp = session.MakeRequest(t, req, http.StatusInternalServerError)
  123. expectedAPIError := context.APIError{
  124. Message: "object does not exist [id: " + ref + ", rel_path: ]",
  125. URL: setting.API.SwaggerURL,
  126. }
  127. var apiError context.APIError
  128. DecodeJSON(t, resp, &apiError)
  129. assert.Equal(t, expectedAPIError, apiError)
  130. // Test accessing private ref with user token that does not have access - should fail
  131. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  132. session.MakeRequest(t, req, http.StatusNotFound)
  133. // Test access private ref of owner of token
  134. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/readme.md?token=%s", user2.Name, repo16.Name, token2)
  135. session.MakeRequest(t, req, http.StatusOK)
  136. // Test access of org user3 private repo file by owner user2
  137. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  138. session.MakeRequest(t, req, http.StatusOK)
  139. }