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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/context"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  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. onGiteaRun(t, testAPIGetFileContents)
  38. }
  39. func testAPIGetFileContents(t *testing.T, u *url.URL) {
  40. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  41. user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
  42. user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
  43. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  44. repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
  45. repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
  46. treePath := "README.md"
  47. // Get user2's token
  48. session := loginUser(t, user2.Name)
  49. token2 := getTokenForLoggedInUser(t, session)
  50. session = emptyTestSession(t)
  51. // Get user4's token
  52. session = loginUser(t, user4.Name)
  53. token4 := getTokenForLoggedInUser(t, session)
  54. session = emptyTestSession(t)
  55. // Make a second master branch in repo1
  56. repo1.CreateNewBranch(user2, repo1.DefaultBranch, "master2")
  57. // ref is default branch
  58. branch := repo1.DefaultBranch
  59. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, branch)
  60. resp := session.MakeRequest(t, req, http.StatusOK)
  61. var fileContentResponse api.FileContentResponse
  62. DecodeJSON(t, resp, &fileContentResponse)
  63. assert.NotNil(t, fileContentResponse)
  64. expectedFileContentResponse := getExpectedFileContentResponseForFileContents(branch)
  65. assert.EqualValues(t, *expectedFileContentResponse, fileContentResponse)
  66. // No ref
  67. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath)
  68. resp = session.MakeRequest(t, req, http.StatusOK)
  69. DecodeJSON(t, resp, &fileContentResponse)
  70. assert.NotNil(t, fileContentResponse)
  71. expectedFileContentResponse = getExpectedFileContentResponseForFileContents(repo1.DefaultBranch)
  72. assert.EqualValues(t, *expectedFileContentResponse, fileContentResponse)
  73. // ref is master2
  74. branch = "master2"
  75. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, branch)
  76. resp = session.MakeRequest(t, req, http.StatusOK)
  77. DecodeJSON(t, resp, &fileContentResponse)
  78. assert.NotNil(t, fileContentResponse)
  79. expectedFileContentResponse = getExpectedFileContentResponseForFileContents("master2")
  80. assert.EqualValues(t, *expectedFileContentResponse, fileContentResponse)
  81. // Test file contents a file with the wrong branch
  82. branch = "badbranch"
  83. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, branch)
  84. resp = session.MakeRequest(t, req, http.StatusInternalServerError)
  85. expectedAPIError := context.APIError{
  86. Message: "object does not exist [id: " + branch + ", rel_path: ]",
  87. URL: setting.API.SwaggerURL,
  88. }
  89. var apiError context.APIError
  90. DecodeJSON(t, resp, &apiError)
  91. assert.Equal(t, expectedAPIError, apiError)
  92. // Test accessing private branch with user token that does not have access - should fail
  93. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  94. session.MakeRequest(t, req, http.StatusNotFound)
  95. // Test access private branch of owner of token
  96. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/readme.md?token=%s", user2.Name, repo16.Name, token2)
  97. session.MakeRequest(t, req, http.StatusOK)
  98. // Test access of org user3 private repo file by owner user2
  99. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  100. session.MakeRequest(t, req, http.StatusOK)
  101. }