##// END OF EJS Templates
fix(tests): fixed tests for vcs-operations
super-admin -
r5292:e8e7c927 default
parent child Browse files
Show More
@@ -1,373 +1,376 b''
1 1
2 2 # Copyright (C) 2010-2023 RhodeCode GmbH
3 3 #
4 4 # This program is free software: you can redistribute it and/or modify
5 5 # it under the terms of the GNU Affero General Public License, version 3
6 6 # (only), as published by the Free Software Foundation.
7 7 #
8 8 # This program is distributed in the hope that it will be useful,
9 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 11 # GNU General Public License for more details.
12 12 #
13 13 # You should have received a copy of the GNU Affero General Public License
14 14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 15 #
16 16 # This program is dual-licensed. If you wish to learn more about the
17 17 # RhodeCode Enterprise Edition, including its added features, Support services,
18 18 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 19
20 20 """
21 21 Test suite for making push/pull operations, on specially modified INI files
22 22
23 23 .. important::
24 24
25 25 You must have git >= 1.8.5 for tests to work fine. With 68b939b git started
26 26 to redirect things to stderr instead of stdout.
27 27 """
28 28
29 29
30 30 import time
31 31 import logging
32 32
33 33 import pytest
34 34
35 35 from rhodecode.lib import rc_cache
36 36 from rhodecode.model.auth_token import AuthTokenModel
37 37 from rhodecode.model.db import Repository, UserIpMap, CacheKey
38 38 from rhodecode.model.meta import Session
39 39 from rhodecode.model.repo import RepoModel
40 40 from rhodecode.model.user import UserModel
41 41 from rhodecode.tests import (GIT_REPO, HG_REPO, TEST_USER_ADMIN_LOGIN)
42 42 from rhodecode.tests.utils import assert_message_in_log
43 43
44 44 from rhodecode.tests.vcs_operations import (
45 45 Command, _check_proper_clone, _check_proper_git_push,
46 46 _add_files_and_push, HG_REPO_WITH_GROUP, GIT_REPO_WITH_GROUP)
47 47
48 48
49 49 @pytest.mark.usefixtures("disable_locking", "disable_anonymous_user")
50 50 class TestVCSOperations(object):
51 51
52 52 def test_clone_hg_repo_by_admin(self, rc_web_server, tmpdir):
53 53 clone_url = rc_web_server.repo_clone_url(HG_REPO)
54 54 stdout, stderr = Command('/tmp').execute(
55 55 'hg clone', clone_url, tmpdir.strpath)
56 56 _check_proper_clone(stdout, stderr, 'hg')
57 57
58 58 def test_clone_hg_repo_by_admin_pull_protocol(self, rc_web_server, tmpdir):
59 59 clone_url = rc_web_server.repo_clone_url(HG_REPO)
60 60 stdout, stderr = Command('/tmp').execute(
61 61 'hg clone --pull', clone_url, tmpdir.strpath)
62 62 _check_proper_clone(stdout, stderr, 'hg')
63 63
64 64 def test_clone_hg_repo_by_admin_pull_stream_protocol(self, rc_web_server, tmpdir):
65 65 clone_url = rc_web_server.repo_clone_url(HG_REPO)
66 66 stdout, stderr = Command('/tmp').execute(
67 67 'hg clone --pull --stream', clone_url, tmpdir.strpath)
68 68 assert 'files to transfer,' in stdout
69 69 assert 'transferred 1.' in stdout
70 70 assert '114 files updated,' in stdout
71 71
72 72 def test_clone_git_repo_by_admin(self, rc_web_server, tmpdir):
73 73 clone_url = rc_web_server.repo_clone_url(GIT_REPO)
74 74 cmd = Command('/tmp')
75 75 stdout, stderr = cmd.execute('git clone', clone_url, tmpdir.strpath)
76 76 _check_proper_clone(stdout, stderr, 'git')
77 77 cmd.assert_returncode_success()
78 78
79 79 def test_clone_git_repo_by_admin_with_git_suffix(self, rc_web_server, tmpdir):
80 80 clone_url = rc_web_server.repo_clone_url(GIT_REPO)
81 81 cmd = Command('/tmp')
82 82 stdout, stderr = cmd.execute('git clone', clone_url+".git", tmpdir.strpath)
83 83 _check_proper_clone(stdout, stderr, 'git')
84 84 cmd.assert_returncode_success()
85 85
86 86 def test_clone_hg_repo_by_id_by_admin(self, rc_web_server, tmpdir):
87 87 repo_id = Repository.get_by_repo_name(HG_REPO).repo_id
88 88 clone_url = rc_web_server.repo_clone_url('_%s' % repo_id)
89 89 stdout, stderr = Command('/tmp').execute(
90 90 'hg clone', clone_url, tmpdir.strpath)
91 91 _check_proper_clone(stdout, stderr, 'hg')
92 92
93 93 def test_clone_git_repo_by_id_by_admin(self, rc_web_server, tmpdir):
94 94 repo_id = Repository.get_by_repo_name(GIT_REPO).repo_id
95 95 clone_url = rc_web_server.repo_clone_url('_%s' % repo_id)
96 96 cmd = Command('/tmp')
97 97 stdout, stderr = cmd.execute('git clone', clone_url, tmpdir.strpath)
98 98 _check_proper_clone(stdout, stderr, 'git')
99 99 cmd.assert_returncode_success()
100 100
101 101 def test_clone_hg_repo_with_group_by_admin(self, rc_web_server, tmpdir):
102 102 clone_url = rc_web_server.repo_clone_url(HG_REPO_WITH_GROUP)
103 103 stdout, stderr = Command('/tmp').execute(
104 104 'hg clone', clone_url, tmpdir.strpath)
105 105 _check_proper_clone(stdout, stderr, 'hg')
106 106
107 107 def test_clone_git_repo_with_group_by_admin(self, rc_web_server, tmpdir):
108 108 clone_url = rc_web_server.repo_clone_url(GIT_REPO_WITH_GROUP)
109 109 cmd = Command('/tmp')
110 110 stdout, stderr = cmd.execute('git clone', clone_url, tmpdir.strpath)
111 111 _check_proper_clone(stdout, stderr, 'git')
112 112 cmd.assert_returncode_success()
113 113
114 114 def test_clone_git_repo_shallow_by_admin(self, rc_web_server, tmpdir):
115 115 clone_url = rc_web_server.repo_clone_url(GIT_REPO)
116 116 cmd = Command('/tmp')
117 117 stdout, stderr = cmd.execute(
118 118 'git clone --depth=1', clone_url, tmpdir.strpath)
119 119
120 120 assert '' == stdout
121 121 assert 'Cloning into' in stderr
122 122 cmd.assert_returncode_success()
123 123
124 124 def test_clone_wrong_credentials_hg(self, rc_web_server, tmpdir):
125 125 clone_url = rc_web_server.repo_clone_url(HG_REPO, passwd='bad!')
126 126 stdout, stderr = Command('/tmp').execute(
127 127 'hg clone', clone_url, tmpdir.strpath)
128 128 assert 'abort: authorization failed' in stderr
129 129
130 130 def test_clone_wrong_credentials_git(self, rc_web_server, tmpdir):
131 131 clone_url = rc_web_server.repo_clone_url(GIT_REPO, passwd='bad!')
132 132 stdout, stderr = Command('/tmp').execute(
133 133 'git clone', clone_url, tmpdir.strpath)
134 134 assert 'fatal: Authentication failed' in stderr
135 135
136 136 def test_clone_git_dir_as_hg(self, rc_web_server, tmpdir):
137 137 clone_url = rc_web_server.repo_clone_url(GIT_REPO)
138 138 stdout, stderr = Command('/tmp').execute(
139 139 'hg clone', clone_url, tmpdir.strpath)
140 140 assert 'HTTP Error 404: Not Found' in stderr
141 141
142 142 def test_clone_hg_repo_as_git(self, rc_web_server, tmpdir):
143 143 clone_url = rc_web_server.repo_clone_url(HG_REPO)
144 144 stdout, stderr = Command('/tmp').execute(
145 145 'git clone', clone_url, tmpdir.strpath)
146 146 assert 'not found' in stderr
147 147
148 148 def test_clone_non_existing_path_hg(self, rc_web_server, tmpdir):
149 149 clone_url = rc_web_server.repo_clone_url('trololo')
150 150 stdout, stderr = Command('/tmp').execute(
151 151 'hg clone', clone_url, tmpdir.strpath)
152 152 assert 'HTTP Error 404: Not Found' in stderr
153 153
154 154 def test_clone_non_existing_path_git(self, rc_web_server, tmpdir):
155 155 clone_url = rc_web_server.repo_clone_url('trololo')
156 156 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
157 157 assert 'not found' in stderr
158 158
159 159 def test_clone_hg_with_slashes(self, rc_web_server, tmpdir):
160 160 clone_url = rc_web_server.repo_clone_url('//' + HG_REPO)
161 161 stdout, stderr = Command('/tmp').execute('hg clone', clone_url, tmpdir.strpath)
162 162 assert 'HTTP Error 404: Not Found' in stderr
163 163
164 164 def test_clone_git_with_slashes(self, rc_web_server, tmpdir):
165 165 clone_url = rc_web_server.repo_clone_url('//' + GIT_REPO)
166 166 stdout, stderr = Command('/tmp').execute('git clone', clone_url)
167 167 assert 'not found' in stderr
168 168
169 169 def test_clone_existing_path_hg_not_in_database(
170 170 self, rc_web_server, tmpdir, fs_repo_only):
171 171
172 172 db_name = fs_repo_only('not-in-db-hg', repo_type='hg')
173 173 clone_url = rc_web_server.repo_clone_url(db_name)
174 174 stdout, stderr = Command('/tmp').execute(
175 175 'hg clone', clone_url, tmpdir.strpath)
176 176 assert 'HTTP Error 404: Not Found' in stderr
177 177
178 178 def test_clone_existing_path_git_not_in_database(
179 179 self, rc_web_server, tmpdir, fs_repo_only):
180 180 db_name = fs_repo_only('not-in-db-git', repo_type='git')
181 181 clone_url = rc_web_server.repo_clone_url(db_name)
182 182 stdout, stderr = Command('/tmp').execute(
183 183 'git clone', clone_url, tmpdir.strpath)
184 184 assert 'not found' in stderr
185 185
186 186 def test_clone_existing_path_hg_not_in_database_different_scm(
187 187 self, rc_web_server, tmpdir, fs_repo_only):
188 188 db_name = fs_repo_only('not-in-db-git', repo_type='git')
189 189 clone_url = rc_web_server.repo_clone_url(db_name)
190 190 stdout, stderr = Command('/tmp').execute(
191 191 'hg clone', clone_url, tmpdir.strpath)
192 192 assert 'HTTP Error 404: Not Found' in stderr
193 193
194 194 def test_clone_existing_path_git_not_in_database_different_scm(
195 195 self, rc_web_server, tmpdir, fs_repo_only):
196 196 db_name = fs_repo_only('not-in-db-hg', repo_type='hg')
197 197 clone_url = rc_web_server.repo_clone_url(db_name)
198 198 stdout, stderr = Command('/tmp').execute(
199 199 'git clone', clone_url, tmpdir.strpath)
200 200 assert 'not found' in stderr
201 201
202 202 def test_clone_non_existing_store_path_hg(self, rc_web_server, tmpdir, user_util):
203 203 repo = user_util.create_repo()
204 204 clone_url = rc_web_server.repo_clone_url(repo.repo_name)
205 205
206 206 # Damage repo by removing it's folder
207 207 RepoModel()._delete_filesystem_repo(repo)
208 208
209 209 stdout, stderr = Command('/tmp').execute(
210 210 'hg clone', clone_url, tmpdir.strpath)
211 211 assert 'HTTP Error 404: Not Found' in stderr
212 212
213 213 def test_clone_non_existing_store_path_git(self, rc_web_server, tmpdir, user_util):
214 214 repo = user_util.create_repo(repo_type='git')
215 215 clone_url = rc_web_server.repo_clone_url(repo.repo_name)
216 216
217 217 # Damage repo by removing it's folder
218 218 RepoModel()._delete_filesystem_repo(repo)
219 219
220 220 stdout, stderr = Command('/tmp').execute(
221 221 'git clone', clone_url, tmpdir.strpath)
222 222 assert 'not found' in stderr
223 223
224 224 def test_push_new_file_hg(self, rc_web_server, tmpdir):
225 225 clone_url = rc_web_server.repo_clone_url(HG_REPO)
226 226 stdout, stderr = Command('/tmp').execute(
227 227 'hg clone', clone_url, tmpdir.strpath)
228 228
229 229 stdout, stderr = _add_files_and_push(
230 230 'hg', tmpdir.strpath, clone_url=clone_url)
231 231
232 232 assert 'pushing to' in stdout
233 233 assert 'size summary' in stdout
234 234
235 235 def test_push_new_file_git(self, rc_web_server, tmpdir):
236 236 clone_url = rc_web_server.repo_clone_url(GIT_REPO)
237 237 stdout, stderr = Command('/tmp').execute(
238 238 'git clone', clone_url, tmpdir.strpath)
239 239
240 240 # commit some stuff into this repo
241 241 stdout, stderr = _add_files_and_push(
242 242 'git', tmpdir.strpath, clone_url=clone_url)
243 243
244 244 _check_proper_git_push(stdout, stderr)
245 245
246 246 def test_push_invalidates_cache(self, rc_web_server, tmpdir):
247 247 hg_repo = Repository.get_by_repo_name(HG_REPO)
248 248
249 249 # init cache objects
250 250 CacheKey.delete_all_cache()
251 cache_namespace_uid = 'cache_push_test.{}'.format(hg_repo.repo_id)
251
252 252 repo_namespace_key = CacheKey.REPO_INVALIDATION_NAMESPACE.format(repo_id=hg_repo.repo_id)
253 253
254 254 inv_context_manager = rc_cache.InvalidationContext(key=repo_namespace_key)
255 255
256 256 with inv_context_manager as invalidation_context:
257 257 # __enter__ will create and register cache objects
258 258 pass
259 259
260 cache_keys = hg_repo.cache_keys
261 assert cache_keys != []
262 old_ids = [x.cache_state_uid for x in cache_keys]
263
260 264 # clone to init cache
261 265 clone_url = rc_web_server.repo_clone_url(hg_repo.repo_name)
262 266 stdout, stderr = Command('/tmp').execute(
263 267 'hg clone', clone_url, tmpdir.strpath)
264 268
265 269 cache_keys = hg_repo.cache_keys
266 270 assert cache_keys != []
267 271 for key in cache_keys:
268 272 assert key.cache_active is True
269 273
270 274 # PUSH that should trigger invalidation cache
271 275 stdout, stderr = _add_files_and_push(
272 276 'hg', tmpdir.strpath, clone_url=clone_url, files_no=1)
273 277
274 278 # flush...
275 279 Session().commit()
276 280 hg_repo = Repository.get_by_repo_name(HG_REPO)
277 281 cache_keys = hg_repo.cache_keys
278 282 assert cache_keys != []
279 for key in cache_keys:
280 # keys should be marked as not active
281 assert key.cache_active is False
283 new_ids = [x.cache_state_uid for x in cache_keys]
284 assert new_ids != old_ids
282 285
283 286 def test_push_wrong_credentials_hg(self, rc_web_server, tmpdir):
284 287 clone_url = rc_web_server.repo_clone_url(HG_REPO)
285 288 stdout, stderr = Command('/tmp').execute(
286 289 'hg clone', clone_url, tmpdir.strpath)
287 290
288 291 push_url = rc_web_server.repo_clone_url(
289 292 HG_REPO, user='bad', passwd='name')
290 293 stdout, stderr = _add_files_and_push(
291 294 'hg', tmpdir.strpath, clone_url=push_url)
292 295
293 296 assert 'abort: authorization failed' in stderr
294 297
295 298 def test_push_wrong_credentials_git(self, rc_web_server, tmpdir):
296 299 clone_url = rc_web_server.repo_clone_url(GIT_REPO)
297 300 stdout, stderr = Command('/tmp').execute(
298 301 'git clone', clone_url, tmpdir.strpath)
299 302
300 303 push_url = rc_web_server.repo_clone_url(
301 304 GIT_REPO, user='bad', passwd='name')
302 305 stdout, stderr = _add_files_and_push(
303 306 'git', tmpdir.strpath, clone_url=push_url)
304 307
305 308 assert 'fatal: Authentication failed' in stderr
306 309
307 310 def test_push_back_to_wrong_url_hg(self, rc_web_server, tmpdir):
308 311 clone_url = rc_web_server.repo_clone_url(HG_REPO)
309 312 stdout, stderr = Command('/tmp').execute(
310 313 'hg clone', clone_url, tmpdir.strpath)
311 314
312 315 stdout, stderr = _add_files_and_push(
313 316 'hg', tmpdir.strpath,
314 317 clone_url=rc_web_server.repo_clone_url('not-existing'))
315 318
316 319 assert 'HTTP Error 404: Not Found' in stderr
317 320
318 321 def test_push_back_to_wrong_url_git(self, rc_web_server, tmpdir):
319 322 clone_url = rc_web_server.repo_clone_url(GIT_REPO)
320 323 stdout, stderr = Command('/tmp').execute(
321 324 'git clone', clone_url, tmpdir.strpath)
322 325
323 326 stdout, stderr = _add_files_and_push(
324 327 'git', tmpdir.strpath,
325 328 clone_url=rc_web_server.repo_clone_url('not-existing'))
326 329
327 330 assert 'not found' in stderr
328 331
329 332 def test_ip_restriction_hg(self, rc_web_server, tmpdir):
330 333 user_model = UserModel()
331 334 try:
332 335 user_model.add_extra_ip(TEST_USER_ADMIN_LOGIN, '10.10.10.10/32')
333 336 Session().commit()
334 337 time.sleep(2)
335 338 clone_url = rc_web_server.repo_clone_url(HG_REPO)
336 339 stdout, stderr = Command('/tmp').execute(
337 340 'hg clone', clone_url, tmpdir.strpath)
338 341 assert 'abort: HTTP Error 403: Forbidden' in stderr
339 342 finally:
340 343 # release IP restrictions
341 344 for ip in UserIpMap.getAll():
342 345 UserIpMap.delete(ip.ip_id)
343 346 Session().commit()
344 347
345 348 time.sleep(2)
346 349
347 350 stdout, stderr = Command('/tmp').execute(
348 351 'hg clone', clone_url, tmpdir.strpath)
349 352 _check_proper_clone(stdout, stderr, 'hg')
350 353
351 354 def test_ip_restriction_git(self, rc_web_server, tmpdir):
352 355 user_model = UserModel()
353 356 try:
354 357 user_model.add_extra_ip(TEST_USER_ADMIN_LOGIN, '10.10.10.10/32')
355 358 Session().commit()
356 359 time.sleep(2)
357 360 clone_url = rc_web_server.repo_clone_url(GIT_REPO)
358 361 stdout, stderr = Command('/tmp').execute(
359 362 'git clone', clone_url, tmpdir.strpath)
360 363 msg = "The requested URL returned error: 403"
361 364 assert msg in stderr
362 365 finally:
363 366 # release IP restrictions
364 367 for ip in UserIpMap.getAll():
365 368 UserIpMap.delete(ip.ip_id)
366 369 Session().commit()
367 370
368 371 time.sleep(2)
369 372
370 373 cmd = Command('/tmp')
371 374 stdout, stderr = cmd.execute('git clone', clone_url, tmpdir.strpath)
372 375 cmd.assert_returncode_success()
373 376 _check_proper_clone(stdout, stderr, 'git')
General Comments 0
You need to be logged in to leave comments. Login now