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