9
9
"net/http"
10
10
"net/url"
11
11
"strconv"
12
+ "strings"
12
13
"testing"
13
14
"time"
14
15
@@ -19,7 +20,6 @@ import (
19
20
"code.gitea.io/gitea/modules/git"
20
21
"code.gitea.io/gitea/modules/gitrepo"
21
22
"code.gitea.io/gitea/modules/setting"
22
- "code.gitea.io/gitea/modules/test"
23
23
gitea_context "code.gitea.io/gitea/services/context"
24
24
"code.gitea.io/gitea/services/migrations"
25
25
mirror_service "code.gitea.io/gitea/services/mirror"
@@ -49,7 +49,8 @@ func testMirrorPush(t *testing.T, u *url.URL) {
49
49
50
50
ctx := NewAPITestContext (t , user .LowerName , srcRepo .Name )
51
51
52
- doCreatePushMirror (ctx , fmt .Sprintf ("%s%s/%s" , u .String (), url .PathEscape (ctx .Username ), url .PathEscape (mirrorRepo .Name )), user .LowerName , userPassword )(t )
52
+ pushMirrorURL := fmt .Sprintf ("%s%s/%s" , u .String (), url .PathEscape (ctx .Username ), url .PathEscape (mirrorRepo .Name ))
53
+ testCreatePushMirror (t , ctx .Session , user .LowerName , srcRepo .Name , pushMirrorURL , user .LowerName , userPassword , "0" )
53
54
54
55
mirrors , _ , err := repo_model .GetPushMirrorsByRepoID (db .DefaultContext , srcRepo .ID , db.ListOptions {})
55
56
assert .NoError (t , err )
@@ -75,127 +76,81 @@ func testMirrorPush(t *testing.T, u *url.URL) {
75
76
assert .Equal (t , srcCommit .ID , mirrorCommit .ID )
76
77
77
78
// Cleanup
78
- doRemovePushMirror ( ctx , fmt . Sprintf ( "%s%s/%s" , u . String (), url . PathEscape ( ctx .Username ), url . PathEscape ( mirrorRepo . Name )), user .LowerName , userPassword , int (mirrors [0 ].ID ))( t )
79
+ assert . True ( t , doRemovePushMirror ( t , ctx .Session , user .LowerName , srcRepo . Name , int (mirrors [0 ].ID )))
79
80
mirrors , _ , err = repo_model .GetPushMirrorsByRepoID (db .DefaultContext , srcRepo .ID , db.ListOptions {})
80
81
assert .NoError (t , err )
81
82
assert .Len (t , mirrors , 0 )
82
83
}
83
84
84
- func doCreatePushMirror (ctx APITestContext , address , username , password string ) func (t * testing.T ) {
85
- return func (t * testing.T ) {
86
- csrf := GetUserCSRFToken (t , ctx .Session )
87
-
88
- req := NewRequestWithValues (t , "POST" , fmt .Sprintf ("/%s/%s/settings" , url .PathEscape (ctx .Username ), url .PathEscape (ctx .Reponame )), map [string ]string {
89
- "_csrf" : csrf ,
90
- "action" : "push-mirror-add" ,
91
- "push_mirror_address" : address ,
92
- "push_mirror_username" : username ,
93
- "push_mirror_password" : password ,
94
- "push_mirror_interval" : "0" ,
95
- })
96
- ctx .Session .MakeRequest (t , req , http .StatusSeeOther )
97
-
98
- flashCookie := ctx .Session .GetCookie (gitea_context .CookieNameFlash )
99
- assert .NotNil (t , flashCookie )
100
- assert .Contains (t , flashCookie .Value , "success" )
101
- }
85
+ func testCreatePushMirror (t * testing.T , session * TestSession , owner , repo , address , username , password , interval string ) {
86
+ req := NewRequestWithValues (t , "POST" , fmt .Sprintf ("/%s/%s/settings" , url .PathEscape (owner ), url .PathEscape (repo )), map [string ]string {
87
+ "_csrf" : GetUserCSRFToken (t , session ),
88
+ "action" : "push-mirror-add" ,
89
+ "push_mirror_address" : address ,
90
+ "push_mirror_username" : username ,
91
+ "push_mirror_password" : password ,
92
+ "push_mirror_interval" : interval ,
93
+ })
94
+ session .MakeRequest (t , req , http .StatusSeeOther )
95
+
96
+ flashCookie := session .GetCookie (gitea_context .CookieNameFlash )
97
+ assert .NotNil (t , flashCookie )
98
+ assert .Contains (t , flashCookie .Value , "success" )
102
99
}
103
100
104
- func doRemovePushMirror (ctx APITestContext , address , username , password string , pushMirrorID int ) func (t * testing.T ) {
105
- return func (t * testing.T ) {
106
- csrf := GetUserCSRFToken (t , ctx .Session )
107
-
108
- req := NewRequestWithValues (t , "POST" , fmt .Sprintf ("/%s/%s/settings" , url .PathEscape (ctx .Username ), url .PathEscape (ctx .Reponame )), map [string ]string {
109
- "_csrf" : csrf ,
110
- "action" : "push-mirror-remove" ,
111
- "push_mirror_id" : strconv .Itoa (pushMirrorID ),
112
- "push_mirror_address" : address ,
113
- "push_mirror_username" : username ,
114
- "push_mirror_password" : password ,
115
- "push_mirror_interval" : "0" ,
116
- })
117
- ctx .Session .MakeRequest (t , req , http .StatusSeeOther )
118
-
119
- flashCookie := ctx .Session .GetCookie (gitea_context .CookieNameFlash )
120
- assert .NotNil (t , flashCookie )
121
- assert .Contains (t , flashCookie .Value , "success" )
122
- }
101
+ func doRemovePushMirror (t * testing.T , session * TestSession , owner , repo string , pushMirrorID int ) bool {
102
+ req := NewRequestWithValues (t , "POST" , fmt .Sprintf ("/%s/%s/settings" , url .PathEscape (owner ), url .PathEscape (repo )), map [string ]string {
103
+ "_csrf" : GetUserCSRFToken (t , session ),
104
+ "action" : "push-mirror-remove" ,
105
+ "push_mirror_id" : strconv .Itoa (pushMirrorID ),
106
+ })
107
+ resp := session .MakeRequest (t , req , NoExpectedStatus )
108
+ flashCookie := session .GetCookie (gitea_context .CookieNameFlash )
109
+ return resp .Code == http .StatusSeeOther && flashCookie != nil && strings .Contains (flashCookie .Value , "success" )
123
110
}
124
111
125
- func TestRepoSettingPushMirror (t * testing.T ) {
112
+ func doUpdatePushMirror (t * testing.T , session * TestSession , owner , repo string , pushMirrorID int64 , interval string ) bool {
113
+ req := NewRequestWithValues (t , "POST" , fmt .Sprintf ("/%s/%s/settings" , owner , repo ), map [string ]string {
114
+ "_csrf" : GetUserCSRFToken (t , session ),
115
+ "action" : "push-mirror-update" ,
116
+ "push_mirror_id" : strconv .FormatInt (pushMirrorID , 10 ),
117
+ "push_mirror_interval" : interval ,
118
+ "push_mirror_defer_sync" : "true" ,
119
+ })
120
+ resp := session .MakeRequest (t , req , NoExpectedStatus )
121
+ return resp .Code == http .StatusSeeOther
122
+ }
123
+
124
+ func TestRepoSettingPushMirrorUpdate (t * testing.T ) {
126
125
defer tests .PrepareTestEnv (t )()
126
+ setting .Migrations .AllowLocalNetworks = true
127
+ assert .NoError (t , migrations .Init ())
127
128
128
129
session := loginUser (t , "user2" )
129
-
130
- repoPrefix := "/user2/repo2"
131
130
repo2 := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 2 })
131
+ testCreatePushMirror (t , session , "user2" , "repo2" , "https://127.0.0.1/user1/repo1.git" , "" , "" , "24h" )
132
132
133
- defer func () {
134
- migrations .Init ()
135
- }()
136
- defer test .MockVariableValue (& setting .Migrations .AllowedDomains , "127.0.0.1" )()
137
- assert .NoError (t , migrations .Init ())
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
- onGiteaRun (t , func (t * testing.T , u * url.URL ) {
145
- t .Run ("Push Mirror Add" , func (t * testing.T ) {
146
- req = NewRequestWithValues (t , "POST" , repoPrefix + "/settings" , map [string ]string {
147
- "_csrf" : htmlDoc .GetCSRF (),
148
- "action" : "push-mirror-add" ,
149
- "push_mirror_address" : u .String () + "/user1/repo1.git" ,
150
- "push_mirror_interval" : "0" ,
151
- })
152
- session .MakeRequest (t , req , http .StatusSeeOther )
153
-
154
- flashCookie := session .GetCookie (gitea_context .CookieNameFlash )
155
- assert .NotNil (t , flashCookie )
156
- assert .Contains (t , flashCookie .Value , "success" )
157
-
158
- mirrors , cnt , err := repo_model .GetPushMirrorsByRepoID (db .DefaultContext , repo2 .ID , db.ListOptions {})
159
- assert .NoError (t , err )
160
- assert .Len (t , mirrors , 1 )
161
- assert .EqualValues (t , 1 , cnt )
162
- assert .EqualValues (t , 0 , mirrors [0 ].Interval )
163
- })
164
-
165
- mirrors , _ , _ := repo_model .GetPushMirrorsByRepoID (db .DefaultContext , repo2 .ID , db.ListOptions {})
166
-
167
- t .Run ("Push Mirror Update" , func (t * testing.T ) {
168
- req := NewRequestWithValues (t , "POST" , repoPrefix + "/settings" , map [string ]string {
169
- "_csrf" : htmlDoc .GetCSRF (),
170
- "action" : "push-mirror-update" ,
171
- "push_mirror_id" : strconv .FormatInt (mirrors [0 ].ID , 10 ),
172
- "push_mirror_interval" : "10m0s" ,
173
- })
174
- session .MakeRequest (t , req , http .StatusSeeOther )
175
-
176
- mirror , err := repo_model .GetPushMirrorByID (db .DefaultContext , mirrors [0 ].ID )
177
- assert .NoError (t , err )
178
- assert .EqualValues (t , 10 * time .Minute , mirror .Interval )
179
-
180
- req = NewRequestWithValues (t , "POST" , repoPrefix + "/settings" , map [string ]string {
181
- "_csrf" : htmlDoc .GetCSRF (),
182
- "action" : "push-mirror-update" ,
183
- "push_mirror_id" : strconv .FormatInt (9999 , 10 ), // 1 is an mirror ID which is not exist
184
- "push_mirror_interval" : "10m0s" ,
185
- })
186
- session .MakeRequest (t , req , http .StatusNotFound )
187
- })
188
-
189
- t .Run ("Push Mirror Remove" , func (t * testing.T ) {
190
- req := NewRequestWithValues (t , "POST" , repoPrefix + "/settings" , map [string ]string {
191
- "_csrf" : htmlDoc .GetCSRF (),
192
- "action" : "push-mirror-remove" ,
193
- "push_mirror_id" : strconv .FormatInt (mirrors [0 ].ID , 10 ),
194
- })
195
- session .MakeRequest (t , req , http .StatusSeeOther )
196
-
197
- _ , err := repo_model .GetPushMirrorByID (db .DefaultContext , mirrors [0 ].ID )
198
- assert .Error (t , err )
199
- })
200
- })
133
+ pushMirrors , cnt , err := repo_model .GetPushMirrorsByRepoID (db .DefaultContext , repo2 .ID , db.ListOptions {})
134
+ assert .NoError (t , err )
135
+ assert .EqualValues (t , 1 , cnt )
136
+ assert .EqualValues (t , 24 * time .Hour , pushMirrors [0 ].Interval )
137
+ repo2PushMirrorID := pushMirrors [0 ].ID
138
+
139
+ // update repo2 push mirror
140
+ assert .True (t , doUpdatePushMirror (t , session , "user2" , "repo2" , repo2PushMirrorID , "10m0s" ))
141
+ pushMirror := unittest .AssertExistsAndLoadBean (t , & repo_model.PushMirror {ID : repo2PushMirrorID })
142
+ assert .EqualValues (t , 10 * time .Minute , pushMirror .Interval )
143
+
144
+ // avoid updating repo1 push mirror
145
+ assert .False (t , doUpdatePushMirror (t , session , "user2" , "repo1" , repo2PushMirrorID , "20m0s" ))
146
+ pushMirror = unittest .AssertExistsAndLoadBean (t , & repo_model.PushMirror {ID : repo2PushMirrorID })
147
+ assert .EqualValues (t , 10 * time .Minute , pushMirror .Interval ) // not changed
148
+
149
+ // avoid deleting repo2 push mirror
150
+ assert .False (t , doRemovePushMirror (t , session , "user2" , "repo1" , int (repo2PushMirrorID )))
151
+ unittest .AssertExistsAndLoadBean (t , & repo_model.PushMirror {ID : repo2PushMirrorID })
152
+
153
+ // delete repo2 push mirror
154
+ assert .True (t , doRemovePushMirror (t , session , "user2" , "repo2" , int (repo2PushMirrorID )))
155
+ unittest .AssertNotExistsBean (t , & repo_model.PushMirror {ID : repo2PushMirrorID })
201
156
}
0 commit comments