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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "path/filepath"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  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 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. Type: "file",
  32. Size: 30,
  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. }
  45. func TestAPIGetContentsList(t *testing.T) {
  46. onGiteaRun(t, testAPIGetContentsList)
  47. }
  48. func testAPIGetContentsList(t *testing.T, u *url.URL) {
  49. /*** SETUP ***/
  50. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) // owner of the repo1 & repo16
  51. user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User) // owner of the repo3, is an org
  52. user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User) // owner of neither repos
  53. repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  54. repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
  55. repo16 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
  56. treePath := "" // root dir
  57. // Get user2's token
  58. session := loginUser(t, user2.Name)
  59. token2 := getTokenForLoggedInUser(t, session)
  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. err := repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
  67. assert.NoError(t, err)
  68. // Get the commit ID of the default branch
  69. gitRepo, err := git.OpenRepository(repo1.RepoPath())
  70. assert.NoError(t, err)
  71. defer gitRepo.Close()
  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 := session.MakeRequest(t, req, http.StatusOK)
  83. var contentsListResponse []*api.ContentsResponse
  84. DecodeJSON(t, resp, &contentsListResponse)
  85. assert.NotNil(t, contentsListResponse)
  86. expectedContentsListResponse := getExpectedContentsListResponseForContents(ref, refType)
  87. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  88. // No ref
  89. refType = "branch"
  90. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath)
  91. resp = session.MakeRequest(t, req, http.StatusOK)
  92. DecodeJSON(t, resp, &contentsListResponse)
  93. assert.NotNil(t, contentsListResponse)
  94. expectedContentsListResponse = getExpectedContentsListResponseForContents(repo1.DefaultBranch, refType)
  95. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  96. // ref is the branch we created above in setup
  97. ref = newBranch
  98. refType = "branch"
  99. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  100. resp = session.MakeRequest(t, req, http.StatusOK)
  101. DecodeJSON(t, resp, &contentsListResponse)
  102. assert.NotNil(t, contentsListResponse)
  103. expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType)
  104. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  105. // ref is the new tag we created above in setup
  106. ref = newTag
  107. refType = "tag"
  108. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  109. resp = session.MakeRequest(t, req, http.StatusOK)
  110. DecodeJSON(t, resp, &contentsListResponse)
  111. assert.NotNil(t, contentsListResponse)
  112. expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType)
  113. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  114. // ref is a commit
  115. ref = commitID
  116. refType = "commit"
  117. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  118. resp = session.MakeRequest(t, req, http.StatusOK)
  119. DecodeJSON(t, resp, &contentsListResponse)
  120. assert.NotNil(t, contentsListResponse)
  121. expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType)
  122. assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
  123. // Test file contents a file with a bad ref
  124. ref = "badref"
  125. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  126. session.MakeRequest(t, req, http.StatusNotFound)
  127. // Test accessing private ref with user token that does not have access - should fail
  128. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  129. session.MakeRequest(t, req, http.StatusNotFound)
  130. // Test access private ref of owner of token
  131. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/readme.md?token=%s", user2.Name, repo16.Name, token2)
  132. session.MakeRequest(t, req, http.StatusOK)
  133. // Test access of org user3 private repo file by owner user2
  134. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  135. session.MakeRequest(t, req, http.StatusOK)
  136. }