code.gitea.io/gitea@v1.22.3/tests/integration/api_repo_get_contents_list_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 "net/url" 9 "path/filepath" 10 "testing" 11 12 auth_model "code.gitea.io/gitea/models/auth" 13 repo_model "code.gitea.io/gitea/models/repo" 14 "code.gitea.io/gitea/models/unittest" 15 user_model "code.gitea.io/gitea/models/user" 16 "code.gitea.io/gitea/modules/git" 17 "code.gitea.io/gitea/modules/gitrepo" 18 "code.gitea.io/gitea/modules/setting" 19 api "code.gitea.io/gitea/modules/structs" 20 repo_service "code.gitea.io/gitea/services/repository" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func getExpectedContentsListResponseForContents(ref, refType, lastCommitSHA string) []*api.ContentsResponse { 26 treePath := "README.md" 27 sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f" 28 selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=" + ref 29 htmlURL := setting.AppURL + "user2/repo1/src/" + refType + "/" + ref + "/" + treePath 30 gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha 31 downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath 32 return []*api.ContentsResponse{ 33 { 34 Name: filepath.Base(treePath), 35 Path: treePath, 36 SHA: sha, 37 LastCommitSHA: lastCommitSHA, 38 Type: "file", 39 Size: 30, 40 URL: &selfURL, 41 HTMLURL: &htmlURL, 42 GitURL: &gitURL, 43 DownloadURL: &downloadURL, 44 Links: &api.FileLinksResponse{ 45 Self: &selfURL, 46 GitURL: &gitURL, 47 HTMLURL: &htmlURL, 48 }, 49 }, 50 } 51 } 52 53 func TestAPIGetContentsList(t *testing.T) { 54 onGiteaRun(t, testAPIGetContentsList) 55 } 56 57 func testAPIGetContentsList(t *testing.T, u *url.URL) { 58 /*** SETUP ***/ 59 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo1 & repo16 60 org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) // owner of the repo3, is an org 61 user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) // owner of neither repos 62 repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo 63 repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) // public repo 64 repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}) // private repo 65 treePath := "" // root dir 66 67 // Get user2's token 68 session := loginUser(t, user2.Name) 69 token2 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository) 70 // Get user4's token 71 session = loginUser(t, user4.Name) 72 token4 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository) 73 74 // Get the commit ID of the default branch 75 gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo1) 76 assert.NoError(t, err) 77 defer gitRepo.Close() 78 79 // Make a new branch in repo1 80 newBranch := "test_branch" 81 err = repo_service.CreateNewBranch(git.DefaultContext, user2, repo1, gitRepo, repo1.DefaultBranch, newBranch) 82 assert.NoError(t, err) 83 84 commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch) 85 // Make a new tag in repo1 86 newTag := "test_tag" 87 err = gitRepo.CreateTag(newTag, commitID) 88 assert.NoError(t, err) 89 /*** END SETUP ***/ 90 91 // ref is default ref 92 ref := repo1.DefaultBranch 93 refType := "branch" 94 req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) 95 resp := MakeRequest(t, req, http.StatusOK) 96 var contentsListResponse []*api.ContentsResponse 97 DecodeJSON(t, resp, &contentsListResponse) 98 assert.NotNil(t, contentsListResponse) 99 lastCommit, err := gitRepo.GetCommitByPath("README.md") 100 assert.NoError(t, err) 101 expectedContentsListResponse := getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String()) 102 assert.EqualValues(t, expectedContentsListResponse, contentsListResponse) 103 104 // No ref 105 refType = "branch" 106 req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath) 107 resp = MakeRequest(t, req, http.StatusOK) 108 DecodeJSON(t, resp, &contentsListResponse) 109 assert.NotNil(t, contentsListResponse) 110 111 expectedContentsListResponse = getExpectedContentsListResponseForContents(repo1.DefaultBranch, refType, lastCommit.ID.String()) 112 assert.EqualValues(t, expectedContentsListResponse, contentsListResponse) 113 114 // ref is the branch we created above in setup 115 ref = newBranch 116 refType = "branch" 117 req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) 118 resp = MakeRequest(t, req, http.StatusOK) 119 DecodeJSON(t, resp, &contentsListResponse) 120 assert.NotNil(t, contentsListResponse) 121 branchCommit, err := gitRepo.GetBranchCommit(ref) 122 assert.NoError(t, err) 123 lastCommit, err = branchCommit.GetCommitByPath("README.md") 124 assert.NoError(t, err) 125 expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String()) 126 assert.EqualValues(t, expectedContentsListResponse, contentsListResponse) 127 128 // ref is the new tag we created above in setup 129 ref = newTag 130 refType = "tag" 131 req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) 132 resp = MakeRequest(t, req, http.StatusOK) 133 DecodeJSON(t, resp, &contentsListResponse) 134 assert.NotNil(t, contentsListResponse) 135 tagCommit, err := gitRepo.GetTagCommit(ref) 136 assert.NoError(t, err) 137 lastCommit, err = tagCommit.GetCommitByPath("README.md") 138 assert.NoError(t, err) 139 expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String()) 140 assert.EqualValues(t, expectedContentsListResponse, contentsListResponse) 141 142 // ref is a commit 143 ref = commitID 144 refType = "commit" 145 req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) 146 resp = MakeRequest(t, req, http.StatusOK) 147 DecodeJSON(t, resp, &contentsListResponse) 148 assert.NotNil(t, contentsListResponse) 149 expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, commitID) 150 assert.EqualValues(t, expectedContentsListResponse, contentsListResponse) 151 152 // Test file contents a file with a bad ref 153 ref = "badref" 154 req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref) 155 MakeRequest(t, req, http.StatusNotFound) 156 157 // Test accessing private ref with user token that does not have access - should fail 158 req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath). 159 AddTokenAuth(token4) 160 MakeRequest(t, req, http.StatusNotFound) 161 162 // Test access private ref of owner of token 163 req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/readme.md", user2.Name, repo16.Name). 164 AddTokenAuth(token2) 165 MakeRequest(t, req, http.StatusOK) 166 167 // Test access of org org3 private repo file by owner user2 168 req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", org3.Name, repo3.Name, treePath). 169 AddTokenAuth(token2) 170 MakeRequest(t, req, http.StatusOK) 171 }