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_list_test.go 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. "path/filepath"
  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 getExpectedContentsListResponseForContents(ref, refType, lastCommitSHA string) []*api.ContentsResponse {
  20. treePath := "README.md"
  21. sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
  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. {
  28. Name: filepath.Base(treePath),
  29. Path: treePath,
  30. SHA: sha,
  31. LastCommitSHA: lastCommitSHA,
  32. Type: "file",
  33. Size: 30,
  34. URL: &selfURL,
  35. HTMLURL: &htmlURL,
  36. GitURL: &gitURL,
  37. DownloadURL: &downloadURL,
  38. Links: &api.FileLinksResponse{
  39. Self: &selfURL,
  40. GitURL: &gitURL,
  41. HTMLURL: &htmlURL,
  42. },
  43. },
  44. }
  45. }
  46. func TestAPIGetContentsList(t *testing.T) {
  47. onGiteaRun(t, testAPIGetContentsList)
  48. }
  49. func testAPIGetContentsList(t *testing.T, u *url.URL) {
  50. /*** SETUP ***/
  51. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo1 & repo16
  52. org3 := 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 := "" // root dir
  58. // Get user2's token
  59. session := loginUser(t, user2.Name)
  60. token2 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
  61. // Get user4's token
  62. session = loginUser(t, user4.Name)
  63. token4 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
  64. // Get the commit ID of the default branch
  65. gitRepo, err := git.OpenRepository(git.DefaultContext, repo1.RepoPath())
  66. assert.NoError(t, err)
  67. defer gitRepo.Close()
  68. // Make a new branch in repo1
  69. newBranch := "test_branch"
  70. err = repo_service.CreateNewBranch(git.DefaultContext, user2, repo1, gitRepo, repo1.DefaultBranch, newBranch)
  71. assert.NoError(t, err)
  72. commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
  73. // Make a new tag in repo1
  74. newTag := "test_tag"
  75. err = gitRepo.CreateTag(newTag, commitID)
  76. assert.NoError(t, err)
  77. /*** END SETUP ***/
  78. // ref is default ref
  79. ref := repo1.DefaultBranch
  80. refType := "branch"
  81. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  82. resp := MakeRequest(t, req, http.StatusOK)
  83. var contentsListResponse []*api.ContentsResponse
  84. DecodeJSON(t, resp, &contentsListResponse)
  85. assert.NotNil(t, contentsListResponse)
  86. lastCommit, err := gitRepo.GetCommitByPath("README.md")
  87. assert.NoError(t, err)
  88. expectedContentsListResponse := getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String())
  89. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  90. // No ref
  91. refType = "branch"
  92. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath)
  93. resp = MakeRequest(t, req, http.StatusOK)
  94. DecodeJSON(t, resp, &contentsListResponse)
  95. assert.NotNil(t, contentsListResponse)
  96. expectedContentsListResponse = getExpectedContentsListResponseForContents(repo1.DefaultBranch, refType, lastCommit.ID.String())
  97. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  98. // ref is the branch we created above in setup
  99. ref = newBranch
  100. refType = "branch"
  101. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  102. resp = MakeRequest(t, req, http.StatusOK)
  103. DecodeJSON(t, resp, &contentsListResponse)
  104. assert.NotNil(t, contentsListResponse)
  105. branchCommit, err := gitRepo.GetBranchCommit(ref)
  106. assert.NoError(t, err)
  107. lastCommit, err = branchCommit.GetCommitByPath("README.md")
  108. assert.NoError(t, err)
  109. expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String())
  110. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  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, &contentsListResponse)
  117. assert.NotNil(t, contentsListResponse)
  118. tagCommit, err := gitRepo.GetTagCommit(ref)
  119. assert.NoError(t, err)
  120. lastCommit, err = tagCommit.GetCommitByPath("README.md")
  121. assert.NoError(t, err)
  122. expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String())
  123. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  124. // ref is a commit
  125. ref = commitID
  126. refType = "commit"
  127. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  128. resp = MakeRequest(t, req, http.StatusOK)
  129. DecodeJSON(t, resp, &contentsListResponse)
  130. assert.NotNil(t, contentsListResponse)
  131. expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, commitID)
  132. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  133. // Test file contents a file with a bad ref
  134. ref = "badref"
  135. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  136. MakeRequest(t, req, http.StatusNotFound)
  137. // Test accessing private ref with user token that does not have access - should fail
  138. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  139. MakeRequest(t, req, http.StatusNotFound)
  140. // Test access private ref of owner of token
  141. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/readme.md?token=%s", user2.Name, repo16.Name, token2)
  142. MakeRequest(t, req, http.StatusOK)
  143. // Test access of org org3 private repo file by owner user2
  144. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", org3.Name, repo3.Name, treePath, token2)
  145. MakeRequest(t, req, http.StatusOK)
  146. }