Skip to content

Commit 8a2f9f3

Browse files
committed
fine tune
1 parent 9071187 commit 8a2f9f3

File tree

2 files changed

+60
-112
lines changed

2 files changed

+60
-112
lines changed

tests/integration/api_repo_files_get_test.go

Lines changed: 54 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44
package integration
55

66
import (
7-
"encoding/base64"
8-
"fmt"
9-
"net/http"
10-
"net/url"
11-
"testing"
12-
"time"
13-
147
auth_model "code.gitea.io/gitea/models/auth"
158
"code.gitea.io/gitea/models/db"
169
repo_model "code.gitea.io/gitea/models/repo"
@@ -20,15 +13,17 @@ import (
2013
"code.gitea.io/gitea/modules/gitrepo"
2114
"code.gitea.io/gitea/modules/setting"
2215
api "code.gitea.io/gitea/modules/structs"
16+
"code.gitea.io/gitea/modules/util"
2317
repo_service "code.gitea.io/gitea/services/repository"
24-
18+
"encoding/base64"
19+
"fmt"
2520
"github.com/stretchr/testify/assert"
21+
"net/http"
22+
"net/url"
23+
"testing"
24+
"time"
2625
)
2726

28-
func getExpectedcontentsListResponseForFiles(ref, refType, lastCommitSHA string) []*api.ContentsResponse {
29-
return []*api.ContentsResponse{getExpectedContentsResponseForContents(ref, refType, lastCommitSHA)}
30-
}
31-
3227
func TestAPIGetRequestedFiles(t *testing.T) {
3328
onGiteaRun(t, testAPIGetRequestedFiles)
3429
}
@@ -41,112 +36,68 @@ func testAPIGetRequestedFiles(t *testing.T, u *url.URL) {
4136
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo
4237
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) // public repo
4338
repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}) // private repo
44-
filesOptions := &api.GetFilesOptions{
45-
Files: []string{
46-
"README.md",
47-
},
48-
}
4939

50-
// Get user2's token req.Body =
40+
// Get user2's token
5141
session := loginUser(t, user2.Name)
5242
token2 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) // TODO: allow for a POST-request to be scope read
5343
// Get user4's token
5444
session = loginUser(t, user4.Name)
5545
token4 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) // TODO: allow for a POST-request to be scope read
5646

57-
// Get the commit ID of the default branch
5847
gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo1)
5948
assert.NoError(t, err)
6049
defer gitRepo.Close()
61-
62-
// Make a new branch in repo1
63-
newBranch := "test_branch"
64-
err = repo_service.CreateNewBranch(git.DefaultContext, user2, repo1, gitRepo, repo1.DefaultBranch, newBranch)
65-
assert.NoError(t, err)
66-
67-
commitID, err := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
68-
assert.NoError(t, err)
69-
// Make a new tag in repo1
70-
newTag := "test_tag"
71-
err = gitRepo.CreateTag(newTag, commitID)
72-
assert.NoError(t, err)
73-
/*** END SETUP ***/
74-
75-
// ref is default ref
76-
ref := repo1.DefaultBranch
77-
refType := "branch"
78-
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files?ref=%s", user2.Name, repo1.Name, ref), &filesOptions)
79-
resp := MakeRequest(t, req, http.StatusOK)
80-
var contentsListResponse []*api.ContentsResponse
81-
DecodeJSON(t, resp, &contentsListResponse)
82-
assert.NotNil(t, contentsListResponse)
8350
lastCommit, _ := gitRepo.GetCommitByPath("README.md")
84-
expectedcontentsListResponse := getExpectedcontentsListResponseForFiles(ref, refType, lastCommit.ID.String())
85-
assert.Equal(t, expectedcontentsListResponse, contentsListResponse)
8651

87-
// No ref
88-
refType = "branch"
89-
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files", user2.Name, repo1.Name), &filesOptions)
90-
resp = MakeRequest(t, req, http.StatusOK)
91-
DecodeJSON(t, resp, &contentsListResponse)
92-
assert.NotNil(t, contentsListResponse)
93-
expectedcontentsListResponse = getExpectedcontentsListResponseForFiles(repo1.DefaultBranch, refType, lastCommit.ID.String())
94-
assert.Equal(t, expectedcontentsListResponse, contentsListResponse)
95-
96-
// ref is the branch we created above in setup
97-
ref = newBranch
98-
refType = "branch"
99-
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files?ref=%s", user2.Name, repo1.Name, ref), &filesOptions)
100-
resp = MakeRequest(t, req, http.StatusOK)
101-
DecodeJSON(t, resp, &contentsListResponse)
102-
assert.NotNil(t, contentsListResponse)
103-
branchCommit, _ := gitRepo.GetBranchCommit(ref)
104-
lastCommit, _ = branchCommit.GetCommitByPath("README.md")
105-
expectedcontentsListResponse = getExpectedcontentsListResponseForFiles(ref, refType, lastCommit.ID.String())
106-
assert.Equal(t, expectedcontentsListResponse, contentsListResponse)
107-
108-
// ref is the new tag we created above in setup
109-
ref = newTag
110-
refType = "tag"
111-
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files?ref=%s", user2.Name, repo1.Name, ref), &filesOptions)
112-
resp = MakeRequest(t, req, http.StatusOK)
113-
DecodeJSON(t, resp, &contentsListResponse)
114-
assert.NotNil(t, contentsListResponse)
115-
tagCommit, _ := gitRepo.GetTagCommit(ref)
116-
lastCommit, _ = tagCommit.GetCommitByPath("README.md")
117-
expectedcontentsListResponse = getExpectedcontentsListResponseForFiles(ref, refType, lastCommit.ID.String())
118-
assert.Equal(t, expectedcontentsListResponse, contentsListResponse)
119-
120-
// ref is a commit
121-
ref = commitID
122-
refType = "commit"
123-
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files?ref=%s", user2.Name, repo1.Name, ref), &filesOptions)
124-
resp = MakeRequest(t, req, http.StatusOK)
125-
DecodeJSON(t, resp, &contentsListResponse)
126-
assert.NotNil(t, contentsListResponse)
127-
expectedcontentsListResponse = getExpectedcontentsListResponseForFiles(ref, refType, commitID)
128-
assert.Equal(t, expectedcontentsListResponse, contentsListResponse)
129-
130-
// Test file contents a file with a bad ref
131-
ref = "badref"
132-
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files?ref=%s", user2.Name, repo1.Name, ref), &filesOptions)
133-
MakeRequest(t, req, http.StatusNotFound)
134-
135-
// Test accessing private ref with user token that does not have access - should fail
136-
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files", user2.Name, repo16.Name), &filesOptions).
137-
AddTokenAuth(token4)
138-
MakeRequest(t, req, http.StatusNotFound)
52+
requestFiles := func(t *testing.T, url string, files []string, expectedStatusCode ...int) (ret []*api.ContentsResponse) {
53+
req := NewRequestWithJSON(t, "POST", url, &api.GetFilesOptions{Files: files})
54+
resp := MakeRequest(t, req, util.OptionalArg(expectedStatusCode, http.StatusOK))
55+
if resp.Code != http.StatusOK {
56+
return nil
57+
}
58+
DecodeJSON(t, resp, &ret)
59+
return ret
60+
}
13961

140-
// Test access private ref of owner of token
141-
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files", user2.Name, repo16.Name), &filesOptions).
142-
AddTokenAuth(token2)
143-
MakeRequest(t, req, http.StatusOK)
62+
t.Run("User2NoRef", func(t *testing.T) {
63+
ret := requestFiles(t, "/api/v1/repos/user2/repo1/files", []string{"README.md"})
64+
expected := []*api.ContentsResponse{getExpectedContentsResponseForContents(repo1.DefaultBranch, "branch", lastCommit.ID.String())}
65+
assert.Equal(t, expected, ret)
66+
})
67+
t.Run("User2RefBranch", func(t *testing.T) {
68+
ret := requestFiles(t, "/api/v1/repos/user2/repo1/files?ref=master", []string{"README.md"})
69+
expected := []*api.ContentsResponse{getExpectedContentsResponseForContents(repo1.DefaultBranch, "branch", lastCommit.ID.String())}
70+
assert.Equal(t, expected, ret)
71+
})
72+
t.Run("User2RefTag", func(t *testing.T) {
73+
ret := requestFiles(t, "/api/v1/repos/user2/repo1/files?ref=v1.1", []string{"README.md"})
74+
expected := []*api.ContentsResponse{getExpectedContentsResponseForContents("v1.1", "tag", lastCommit.ID.String())}
75+
assert.Equal(t, expected, ret)
76+
})
77+
t.Run("User2RefCommit", func(t *testing.T) {
78+
ret := requestFiles(t, "/api/v1/repos/user2/repo1/files?ref=65f1bf27bc3bf70f64657658635e66094edbcb4d", []string{"README.md"})
79+
expected := []*api.ContentsResponse{getExpectedContentsResponseForContents("65f1bf27bc3bf70f64657658635e66094edbcb4d", "commit", lastCommit.ID.String())}
80+
assert.Equal(t, expected, ret)
81+
})
82+
t.Run("User2RefNotExist", func(t *testing.T) {
83+
ret := requestFiles(t, "/api/v1/repos/user2/repo1/files?ref=not-exist", []string{"README.md"}, http.StatusNotFound)
84+
assert.Empty(t, ret)
85+
})
14486

145-
// Test access of org org3 private repo file by owner user2
146-
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files", org3.Name, repo3.Name), &filesOptions).
147-
AddTokenAuth(token2)
148-
MakeRequest(t, req, http.StatusOK)
87+
t.Run("PermissionCheck", func(t *testing.T) {
88+
filesOptions := &api.GetFilesOptions{Files: []string{"README.md"}}
89+
// Test accessing private ref with user token that does not have access - should fail
90+
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files", user2.Name, repo16.Name), &filesOptions).AddTokenAuth(token4)
91+
MakeRequest(t, req, http.StatusNotFound)
92+
// Test access private ref of owner of token
93+
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files", user2.Name, repo16.Name), &filesOptions).AddTokenAuth(token2)
94+
MakeRequest(t, req, http.StatusOK)
95+
// Test access of org org3 private repo file by owner user2
96+
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/files", org3.Name, repo3.Name), &filesOptions).AddTokenAuth(token2)
97+
MakeRequest(t, req, http.StatusOK)
98+
})
14999

100+
// TODO: use mocked config to test without creating new files (to speed up the test)
150101
// Test pagination
151102
for i := 0; i < 40; i++ {
152103
filesOptions.Files = append(filesOptions.Files, filesOptions.Files[0])

tests/integration/api_repo_get_contents_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package integration
55

66
import (
7+
"code.gitea.io/gitea/modules/util"
78
"io"
89
"net/http"
910
"net/url"
@@ -26,28 +27,24 @@ import (
2627

2728
func getExpectedContentsResponseForContents(ref, refType, lastCommitSHA string) *api.ContentsResponse {
2829
treePath := "README.md"
29-
sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
30-
encoding := "base64"
31-
content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
3230
selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=" + ref
3331
htmlURL := setting.AppURL + "user2/repo1/src/" + refType + "/" + ref + "/" + treePath
34-
gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
35-
downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath
32+
gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f"
3633
return &api.ContentsResponse{
3734
Name: treePath,
3835
Path: treePath,
39-
SHA: sha,
36+
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
4037
LastCommitSHA: lastCommitSHA,
4138
LastCommitterDate: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
4239
LastAuthorDate: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
4340
Type: "file",
4441
Size: 30,
45-
Encoding: &encoding,
46-
Content: &content,
42+
Encoding: util.ToPointer("base64"),
43+
Content: util.ToPointer("IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"),
4744
URL: &selfURL,
4845
HTMLURL: &htmlURL,
4946
GitURL: &gitURL,
50-
DownloadURL: &downloadURL,
47+
DownloadURL: util.ToPointer(setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath),
5148
Links: &api.FileLinksResponse{
5249
Self: &selfURL,
5350
GitURL: &gitURL,

0 commit comments

Comments
 (0)