gitea源码

api_repo_get_contents_test.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. "slices"
  9. "testing"
  10. "time"
  11. auth_model "code.gitea.io/gitea/models/auth"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. "code.gitea.io/gitea/models/unittest"
  14. user_model "code.gitea.io/gitea/models/user"
  15. "code.gitea.io/gitea/modules/gitrepo"
  16. "code.gitea.io/gitea/modules/setting"
  17. api "code.gitea.io/gitea/modules/structs"
  18. "code.gitea.io/gitea/modules/util"
  19. repo_service "code.gitea.io/gitea/services/repository"
  20. "github.com/stretchr/testify/assert"
  21. "github.com/stretchr/testify/require"
  22. )
  23. func getExpectedContentsResponseForContents(ref, refType, lastCommitSHA string) *api.ContentsResponse {
  24. treePath := "README.md"
  25. selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=" + ref
  26. htmlURL := setting.AppURL + "user2/repo1/src/" + refType + "/" + ref + "/" + treePath
  27. gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f"
  28. return &api.ContentsResponse{
  29. Name: treePath,
  30. Path: treePath,
  31. SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
  32. LastCommitSHA: util.ToPointer(lastCommitSHA),
  33. LastCommitterDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))),
  34. LastAuthorDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))),
  35. Type: "file",
  36. Size: 30,
  37. Encoding: util.ToPointer("base64"),
  38. Content: util.ToPointer("IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"),
  39. URL: &selfURL,
  40. HTMLURL: &htmlURL,
  41. GitURL: &gitURL,
  42. DownloadURL: util.ToPointer(setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath),
  43. Links: &api.FileLinksResponse{
  44. Self: &selfURL,
  45. GitURL: &gitURL,
  46. HTMLURL: &htmlURL,
  47. },
  48. }
  49. }
  50. func TestAPIGetContents(t *testing.T) {
  51. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  52. testAPIGetContentsRefFormats(t)
  53. testAPIGetContents(t, u)
  54. testAPIGetContentsExt(t)
  55. })
  56. }
  57. func testAPIGetContents(t *testing.T, u *url.URL) {
  58. /*** SETUP ***/
  59. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo1 & repo16
  60. org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) // owner of the repo3, is an org
  61. user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) // owner of neither repos
  62. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo
  63. repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) // public repo
  64. repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}) // private repo
  65. treePath := "README.md"
  66. // Get user2's token
  67. session := loginUser(t, user2.Name)
  68. token2 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
  69. // Get user4's token
  70. session = loginUser(t, user4.Name)
  71. token4 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
  72. // Get the commit ID of the default branch
  73. gitRepo, err := gitrepo.OpenRepository(t.Context(), repo1)
  74. require.NoError(t, err)
  75. defer gitRepo.Close()
  76. // Make a new branch in repo1
  77. newBranch := "test_branch"
  78. err = repo_service.CreateNewBranch(t.Context(), user2, repo1, gitRepo, repo1.DefaultBranch, newBranch)
  79. require.NoError(t, err)
  80. commitID, err := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
  81. require.NoError(t, err)
  82. // Make a new tag in repo1
  83. newTag := "test_tag"
  84. err = gitRepo.CreateTag(newTag, commitID)
  85. require.NoError(t, err)
  86. /*** END SETUP ***/
  87. // not found
  88. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/no-such/file.md", user2.Name, repo1.Name)
  89. resp := MakeRequest(t, req, http.StatusNotFound)
  90. assert.Contains(t, resp.Body.String(), "object does not exist [id: , rel_path: no-such]")
  91. // ref is default ref
  92. ref := repo1.DefaultBranch
  93. refType := "branch"
  94. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  95. resp = MakeRequest(t, req, http.StatusOK)
  96. var contentsResponse api.ContentsResponse
  97. DecodeJSON(t, resp, &contentsResponse)
  98. lastCommit, _ := gitRepo.GetCommitByPath("README.md")
  99. expectedContentsResponse := getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String())
  100. assert.Equal(t, *expectedContentsResponse, contentsResponse)
  101. // No ref
  102. refType = "branch"
  103. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath)
  104. resp = MakeRequest(t, req, http.StatusOK)
  105. DecodeJSON(t, resp, &contentsResponse)
  106. expectedContentsResponse = getExpectedContentsResponseForContents(repo1.DefaultBranch, refType, lastCommit.ID.String())
  107. assert.Equal(t, *expectedContentsResponse, contentsResponse)
  108. // ref is the branch we created above in setup
  109. ref = newBranch
  110. refType = "branch"
  111. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  112. resp = MakeRequest(t, req, http.StatusOK)
  113. DecodeJSON(t, resp, &contentsResponse)
  114. branchCommit, _ := gitRepo.GetBranchCommit(ref)
  115. lastCommit, _ = branchCommit.GetCommitByPath("README.md")
  116. expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String())
  117. assert.Equal(t, *expectedContentsResponse, contentsResponse)
  118. // ref is the new tag we created above in setup
  119. ref = newTag
  120. refType = "tag"
  121. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  122. resp = MakeRequest(t, req, http.StatusOK)
  123. DecodeJSON(t, resp, &contentsResponse)
  124. tagCommit, _ := gitRepo.GetTagCommit(ref)
  125. lastCommit, _ = tagCommit.GetCommitByPath("README.md")
  126. expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String())
  127. assert.Equal(t, *expectedContentsResponse, contentsResponse)
  128. // ref is a commit
  129. ref = commitID
  130. refType = "commit"
  131. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  132. resp = MakeRequest(t, req, http.StatusOK)
  133. DecodeJSON(t, resp, &contentsResponse)
  134. expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType, commitID)
  135. assert.Equal(t, *expectedContentsResponse, contentsResponse)
  136. // Test file contents a file with a bad ref
  137. ref = "badref"
  138. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  139. MakeRequest(t, req, http.StatusNotFound)
  140. // Test accessing private ref with user token that does not have access - should fail
  141. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath).
  142. AddTokenAuth(token4)
  143. MakeRequest(t, req, http.StatusNotFound)
  144. // Test access private ref of owner of token
  145. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/readme.md", user2.Name, repo16.Name).
  146. AddTokenAuth(token2)
  147. MakeRequest(t, req, http.StatusOK)
  148. // Test access of org org3 private repo file by owner user2
  149. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", org3.Name, repo3.Name, treePath).
  150. AddTokenAuth(token2)
  151. MakeRequest(t, req, http.StatusOK)
  152. }
  153. func testAPIGetContentsRefFormats(t *testing.T) {
  154. file := "README.md"
  155. sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
  156. content := "# repo1\n\nDescription for repo1"
  157. resp := MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/"+file), http.StatusOK)
  158. raw, err := io.ReadAll(resp.Body)
  159. assert.NoError(t, err)
  160. assert.Equal(t, content, string(raw))
  161. resp = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/"+sha+"/"+file), http.StatusOK)
  162. raw, err = io.ReadAll(resp.Body)
  163. assert.NoError(t, err)
  164. assert.Equal(t, content, string(raw))
  165. resp = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/"+file+"?ref="+sha), http.StatusOK)
  166. raw, err = io.ReadAll(resp.Body)
  167. assert.NoError(t, err)
  168. assert.Equal(t, content, string(raw))
  169. resp = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/"+file+"?ref=master"), http.StatusOK)
  170. raw, err = io.ReadAll(resp.Body)
  171. assert.NoError(t, err)
  172. assert.Equal(t, content, string(raw))
  173. _ = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/docs/README.md?ref=main"), http.StatusNotFound)
  174. _ = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/README.md?ref=main"), http.StatusOK)
  175. _ = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/docs/README.md?ref=sub-home-md-img-check"), http.StatusOK)
  176. _ = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/README.md?ref=sub-home-md-img-check"), http.StatusNotFound)
  177. // FIXME: this is an incorrect behavior, non-existing branch falls back to default branch
  178. _ = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/README.md?ref=no-such"), http.StatusOK)
  179. }
  180. func testAPIGetContentsExt(t *testing.T) {
  181. session := loginUser(t, "user2")
  182. token2 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  183. t.Run("DirContents", func(t *testing.T) {
  184. req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/contents-ext?ref=sub-home-md-img-check")
  185. resp := MakeRequest(t, req, http.StatusOK)
  186. var contentsResponse api.ContentsExtResponse
  187. DecodeJSON(t, resp, &contentsResponse)
  188. assert.Nil(t, contentsResponse.FileContents)
  189. assert.NotNil(t, contentsResponse.DirContents)
  190. req = NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/contents-ext/.?ref=sub-home-md-img-check")
  191. resp = MakeRequest(t, req, http.StatusOK)
  192. contentsResponse = api.ContentsExtResponse{}
  193. DecodeJSON(t, resp, &contentsResponse)
  194. assert.Nil(t, contentsResponse.FileContents)
  195. assert.NotNil(t, contentsResponse.DirContents)
  196. req = NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/contents-ext/docs?ref=sub-home-md-img-check")
  197. resp = MakeRequest(t, req, http.StatusOK)
  198. contentsResponse = api.ContentsExtResponse{}
  199. DecodeJSON(t, resp, &contentsResponse)
  200. assert.Nil(t, contentsResponse.FileContents)
  201. assert.Equal(t, "README.md", contentsResponse.DirContents[0].Name)
  202. assert.Nil(t, contentsResponse.DirContents[0].Encoding)
  203. assert.Nil(t, contentsResponse.DirContents[0].Content)
  204. assert.Nil(t, contentsResponse.DirContents[0].LastCommitSHA)
  205. assert.Nil(t, contentsResponse.DirContents[0].LastCommitMessage)
  206. // "includes=file_content" shouldn't affect directory listing
  207. req = NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/contents-ext/docs?ref=sub-home-md-img-check&includes=file_content")
  208. resp = MakeRequest(t, req, http.StatusOK)
  209. contentsResponse = api.ContentsExtResponse{}
  210. DecodeJSON(t, resp, &contentsResponse)
  211. assert.Nil(t, contentsResponse.FileContents)
  212. assert.Equal(t, "README.md", contentsResponse.DirContents[0].Name)
  213. assert.Nil(t, contentsResponse.DirContents[0].Encoding)
  214. assert.Nil(t, contentsResponse.DirContents[0].Content)
  215. req = NewRequestf(t, "GET", "/api/v1/repos/user2/lfs/contents-ext?includes=file_content,lfs_metadata").AddTokenAuth(token2)
  216. resp = session.MakeRequest(t, req, http.StatusOK)
  217. contentsResponse = api.ContentsExtResponse{}
  218. DecodeJSON(t, resp, &contentsResponse)
  219. assert.Nil(t, contentsResponse.FileContents)
  220. respFileIdx := slices.IndexFunc(contentsResponse.DirContents, func(response *api.ContentsResponse) bool { return response.Name == "jpeg.jpg" })
  221. require.NotEqual(t, -1, respFileIdx)
  222. respFile := contentsResponse.DirContents[respFileIdx]
  223. assert.Equal(t, "jpeg.jpg", respFile.Name)
  224. assert.Nil(t, respFile.Encoding)
  225. assert.Nil(t, respFile.Content)
  226. assert.Equal(t, util.ToPointer(int64(107)), respFile.LfsSize)
  227. assert.Equal(t, util.ToPointer("0b8d8b5f15046343fd32f451df93acc2bdd9e6373be478b968e4cad6b6647351"), respFile.LfsOid)
  228. })
  229. t.Run("FileContents", func(t *testing.T) {
  230. // by default, no file content or commit info is returned
  231. req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/contents-ext/docs/README.md?ref=sub-home-md-img-check")
  232. resp := MakeRequest(t, req, http.StatusOK)
  233. var contentsResponse api.ContentsExtResponse
  234. DecodeJSON(t, resp, &contentsResponse)
  235. assert.Nil(t, contentsResponse.DirContents)
  236. assert.Equal(t, "README.md", contentsResponse.FileContents.Name)
  237. assert.Nil(t, contentsResponse.FileContents.Encoding)
  238. assert.Nil(t, contentsResponse.FileContents.Content)
  239. assert.Nil(t, contentsResponse.FileContents.LastCommitSHA)
  240. assert.Nil(t, contentsResponse.FileContents.LastCommitMessage)
  241. // file content is only returned when `includes=file_content`
  242. req = NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/contents-ext/docs/README.md?ref=sub-home-md-img-check&includes=file_content,commit_metadata,commit_message")
  243. resp = MakeRequest(t, req, http.StatusOK)
  244. contentsResponse = api.ContentsExtResponse{}
  245. DecodeJSON(t, resp, &contentsResponse)
  246. assert.Nil(t, contentsResponse.DirContents)
  247. assert.Equal(t, "README.md", contentsResponse.FileContents.Name)
  248. assert.NotNil(t, contentsResponse.FileContents.Encoding)
  249. assert.NotNil(t, contentsResponse.FileContents.Content)
  250. assert.Equal(t, "4649299398e4d39a5c09eb4f534df6f1e1eb87cc", *contentsResponse.FileContents.LastCommitSHA)
  251. assert.Equal(t, "Test how READMEs render images when found in a subfolder\n", *contentsResponse.FileContents.LastCommitMessage)
  252. req = NewRequestf(t, "GET", "/api/v1/repos/user2/lfs/contents-ext/jpeg.jpg?includes=file_content").AddTokenAuth(token2)
  253. resp = session.MakeRequest(t, req, http.StatusOK)
  254. contentsResponse = api.ContentsExtResponse{}
  255. DecodeJSON(t, resp, &contentsResponse)
  256. assert.Nil(t, contentsResponse.DirContents)
  257. assert.NotNil(t, contentsResponse.FileContents)
  258. respFile := contentsResponse.FileContents
  259. assert.Equal(t, "jpeg.jpg", respFile.Name)
  260. assert.NotNil(t, respFile.Encoding)
  261. assert.NotNil(t, respFile.Content)
  262. assert.Nil(t, contentsResponse.FileContents.LastCommitSHA)
  263. assert.Nil(t, contentsResponse.FileContents.LastCommitMessage)
  264. assert.Equal(t, util.ToPointer(int64(107)), respFile.LfsSize)
  265. assert.Equal(t, util.ToPointer("0b8d8b5f15046343fd32f451df93acc2bdd9e6373be478b968e4cad6b6647351"), respFile.LfsOid)
  266. })
  267. }