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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "testing"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/models/unittest"
  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 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. Type: "file",
  31. Size: 30,
  32. Encoding: &encoding,
  33. Content: &content,
  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. func TestAPIGetContents(t *testing.T) {
  46. onGiteaRun(t, testAPIGetContents)
  47. }
  48. func testAPIGetContents(t *testing.T, u *url.URL) {
  49. /*** SETUP ***/
  50. user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  51. user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
  52. user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.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 := "README.md"
  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, err := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
  73. assert.NoError(t, err)
  74. // Make a new tag in repo1
  75. newTag := "test_tag"
  76. err = gitRepo.CreateTag(newTag, commitID)
  77. assert.NoError(t, err)
  78. /*** END SETUP ***/
  79. // ref is default ref
  80. ref := repo1.DefaultBranch
  81. refType := "branch"
  82. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  83. resp := session.MakeRequest(t, req, http.StatusOK)
  84. var contentsResponse api.ContentsResponse
  85. DecodeJSON(t, resp, &contentsResponse)
  86. assert.NotNil(t, contentsResponse)
  87. expectedContentsResponse := getExpectedContentsResponseForContents(ref, refType)
  88. assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
  89. // No ref
  90. refType = "branch"
  91. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath)
  92. resp = session.MakeRequest(t, req, http.StatusOK)
  93. DecodeJSON(t, resp, &contentsResponse)
  94. assert.NotNil(t, contentsResponse)
  95. expectedContentsResponse = getExpectedContentsResponseForContents(repo1.DefaultBranch, refType)
  96. assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
  97. // ref is the branch we created above in setup
  98. ref = newBranch
  99. refType = "branch"
  100. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  101. resp = session.MakeRequest(t, req, http.StatusOK)
  102. DecodeJSON(t, resp, &contentsResponse)
  103. assert.NotNil(t, contentsResponse)
  104. expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType)
  105. assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
  106. // ref is the new tag we created above in setup
  107. ref = newTag
  108. refType = "tag"
  109. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  110. resp = session.MakeRequest(t, req, http.StatusOK)
  111. DecodeJSON(t, resp, &contentsResponse)
  112. assert.NotNil(t, contentsResponse)
  113. expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType)
  114. assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
  115. // ref is a commit
  116. ref = commitID
  117. refType = "commit"
  118. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  119. resp = session.MakeRequest(t, req, http.StatusOK)
  120. DecodeJSON(t, resp, &contentsResponse)
  121. assert.NotNil(t, contentsResponse)
  122. expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType)
  123. assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
  124. // Test file contents a file with a bad ref
  125. ref = "badref"
  126. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  127. session.MakeRequest(t, req, http.StatusNotFound)
  128. // Test accessing private ref with user token that does not have access - should fail
  129. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  130. session.MakeRequest(t, req, http.StatusNotFound)
  131. // Test access private ref of owner of token
  132. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/readme.md?token=%s", user2.Name, repo16.Name, token2)
  133. session.MakeRequest(t, req, http.StatusOK)
  134. // Test access of org user3 private repo file by owner user2
  135. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  136. session.MakeRequest(t, req, http.StatusOK)
  137. }