88 "fmt"
99 "net/http"
1010 "net/url"
11- "path/filepath "
11+ "path"
1212 "testing"
1313 "time"
1414
@@ -49,28 +49,42 @@ func getCreateFileOptions() api.CreateFileOptions {
4949 }
5050}
5151
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 {
5365 sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
5466 encoding := "base64"
5567 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 {
6173 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 ,
7488 Links : & api.FileLinksResponse {
7589 Self : & selfURL ,
7690 GitURL : & gitURL ,
@@ -79,10 +93,10 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
7993 },
8094 Commit : & api.FileCommitResponse {
8195 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 ,
8498 },
85- HTMLURL : setting .AppURL + repoFullName + "/commit/" + commitID ,
99+ HTMLURL : setting .AppURL + info . repoFullName + "/commit/" + info . commitID ,
86100 Author : & api.CommitUser {
87101 Identity : api.Identity {
88102 Name : "Anne Doe" ,
@@ -106,6 +120,8 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
106120 Payload : "" ,
107121 },
108122 }
123+ normalizeFileContentResponseCommitTime (ret .Content )
124+ return ret
109125}
110126
111127func BenchmarkAPICreateFileSmall (b * testing.B ) {
@@ -167,11 +183,20 @@ func TestAPICreateFile(t *testing.T) {
167183 AddTokenAuth (token2 )
168184 resp := MakeRequest (t , req , http .StatusCreated )
169185 gitRepo , _ := gitrepo .OpenRepository (t .Context (), repo1 )
186+ defer gitRepo .Close ()
170187 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+ })
173197 var fileResponse api.FileResponse
174198 DecodeJSON (t , resp , & fileResponse )
199+ normalizeFileContentResponseCommitTime (fileResponse .Content )
175200 assert .Equal (t , expectedFileResponse .Content , fileResponse .Content )
176201 assert .Equal (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
177202 assert .Equal (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
@@ -181,7 +206,6 @@ func TestAPICreateFile(t *testing.T) {
181206 assert .Equal (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
182207 assert .Equal (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
183208 assert .Equal (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
184- gitRepo .Close ()
185209 }
186210
187211 // Test creating a file in a new branch
@@ -285,10 +309,19 @@ func TestAPICreateFile(t *testing.T) {
285309 resp = MakeRequest (t , req , http .StatusCreated )
286310 emptyRepo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {OwnerName : "user2" , Name : "empty-repo" }) // public repo
287311 gitRepo , _ := gitrepo .OpenRepository (t .Context (), emptyRepo )
312+ defer gitRepo .Close ()
288313 commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
289314 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+ })
291323 DecodeJSON (t , resp , & fileResponse )
324+ normalizeFileContentResponseCommitTime (fileResponse .Content )
292325 assert .Equal (t , expectedFileResponse .Content , fileResponse .Content )
293326 assert .Equal (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
294327 assert .Equal (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
@@ -298,6 +331,5 @@ func TestAPICreateFile(t *testing.T) {
298331 assert .Equal (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
299332 assert .Equal (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
300333 assert .Equal (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
301- gitRepo .Close ()
302334 })
303335}
0 commit comments