@@ -10,6 +10,7 @@ import (
1010	"net/url" 
1111	"strconv" 
1212	"testing" 
13+ 	"time" 
1314
1415	"code.gitea.io/gitea/models/db" 
1516	repo_model "code.gitea.io/gitea/models/repo" 
@@ -18,6 +19,7 @@ import (
1819	"code.gitea.io/gitea/modules/git" 
1920	"code.gitea.io/gitea/modules/gitrepo" 
2021	"code.gitea.io/gitea/modules/setting" 
22+ 	"code.gitea.io/gitea/modules/test" 
2123	gitea_context "code.gitea.io/gitea/services/context" 
2224	"code.gitea.io/gitea/services/migrations" 
2325	mirror_service "code.gitea.io/gitea/services/mirror" 
@@ -119,3 +121,88 @@ func doRemovePushMirror(ctx APITestContext, address, username, password string,
119121		assert .Contains (t , flashCookie .Value , "success" )
120122	}
121123}
124+ 
125+ func  TestRepoSettingPushMirror (t  * testing.T ) {
126+ 	defer  tests .PrepareTestEnv (t )()
127+ 
128+ 	session  :=  loginUser (t , "user2" )
129+ 
130+ 	repoPrefix  :=  "/user2/repo2" 
131+ 	repo2  :=  unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 2 })
132+ 
133+ 	defer  test .MockVariableValue (& setting .Migrations .AllowedDomains , "127.0.0.1" )()
134+ 	assert .NoError (t , migrations .Init ())
135+ 	defer  func () {
136+ 		migrations .Init ()
137+ 	}()
138+ 
139+ 	// visit repository setting page 
140+ 	req  :=  NewRequest (t , "GET" , repoPrefix + "/settings" )
141+ 	resp  :=  session .MakeRequest (t , req , http .StatusOK )
142+ 	htmlDoc  :=  NewHTMLParser (t , resp .Body )
143+ 
144+ 	defer  func () {
145+ 		// avoid dirty mirror data once test failure 
146+ 		repo_model .DeletePushMirrors (db .DefaultContext , repo_model.PushMirrorOptions {
147+ 			RepoID : repo2 .ID ,
148+ 		})
149+ 	}()
150+ 
151+ 	onGiteaRun (t , func (t  * testing.T , u  * url.URL ) {
152+ 		t .Run ("Push Mirror Add" , func (t  * testing.T ) {
153+ 			req  =  NewRequestWithValues (t , "POST" , repoPrefix + "/settings" , map [string ]string {
154+ 				"_csrf" :                htmlDoc .GetCSRF (),
155+ 				"action" :               "push-mirror-add" ,
156+ 				"push_mirror_address" :  u .String () +  "/user1/repo1.git" ,
157+ 				"push_mirror_interval" : "0" ,
158+ 			})
159+ 			session .MakeRequest (t , req , http .StatusSeeOther )
160+ 
161+ 			flashCookie  :=  session .GetCookie (gitea_context .CookieNameFlash )
162+ 			assert .NotNil (t , flashCookie )
163+ 			assert .Contains (t , flashCookie .Value , "success" )
164+ 
165+ 			mirrors , cnt , err  :=  repo_model .GetPushMirrorsByRepoID (db .DefaultContext , repo2 .ID , db.ListOptions {})
166+ 			assert .NoError (t , err )
167+ 			assert .Len (t , mirrors , 1 )
168+ 			assert .EqualValues (t , 1 , cnt )
169+ 			assert .EqualValues (t , 0 , mirrors [0 ].Interval )
170+ 		})
171+ 
172+ 		mirrors , _ , _  :=  repo_model .GetPushMirrorsByRepoID (db .DefaultContext , repo2 .ID , db.ListOptions {})
173+ 
174+ 		t .Run ("Push Mirror Update" , func (t  * testing.T ) {
175+ 			req  :=  NewRequestWithValues (t , "POST" , repoPrefix + "/settings" , map [string ]string {
176+ 				"_csrf" :                htmlDoc .GetCSRF (),
177+ 				"action" :               "push-mirror-update" ,
178+ 				"push_mirror_id" :       strconv .FormatInt (mirrors [0 ].ID , 10 ),
179+ 				"push_mirror_interval" : "10m0s" ,
180+ 			})
181+ 			session .MakeRequest (t , req , http .StatusSeeOther )
182+ 
183+ 			mirror , err  :=  repo_model .GetPushMirrorByID (db .DefaultContext , mirrors [0 ].ID )
184+ 			assert .NoError (t , err )
185+ 			assert .EqualValues (t , 10 * time .Minute , mirror .Interval )
186+ 
187+ 			req  =  NewRequestWithValues (t , "POST" , repoPrefix + "/settings" , map [string ]string {
188+ 				"_csrf" :                htmlDoc .GetCSRF (),
189+ 				"action" :               "push-mirror-update" ,
190+ 				"push_mirror_id" :       strconv .FormatInt (9999 , 10 ), // 1 is an mirror ID which is not exist 
191+ 				"push_mirror_interval" : "10m0s" ,
192+ 			})
193+ 			session .MakeRequest (t , req , http .StatusNotFound )
194+ 		})
195+ 
196+ 		t .Run ("Push Mirror Remove" , func (t  * testing.T ) {
197+ 			req  :=  NewRequestWithValues (t , "POST" , repoPrefix + "/settings" , map [string ]string {
198+ 				"_csrf" :          htmlDoc .GetCSRF (),
199+ 				"action" :         "push-mirror-remove" ,
200+ 				"push_mirror_id" : strconv .FormatInt (mirrors [0 ].ID , 10 ),
201+ 			})
202+ 			session .MakeRequest (t , req , http .StatusSeeOther )
203+ 
204+ 			_ , err  :=  repo_model .GetPushMirrorByID (db .DefaultContext , mirrors [0 ].ID )
205+ 			assert .Error (t , err )
206+ 		})
207+ 	})
208+ }
0 commit comments