##// END OF EJS Templates
tests: Fix a test that was broken due to changes around the rebase-merge option.
Martin Bornhold -
r421:381afb6c default
parent child Browse files
Show More
@@ -1,1037 +1,1038 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2016 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import mock
22 22 import pytest
23 23
24 24 from rhodecode.lib.utils2 import str2bool
25 25 from rhodecode.model.meta import Session
26 26 from rhodecode.model.settings import VcsSettingsModel, UiSetting
27 27
28 28
29 29 HOOKS_FORM_DATA = {
30 30 'hooks_changegroup_repo_size': True,
31 31 'hooks_changegroup_push_logger': True,
32 32 'hooks_outgoing_pull_logger': True
33 33 }
34 34
35 35 SVN_FORM_DATA = {
36 36 'new_svn_branch': 'test-branch',
37 37 'new_svn_tag': 'test-tag'
38 38 }
39 39
40 40 GENERAL_FORM_DATA = {
41 41 'rhodecode_pr_merge_enabled': True,
42 'rhodecode_use_outdated_comments': True
42 'rhodecode_use_outdated_comments': True,
43 'rhodecode_hg_use_rebase_for_merging': True,
43 44 }
44 45
45 46
46 47 class TestInheritGlobalSettingsProperty(object):
47 48 def test_get_raises_exception_when_repository_not_specified(self):
48 49 model = VcsSettingsModel()
49 50 with pytest.raises(Exception) as exc_info:
50 51 model.inherit_global_settings
51 52 assert exc_info.value.message == 'Repository is not specified'
52 53
53 54 def test_true_is_returned_when_value_is_not_found(self, repo_stub):
54 55 model = VcsSettingsModel(repo=repo_stub.repo_name)
55 56 assert model.inherit_global_settings is True
56 57
57 58 def test_value_is_returned(self, repo_stub, settings_util):
58 59 model = VcsSettingsModel(repo=repo_stub.repo_name)
59 60 settings_util.create_repo_rhodecode_setting(
60 61 repo_stub, VcsSettingsModel.INHERIT_SETTINGS, False, 'bool')
61 62 assert model.inherit_global_settings is False
62 63
63 64 def test_value_is_set(self, repo_stub):
64 65 model = VcsSettingsModel(repo=repo_stub.repo_name)
65 66 model.inherit_global_settings = False
66 67 setting = model.repo_settings.get_setting_by_name(
67 68 VcsSettingsModel.INHERIT_SETTINGS)
68 69 try:
69 70 assert setting.app_settings_type == 'bool'
70 71 assert setting.app_settings_value is False
71 72 finally:
72 73 Session().delete(setting)
73 74 Session().commit()
74 75
75 76 def test_set_raises_exception_when_repository_not_specified(self):
76 77 model = VcsSettingsModel()
77 78 with pytest.raises(Exception) as exc_info:
78 79 model.inherit_global_settings = False
79 80 assert exc_info.value.message == 'Repository is not specified'
80 81
81 82
82 83 class TestVcsSettingsModel(object):
83 84 def test_global_svn_branch_patterns(self):
84 85 model = VcsSettingsModel()
85 86 expected_result = {'test': 'test'}
86 87 with mock.patch.object(model, 'global_settings') as settings_mock:
87 88 get_settings = settings_mock.get_ui_by_section
88 89 get_settings.return_value = expected_result
89 90 settings_mock.return_value = expected_result
90 91 result = model.get_global_svn_branch_patterns()
91 92
92 93 get_settings.assert_called_once_with(model.SVN_BRANCH_SECTION)
93 94 assert expected_result == result
94 95
95 96 def test_repo_svn_branch_patterns(self):
96 97 model = VcsSettingsModel()
97 98 expected_result = {'test': 'test'}
98 99 with mock.patch.object(model, 'repo_settings') as settings_mock:
99 100 get_settings = settings_mock.get_ui_by_section
100 101 get_settings.return_value = expected_result
101 102 settings_mock.return_value = expected_result
102 103 result = model.get_repo_svn_branch_patterns()
103 104
104 105 get_settings.assert_called_once_with(model.SVN_BRANCH_SECTION)
105 106 assert expected_result == result
106 107
107 108 def test_repo_svn_branch_patterns_raises_exception_when_repo_is_not_set(
108 109 self):
109 110 model = VcsSettingsModel()
110 111 with pytest.raises(Exception) as exc_info:
111 112 model.get_repo_svn_branch_patterns()
112 113 assert exc_info.value.message == 'Repository is not specified'
113 114
114 115 def test_global_svn_tag_patterns(self):
115 116 model = VcsSettingsModel()
116 117 expected_result = {'test': 'test'}
117 118 with mock.patch.object(model, 'global_settings') as settings_mock:
118 119 get_settings = settings_mock.get_ui_by_section
119 120 get_settings.return_value = expected_result
120 121 settings_mock.return_value = expected_result
121 122 result = model.get_global_svn_tag_patterns()
122 123
123 124 get_settings.assert_called_once_with(model.SVN_TAG_SECTION)
124 125 assert expected_result == result
125 126
126 127 def test_repo_svn_tag_patterns(self):
127 128 model = VcsSettingsModel()
128 129 expected_result = {'test': 'test'}
129 130 with mock.patch.object(model, 'repo_settings') as settings_mock:
130 131 get_settings = settings_mock.get_ui_by_section
131 132 get_settings.return_value = expected_result
132 133 settings_mock.return_value = expected_result
133 134 result = model.get_repo_svn_tag_patterns()
134 135
135 136 get_settings.assert_called_once_with(model.SVN_TAG_SECTION)
136 137 assert expected_result == result
137 138
138 139 def test_repo_svn_tag_patterns_raises_exception_when_repo_is_not_set(self):
139 140 model = VcsSettingsModel()
140 141 with pytest.raises(Exception) as exc_info:
141 142 model.get_repo_svn_tag_patterns()
142 143 assert exc_info.value.message == 'Repository is not specified'
143 144
144 145 def test_get_global_settings(self):
145 146 expected_result = {'test': 'test'}
146 147 model = VcsSettingsModel()
147 148 with mock.patch.object(model, '_collect_all_settings') as collect_mock:
148 149 collect_mock.return_value = expected_result
149 150 result = model.get_global_settings()
150 151
151 152 collect_mock.assert_called_once_with(global_=True)
152 153 assert result == expected_result
153 154
154 155 def test_get_repo_settings(self, repo_stub):
155 156 model = VcsSettingsModel(repo=repo_stub.repo_name)
156 157 expected_result = {'test': 'test'}
157 158 with mock.patch.object(model, '_collect_all_settings') as collect_mock:
158 159 collect_mock.return_value = expected_result
159 160 result = model.get_repo_settings()
160 161
161 162 collect_mock.assert_called_once_with(global_=False)
162 163 assert result == expected_result
163 164
164 165 @pytest.mark.parametrize('settings, global_', [
165 166 ('global_settings', True),
166 167 ('repo_settings', False)
167 168 ])
168 169 def test_collect_all_settings(self, settings, global_):
169 170 model = VcsSettingsModel()
170 171 result_mock = self._mock_result()
171 172
172 173 settings_patch = mock.patch.object(model, settings)
173 174 with settings_patch as settings_mock:
174 175 settings_mock.get_ui_by_section_and_key.return_value = result_mock
175 176 settings_mock.get_setting_by_name.return_value = result_mock
176 177 result = model._collect_all_settings(global_=global_)
177 178
178 179 ui_settings = model.HG_SETTINGS + model.HOOKS_SETTINGS
179 180 self._assert_get_settings_calls(
180 181 settings_mock, ui_settings, model.GENERAL_SETTINGS)
181 182 self._assert_collect_all_settings_result(
182 183 ui_settings, model.GENERAL_SETTINGS, result)
183 184
184 185 @pytest.mark.parametrize('settings, global_', [
185 186 ('global_settings', True),
186 187 ('repo_settings', False)
187 188 ])
188 189 def test_collect_all_settings_without_empty_value(self, settings, global_):
189 190 model = VcsSettingsModel()
190 191
191 192 settings_patch = mock.patch.object(model, settings)
192 193 with settings_patch as settings_mock:
193 194 settings_mock.get_ui_by_section_and_key.return_value = None
194 195 settings_mock.get_setting_by_name.return_value = None
195 196 result = model._collect_all_settings(global_=global_)
196 197
197 198 assert result == {}
198 199
199 200 def _mock_result(self):
200 201 result_mock = mock.Mock()
201 202 result_mock.ui_value = 'ui_value'
202 203 result_mock.ui_active = True
203 204 result_mock.app_settings_value = 'setting_value'
204 205 return result_mock
205 206
206 207 def _assert_get_settings_calls(
207 208 self, settings_mock, ui_settings, general_settings):
208 209 assert (
209 210 settings_mock.get_ui_by_section_and_key.call_count ==
210 211 len(ui_settings))
211 212 assert (
212 213 settings_mock.get_setting_by_name.call_count ==
213 214 len(general_settings))
214 215
215 216 for section, key in ui_settings:
216 217 expected_call = mock.call(section, key)
217 218 assert (
218 219 expected_call in
219 220 settings_mock.get_ui_by_section_and_key.call_args_list)
220 221
221 222 for name in general_settings:
222 223 expected_call = mock.call(name)
223 224 assert (
224 225 expected_call in
225 226 settings_mock.get_setting_by_name.call_args_list)
226 227
227 228 def _assert_collect_all_settings_result(
228 229 self, ui_settings, general_settings, result):
229 230 expected_result = {}
230 231 for section, key in ui_settings:
231 232 key = '{}_{}'.format(section, key.replace('.', '_'))
232 233 value = True if section in ('extensions', 'hooks') else 'ui_value'
233 234 expected_result[key] = value
234 235
235 236 for name in general_settings:
236 237 key = 'rhodecode_' + name
237 238 expected_result[key] = 'setting_value'
238 239
239 240 assert expected_result == result
240 241
241 242
242 243 class TestCreateOrUpdateRepoHookSettings(object):
243 244 def test_create_when_no_repo_object_found(self, repo_stub):
244 245 model = VcsSettingsModel(repo=repo_stub.repo_name)
245 246
246 247 self._create_settings(model, HOOKS_FORM_DATA)
247 248
248 249 cleanup = []
249 250 try:
250 251 for section, key in model.HOOKS_SETTINGS:
251 252 ui = model.repo_settings.get_ui_by_section_and_key(
252 253 section, key)
253 254 assert ui.ui_active is True
254 255 cleanup.append(ui)
255 256 finally:
256 257 for ui in cleanup:
257 258 Session().delete(ui)
258 259 Session().commit()
259 260
260 261 def test_create_raises_exception_when_data_incomplete(self, repo_stub):
261 262 model = VcsSettingsModel(repo=repo_stub.repo_name)
262 263
263 264 deleted_key = 'hooks_changegroup_repo_size'
264 265 data = HOOKS_FORM_DATA.copy()
265 266 data.pop(deleted_key)
266 267
267 268 with pytest.raises(ValueError) as exc_info:
268 269 model.create_or_update_repo_hook_settings(data)
269 270 assert (
270 271 exc_info.value.message ==
271 272 'The given data does not contain {} key'.format(deleted_key))
272 273
273 274 def test_update_when_repo_object_found(self, repo_stub, settings_util):
274 275 model = VcsSettingsModel(repo=repo_stub.repo_name)
275 276 for section, key in model.HOOKS_SETTINGS:
276 277 settings_util.create_repo_rhodecode_ui(
277 278 repo_stub, section, None, key=key, active=False)
278 279 model.create_or_update_repo_hook_settings(HOOKS_FORM_DATA)
279 280 for section, key in model.HOOKS_SETTINGS:
280 281 ui = model.repo_settings.get_ui_by_section_and_key(section, key)
281 282 assert ui.ui_active is True
282 283
283 284 def _create_settings(self, model, data):
284 285 global_patch = mock.patch.object(model, 'global_settings')
285 286 global_setting = mock.Mock()
286 287 global_setting.ui_value = 'Test value'
287 288 with global_patch as global_mock:
288 289 global_mock.get_ui_by_section_and_key.return_value = global_setting
289 290 model.create_or_update_repo_hook_settings(HOOKS_FORM_DATA)
290 291
291 292
292 293 class TestUpdateGlobalHookSettings(object):
293 294 def test_update_raises_exception_when_data_incomplete(self):
294 295 model = VcsSettingsModel()
295 296
296 297 deleted_key = 'hooks_changegroup_repo_size'
297 298 data = HOOKS_FORM_DATA.copy()
298 299 data.pop(deleted_key)
299 300
300 301 with pytest.raises(ValueError) as exc_info:
301 302 model.update_global_hook_settings(data)
302 303 assert (
303 304 exc_info.value.message ==
304 305 'The given data does not contain {} key'.format(deleted_key))
305 306
306 307 def test_update_global_hook_settings(self, settings_util):
307 308 model = VcsSettingsModel()
308 309 setting_mock = mock.MagicMock()
309 310 setting_mock.ui_active = False
310 311 get_settings_patcher = mock.patch.object(
311 312 model.global_settings, 'get_ui_by_section_and_key',
312 313 return_value=setting_mock)
313 314 session_patcher = mock.patch('rhodecode.model.settings.Session')
314 315 with get_settings_patcher as get_settings_mock, session_patcher:
315 316 model.update_global_hook_settings(HOOKS_FORM_DATA)
316 317 assert setting_mock.ui_active is True
317 318 assert get_settings_mock.call_count == 3
318 319
319 320
320 321 class TestCreateOrUpdateRepoGeneralSettings(object):
321 322 def test_calls_create_or_update_general_settings(self, repo_stub):
322 323 model = VcsSettingsModel(repo=repo_stub.repo_name)
323 324 create_patch = mock.patch.object(
324 325 model, '_create_or_update_general_settings')
325 326 with create_patch as create_mock:
326 327 model.create_or_update_repo_pr_settings(GENERAL_FORM_DATA)
327 328 create_mock.assert_called_once_with(
328 329 model.repo_settings, GENERAL_FORM_DATA)
329 330
330 331 def test_raises_exception_when_repository_is_not_specified(self):
331 332 model = VcsSettingsModel()
332 333 with pytest.raises(Exception) as exc_info:
333 334 model.create_or_update_repo_pr_settings(GENERAL_FORM_DATA)
334 335 assert exc_info.value.message == 'Repository is not specified'
335 336
336 337
337 338 class TestCreateOrUpdatGlobalGeneralSettings(object):
338 339 def test_calls_create_or_update_general_settings(self):
339 340 model = VcsSettingsModel()
340 341 create_patch = mock.patch.object(
341 342 model, '_create_or_update_general_settings')
342 343 with create_patch as create_mock:
343 344 model.create_or_update_global_pr_settings(GENERAL_FORM_DATA)
344 345 create_mock.assert_called_once_with(
345 346 model.global_settings, GENERAL_FORM_DATA)
346 347
347 348
348 349 class TestCreateOrUpdateGeneralSettings(object):
349 350 def test_create_when_no_repo_settings_found(self, repo_stub):
350 351 model = VcsSettingsModel(repo=repo_stub.repo_name)
351 352 model._create_or_update_general_settings(
352 353 model.repo_settings, GENERAL_FORM_DATA)
353 354
354 355 cleanup = []
355 356 try:
356 357 for name in model.GENERAL_SETTINGS:
357 358 setting = model.repo_settings.get_setting_by_name(name)
358 359 assert setting.app_settings_value is True
359 360 cleanup.append(setting)
360 361 finally:
361 362 for setting in cleanup:
362 363 Session().delete(setting)
363 364 Session().commit()
364 365
365 366 def test_create_raises_exception_when_data_incomplete(self, repo_stub):
366 367 model = VcsSettingsModel(repo=repo_stub.repo_name)
367 368
368 369 deleted_key = 'rhodecode_pr_merge_enabled'
369 370 data = GENERAL_FORM_DATA.copy()
370 371 data.pop(deleted_key)
371 372
372 373 with pytest.raises(ValueError) as exc_info:
373 374 model._create_or_update_general_settings(model.repo_settings, data)
374 375 assert (
375 376 exc_info.value.message ==
376 377 'The given data does not contain {} key'.format(deleted_key))
377 378
378 379 def test_update_when_repo_setting_found(self, repo_stub, settings_util):
379 380 model = VcsSettingsModel(repo=repo_stub.repo_name)
380 381 for name in model.GENERAL_SETTINGS:
381 382 settings_util.create_repo_rhodecode_setting(
382 383 repo_stub, name, False, 'bool')
383 384
384 385 model._create_or_update_general_settings(
385 386 model.repo_settings, GENERAL_FORM_DATA)
386 387
387 388 for name in model.GENERAL_SETTINGS:
388 389 setting = model.repo_settings.get_setting_by_name(name)
389 390 assert setting.app_settings_value is True
390 391
391 392
392 393 class TestCreateRepoSvnSettings(object):
393 394 def test_calls_create_svn_settings(self, repo_stub):
394 395 model = VcsSettingsModel(repo=repo_stub.repo_name)
395 396 with mock.patch.object(model, '_create_svn_settings') as create_mock:
396 397 model.create_repo_svn_settings(SVN_FORM_DATA)
397 398 create_mock.assert_called_once_with(model.repo_settings, SVN_FORM_DATA)
398 399
399 400 def test_raises_exception_when_repository_is_not_specified(self):
400 401 model = VcsSettingsModel()
401 402 with pytest.raises(Exception) as exc_info:
402 403 model.create_repo_svn_settings(SVN_FORM_DATA)
403 404 assert exc_info.value.message == 'Repository is not specified'
404 405
405 406
406 407 class TestCreateGlobalSvnSettings(object):
407 408 def test_calls_create_svn_settings(self):
408 409 model = VcsSettingsModel()
409 410 with mock.patch.object(model, '_create_svn_settings') as create_mock:
410 411 model.create_global_svn_settings(SVN_FORM_DATA)
411 412 create_mock.assert_called_once_with(
412 413 model.global_settings, SVN_FORM_DATA)
413 414
414 415
415 416 class TestCreateSvnSettings(object):
416 417 def test_create(self, repo_stub):
417 418 model = VcsSettingsModel(repo=repo_stub.repo_name)
418 419 model._create_svn_settings(model.repo_settings, SVN_FORM_DATA)
419 420 Session().commit()
420 421
421 422 branch_ui = model.repo_settings.get_ui_by_section(
422 423 model.SVN_BRANCH_SECTION)
423 424 tag_ui = model.repo_settings.get_ui_by_section(
424 425 model.SVN_TAG_SECTION)
425 426
426 427 try:
427 428 assert len(branch_ui) == 1
428 429 assert len(tag_ui) == 1
429 430 finally:
430 431 Session().delete(branch_ui[0])
431 432 Session().delete(tag_ui[0])
432 433 Session().commit()
433 434
434 435 def test_create_tag(self, repo_stub):
435 436 model = VcsSettingsModel(repo=repo_stub.repo_name)
436 437 data = SVN_FORM_DATA.copy()
437 438 data.pop('new_svn_branch')
438 439 model._create_svn_settings(model.repo_settings, data)
439 440 Session().commit()
440 441
441 442 branch_ui = model.repo_settings.get_ui_by_section(
442 443 model.SVN_BRANCH_SECTION)
443 444 tag_ui = model.repo_settings.get_ui_by_section(
444 445 model.SVN_TAG_SECTION)
445 446
446 447 try:
447 448 assert len(branch_ui) == 0
448 449 assert len(tag_ui) == 1
449 450 finally:
450 451 Session().delete(tag_ui[0])
451 452 Session().commit()
452 453
453 454 def test_create_nothing_when_no_svn_settings_specified(self, repo_stub):
454 455 model = VcsSettingsModel(repo=repo_stub.repo_name)
455 456 model._create_svn_settings(model.repo_settings, {})
456 457 Session().commit()
457 458
458 459 branch_ui = model.repo_settings.get_ui_by_section(
459 460 model.SVN_BRANCH_SECTION)
460 461 tag_ui = model.repo_settings.get_ui_by_section(
461 462 model.SVN_TAG_SECTION)
462 463
463 464 assert len(branch_ui) == 0
464 465 assert len(tag_ui) == 0
465 466
466 467 def test_create_nothing_when_empty_settings_specified(self, repo_stub):
467 468 model = VcsSettingsModel(repo=repo_stub.repo_name)
468 469 data = {
469 470 'new_svn_branch': '',
470 471 'new_svn_tag': ''
471 472 }
472 473 model._create_svn_settings(model.repo_settings, data)
473 474 Session().commit()
474 475
475 476 branch_ui = model.repo_settings.get_ui_by_section(
476 477 model.SVN_BRANCH_SECTION)
477 478 tag_ui = model.repo_settings.get_ui_by_section(
478 479 model.SVN_TAG_SECTION)
479 480
480 481 assert len(branch_ui) == 0
481 482 assert len(tag_ui) == 0
482 483
483 484
484 485 class TestCreateOrUpdateUi(object):
485 486 def test_create(self, repo_stub):
486 487 model = VcsSettingsModel(repo=repo_stub.repo_name)
487 488 model._create_or_update_ui(
488 489 model.repo_settings, 'test-section', 'test-key', active=False,
489 490 value='False')
490 491 Session().commit()
491 492
492 493 created_ui = model.repo_settings.get_ui_by_section_and_key(
493 494 'test-section', 'test-key')
494 495
495 496 try:
496 497 assert created_ui.ui_active is False
497 498 assert str2bool(created_ui.ui_value) is False
498 499 finally:
499 500 Session().delete(created_ui)
500 501 Session().commit()
501 502
502 503 def test_update(self, repo_stub, settings_util):
503 504 model = VcsSettingsModel(repo=repo_stub.repo_name)
504 505
505 506 largefiles, phases = model.HG_SETTINGS
506 507 section = 'test-section'
507 508 key = 'test-key'
508 509 settings_util.create_repo_rhodecode_ui(
509 510 repo_stub, section, 'True', key=key, active=True)
510 511
511 512 model._create_or_update_ui(
512 513 model.repo_settings, section, key, active=False, value='False')
513 514 Session().commit()
514 515
515 516 created_ui = model.repo_settings.get_ui_by_section_and_key(
516 517 section, key)
517 518 assert created_ui.ui_active is False
518 519 assert str2bool(created_ui.ui_value) is False
519 520
520 521
521 522 class TestCreateOrUpdateRepoHgSettings(object):
522 523 FORM_DATA = {
523 524 'extensions_largefiles': False,
524 525 'phases_publish': False
525 526 }
526 527
527 528 def test_creates_repo_hg_settings_when_data_is_correct(self, repo_stub):
528 529 model = VcsSettingsModel(repo=repo_stub.repo_name)
529 530 with mock.patch.object(model, '_create_or_update_ui') as create_mock:
530 531 model.create_or_update_repo_hg_settings(self.FORM_DATA)
531 532 expected_calls = [
532 533 mock.call(model.repo_settings, 'extensions', 'largefiles',
533 534 active=False, value=''),
534 535 mock.call(model.repo_settings, 'phases', 'publish', value='False'),
535 536 ]
536 537 assert expected_calls == create_mock.call_args_list
537 538
538 539 @pytest.mark.parametrize('field_to_remove', FORM_DATA.keys())
539 540 def test_key_is_not_found(self, repo_stub, field_to_remove):
540 541 model = VcsSettingsModel(repo=repo_stub.repo_name)
541 542 data = self.FORM_DATA.copy()
542 543 data.pop(field_to_remove)
543 544 with pytest.raises(ValueError) as exc_info:
544 545 model.create_or_update_repo_hg_settings(data)
545 546 expected_message = 'The given data does not contain {} key'.format(
546 547 field_to_remove)
547 548 assert exc_info.value.message == expected_message
548 549
549 550 def test_create_raises_exception_when_repository_not_specified(self):
550 551 model = VcsSettingsModel()
551 552 with pytest.raises(Exception) as exc_info:
552 553 model.create_or_update_repo_hg_settings(self.FORM_DATA)
553 554 assert exc_info.value.message == 'Repository is not specified'
554 555
555 556
556 557 class TestUpdateGlobalSslSetting(object):
557 558 def test_updates_global_hg_settings(self):
558 559 model = VcsSettingsModel()
559 560 with mock.patch.object(model, '_create_or_update_ui') as create_mock:
560 561 model.update_global_ssl_setting('False')
561 562 create_mock.assert_called_once_with(
562 563 model.global_settings, 'web', 'push_ssl', value='False')
563 564
564 565
565 566 class TestUpdateGlobalPathSetting(object):
566 567 def test_updates_global_path_settings(self):
567 568 model = VcsSettingsModel()
568 569 with mock.patch.object(model, '_create_or_update_ui') as create_mock:
569 570 model.update_global_path_setting('False')
570 571 create_mock.assert_called_once_with(
571 572 model.global_settings, 'paths', '/', value='False')
572 573
573 574
574 575 class TestCreateOrUpdateGlobalHgSettings(object):
575 576 FORM_DATA = {
576 577 'extensions_largefiles': False,
577 578 'phases_publish': False,
578 579 'extensions_hgsubversion': False
579 580 }
580 581
581 582 def test_creates_repo_hg_settings_when_data_is_correct(self):
582 583 model = VcsSettingsModel()
583 584 with mock.patch.object(model, '_create_or_update_ui') as create_mock:
584 585 model.create_or_update_global_hg_settings(self.FORM_DATA)
585 586 expected_calls = [
586 587 mock.call(model.global_settings, 'extensions', 'largefiles',
587 588 active=False, value=''),
588 589 mock.call(model.global_settings, 'phases', 'publish',
589 590 value='False'),
590 591 mock.call(model.global_settings, 'extensions', 'hgsubversion',
591 592 active=False)
592 593 ]
593 594 assert expected_calls == create_mock.call_args_list
594 595
595 596 @pytest.mark.parametrize('field_to_remove', FORM_DATA.keys())
596 597 def test_key_is_not_found(self, repo_stub, field_to_remove):
597 598 model = VcsSettingsModel(repo=repo_stub.repo_name)
598 599 data = self.FORM_DATA.copy()
599 600 data.pop(field_to_remove)
600 601 with pytest.raises(Exception) as exc_info:
601 602 model.create_or_update_global_hg_settings(data)
602 603 expected_message = 'The given data does not contain {} key'.format(
603 604 field_to_remove)
604 605 assert exc_info.value.message == expected_message
605 606
606 607
607 608 class TestDeleteRepoSvnPattern(object):
608 609 def test_success_when_repo_is_set(self, backend_svn):
609 610 repo_name = backend_svn.repo_name
610 611 model = VcsSettingsModel(repo=repo_name)
611 612 delete_ui_patch = mock.patch.object(model.repo_settings, 'delete_ui')
612 613 with delete_ui_patch as delete_ui_mock:
613 614 model.delete_repo_svn_pattern(123)
614 615 delete_ui_mock.assert_called_once_with(123)
615 616
616 617 def test_raises_exception_when_repository_is_not_specified(self):
617 618 model = VcsSettingsModel()
618 619 with pytest.raises(Exception) as exc_info:
619 620 model.delete_repo_svn_pattern(123)
620 621 assert exc_info.value.message == 'Repository is not specified'
621 622
622 623
623 624 class TestDeleteGlobalSvnPattern(object):
624 625 def test_delete_global_svn_pattern_calls_delete_ui(self):
625 626 model = VcsSettingsModel()
626 627 delete_ui_patch = mock.patch.object(model.global_settings, 'delete_ui')
627 628 with delete_ui_patch as delete_ui_mock:
628 629 model.delete_global_svn_pattern(123)
629 630 delete_ui_mock.assert_called_once_with(123)
630 631
631 632
632 633 class TestFilterUiSettings(object):
633 634 def test_settings_are_filtered(self):
634 635 model = VcsSettingsModel()
635 636 repo_settings = [
636 637 UiSetting('extensions', 'largefiles', '', True),
637 638 UiSetting('phases', 'publish', 'True', True),
638 639 UiSetting('hooks', 'changegroup.repo_size', 'hook', True),
639 640 UiSetting('hooks', 'changegroup.push_logger', 'hook', True),
640 641 UiSetting('hooks', 'outgoing.pull_logger', 'hook', True),
641 642 UiSetting(
642 643 'vcs_svn_branch', '84223c972204fa545ca1b22dac7bef5b68d7442d',
643 644 'test_branch', True),
644 645 UiSetting(
645 646 'vcs_svn_tag', '84229c972204fa545ca1b22dac7bef5b68d7442d',
646 647 'test_tag', True),
647 648 ]
648 649 non_repo_settings = [
649 650 UiSetting('test', 'outgoing.pull_logger', 'hook', True),
650 651 UiSetting('hooks', 'test2', 'hook', True),
651 652 UiSetting(
652 653 'vcs_svn_repo', '84229c972204fa545ca1b22dac7bef5b68d7442d',
653 654 'test_tag', True),
654 655 ]
655 656 settings = repo_settings + non_repo_settings
656 657 filtered_settings = model._filter_ui_settings(settings)
657 658 assert sorted(filtered_settings) == sorted(repo_settings)
658 659
659 660
660 661 class TestFilterGeneralSettings(object):
661 662 def test_settings_are_filtered(self):
662 663 model = VcsSettingsModel()
663 664 settings = {
664 665 'rhodecode_abcde': 'value1',
665 666 'rhodecode_vwxyz': 'value2',
666 667 }
667 668 general_settings = {
668 669 'rhodecode_{}'.format(key): 'value'
669 670 for key in VcsSettingsModel.GENERAL_SETTINGS
670 671 }
671 672 settings.update(general_settings)
672 673
673 674 filtered_settings = model._filter_general_settings(general_settings)
674 675 assert sorted(filtered_settings) == sorted(general_settings)
675 676
676 677
677 678 class TestGetRepoUiSettings(object):
678 679 def test_global_uis_are_returned_when_no_repo_uis_found(
679 680 self, repo_stub):
680 681 model = VcsSettingsModel(repo=repo_stub.repo_name)
681 682 result = model.get_repo_ui_settings()
682 683 svn_sections = (
683 684 VcsSettingsModel.SVN_TAG_SECTION,
684 685 VcsSettingsModel.SVN_BRANCH_SECTION)
685 686 expected_result = [
686 687 s for s in model.global_settings.get_ui()
687 688 if s.section not in svn_sections]
688 689 assert sorted(result) == sorted(expected_result)
689 690
690 691 def test_repo_uis_are_overriding_global_uis(
691 692 self, repo_stub, settings_util):
692 693 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
693 694 settings_util.create_repo_rhodecode_ui(
694 695 repo_stub, section, 'repo', key=key, active=False)
695 696 model = VcsSettingsModel(repo=repo_stub.repo_name)
696 697 result = model.get_repo_ui_settings()
697 698 for setting in result:
698 699 locator = (setting.section, setting.key)
699 700 if locator in VcsSettingsModel.HOOKS_SETTINGS:
700 701 assert setting.value == 'repo'
701 702
702 703 assert setting.active is False
703 704
704 705 def test_global_svn_patterns_are_not_in_list(
705 706 self, repo_stub, settings_util):
706 707 svn_sections = (
707 708 VcsSettingsModel.SVN_TAG_SECTION,
708 709 VcsSettingsModel.SVN_BRANCH_SECTION)
709 710 for section in svn_sections:
710 711 settings_util.create_rhodecode_ui(
711 712 section, 'repo', key='deadbeef' + section, active=False)
712 713 model = VcsSettingsModel(repo=repo_stub.repo_name)
713 714 result = model.get_repo_ui_settings()
714 715 for setting in result:
715 716 assert setting.section not in svn_sections
716 717
717 718 def test_repo_uis_filtered_by_section_are_returned(
718 719 self, repo_stub, settings_util):
719 720 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
720 721 settings_util.create_repo_rhodecode_ui(
721 722 repo_stub, section, 'repo', key=key, active=False)
722 723 model = VcsSettingsModel(repo=repo_stub.repo_name)
723 724 section, key = VcsSettingsModel.HOOKS_SETTINGS[0]
724 725 result = model.get_repo_ui_settings(section=section)
725 726 for setting in result:
726 727 assert setting.section == section
727 728
728 729 def test_repo_uis_filtered_by_key_are_returned(
729 730 self, repo_stub, settings_util):
730 731 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
731 732 settings_util.create_repo_rhodecode_ui(
732 733 repo_stub, section, 'repo', key=key, active=False)
733 734 model = VcsSettingsModel(repo=repo_stub.repo_name)
734 735 section, key = VcsSettingsModel.HOOKS_SETTINGS[0]
735 736 result = model.get_repo_ui_settings(key=key)
736 737 for setting in result:
737 738 assert setting.key == key
738 739
739 740 def test_raises_exception_when_repository_is_not_specified(self):
740 741 model = VcsSettingsModel()
741 742 with pytest.raises(Exception) as exc_info:
742 743 model.get_repo_ui_settings()
743 744 assert exc_info.value.message == 'Repository is not specified'
744 745
745 746
746 747 class TestGetRepoGeneralSettings(object):
747 748 def test_global_settings_are_returned_when_no_repo_settings_found(
748 749 self, repo_stub):
749 750 model = VcsSettingsModel(repo=repo_stub.repo_name)
750 751 result = model.get_repo_general_settings()
751 752 expected_result = model.global_settings.get_all_settings()
752 753 assert sorted(result) == sorted(expected_result)
753 754
754 755 def test_repo_uis_are_overriding_global_uis(
755 756 self, repo_stub, settings_util):
756 757 for key in VcsSettingsModel.GENERAL_SETTINGS:
757 758 settings_util.create_repo_rhodecode_setting(
758 759 repo_stub, key, 'abcde', type_='unicode')
759 760 model = VcsSettingsModel(repo=repo_stub.repo_name)
760 761 result = model.get_repo_ui_settings()
761 762 for key in result:
762 763 if key in VcsSettingsModel.GENERAL_SETTINGS:
763 764 assert result[key] == 'abcde'
764 765
765 766 def test_raises_exception_when_repository_is_not_specified(self):
766 767 model = VcsSettingsModel()
767 768 with pytest.raises(Exception) as exc_info:
768 769 model.get_repo_general_settings()
769 770 assert exc_info.value.message == 'Repository is not specified'
770 771
771 772
772 773 class TestGetGlobalGeneralSettings(object):
773 774 def test_global_settings_are_returned(self, repo_stub):
774 775 model = VcsSettingsModel()
775 776 result = model.get_global_general_settings()
776 777 expected_result = model.global_settings.get_all_settings()
777 778 assert sorted(result) == sorted(expected_result)
778 779
779 780 def test_repo_uis_are_not_overriding_global_uis(
780 781 self, repo_stub, settings_util):
781 782 for key in VcsSettingsModel.GENERAL_SETTINGS:
782 783 settings_util.create_repo_rhodecode_setting(
783 784 repo_stub, key, 'abcde', type_='unicode')
784 785 model = VcsSettingsModel(repo=repo_stub.repo_name)
785 786 result = model.get_global_general_settings()
786 787 expected_result = model.global_settings.get_all_settings()
787 788 assert sorted(result) == sorted(expected_result)
788 789
789 790
790 791 class TestGetGlobalUiSettings(object):
791 792 def test_global_uis_are_returned(self, repo_stub):
792 793 model = VcsSettingsModel()
793 794 result = model.get_global_ui_settings()
794 795 expected_result = model.global_settings.get_ui()
795 796 assert sorted(result) == sorted(expected_result)
796 797
797 798 def test_repo_uis_are_not_overriding_global_uis(
798 799 self, repo_stub, settings_util):
799 800 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
800 801 settings_util.create_repo_rhodecode_ui(
801 802 repo_stub, section, 'repo', key=key, active=False)
802 803 model = VcsSettingsModel(repo=repo_stub.repo_name)
803 804 result = model.get_global_ui_settings()
804 805 expected_result = model.global_settings.get_ui()
805 806 assert sorted(result) == sorted(expected_result)
806 807
807 808 def test_ui_settings_filtered_by_section(
808 809 self, repo_stub, settings_util):
809 810 model = VcsSettingsModel(repo=repo_stub.repo_name)
810 811 section, key = VcsSettingsModel.HOOKS_SETTINGS[0]
811 812 result = model.get_global_ui_settings(section=section)
812 813 expected_result = model.global_settings.get_ui(section=section)
813 814 assert sorted(result) == sorted(expected_result)
814 815
815 816 def test_ui_settings_filtered_by_key(
816 817 self, repo_stub, settings_util):
817 818 model = VcsSettingsModel(repo=repo_stub.repo_name)
818 819 section, key = VcsSettingsModel.HOOKS_SETTINGS[0]
819 820 result = model.get_global_ui_settings(key=key)
820 821 expected_result = model.global_settings.get_ui(key=key)
821 822 assert sorted(result) == sorted(expected_result)
822 823
823 824
824 825 class TestGetGeneralSettings(object):
825 826 def test_global_settings_are_returned_when_inherited_is_true(
826 827 self, repo_stub, settings_util):
827 828 model = VcsSettingsModel(repo=repo_stub.repo_name)
828 829 model.inherit_global_settings = True
829 830 for key in VcsSettingsModel.GENERAL_SETTINGS:
830 831 settings_util.create_repo_rhodecode_setting(
831 832 repo_stub, key, 'abcde', type_='unicode')
832 833 result = model.get_general_settings()
833 834 expected_result = model.get_global_general_settings()
834 835 assert sorted(result) == sorted(expected_result)
835 836
836 837 def test_repo_settings_are_returned_when_inherited_is_false(
837 838 self, repo_stub, settings_util):
838 839 model = VcsSettingsModel(repo=repo_stub.repo_name)
839 840 model.inherit_global_settings = False
840 841 for key in VcsSettingsModel.GENERAL_SETTINGS:
841 842 settings_util.create_repo_rhodecode_setting(
842 843 repo_stub, key, 'abcde', type_='unicode')
843 844 result = model.get_general_settings()
844 845 expected_result = model.get_repo_general_settings()
845 846 assert sorted(result) == sorted(expected_result)
846 847
847 848 def test_global_settings_are_returned_when_no_repository_specified(self):
848 849 model = VcsSettingsModel()
849 850 result = model.get_general_settings()
850 851 expected_result = model.get_global_general_settings()
851 852 assert sorted(result) == sorted(expected_result)
852 853
853 854
854 855 class TestGetUiSettings(object):
855 856 def test_global_settings_are_returned_when_inherited_is_true(
856 857 self, repo_stub, settings_util):
857 858 model = VcsSettingsModel(repo=repo_stub.repo_name)
858 859 model.inherit_global_settings = True
859 860 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
860 861 settings_util.create_repo_rhodecode_ui(
861 862 repo_stub, section, 'repo', key=key, active=True)
862 863 result = model.get_ui_settings()
863 864 expected_result = model.get_global_ui_settings()
864 865 assert sorted(result) == sorted(expected_result)
865 866
866 867 def test_repo_settings_are_returned_when_inherited_is_false(
867 868 self, repo_stub, settings_util):
868 869 model = VcsSettingsModel(repo=repo_stub.repo_name)
869 870 model.inherit_global_settings = False
870 871 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
871 872 settings_util.create_repo_rhodecode_ui(
872 873 repo_stub, section, 'repo', key=key, active=True)
873 874 result = model.get_ui_settings()
874 875 expected_result = model.get_repo_ui_settings()
875 876 assert sorted(result) == sorted(expected_result)
876 877
877 878 def test_repo_settings_filtered_by_section_and_key(self, repo_stub):
878 879 model = VcsSettingsModel(repo=repo_stub.repo_name)
879 880 model.inherit_global_settings = False
880 881 args = ('section', 'key')
881 882 with mock.patch.object(model, 'get_repo_ui_settings') as settings_mock:
882 883 model.get_ui_settings(*args)
883 884 settings_mock.assert_called_once_with(*args)
884 885
885 886 def test_global_settings_filtered_by_section_and_key(self):
886 887 model = VcsSettingsModel()
887 888 args = ('section', 'key')
888 889 with mock.patch.object(model, 'get_global_ui_settings') as (
889 890 settings_mock):
890 891 model.get_ui_settings(*args)
891 892 settings_mock.assert_called_once_with(*args)
892 893
893 894 def test_global_settings_are_returned_when_no_repository_specified(self):
894 895 model = VcsSettingsModel()
895 896 result = model.get_ui_settings()
896 897 expected_result = model.get_global_ui_settings()
897 898 assert sorted(result) == sorted(expected_result)
898 899
899 900
900 901 class TestGetSvnPatterns(object):
901 902 def test_repo_settings_filtered_by_section_and_key(self, repo_stub):
902 903 model = VcsSettingsModel(repo=repo_stub.repo_name)
903 904 args = ('section', )
904 905 with mock.patch.object(model, 'get_repo_ui_settings') as settings_mock:
905 906 model.get_svn_patterns(*args)
906 907 settings_mock.assert_called_once_with(*args)
907 908
908 909 def test_global_settings_filtered_by_section_and_key(self):
909 910 model = VcsSettingsModel()
910 911 args = ('section', )
911 912 with mock.patch.object(model, 'get_global_ui_settings') as (
912 913 settings_mock):
913 914 model.get_svn_patterns(*args)
914 915 settings_mock.assert_called_once_with(*args)
915 916
916 917
917 918 class TestGetReposLocation(object):
918 919 def test_returns_repos_location(self, repo_stub):
919 920 model = VcsSettingsModel()
920 921
921 922 result_mock = mock.Mock()
922 923 result_mock.ui_value = '/tmp'
923 924
924 925 with mock.patch.object(model, 'global_settings') as settings_mock:
925 926 settings_mock.get_ui_by_key.return_value = result_mock
926 927 result = model.get_repos_location()
927 928
928 929 settings_mock.get_ui_by_key.assert_called_once_with('/')
929 930 assert result == '/tmp'
930 931
931 932
932 933 class TestCreateOrUpdateRepoSettings(object):
933 934 FORM_DATA = {
934 935 'inherit_global_settings': False,
935 936 'hooks_changegroup_repo_size': False,
936 937 'hooks_changegroup_push_logger': False,
937 938 'hooks_outgoing_pull_logger': False,
938 939 'extensions_largefiles': False,
939 940 'phases_publish': 'false',
940 941 'rhodecode_pr_merge_enabled': False,
941 942 'rhodecode_use_outdated_comments': False,
942 943 'new_svn_branch': '',
943 944 'new_svn_tag': ''
944 945 }
945 946
946 947 def test_get_raises_exception_when_repository_not_specified(self):
947 948 model = VcsSettingsModel()
948 949 with pytest.raises(Exception) as exc_info:
949 950 model.create_or_update_repo_settings(data=self.FORM_DATA)
950 951 assert exc_info.value.message == 'Repository is not specified'
951 952
952 953 def test_only_svn_settings_are_updated_when_type_is_svn(self, backend_svn):
953 954 repo = backend_svn.create_repo()
954 955 model = VcsSettingsModel(repo=repo)
955 956 with self._patch_model(model) as mocks:
956 957 model.create_or_update_repo_settings(
957 958 data=self.FORM_DATA, inherit_global_settings=False)
958 959 mocks['create_repo_svn_settings'].assert_called_once_with(
959 960 self.FORM_DATA)
960 961 non_called_methods = (
961 962 'create_or_update_repo_hook_settings',
962 963 'create_or_update_repo_pr_settings',
963 964 'create_or_update_repo_hg_settings')
964 965 for method in non_called_methods:
965 966 assert mocks[method].call_count == 0
966 967
967 968 def test_non_svn_settings_are_updated_when_type_is_hg(self, backend_hg):
968 969 repo = backend_hg.create_repo()
969 970 model = VcsSettingsModel(repo=repo)
970 971 with self._patch_model(model) as mocks:
971 972 model.create_or_update_repo_settings(
972 973 data=self.FORM_DATA, inherit_global_settings=False)
973 974
974 975 assert mocks['create_repo_svn_settings'].call_count == 0
975 976 called_methods = (
976 977 'create_or_update_repo_hook_settings',
977 978 'create_or_update_repo_pr_settings',
978 979 'create_or_update_repo_hg_settings')
979 980 for method in called_methods:
980 981 mocks[method].assert_called_once_with(self.FORM_DATA)
981 982
982 983 def test_non_svn_and_hg_settings_are_updated_when_type_is_git(
983 984 self, backend_git):
984 985 repo = backend_git.create_repo()
985 986 model = VcsSettingsModel(repo=repo)
986 987 with self._patch_model(model) as mocks:
987 988 model.create_or_update_repo_settings(
988 989 data=self.FORM_DATA, inherit_global_settings=False)
989 990
990 991 assert mocks['create_repo_svn_settings'].call_count == 0
991 992 called_methods = (
992 993 'create_or_update_repo_hook_settings',
993 994 'create_or_update_repo_pr_settings')
994 995 non_called_methods = (
995 996 'create_repo_svn_settings',
996 997 'create_or_update_repo_hg_settings'
997 998 )
998 999 for method in called_methods:
999 1000 mocks[method].assert_called_once_with(self.FORM_DATA)
1000 1001 for method in non_called_methods:
1001 1002 assert mocks[method].call_count == 0
1002 1003
1003 1004 def test_no_methods_are_called_when_settings_are_inherited(
1004 1005 self, backend):
1005 1006 repo = backend.create_repo()
1006 1007 model = VcsSettingsModel(repo=repo)
1007 1008 with self._patch_model(model) as mocks:
1008 1009 model.create_or_update_repo_settings(
1009 1010 data=self.FORM_DATA, inherit_global_settings=True)
1010 1011 for method_name in mocks:
1011 1012 assert mocks[method_name].call_count == 0
1012 1013
1013 1014 def test_cache_is_marked_for_invalidation(self, repo_stub):
1014 1015 model = VcsSettingsModel(repo=repo_stub)
1015 1016 invalidation_patcher = mock.patch(
1016 1017 'rhodecode.controllers.admin.repos.ScmModel.mark_for_invalidation')
1017 1018 with invalidation_patcher as invalidation_mock:
1018 1019 model.create_or_update_repo_settings(
1019 1020 data=self.FORM_DATA, inherit_global_settings=True)
1020 1021 invalidation_mock.assert_called_once_with(
1021 1022 repo_stub.repo_name, delete=True)
1022 1023
1023 1024 def test_inherit_flag_is_saved(self, repo_stub):
1024 1025 model = VcsSettingsModel(repo=repo_stub)
1025 1026 model.inherit_global_settings = True
1026 1027 with self._patch_model(model):
1027 1028 model.create_or_update_repo_settings(
1028 1029 data=self.FORM_DATA, inherit_global_settings=False)
1029 1030 assert model.inherit_global_settings is False
1030 1031
1031 1032 def _patch_model(self, model):
1032 1033 return mock.patch.multiple(
1033 1034 model,
1034 1035 create_repo_svn_settings=mock.DEFAULT,
1035 1036 create_or_update_repo_hook_settings=mock.DEFAULT,
1036 1037 create_or_update_repo_pr_settings=mock.DEFAULT,
1037 1038 create_or_update_repo_hg_settings=mock.DEFAULT)
General Comments 0
You need to be logged in to leave comments. Login now