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_file_content_test.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "path/filepath"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/sdk/gitea"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func getExpectedFileContentResponseForFileContents(branch string) *api.FileContentResponse {
  17. treePath := "README.md"
  18. sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
  19. return &api.FileContentResponse{
  20. Name: filepath.Base(treePath),
  21. Path: treePath,
  22. SHA: sha,
  23. Size: 30,
  24. URL: setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath,
  25. HTMLURL: setting.AppURL + "user2/repo1/blob/" + branch + "/" + treePath,
  26. GitURL: setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha,
  27. DownloadURL: setting.AppURL + "user2/repo1/raw/branch/" + branch + "/" + treePath,
  28. Type: "blob",
  29. Links: &api.FileLinksResponse{
  30. Self: setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath,
  31. GitURL: setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha,
  32. HTMLURL: setting.AppURL + "user2/repo1/blob/" + branch + "/" + treePath,
  33. },
  34. }
  35. }
  36. func TestAPIGetFileContents(t *testing.T) {
  37. prepareTestEnv(t)
  38. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  39. user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
  40. user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
  41. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  42. repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
  43. repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
  44. treePath := "README.md"
  45. // Get user2's token
  46. session := loginUser(t, user2.Name)
  47. token2 := getTokenForLoggedInUser(t, session)
  48. session = emptyTestSession(t)
  49. // Get user4's token
  50. session = loginUser(t, user4.Name)
  51. token4 := getTokenForLoggedInUser(t, session)
  52. session = emptyTestSession(t)
  53. // Make a second master branch in repo1
  54. repo1.CreateNewBranch(user2, repo1.DefaultBranch, "master2")
  55. // ref is default branch
  56. branch := repo1.DefaultBranch
  57. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, branch)
  58. resp := session.MakeRequest(t, req, http.StatusOK)
  59. var fileContentResponse api.FileContentResponse
  60. DecodeJSON(t, resp, &fileContentResponse)
  61. assert.NotNil(t, fileContentResponse)
  62. expectedFileContentResponse := getExpectedFileContentResponseForFileContents(branch)
  63. assert.EqualValues(t, *expectedFileContentResponse, fileContentResponse)
  64. // No ref
  65. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath)
  66. resp = session.MakeRequest(t, req, http.StatusOK)
  67. DecodeJSON(t, resp, &fileContentResponse)
  68. assert.NotNil(t, fileContentResponse)
  69. expectedFileContentResponse = getExpectedFileContentResponseForFileContents(repo1.DefaultBranch)
  70. assert.EqualValues(t, *expectedFileContentResponse, fileContentResponse)
  71. // ref is master2
  72. branch = "master2"
  73. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, branch)
  74. resp = session.MakeRequest(t, req, http.StatusOK)
  75. DecodeJSON(t, resp, &fileContentResponse)
  76. assert.NotNil(t, fileContentResponse)
  77. expectedFileContentResponse = getExpectedFileContentResponseForFileContents("master2")
  78. assert.EqualValues(t, *expectedFileContentResponse, fileContentResponse)
  79. // Test file contents a file with the wrong branch
  80. branch = "badbranch"
  81. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, branch)
  82. resp = session.MakeRequest(t, req, http.StatusInternalServerError)
  83. expectedAPIError := context.APIError{
  84. Message: "object does not exist [id: " + branch + ", rel_path: ]",
  85. URL: base.DocURL,
  86. }
  87. var apiError context.APIError
  88. DecodeJSON(t, resp, &apiError)
  89. assert.Equal(t, expectedAPIError, apiError)
  90. // Test accessing private branch with user token that does not have access - should fail
  91. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  92. session.MakeRequest(t, req, http.StatusNotFound)
  93. // Test access private branch of owner of token
  94. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/readme.md?token=%s", user2.Name, repo16.Name, token2)
  95. session.MakeRequest(t, req, http.StatusOK)
  96. // Test access of org user3 private repo file by owner user2
  97. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  98. session.MakeRequest(t, req, http.StatusOK)
  99. }