@@ -22,32 +22,83 @@ import (
22
22
"github.com/stretchr/testify/assert"
23
23
)
24
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
- },
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 ,
49
87
},
50
88
}
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
51
102
}
52
103
53
104
func TestAPIGetContentsList (t * testing.T ) {
@@ -96,9 +147,10 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
96
147
var contentsListResponse []* api.ContentsResponse
97
148
DecodeJSON (t , resp , & contentsListResponse )
98
149
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
+ })
102
154
assert .EqualValues (t , expectedContentsListResponse , contentsListResponse )
103
155
104
156
// No ref
@@ -107,8 +159,10 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
107
159
resp = MakeRequest (t , req , http .StatusOK )
108
160
DecodeJSON (t , resp , & contentsListResponse )
109
161
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
+ })
112
166
assert .EqualValues (t , expectedContentsListResponse , contentsListResponse )
113
167
114
168
// ref is the branch we created above in setup
@@ -118,11 +172,10 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
118
172
resp = MakeRequest (t , req , http .StatusOK )
119
173
DecodeJSON (t , resp , & contentsListResponse )
120
174
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
+ })
126
179
assert .EqualValues (t , expectedContentsListResponse , contentsListResponse )
127
180
128
181
// ref is the new tag we created above in setup
@@ -132,11 +185,10 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
132
185
resp = MakeRequest (t , req , http .StatusOK )
133
186
DecodeJSON (t , resp , & contentsListResponse )
134
187
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
+ })
140
192
assert .EqualValues (t , expectedContentsListResponse , contentsListResponse )
141
193
142
194
// ref is a commit
@@ -146,7 +198,10 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
146
198
resp = MakeRequest (t , req , http .StatusOK )
147
199
DecodeJSON (t , resp , & contentsListResponse )
148
200
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
+ })
150
205
assert .EqualValues (t , expectedContentsListResponse , contentsListResponse )
151
206
152
207
// Test file contents a file with a bad ref
0 commit comments