@@ -30,13 +30,21 @@ import (
3030func TestCreateFile (t * testing.T ) {
3131 onGiteaRun (t , func (t * testing.T , u * url.URL ) {
3232 session := loginUser (t , "user2" )
33- testCreateFile (t , session , "user2" , "repo1" , "master" , "test.txt" , "Content" )
33+ testCreateFile (t , session , "user2" , "user2" , " repo1" , "master" , "master" , "direct " , "test.txt" , "Content" )
3434 })
3535}
3636
37- func testCreateFile (t * testing.T , session * TestSession , user , repo , branch , filePath , content string ) * httptest.ResponseRecorder {
37+ func TestCreateFileFork (t * testing.T ) {
38+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
39+ session := loginUser (t , "user4" )
40+ forkToEdit (t , session , "user2" , "repo1" , "_new" , "master" , "test.txt" )
41+ testCreateFile (t , session , "user4" , "user2" , "repo1" , "master" , "feature/test" , "commit-to-new-branch" , "test.txt" , "Content" )
42+ })
43+ }
44+
45+ func testCreateFile (t * testing.T , session * TestSession , user , owner , repo , branch , targetBranch , commitChoice , filePath , content string ) {
3846 // Request editor page
39- newURL := fmt .Sprintf ("/%s/%s/_new/%s/" , user , repo , branch )
47+ newURL := fmt .Sprintf ("/%s/%s/_new/%s/" , owner , repo , branch )
4048 req := NewRequest (t , "GET" , newURL )
4149 resp := session .MakeRequest (t , req , http .StatusOK )
4250
@@ -46,70 +54,87 @@ func testCreateFile(t *testing.T, session *TestSession, user, repo, branch, file
4654
4755 // Save new file to master branch
4856 req = NewRequestWithValues (t , "POST" , newURL , map [string ]string {
49- "_csrf" : doc .GetCSRF (),
50- "last_commit" : lastCommit ,
51- "tree_path" : filePath ,
52- "content" : content ,
53- "commit_choice" : "direct" ,
57+ "_csrf" : doc .GetCSRF (),
58+ "last_commit" : lastCommit ,
59+ "tree_path" : filePath ,
60+ "content" : content ,
61+ "commit_choice" : commitChoice ,
62+ "new_branch_name" : targetBranch ,
5463 })
55- return session .MakeRequest (t , req , http .StatusSeeOther )
64+ session .MakeRequest (t , req , http .StatusSeeOther )
65+
66+ // Check new file exists
67+ req = NewRequestf (t , "GET" , "/%s/%s/src/branch/%s/%s" , user , repo , targetBranch , filePath )
68+ session .MakeRequest (t , req , http .StatusOK )
5669}
5770
5871func TestCreateFileOnProtectedBranch (t * testing.T ) {
5972 onGiteaRun (t , func (t * testing.T , u * url.URL ) {
6073 session := loginUser (t , "user2" )
74+ testCreateFileOnProtectedBranch (t , session , "user2" , "user2" , "repo1" , "master" , "master" , "direct" )
75+ })
76+ }
6177
62- csrf := GetUserCSRFToken (t , session )
63- // Change master branch to protected
64- req := NewRequestWithValues (t , "POST" , "/user2/repo1/settings/branches/edit" , map [string ]string {
65- "_csrf" : csrf ,
66- "rule_name" : "master" ,
67- "enable_push" : "true" ,
68- })
69- session .MakeRequest (t , req , http .StatusSeeOther )
70- // Check if master branch has been locked successfully
71- flashMsg := session .GetCookieFlashMessage ()
72- assert .Equal (t , `Branch protection for rule "master" has been updated.` , flashMsg .SuccessMsg )
73-
74- // Request editor page
75- req = NewRequest (t , "GET" , "/user2/repo1/_new/master/" )
76- resp := session .MakeRequest (t , req , http .StatusOK )
77-
78- doc := NewHTMLParser (t , resp .Body )
79- lastCommit := doc .GetInputValueByName ("last_commit" )
80- assert .NotEmpty (t , lastCommit )
81-
82- // Save new file to master branch
83- req = NewRequestWithValues (t , "POST" , "/user2/repo1/_new/master/" , map [string ]string {
84- "_csrf" : doc .GetCSRF (),
85- "last_commit" : lastCommit ,
86- "tree_path" : "test.txt" ,
87- "content" : "Content" ,
88- "commit_choice" : "direct" ,
89- })
78+ func TestCreateFileOnProtectedBranchFork (t * testing.T ) {
79+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
80+ session := loginUser (t , "user4" )
81+ forkToEdit (t , session , "user2" , "repo1" , "_new" , "master" , "test.txt" )
82+ testCreateFileOnProtectedBranch (t , session , "user4" , "user2" , "repo1" , "master" , "feature/test" , "commit-to-new-branch" )
83+ })
84+ }
9085
91- resp = session .MakeRequest (t , req , http .StatusOK )
92- // Check body for error message
93- assert .Contains (t , resp .Body .String (), "Cannot commit to protected branch "master"." )
86+ func testCreateFileOnProtectedBranch (t * testing.T , session * TestSession , user , owner , repo , branch , targetBranch , commitChoice string ) {
87+ csrf := GetUserCSRFToken (t , session )
88+ // Change target branch to protected
89+ req := NewRequestWithValues (t , "POST" , path .Join (user , repo , "settings" , "branches" , "edit" ), map [string ]string {
90+ "_csrf" : csrf ,
91+ "rule_name" : targetBranch ,
92+ "enable_push" : "true" ,
93+ })
94+ session .MakeRequest (t , req , http .StatusSeeOther )
95+ // Check if target branch has been locked successfully
96+ flashMsg := session .GetCookieFlashMessage ()
97+ assert .Equal (t , fmt .Sprintf (`Branch protection for rule "%s" has been updated.` , targetBranch ), flashMsg .SuccessMsg )
9498
95- // remove the protected branch
96- csrf = GetUserCSRFToken (t , session )
99+ // Request editor page
100+ req = NewRequest (t , "GET" , path .Join (owner , repo , "_new" , branch ))
101+ resp := session .MakeRequest (t , req , http .StatusOK )
97102
98- // Change master branch to protected
99- req = NewRequestWithValues (t , "POST" , "/user2/repo1/settings/branches/1/delete" , map [string ]string {
100- "_csrf" : csrf ,
101- })
103+ doc := NewHTMLParser (t , resp .Body )
104+ lastCommit := doc .GetInputValueByName ("last_commit" )
105+ assert .NotEmpty (t , lastCommit )
102106
103- resp = session .MakeRequest (t , req , http .StatusOK )
107+ // Save new file to target branch
108+ req = NewRequestWithValues (t , "POST" , path .Join (owner , repo , "_new" , branch ), map [string ]string {
109+ "_csrf" : doc .GetCSRF (),
110+ "last_commit" : lastCommit ,
111+ "tree_path" : "test.txt" ,
112+ "content" : "Content" ,
113+ "commit_choice" : commitChoice ,
114+ "new_branch_name" : targetBranch ,
115+ })
104116
105- res := make (map [string ]string )
106- assert .NoError (t , json .NewDecoder (resp .Body ).Decode (& res ))
107- assert .Equal (t , "/user2/repo1/settings/branches" , res ["redirect" ])
117+ resp = session .MakeRequest (t , req , http .StatusOK )
118+ // Check body for error message
119+ assert .Contains (t , resp .Body .String (), fmt .Sprintf ("Cannot commit to protected branch "%s"." , targetBranch ))
120+
121+ // remove the protected branch
122+ csrf = GetUserCSRFToken (t , session )
108123
109- // Check if master branch has been locked successfully
110- flashMsg = session . GetCookieFlashMessage ()
111- assert . Equal ( t , `Removing branch protection rule "1" failed.` , flashMsg . ErrorMsg )
124+ // Change target branch to protected
125+ req = NewRequestWithValues ( t , "POST" , path . Join ( user , repo , "settings" , "branches" , "1" , "delete" ), map [ string ] string {
126+ "_csrf" : csrf ,
112127 })
128+
129+ resp = session .MakeRequest (t , req , http .StatusOK )
130+
131+ res := make (map [string ]string )
132+ assert .NoError (t , json .NewDecoder (resp .Body ).Decode (& res ))
133+ assert .Equal (t , "/" + path .Join (user , repo , "settings" , "branches" ), res ["redirect" ])
134+
135+ // Check if target branch has been locked successfully
136+ flashMsg = session .GetCookieFlashMessage ()
137+ assert .Equal (t , `Removing branch protection rule "1" failed.` , flashMsg .ErrorMsg )
113138}
114139
115140func testEditFile (t * testing.T , session * TestSession , user , repo , branch , filePath , newContent string ) * httptest.ResponseRecorder {
@@ -141,17 +166,17 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
141166 return resp
142167}
143168
144- func testEditFileToNewBranch (t * testing.T , session * TestSession , user , repo , branch , targetBranch , filePath , newContent string ) * httptest.ResponseRecorder {
169+ func testEditFileToNewBranch (t * testing.T , session * TestSession , user , owner , repo , branch , targetBranch , filePath , newContent string ) * httptest.ResponseRecorder {
145170 // Get to the 'edit this file' page
146- req := NewRequest (t , "GET" , path .Join (user , repo , "_edit" , branch , filePath ))
171+ req := NewRequest (t , "GET" , path .Join (owner , repo , "_edit" , branch , filePath ))
147172 resp := session .MakeRequest (t , req , http .StatusOK )
148173
149174 htmlDoc := NewHTMLParser (t , resp .Body )
150175 lastCommit := htmlDoc .GetInputValueByName ("last_commit" )
151176 assert .NotEmpty (t , lastCommit )
152177
153178 // Submit the edits
154- req = NewRequestWithValues (t , "POST" , path .Join (user , repo , "_edit" , branch , filePath ),
179+ req = NewRequestWithValues (t , "POST" , path .Join (owner , repo , "_edit" , branch , filePath ),
155180 map [string ]string {
156181 "_csrf" : htmlDoc .GetCSRF (),
157182 "last_commit" : lastCommit ,
@@ -181,10 +206,110 @@ func TestEditFile(t *testing.T) {
181206func TestEditFileToNewBranch (t * testing.T ) {
182207 onGiteaRun (t , func (t * testing.T , u * url.URL ) {
183208 session := loginUser (t , "user2" )
184- testEditFileToNewBranch (t , session , "user2" , "repo1" , "master" , "feature/test" , "README.md" , "Hello, World (Edited)\n " )
209+ testEditFileToNewBranch (t , session , "user2" , "user2" , "repo1" , "master" , "feature/test" , "README.md" , "Hello, World (Edited)\n " )
210+ })
211+ }
212+
213+ func TestEditFileToNewBranchFork (t * testing.T ) {
214+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
215+ session := loginUser (t , "user4" )
216+ forkToEdit (t , session , "user2" , "repo1" , "_edit" , "master" , "README.md" )
217+ testEditFileToNewBranch (t , session , "user4" , "user2" , "repo1" , "master" , "feature/test" , "README.md" , "Hello, World (Edited)\n " )
218+ })
219+ }
220+
221+ func TestDeleteFile (t * testing.T ) {
222+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
223+ session := loginUser (t , "user2" )
224+ testDeleteFile (t , session , "user2" , "user2" , "repo1" , "master" , "master" , "direct" , "README.md" )
225+ })
226+ }
227+
228+ func TestDeleteFileFork (t * testing.T ) {
229+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
230+ session := loginUser (t , "user4" )
231+ forkToEdit (t , session , "user2" , "repo1" , "_delete" , "master" , "README.md" )
232+ testDeleteFile (t , session , "user4" , "user2" , "repo1" , "master" , "feature/test" , "commit-to-new-branch" , "README.md" )
185233 })
186234}
187235
236+ func testDeleteFile (t * testing.T , session * TestSession , user , owner , repo , branch , targetBranch , commitChoice , filePath string ) {
237+ // Check file exists
238+ req := NewRequestf (t , "GET" , "/%s/%s/src/branch/%s/%s" , owner , repo , branch , filePath )
239+ session .MakeRequest (t , req , http .StatusOK )
240+
241+ // Request editor page
242+ newURL := fmt .Sprintf ("/%s/%s/_delete/%s/%s" , owner , repo , branch , filePath )
243+ req = NewRequest (t , "GET" , newURL )
244+ resp := session .MakeRequest (t , req , http .StatusOK )
245+
246+ doc := NewHTMLParser (t , resp .Body )
247+ lastCommit := doc .GetInputValueByName ("last_commit" )
248+ assert .NotEmpty (t , lastCommit )
249+
250+ // Save new file to master branch
251+ req = NewRequestWithValues (t , "POST" , newURL , map [string ]string {
252+ "_csrf" : doc .GetCSRF (),
253+ "last_commit" : lastCommit ,
254+ "tree_path" : filePath ,
255+ "commit_choice" : commitChoice ,
256+ "new_branch_name" : targetBranch ,
257+ })
258+ session .MakeRequest (t , req , http .StatusSeeOther )
259+
260+ // Check file was deleted
261+ req = NewRequestf (t , "GET" , "/%s/%s/src/branch/%s/%s" , user , repo , targetBranch , filePath )
262+ session .MakeRequest (t , req , http .StatusNotFound )
263+ }
264+
265+ func TestPatchFile (t * testing.T ) {
266+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
267+ session := loginUser (t , "user2" )
268+ testPatchFile (t , session , "user2" , "user2" , "repo1" , "master" , "feature/test" )
269+ })
270+ }
271+
272+ func TestPatchFileFork (t * testing.T ) {
273+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
274+ session := loginUser (t , "user4" )
275+ forkToEdit (t , session , "user2" , "repo1" , "_diffpatch" , "master" , "README.md" )
276+ testPatchFile (t , session , "user4" , "user2" , "repo1" , "master" , "feature/test" )
277+ })
278+ }
279+
280+ func testPatchFile (t * testing.T , session * TestSession , user , owner , repo , branch , targetBranch string ) {
281+ // Request editor page
282+ newURL := fmt .Sprintf ("/%s/%s/_diffpatch/%s/" , owner , repo , branch )
283+ req := NewRequest (t , "GET" , newURL )
284+ resp := session .MakeRequest (t , req , http .StatusOK )
285+
286+ doc := NewHTMLParser (t , resp .Body )
287+ lastCommit := doc .GetInputValueByName ("last_commit" )
288+ assert .NotEmpty (t , lastCommit )
289+
290+ // Save new file to master branch
291+ req = NewRequestWithValues (t , "POST" , newURL , map [string ]string {
292+ "_csrf" : doc .GetCSRF (),
293+ "last_commit" : lastCommit ,
294+ "tree_path" : "__dummy__" ,
295+ "content" : `diff --git a/patch-file-1.txt b/patch-file-1.txt
296+ new file mode 100644
297+ index 0000000000..aaaaaaaaaa
298+ --- /dev/null
299+ +++ b/patch-file-1.txt
300+ @@ -0,0 +1 @@
301+ +File 1
302+ ` ,
303+ "commit_choice" : "commit-to-new-branch" ,
304+ "new_branch_name" : targetBranch ,
305+ })
306+ session .MakeRequest (t , req , http .StatusSeeOther )
307+
308+ // Check new file exists
309+ req = NewRequestf (t , "GET" , "/%s/%s/src/branch/%s/%s" , user , repo , targetBranch , "patch-file-1.txt" )
310+ session .MakeRequest (t , req , http .StatusOK )
311+ }
312+
188313func TestWebGitCommitEmail (t * testing.T ) {
189314 onGiteaRun (t , func (t * testing.T , _ * url.URL ) {
190315 user := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
@@ -337,3 +462,69 @@ index 0000000000..bbbbbbbbbb
337462 })
338463 })
339464}
465+
466+ func forkToEdit (t * testing.T , session * TestSession , owner , repo , operation , branch , filePath string ) {
467+ // Attempt to edit file
468+ req := NewRequest (t , "GET" , path .Join (owner , repo , operation , branch , filePath ))
469+ resp := session .MakeRequest (t , req , http .StatusOK )
470+ htmlDoc := NewHTMLParser (t , resp .Body )
471+
472+ // Fork
473+ req = NewRequestWithValues (t , "POST" , path .Join (owner , repo , "_fork_to_edit" , branch ),
474+ map [string ]string {
475+ "_csrf" : htmlDoc .GetCSRF (),
476+ "tree_path" : filePath ,
477+ "edit_operation" : operation ,
478+ },
479+ )
480+ session .MakeRequest (t , req , http .StatusSeeOther )
481+ }
482+
483+ func testForkToEdit (t * testing.T , session * TestSession , user , owner , repo , branch , filePath string ) {
484+ // Fork repository because we can't edit it
485+ forkToEdit (t , session , owner , repo , "_edit" , branch , filePath )
486+
487+ // Check the existence of the forked repo
488+ req := NewRequestf (t , "GET" , "/%s/%s/settings" , user , repo )
489+ resp := session .MakeRequest (t , req , http .StatusOK )
490+ htmlDoc := NewHTMLParser (t , resp .Body )
491+
492+ // Archive the repository
493+ req = NewRequestWithValues (t , "POST" , path .Join (user , repo , "settings" ),
494+ map [string ]string {
495+ "_csrf" : htmlDoc .GetCSRF (),
496+ "repo_name" : repo ,
497+ "action" : "archive" ,
498+ },
499+ )
500+ session .MakeRequest (t , req , http .StatusSeeOther )
501+
502+ // Check editing archived repository is disabled
503+ req = NewRequest (t , "GET" , path .Join (owner , repo , "_edit" , branch , filePath ))
504+ resp = session .MakeRequest (t , req , http .StatusOK )
505+ assert .Contains (t , resp .Body .String (), "Fork Repository Not Editable" )
506+
507+ // Unfork the repository
508+ req = NewRequestWithValues (t , "POST" , path .Join (user , repo , "settings" ),
509+ map [string ]string {
510+ "_csrf" : htmlDoc .GetCSRF (),
511+ "repo_name" : repo ,
512+ "action" : "convert_fork" ,
513+ },
514+ )
515+ session .MakeRequest (t , req , http .StatusSeeOther )
516+
517+ // Fork repository again
518+ forkToEdit (t , session , owner , repo , "_edit" , branch , filePath )
519+
520+ // Check the existence of the forked repo with unique name
521+ req = NewRequestf (t , "GET" , "/%s/%s-1" , user , repo )
522+ session .MakeRequest (t , req , http .StatusOK )
523+ }
524+
525+ func TestForkToEdit (t * testing.T ) {
526+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
527+ session := loginUser (t , "user4" )
528+ testForkToEdit (t , session , "user4" , "user2" , "repo1" , "master" , "README.md" )
529+ })
530+ }
0 commit comments