Skip to content

Commit 58bc9cb

Browse files
committed
fix
1 parent 08bf2d6 commit 58bc9cb

File tree

4 files changed

+33
-106
lines changed

4 files changed

+33
-106
lines changed

services/repository/files/content_test.go

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,21 @@ package files
55

66
import (
77
"testing"
8-
"time"
98

109
"code.gitea.io/gitea/models/unittest"
1110
api "code.gitea.io/gitea/modules/structs"
1211
"code.gitea.io/gitea/modules/util"
13-
"code.gitea.io/gitea/routers/api/v1/utils"
1412
"code.gitea.io/gitea/services/contexttest"
1513

1614
_ "code.gitea.io/gitea/models/actions"
1715

1816
"github.com/stretchr/testify/assert"
19-
"github.com/stretchr/testify/require"
2017
)
2118

2219
func TestMain(m *testing.M) {
2320
unittest.MainTest(m)
2421
}
2522

26-
func getExpectedReadmeContentsResponse() *api.ContentsResponse {
27-
treePath := "README.md"
28-
sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
29-
encoding := "base64"
30-
content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
31-
selfURL := "https://try.gitea.io/api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
32-
htmlURL := "https://try.gitea.io/user2/repo1/src/branch/master/" + treePath
33-
gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
34-
downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
35-
return &api.ContentsResponse{
36-
Name: treePath,
37-
Path: treePath,
38-
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
39-
LastCommitSHA: util.ToPointer("65f1bf27bc3bf70f64657658635e66094edbcb4d"),
40-
LastCommitterDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))),
41-
LastAuthorDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))),
42-
LastCommitMessage: util.ToPointer("Initial commit\n"),
43-
Type: "file",
44-
Size: 30,
45-
Encoding: &encoding,
46-
Content: &content,
47-
URL: &selfURL,
48-
HTMLURL: &htmlURL,
49-
GitURL: &gitURL,
50-
DownloadURL: &downloadURL,
51-
Links: &api.FileLinksResponse{
52-
Self: &selfURL,
53-
GitURL: &gitURL,
54-
HTMLURL: &htmlURL,
55-
},
56-
}
57-
}
58-
5923
func TestGetContents(t *testing.T) {
6024
unittest.PrepareTestEnv(t)
6125
ctx, _ := contexttest.MockContext(t, "user2/repo1")
@@ -64,45 +28,8 @@ func TestGetContents(t *testing.T) {
6428
contexttest.LoadRepoCommit(t, ctx)
6529
contexttest.LoadUser(t, ctx, 2)
6630
contexttest.LoadGitRepo(t, ctx)
67-
defer ctx.Repo.GitRepo.Close()
68-
repo, gitRepo := ctx.Repo.Repository, ctx.Repo.GitRepo
69-
refCommit, err := utils.ResolveRefCommit(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch)
70-
require.NoError(t, err)
71-
72-
t.Run("GetContentsOrList(README.md)-MetaOnly", func(t *testing.T) {
73-
expectedContentsResponse := getExpectedReadmeContentsResponse()
74-
expectedContentsResponse.Encoding = nil // because will be in a list, doesn't have encoding and content
75-
expectedContentsResponse.Content = nil
76-
extResp, err := GetContentsOrList(ctx, repo, gitRepo, refCommit, GetContentsOrListOptions{TreePath: "README.md", IncludeSingleFileContent: false})
77-
assert.Equal(t, expectedContentsResponse, extResp.FileContents)
78-
assert.NoError(t, err)
79-
})
80-
81-
t.Run("GetContentsOrList(README.md)", func(t *testing.T) {
82-
expectedContentsResponse := getExpectedReadmeContentsResponse()
83-
extResp, err := GetContentsOrList(ctx, repo, gitRepo, refCommit, GetContentsOrListOptions{TreePath: "README.md", IncludeSingleFileContent: true})
84-
assert.Equal(t, expectedContentsResponse, extResp.FileContents)
85-
assert.NoError(t, err)
86-
})
87-
88-
t.Run("GetContentsOrList(RootDir)", func(t *testing.T) {
89-
readmeContentsResponse := getExpectedReadmeContentsResponse()
90-
readmeContentsResponse.Encoding = nil // because will be in a list, doesn't have encoding and content
91-
readmeContentsResponse.Content = nil
92-
expectedContentsListResponse := []*api.ContentsResponse{readmeContentsResponse}
93-
// even if IncludeFileContent is true, it has no effect for directory listing
94-
extResp, err := GetContentsOrList(ctx, repo, gitRepo, refCommit, GetContentsOrListOptions{TreePath: "", IncludeSingleFileContent: true})
95-
assert.Equal(t, expectedContentsListResponse, extResp.DirContents)
96-
assert.NoError(t, err)
97-
})
9831

99-
t.Run("GetContentsOrList(NoSuchTreePath)", func(t *testing.T) {
100-
extResp, err := GetContentsOrList(ctx, repo, gitRepo, refCommit, GetContentsOrListOptions{TreePath: "no-such/file.md"})
101-
assert.Error(t, err)
102-
assert.EqualError(t, err, "object does not exist [id: , rel_path: no-such]")
103-
assert.Nil(t, extResp.DirContents)
104-
assert.Nil(t, extResp.FileContents)
105-
})
32+
// GetContentsOrList's behavior is fully tested in integration tests, so we don't need to test it here.
10633

10734
t.Run("GetBlobBySHA", func(t *testing.T) {
10835
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"

tests/integration/api_repo_get_contents_list_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ func getExpectedContentsListResponseForContents(ref, refType, lastCommitSHA stri
3939
LastCommitSHA: util.ToPointer(lastCommitSHA),
4040
LastCommitterDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))),
4141
LastAuthorDate: util.ToPointer(time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400))),
42-
LastCommitMessage: util.ToPointer("Initial commit"),
4342
Type: "file",
4443
Size: 30,
4544
URL: &selfURL,
@@ -67,7 +66,6 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
6766
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) // public repo
6867
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) // public repo
6968
repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}) // private repo
70-
treePath := "" // root dir
7169

7270
// Get user2's token
7371
session := loginUser(t, user2.Name)
@@ -96,7 +94,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
9694
// ref is default ref
9795
ref := repo1.DefaultBranch
9896
refType := "branch"
99-
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
97+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents?ref=%s", user2.Name, repo1.Name, ref)
10098
resp := MakeRequest(t, req, http.StatusOK)
10199
var contentsListResponse []*api.ContentsResponse
102100
DecodeJSON(t, resp, &contentsListResponse)
@@ -108,7 +106,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
108106

109107
// No ref
110108
refType = "branch"
111-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath)
109+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/", user2.Name, repo1.Name)
112110
resp = MakeRequest(t, req, http.StatusOK)
113111
DecodeJSON(t, resp, &contentsListResponse)
114112
assert.NotNil(t, contentsListResponse)
@@ -119,7 +117,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
119117
// ref is the branch we created above in setup
120118
ref = newBranch
121119
refType = "branch"
122-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
120+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents?ref=%s", user2.Name, repo1.Name, ref)
123121
resp = MakeRequest(t, req, http.StatusOK)
124122
DecodeJSON(t, resp, &contentsListResponse)
125123
assert.NotNil(t, contentsListResponse)
@@ -133,7 +131,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
133131
// ref is the new tag we created above in setup
134132
ref = newTag
135133
refType = "tag"
136-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
134+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/?ref=%s", user2.Name, repo1.Name, ref)
137135
resp = MakeRequest(t, req, http.StatusOK)
138136
DecodeJSON(t, resp, &contentsListResponse)
139137
assert.NotNil(t, contentsListResponse)
@@ -147,7 +145,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
147145
// ref is a commit
148146
ref = commitID
149147
refType = "commit"
150-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
148+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/?ref=%s", user2.Name, repo1.Name, ref)
151149
resp = MakeRequest(t, req, http.StatusOK)
152150
DecodeJSON(t, resp, &contentsListResponse)
153151
assert.NotNil(t, contentsListResponse)
@@ -156,21 +154,21 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
156154

157155
// Test file contents a file with a bad ref
158156
ref = "badref"
159-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
157+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/?ref=%s", user2.Name, repo1.Name, ref)
160158
MakeRequest(t, req, http.StatusNotFound)
161159

162160
// Test accessing private ref with user token that does not have access - should fail
163-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath).
161+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/", user2.Name, repo16.Name).
164162
AddTokenAuth(token4)
165163
MakeRequest(t, req, http.StatusNotFound)
166164

167165
// Test access private ref of owner of token
168-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/readme.md", user2.Name, repo16.Name).
166+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/", user2.Name, repo16.Name).
169167
AddTokenAuth(token2)
170168
MakeRequest(t, req, http.StatusOK)
171169

172170
// Test access of org org3 private repo file by owner user2
173-
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s", org3.Name, repo3.Name, treePath).
171+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/", org3.Name, repo3.Name).
174172
AddTokenAuth(token2)
175173
MakeRequest(t, req, http.StatusOK)
176174
}

tests/integration/api_repo_get_contents_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,16 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
9797
require.NoError(t, err)
9898
/*** END SETUP ***/
9999

100+
// not found
101+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/no-such/file.md", user2.Name, repo1.Name)
102+
resp := MakeRequest(t, req, http.StatusNotFound)
103+
assert.Contains(t, resp.Body.String(), "object does not exist [id: , rel_path: no-such]")
104+
100105
// ref is default ref
101106
ref := repo1.DefaultBranch
102107
refType := "branch"
103-
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
104-
resp := MakeRequest(t, req, http.StatusOK)
108+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
109+
resp = MakeRequest(t, req, http.StatusOK)
105110
var contentsResponse api.ContentsResponse
106111
DecodeJSON(t, resp, &contentsResponse)
107112
lastCommit, _ := gitRepo.GetCommitByPath("README.md")
@@ -116,7 +121,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
116121
expectedContentsResponse = getExpectedContentsResponseForContents(repo1.DefaultBranch, refType, lastCommit.ID.String())
117122
assert.Equal(t, *expectedContentsResponse, contentsResponse)
118123

119-
// ref is the branch we created above in setup
124+
// ref is the branch we created above in setup
120125
ref = newBranch
121126
refType = "branch"
122127
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
@@ -256,7 +261,7 @@ func testAPIGetContentsExt(t *testing.T) {
256261
assert.Equal(t, util.ToPointer("0b8d8b5f15046343fd32f451df93acc2bdd9e6373be478b968e4cad6b6647351"), respFile.LfsOid)
257262
})
258263
t.Run("FileContents", func(t *testing.T) {
259-
// by default, no file content is returned
264+
// by default, no file content or commit info is returned
260265
req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/contents-ext/docs/README.md?ref=sub-home-md-img-check")
261266
resp := MakeRequest(t, req, http.StatusOK)
262267
var contentsResponse api.ContentsExtResponse
@@ -277,8 +282,8 @@ func testAPIGetContentsExt(t *testing.T) {
277282
assert.Equal(t, "README.md", contentsResponse.FileContents.Name)
278283
assert.NotNil(t, contentsResponse.FileContents.Encoding)
279284
assert.NotNil(t, contentsResponse.FileContents.Content)
280-
assert.NotNil(t, contentsResponse.FileContents.LastCommitSHA)
281-
assert.NotNil(t, contentsResponse.FileContents.LastCommitMessage)
285+
assert.Equal(t, "4649299398e4d39a5c09eb4f534df6f1e1eb87cc", *contentsResponse.FileContents.LastCommitSHA)
286+
assert.Equal(t, "Test how READMEs render images when found in a subfolder\n", *contentsResponse.FileContents.LastCommitMessage)
282287

283288
req = NewRequestf(t, "GET", "/api/v1/repos/user2/lfs/contents-ext/jpeg.jpg?includes=file_content").AddTokenAuth(token2)
284289
resp = session.MakeRequest(t, req, http.StatusOK)

tests/integration/repofiles_change_test.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ func getExpectedFileResponseForRepoFilesCreate(commitID string, lastCommit *git.
158158
LastCommitSHA: util.ToPointer(lastCommit.ID.String()),
159159
LastCommitterDate: util.ToPointer(lastCommit.Committer.When),
160160
LastAuthorDate: util.ToPointer(lastCommit.Author.When),
161-
LastCommitMessage: util.ToPointer("Creates new/file.txt\n"),
162161
Type: "file",
163162
Size: 18,
164163
Encoding: &encoding,
@@ -229,7 +228,6 @@ func getExpectedFileResponseForRepoFilesUpdate(commitID, filename, lastCommitSHA
229228
LastCommitSHA: util.ToPointer(lastCommitSHA),
230229
LastCommitterDate: util.ToPointer(lastCommitterWhen),
231230
LastAuthorDate: util.ToPointer(lastAuthorWhen),
232-
LastCommitMessage: util.ToPointer("Updates README.md\n"),
233231
Type: "file",
234232
Size: 43,
235233
Encoding: &encoding,
@@ -330,19 +328,18 @@ func getExpectedFileResponseForRepoFilesUpdateRename(commitID, lastCommitSHA str
330328
downloadURL := setting.AppURL + "user2/lfs/raw/branch/master/" + detail.filename
331329
// don't set time related fields because there might be different time in one operation
332330
responses = append(responses, &api.ContentsResponse{
333-
Name: detail.filename,
334-
Path: detail.filename,
335-
SHA: detail.sha,
336-
LastCommitSHA: util.ToPointer(lastCommitSHA),
337-
LastCommitMessage: util.ToPointer("Rename files\n"),
338-
Type: "file",
339-
Size: detail.size,
340-
Encoding: util.ToPointer("base64"),
341-
Content: &detail.content,
342-
URL: &selfURL,
343-
HTMLURL: &htmlURL,
344-
GitURL: &gitURL,
345-
DownloadURL: &downloadURL,
331+
Name: detail.filename,
332+
Path: detail.filename,
333+
SHA: detail.sha,
334+
LastCommitSHA: util.ToPointer(lastCommitSHA),
335+
Type: "file",
336+
Size: detail.size,
337+
Encoding: util.ToPointer("base64"),
338+
Content: &detail.content,
339+
URL: &selfURL,
340+
HTMLURL: &htmlURL,
341+
GitURL: &gitURL,
342+
DownloadURL: &downloadURL,
346343
Links: &api.FileLinksResponse{
347344
Self: &selfURL,
348345
GitURL: &gitURL,

0 commit comments

Comments
 (0)