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