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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/setting"
  14. api "code.gitea.io/gitea/modules/structs"
  15. repo_service "code.gitea.io/gitea/services/repository"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func getExpectedContentsResponseForContents(ref, refType string) *api.ContentsResponse {
  19. treePath := "README.md"
  20. sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
  21. encoding := "base64"
  22. content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
  23. selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=" + ref
  24. htmlURL := setting.AppURL + "user2/repo1/src/" + refType + "/" + ref + "/" + treePath
  25. gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
  26. downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath
  27. return &api.ContentsResponse{
  28. Name: treePath,
  29. Path: treePath,
  30. SHA: sha,
  31. Type: "file",
  32. Size: 30,
  33. Encoding: &encoding,
  34. Content: &content,
  35. URL: &selfURL,
  36. HTMLURL: &htmlURL,
  37. GitURL: &gitURL,
  38. DownloadURL: &downloadURL,
  39. Links: &api.FileLinksResponse{
  40. Self: &selfURL,
  41. GitURL: &gitURL,
  42. HTMLURL: &htmlURL,
  43. },
  44. }
  45. }
  46. func TestAPIGetContents(t *testing.T) {
  47. onGiteaRun(t, testAPIGetContents)
  48. }
  49. func testAPIGetContents(t *testing.T, u *url.URL) {
  50. /*** SETUP ***/
  51. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) // owner of the repo1 & repo16
  52. user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User) // owner of the repo3, is an org
  53. user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User) // owner of neither repos
  54. repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  55. repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
  56. repo16 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
  57. treePath := "README.md"
  58. // Get user2's token
  59. session := loginUser(t, user2.Name)
  60. token2 := getTokenForLoggedInUser(t, session)
  61. // Get user4's token
  62. session = loginUser(t, user4.Name)
  63. token4 := getTokenForLoggedInUser(t, session)
  64. session = emptyTestSession(t)
  65. // Make a new branch in repo1
  66. newBranch := "test_branch"
  67. err := repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
  68. assert.NoError(t, err)
  69. // Get the commit ID of the default branch
  70. gitRepo, err := git.OpenRepository(repo1.RepoPath())
  71. assert.NoError(t, err)
  72. defer gitRepo.Close()
  73. commitID, err := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
  74. assert.NoError(t, err)
  75. // Make a new tag in repo1
  76. newTag := "test_tag"
  77. err = gitRepo.CreateTag(newTag, commitID)
  78. assert.NoError(t, err)
  79. /*** END SETUP ***/
  80. // ref is default ref
  81. ref := repo1.DefaultBranch
  82. refType := "branch"
  83. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  84. resp := session.MakeRequest(t, req, http.StatusOK)
  85. var contentsResponse api.ContentsResponse
  86. DecodeJSON(t, resp, &contentsResponse)
  87. assert.NotNil(t, contentsResponse)
  88. expectedContentsResponse := getExpectedContentsResponseForContents(ref, refType)
  89. assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
  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 = session.MakeRequest(t, req, http.StatusOK)
  94. DecodeJSON(t, resp, &contentsResponse)
  95. assert.NotNil(t, contentsResponse)
  96. expectedContentsResponse = getExpectedContentsResponseForContents(repo1.DefaultBranch, refType)
  97. assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
  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 = session.MakeRequest(t, req, http.StatusOK)
  103. DecodeJSON(t, resp, &contentsResponse)
  104. assert.NotNil(t, contentsResponse)
  105. expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType)
  106. assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
  107. // ref is the new tag we created above in setup
  108. ref = newTag
  109. refType = "tag"
  110. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  111. resp = session.MakeRequest(t, req, http.StatusOK)
  112. DecodeJSON(t, resp, &contentsResponse)
  113. assert.NotNil(t, contentsResponse)
  114. expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType)
  115. assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
  116. // ref is a commit
  117. ref = commitID
  118. refType = "commit"
  119. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  120. resp = session.MakeRequest(t, req, http.StatusOK)
  121. DecodeJSON(t, resp, &contentsResponse)
  122. assert.NotNil(t, contentsResponse)
  123. expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType)
  124. assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
  125. // Test file contents a file with a bad ref
  126. ref = "badref"
  127. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
  128. session.MakeRequest(t, req, http.StatusNotFound)
  129. // Test accessing private ref with user token that does not have access - should fail
  130. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  131. session.MakeRequest(t, req, http.StatusNotFound)
  132. // Test access private ref of owner of token
  133. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/readme.md?token=%s", user2.Name, repo16.Name, token2)
  134. session.MakeRequest(t, req, http.StatusOK)
  135. // Test access of org user3 private repo file by owner user2
  136. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  137. session.MakeRequest(t, req, http.StatusOK)
  138. }