You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_repo_file_content_test.go 5.2 kB

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