8
8
"fmt"
9
9
"net/http"
10
10
"net/url"
11
- "path/filepath "
11
+ "path"
12
12
"testing"
13
13
"time"
14
14
@@ -49,28 +49,42 @@ func getCreateFileOptions() api.CreateFileOptions {
49
49
}
50
50
}
51
51
52
- func getExpectedFileResponseForCreate (repoFullName , commitID , treePath , latestCommitSHA string ) * api.FileResponse {
52
+ func normalizeFileContentResponseCommitTime (c * api.ContentsResponse ) {
53
+ // decoded JSON response may contain different timezone from the one parsed by git commit
54
+ // so we need to normalize the time to UTC to make "assert.Equal" pass
55
+ c .LastCommitterDate = c .LastCommitterDate .UTC ()
56
+ c .LastAuthorDate = c .LastAuthorDate .UTC ()
57
+ }
58
+
59
+ type apiFileResponseInfo struct {
60
+ repoFullName , commitID , treePath , lastCommitSHA string
61
+ lastCommitterWhen , lastAuthorWhen time.Time
62
+ }
63
+
64
+ func getExpectedFileResponseForCreate (info apiFileResponseInfo ) * api.FileResponse {
53
65
sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
54
66
encoding := "base64"
55
67
content := "VGhpcyBpcyBuZXcgdGV4dA=="
56
- selfURL := setting .AppURL + "api/v1/repos/" + repoFullName + "/contents/" + treePath + "?ref=master"
57
- htmlURL := setting .AppURL + repoFullName + "/src/branch/master/" + treePath
58
- gitURL := setting .AppURL + "api/v1/repos/" + repoFullName + "/git/blobs/" + sha
59
- downloadURL := setting .AppURL + repoFullName + "/raw/branch/master/" + treePath
60
- return & api.FileResponse {
68
+ selfURL := setting .AppURL + "api/v1/repos/" + info . repoFullName + "/contents/" + info . treePath + "?ref=master"
69
+ htmlURL := setting .AppURL + info . repoFullName + "/src/branch/master/" + info . treePath
70
+ gitURL := setting .AppURL + "api/v1/repos/" + info . repoFullName + "/git/blobs/" + sha
71
+ downloadURL := setting .AppURL + info . repoFullName + "/raw/branch/master/" + info . treePath
72
+ ret := & api.FileResponse {
61
73
Content : & api.ContentsResponse {
62
- Name : filepath .Base (treePath ),
63
- Path : treePath ,
64
- SHA : sha ,
65
- LastCommitSHA : latestCommitSHA ,
66
- Size : 16 ,
67
- Type : "file" ,
68
- Encoding : & encoding ,
69
- Content : & content ,
70
- URL : & selfURL ,
71
- HTMLURL : & htmlURL ,
72
- GitURL : & gitURL ,
73
- DownloadURL : & downloadURL ,
74
+ Name : path .Base (info .treePath ),
75
+ Path : info .treePath ,
76
+ SHA : sha ,
77
+ LastCommitSHA : info .lastCommitSHA ,
78
+ LastCommitterDate : info .lastCommitterWhen ,
79
+ LastAuthorDate : info .lastAuthorWhen ,
80
+ Size : 16 ,
81
+ Type : "file" ,
82
+ Encoding : & encoding ,
83
+ Content : & content ,
84
+ URL : & selfURL ,
85
+ HTMLURL : & htmlURL ,
86
+ GitURL : & gitURL ,
87
+ DownloadURL : & downloadURL ,
74
88
Links : & api.FileLinksResponse {
75
89
Self : & selfURL ,
76
90
GitURL : & gitURL ,
@@ -79,10 +93,10 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
79
93
},
80
94
Commit : & api.FileCommitResponse {
81
95
CommitMeta : api.CommitMeta {
82
- URL : setting .AppURL + "api/v1/repos/" + repoFullName + "/git/commits/" + commitID ,
83
- SHA : commitID ,
96
+ URL : setting .AppURL + "api/v1/repos/" + info . repoFullName + "/git/commits/" + info . commitID ,
97
+ SHA : info . commitID ,
84
98
},
85
- HTMLURL : setting .AppURL + repoFullName + "/commit/" + commitID ,
99
+ HTMLURL : setting .AppURL + info . repoFullName + "/commit/" + info . commitID ,
86
100
Author : & api.CommitUser {
87
101
Identity : api.Identity {
88
102
Name : "Anne Doe" ,
@@ -106,6 +120,8 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
106
120
Payload : "" ,
107
121
},
108
122
}
123
+ normalizeFileContentResponseCommitTime (ret .Content )
124
+ return ret
109
125
}
110
126
111
127
func BenchmarkAPICreateFileSmall (b * testing.B ) {
@@ -167,11 +183,20 @@ func TestAPICreateFile(t *testing.T) {
167
183
AddTokenAuth (token2 )
168
184
resp := MakeRequest (t , req , http .StatusCreated )
169
185
gitRepo , _ := gitrepo .OpenRepository (t .Context (), repo1 )
186
+ defer gitRepo .Close ()
170
187
commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
171
- latestCommit , _ := gitRepo .GetCommitByPath (treePath )
172
- expectedFileResponse := getExpectedFileResponseForCreate ("user2/repo1" , commitID , treePath , latestCommit .ID .String ())
188
+ lastCommit , _ := gitRepo .GetCommitByPath (treePath )
189
+ expectedFileResponse := getExpectedFileResponseForCreate (apiFileResponseInfo {
190
+ repoFullName : "user2/repo1" ,
191
+ commitID : commitID ,
192
+ treePath : treePath ,
193
+ lastCommitSHA : lastCommit .ID .String (),
194
+ lastCommitterWhen : lastCommit .Committer .When ,
195
+ lastAuthorWhen : lastCommit .Author .When ,
196
+ })
173
197
var fileResponse api.FileResponse
174
198
DecodeJSON (t , resp , & fileResponse )
199
+ normalizeFileContentResponseCommitTime (fileResponse .Content )
175
200
assert .Equal (t , expectedFileResponse .Content , fileResponse .Content )
176
201
assert .Equal (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
177
202
assert .Equal (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
@@ -181,7 +206,6 @@ func TestAPICreateFile(t *testing.T) {
181
206
assert .Equal (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
182
207
assert .Equal (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
183
208
assert .Equal (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
184
- gitRepo .Close ()
185
209
}
186
210
187
211
// Test creating a file in a new branch
@@ -285,10 +309,19 @@ func TestAPICreateFile(t *testing.T) {
285
309
resp = MakeRequest (t , req , http .StatusCreated )
286
310
emptyRepo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {OwnerName : "user2" , Name : "empty-repo" }) // public repo
287
311
gitRepo , _ := gitrepo .OpenRepository (t .Context (), emptyRepo )
312
+ defer gitRepo .Close ()
288
313
commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
289
314
latestCommit , _ := gitRepo .GetCommitByPath (treePath )
290
- expectedFileResponse := getExpectedFileResponseForCreate ("user2/empty-repo" , commitID , treePath , latestCommit .ID .String ())
315
+ expectedFileResponse := getExpectedFileResponseForCreate (apiFileResponseInfo {
316
+ repoFullName : "user2/empty-repo" ,
317
+ commitID : commitID ,
318
+ treePath : treePath ,
319
+ lastCommitSHA : latestCommit .ID .String (),
320
+ lastCommitterWhen : latestCommit .Committer .When ,
321
+ lastAuthorWhen : latestCommit .Author .When ,
322
+ })
291
323
DecodeJSON (t , resp , & fileResponse )
324
+ normalizeFileContentResponseCommitTime (fileResponse .Content )
292
325
assert .Equal (t , expectedFileResponse .Content , fileResponse .Content )
293
326
assert .Equal (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
294
327
assert .Equal (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
@@ -298,6 +331,5 @@ func TestAPICreateFile(t *testing.T) {
298
331
assert .Equal (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
299
332
assert .Equal (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
300
333
assert .Equal (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
301
- gitRepo .Close ()
302
334
})
303
335
}
0 commit comments