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_repo_get_contents_test.go 6.6KB

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