Skip to content

Commit ee81381

Browse files
committed
fix TestAPIGetContentsList
1 parent 3050f62 commit ee81381

File tree

2 files changed

+98
-43
lines changed

2 files changed

+98
-43
lines changed

models/fixtures/branch.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
id: 4
3939
repo_id: 1
4040
name: 'master'
41-
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
42-
commit_message: 'Initial commit'
43-
commit_time: 1489927679
41+
commit_id: '94dc45d6ec51090b36c4d5bd5d9e6f5a70da9edf'
42+
commit_message: 'add workflow'
43+
commit_time: 1710312301
4444
pusher_id: 1
4545
is_deleted: false
4646
deleted_by_id: 0

tests/integration/api_repo_get_contents_list_test.go

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,83 @@ import (
2222
"github.com/stretchr/testify/assert"
2323
)
2424

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-
},
25+
type Content struct {
26+
treePath string
27+
sha string
28+
contentType string
29+
ref string
30+
refType string
31+
lastCommitSHA string
32+
size int64
33+
}
34+
35+
func getContent(t *testing.T, gitRepo *git.Repository, tree, sha, contentType, ref, refType, commitSHA string, size int64) *Content {
36+
if commitSHA == "" {
37+
var commit *git.Commit
38+
var err error
39+
if refType == "branch" {
40+
commit, err = gitRepo.GetBranchCommit(ref)
41+
} else {
42+
commit, err = gitRepo.GetTagCommit(ref)
43+
}
44+
assert.NoError(t, err)
45+
lastCommit, err := commit.GetCommitByPath(tree)
46+
assert.NoError(t, err)
47+
commitSHA = lastCommit.ID.String()
48+
}
49+
return &Content{
50+
treePath: tree,
51+
sha: sha,
52+
contentType: contentType,
53+
ref: ref,
54+
refType: refType,
55+
lastCommitSHA: commitSHA,
56+
size: size,
57+
}
58+
}
59+
60+
func getReadmeContent(t *testing.T, gitRepo *git.Repository, ref, refType, commitSHA string) *Content {
61+
return getContent(t, gitRepo, "README.md", "4b4851ad51df6a7d9f25c979345979eaeb5b349f", "file", ref, refType, commitSHA, 30)
62+
}
63+
64+
func getGiteaContent(t *testing.T, gitRepo *git.Repository, ref, refType, commitSHA string) *Content {
65+
return getContent(t, gitRepo, ".gitea", "b5f7dda5e39c1eef0d4da9c20b7d8322daa7add5", "dir", ref, refType, commitSHA, 0)
66+
}
67+
68+
func getExpectedContentsResponseForContent(content *Content) *api.ContentsResponse {
69+
selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + content.treePath + "?ref=" + content.ref
70+
htmlURL := setting.AppURL + "user2/repo1/src/" + content.refType + "/" + content.ref + "/" + content.treePath
71+
gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + content.sha
72+
73+
resp := &api.ContentsResponse{
74+
Name: filepath.Base(content.treePath),
75+
Path: content.treePath,
76+
SHA: content.sha,
77+
LastCommitSHA: content.lastCommitSHA,
78+
Type: content.contentType,
79+
Size: content.size,
80+
URL: &selfURL,
81+
HTMLURL: &htmlURL,
82+
GitURL: &gitURL,
83+
Links: &api.FileLinksResponse{
84+
Self: &selfURL,
85+
GitURL: &gitURL,
86+
HTMLURL: &htmlURL,
4987
},
5088
}
89+
if content.contentType == "file" {
90+
downloadURL := setting.AppURL + "user2/repo1/raw/" + content.refType + "/" + content.ref + "/" + content.treePath
91+
resp.DownloadURL = &downloadURL
92+
}
93+
return resp
94+
}
95+
96+
func getExpectedContentsListResponseForContents(contents []*Content) []*api.ContentsResponse {
97+
resps := make([]*api.ContentsResponse, 0, len(contents))
98+
for _, content := range contents {
99+
resps = append(resps, getExpectedContentsResponseForContent(content))
100+
}
101+
return resps
51102
}
52103

53104
func TestAPIGetContentsList(t *testing.T) {
@@ -96,9 +147,10 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
96147
var contentsListResponse []*api.ContentsResponse
97148
DecodeJSON(t, resp, &contentsListResponse)
98149
assert.NotNil(t, contentsListResponse)
99-
lastCommit, err := gitRepo.GetCommitByPath("README.md")
100-
assert.NoError(t, err)
101-
expectedContentsListResponse := getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String())
150+
expectedContentsListResponse := getExpectedContentsListResponseForContents([]*Content{
151+
getGiteaContent(t, gitRepo, ref, refType, ""),
152+
getReadmeContent(t, gitRepo, ref, refType, ""),
153+
})
102154
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
103155

104156
// No ref
@@ -107,8 +159,10 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
107159
resp = MakeRequest(t, req, http.StatusOK)
108160
DecodeJSON(t, resp, &contentsListResponse)
109161
assert.NotNil(t, contentsListResponse)
110-
111-
expectedContentsListResponse = getExpectedContentsListResponseForContents(repo1.DefaultBranch, refType, lastCommit.ID.String())
162+
expectedContentsListResponse = getExpectedContentsListResponseForContents([]*Content{
163+
getGiteaContent(t, gitRepo, ref, refType, ""),
164+
getReadmeContent(t, gitRepo, repo1.DefaultBranch, refType, ""),
165+
})
112166
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
113167

114168
// ref is the branch we created above in setup
@@ -118,11 +172,10 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
118172
resp = MakeRequest(t, req, http.StatusOK)
119173
DecodeJSON(t, resp, &contentsListResponse)
120174
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())
175+
expectedContentsListResponse = getExpectedContentsListResponseForContents([]*Content{
176+
getGiteaContent(t, gitRepo, ref, refType, ""),
177+
getReadmeContent(t, gitRepo, ref, refType, ""),
178+
})
126179
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
127180

128181
// ref is the new tag we created above in setup
@@ -132,11 +185,10 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
132185
resp = MakeRequest(t, req, http.StatusOK)
133186
DecodeJSON(t, resp, &contentsListResponse)
134187
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())
188+
expectedContentsListResponse = getExpectedContentsListResponseForContents([]*Content{
189+
getGiteaContent(t, gitRepo, ref, refType, ""),
190+
getReadmeContent(t, gitRepo, ref, refType, ""),
191+
})
140192
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
141193

142194
// ref is a commit
@@ -146,7 +198,10 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
146198
resp = MakeRequest(t, req, http.StatusOK)
147199
DecodeJSON(t, resp, &contentsListResponse)
148200
assert.NotNil(t, contentsListResponse)
149-
expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, commitID)
201+
expectedContentsListResponse = getExpectedContentsListResponseForContents([]*Content{
202+
getGiteaContent(t, gitRepo, ref, refType, commitID),
203+
getReadmeContent(t, gitRepo, ref, refType, "65f1bf27bc3bf70f64657658635e66094edbcb4d"), // readme didn't changed since this commit
204+
})
150205
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
151206

152207
// Test file contents a file with a bad ref

0 commit comments

Comments
 (0)