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.

156 lines
6.2 KiB

  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/modules/git"
  11. repo_module "code.gitea.io/gitea/modules/repository"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func getExpectedContentsResponseForContents(ref, refType string) *api.ContentsResponse {
  17. treePath := "README.md"
  18. sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
  19. encoding := "base64"
  20. content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
  21. selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=" + ref
  22. htmlURL := setting.AppURL + "user2/repo1/src/" + refType + "/" + ref + "/" + treePath
  23. gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
  24. downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath
  25. return &api.ContentsResponse{
  26. Name: treePath,
  27. Path: treePath,
  28. SHA: sha,
  29. Type: "file",
  30. Size: 30,
  31. Encoding: &encoding,
  32. Content: &content,
  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. func TestAPIGetContents(t *testing.T) {
  45. onGiteaRun(t, testAPIGetContents)
  46. }
  47. func testAPIGetContents(t *testing.T, u *url.URL) {
  48. /*** SETUP ***/
  49. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  50. user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
  51. user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
  52. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  53. repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
  54. repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
  55. treePath := "README.md"
  56. // Get user2's token
  57. session := loginUser(t, user2.Name)
  58. token2 := getTokenForLoggedInUser(t, session)
  59. session = emptyTestSession(t)
  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_module.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. resp = 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. }