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_git_trees_test.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/http"
  6. "testing"
  7. auth_model "code.gitea.io/gitea/models/auth"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. "code.gitea.io/gitea/models/unittest"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/tests"
  12. )
  13. func TestAPIReposGitTrees(t *testing.T) {
  14. defer tests.PrepareTestEnv(t)()
  15. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo1 & repo16
  16. org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) // owner of the repo3
  17. user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) // owner of neither repos
  18. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo
  19. repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) // public repo
  20. repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}) // private repo
  21. repo1TreeSHA := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
  22. repo3TreeSHA := "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6"
  23. repo16TreeSHA := "69554a64c1e6030f051e5c3f94bfbd773cd6a324"
  24. badSHA := "0000000000000000000000000000000000000000"
  25. // Login as User2.
  26. session := loginUser(t, user2.Name)
  27. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
  28. // Test a public repo that anyone can GET the tree of
  29. for _, ref := range [...]string{
  30. "master", // Branch
  31. repo1TreeSHA, // Tree SHA
  32. } {
  33. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/trees/%s", user2.Name, repo1.Name, ref)
  34. MakeRequest(t, req, http.StatusOK)
  35. }
  36. // Tests a private repo with no token so will fail
  37. for _, ref := range [...]string{
  38. "master", // Branch
  39. repo1TreeSHA, // Tag
  40. } {
  41. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/trees/%s", user2.Name, repo16.Name, ref)
  42. MakeRequest(t, req, http.StatusNotFound)
  43. }
  44. // Test using access token for a private repo that the user of the token owns
  45. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/trees/%s?token=%s", user2.Name, repo16.Name, repo16TreeSHA, token)
  46. MakeRequest(t, req, http.StatusOK)
  47. // Test using bad sha
  48. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/trees/%s", user2.Name, repo1.Name, badSHA)
  49. MakeRequest(t, req, http.StatusBadRequest)
  50. // Test using org repo "org3/repo3" where user2 is a collaborator
  51. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/trees/%s?token=%s", org3.Name, repo3.Name, repo3TreeSHA, token)
  52. MakeRequest(t, req, http.StatusOK)
  53. // Test using org repo "org3/repo3" with no user token
  54. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/trees/%s", org3.Name, repo3TreeSHA, repo3.Name)
  55. MakeRequest(t, req, http.StatusNotFound)
  56. // Login as User4.
  57. session = loginUser(t, user4.Name)
  58. token4 := getTokenForLoggedInUser(t, session)
  59. // Test using org repo "org3/repo3" where user4 is a NOT collaborator
  60. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/trees/d56a3073c1dbb7b15963110a049d50cdb5db99fc?access=%s", org3.Name, repo3.Name, token4)
  61. MakeRequest(t, req, http.StatusNotFound)
  62. }