您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

api_repo_get_contents_test.go 7.6KB

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