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.

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