##// END OF EJS Templates
exceptions: fixed py3 incompatible calls
marcink -
r3108:ed4aee63 default
parent child Browse files
Show More
@@ -1,129 +1,129 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 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.model.meta import Session
25 25 from rhodecode.model.settings import SettingsModel, IssueTrackerSettingsModel
26 26
27 27
28 28 class TestIssueTrackerSettingsModel(object):
29 29 def test_get_global_settings(self):
30 30 model = IssueTrackerSettingsModel()
31 31 input_settings = {
32 32 'rhodecode_issuetracker_pat_123': 'pat',
33 33 'rhodecode_issuetracker_url_123': 'url',
34 34 'rhodecode_issuetracker_desc_123': 'desc',
35 35 'rhodecode_issuetracker_pref_123': 'pref',
36 36 }
37 37 expected_result = {
38 38 '123': {
39 39 'desc': 'desc', 'pat': 'pat', 'pref': 'pref', 'url': 'url'}}
40 40 with mock.patch.object(model, 'global_settings') as settings_mock:
41 41 get_settings = settings_mock.get_all_settings
42 42 get_settings.return_value = input_settings
43 43 settings_mock.return_value = input_settings
44 44 result = model.get_global_settings(cache=True)
45 45 get_settings.assert_called_once_with(cache=True)
46 46 assert expected_result == result
47 47
48 48 def test_get_repo_settings_raise_exception_when_repo_is_not_set(self):
49 49 model = IssueTrackerSettingsModel()
50 50 with pytest.raises(Exception) as exc_info:
51 51 model.get_repo_settings(cache=True)
52 assert exc_info.value.message == 'Repository is not specified'
52 assert str(exc_info.value) == 'Repository is not specified'
53 53
54 54 def test_get_repo_settings(self, repo_stub):
55 55 model = IssueTrackerSettingsModel(repo=repo_stub.repo_name)
56 56 input_settings = {
57 57 'rhodecode_issuetracker_pat_123': 'pat',
58 58 'rhodecode_issuetracker_url_123': 'url',
59 59 'rhodecode_issuetracker_desc_123': 'desc',
60 60 'rhodecode_issuetracker_pref_123': 'pref',
61 61 }
62 62 expected_result = {
63 63 '123': {
64 64 'desc': 'desc', 'pat': 'pat', 'pref': 'pref', 'url': 'url'}}
65 65 with mock.patch.object(model, 'repo_settings') as settings_mock:
66 66 get_settings = settings_mock.get_all_settings
67 67 get_settings.return_value = input_settings
68 68 settings_mock.return_value = input_settings
69 69 result = model.get_repo_settings(cache=True)
70 70 get_settings.assert_called_once_with(cache=True)
71 71 assert expected_result == result
72 72
73 73 @pytest.mark.parametrize("inherit_settings, method", [
74 74 (True, 'get_global_settings'),
75 75 (False, 'get_repo_settings')
76 76 ])
77 77 def test_effective_settings(self, inherit_settings, method):
78 78 model = IssueTrackerSettingsModel()
79 79 expected_result = {'test': 'test'}
80 80 inherit_patch = mock.patch(
81 81 'rhodecode.model.settings.IssueTrackerSettingsModel'
82 82 '.inherit_global_settings', inherit_settings)
83 83 settings_patch = mock.patch.object(
84 84 model, method, return_value=expected_result)
85 85 with inherit_patch, settings_patch as settings_mock:
86 86 result = model.get_settings(cache=True)
87 87 settings_mock.assert_called_once_with(cache=True)
88 88 assert result == expected_result
89 89
90 90
91 91 class TestInheritIssueTrackerSettingsProperty(object):
92 92 def test_true_is_returned_when_repository_not_specified(self):
93 93 model = IssueTrackerSettingsModel()
94 94 assert model.inherit_global_settings is True
95 95
96 96 def test_true_is_returned_when_value_is_not_found(self, repo_stub):
97 97 model = IssueTrackerSettingsModel(repo=repo_stub.repo_name)
98 98 assert model.inherit_global_settings is True
99 99
100 100 def test_value_is_returned(self, repo_stub, settings_util):
101 101 model = IssueTrackerSettingsModel(repo=repo_stub.repo_name)
102 102 settings_util.create_repo_rhodecode_setting(
103 103 repo_stub, IssueTrackerSettingsModel.INHERIT_SETTINGS, False,
104 104 'bool')
105 105 assert model.inherit_global_settings is False
106 106
107 107 def test_value_is_set(self, repo_stub):
108 108 model = IssueTrackerSettingsModel(repo=repo_stub.repo_name)
109 109 model.inherit_global_settings = False
110 110 setting = model.repo_settings.get_setting_by_name(
111 111 IssueTrackerSettingsModel.INHERIT_SETTINGS)
112 112 try:
113 113 assert setting.app_settings_type == 'bool'
114 114 assert setting.app_settings_value is False
115 115 finally:
116 116 Session().delete(setting)
117 117 Session().commit()
118 118
119 119 def test_value_is_not_set_when_repository_is_not_specified(
120 120 self, repo_stub):
121 121 it_model = IssueTrackerSettingsModel()
122 122 it_model.inherit_global_settings = False
123 123 settings_model = SettingsModel(repo=repo_stub.repo_name)
124 124 setting = settings_model.get_setting_by_name(
125 125 IssueTrackerSettingsModel.INHERIT_SETTINGS)
126 126 assert setting is None
127 127 global_setting = it_model.global_settings.get_setting_by_name(
128 128 IssueTrackerSettingsModel.INHERIT_SETTINGS)
129 129 assert global_setting is None
@@ -1,807 +1,807 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 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 safe_str
25 25 from rhodecode.model.db import (
26 26 RhodeCodeUi, RepoRhodeCodeUi, RhodeCodeSetting, RepoRhodeCodeSetting)
27 27 from rhodecode.model.meta import Session
28 28 from rhodecode.model.settings import SettingsModel, SettingNotFound, UiSetting
29 29
30 30
31 31 class TestRepoGetUiByKey(object):
32 32 def test_ui_settings_are_returned_when_key_is_found(
33 33 self, repo_stub, settings_util):
34 34 section = 'test section'
35 35 value = 'test value'
36 36
37 37 settings_util.create_repo_rhodecode_ui(
38 38 repo_stub, 'wrong section', 'wrong value')
39 39 setting = settings_util.create_repo_rhodecode_ui(
40 40 repo_stub, section, value)
41 41 key = setting.ui_key
42 42
43 43 model = SettingsModel(repo=repo_stub.repo_name)
44 44 result = model.get_ui_by_key(key)
45 45 assert result.ui_value == value
46 46 assert result.ui_section == section
47 47 assert result.ui_active is True
48 48
49 49 def test_none_is_returned_when_key_is_not_found(
50 50 self, repo_stub, settings_util):
51 51 settings_util.create_repo_rhodecode_ui(
52 52 repo_stub, 'wrong section', 'wrong value')
53 53
54 54 model = SettingsModel(repo=repo_stub.repo_name)
55 55 result = model.get_ui_by_key('abcde')
56 56 assert result is None
57 57
58 58
59 59 class TestGlobalGetUiByKey(object):
60 60 def test_ui_settings_are_returned_when_key_is_found(self, settings_util):
61 61 section = 'test section'
62 62 value = 'test value'
63 63
64 64 settings_util.create_rhodecode_ui('wrong section', 'wrong value')
65 65 setting = settings_util.create_rhodecode_ui(section, value)
66 66 key = setting.ui_key
67 67
68 68 model = SettingsModel()
69 69 result = model.get_ui_by_key(key)
70 70 assert result.ui_value == value
71 71 assert result.ui_section == section
72 72 assert result.ui_active is True
73 73
74 74 def test_none_is_returned_when_key_is_not_found(self, settings_util):
75 75 settings_util.create_rhodecode_ui('wrong section', 'wrong value')
76 76 model = SettingsModel()
77 77 result = model.get_ui_by_key('abcde')
78 78 assert result is None
79 79
80 80
81 81 class TestRepoGetUiBySection(object):
82 82 def test_ui_settings_are_returned_when_section_is_found(
83 83 self, repo_stub, settings_util):
84 84 section = 'test section'
85 85 values = ['test value 1', 'test value 2']
86 86
87 87 expected_pairs = []
88 88 for value in values:
89 89 setting = settings_util.create_repo_rhodecode_ui(
90 90 repo_stub, section, value)
91 91 expected_pairs.append((setting.ui_key, value))
92 92
93 93 model = SettingsModel(repo=repo_stub.repo_name)
94 94 result = model.get_ui_by_section(section)
95 95 result_pairs = [(r.ui_key, r.ui_value) for r in result]
96 96 assert sorted(result_pairs) == sorted(expected_pairs)
97 97
98 98 def test_empty_list_is_returned_when_section_is_not_found(
99 99 self, repo_stub, settings_util):
100 100 settings_util.create_repo_rhodecode_ui(
101 101 repo_stub, 'wrong section', 'wrong value')
102 102
103 103 model = SettingsModel(repo=repo_stub.repo_name)
104 104 result = model.get_ui_by_section('correct section')
105 105 assert result == []
106 106
107 107
108 108 class TestGlobalGetUiBySection(object):
109 109 def test_ui_settings_are_returned_when_section_is_found(
110 110 self, settings_util):
111 111 section = 'test section'
112 112 values = ['test value 1', 'test value 2']
113 113
114 114 expected_pairs = []
115 115 for value in values:
116 116 setting = settings_util.create_rhodecode_ui(section, value)
117 117 expected_pairs.append((setting.ui_key, value))
118 118
119 119 model = SettingsModel()
120 120 result = model.get_ui_by_section(section)
121 121 result_pairs = [(r.ui_key, r.ui_value) for r in result]
122 122 assert sorted(result_pairs) == sorted(expected_pairs)
123 123
124 124 def test_empty_list_is_returned_when_section_is_not_found(
125 125 self, settings_util):
126 126 settings_util.create_rhodecode_ui('wrong section', 'wrong value')
127 127
128 128 model = SettingsModel()
129 129 result = model.get_ui_by_section('correct section')
130 130 assert result == []
131 131
132 132
133 133 class TestRepoGetUiBySectionAndKey(object):
134 134 def test_ui_settings_are_returned_when_section_and_key_are_found(
135 135 self, repo_stub, settings_util):
136 136 section = 'test section'
137 137 value = 'test value'
138 138 key = 'test key'
139 139
140 140 settings_util.create_rhodecode_ui(
141 141 'wrong section', 'wrong value', key='wrong key')
142 142 setting = settings_util.create_repo_rhodecode_ui(
143 143 repo_stub, section, value, key=key)
144 144 key = setting.ui_key
145 145
146 146 model = SettingsModel(repo=repo_stub.repo_name)
147 147 result = model.get_ui_by_section_and_key(section, key)
148 148 assert result.ui_value == value
149 149 assert result.ui_section == section
150 150 assert result.ui_active is True
151 151
152 152 def test_none_is_returned_when_key_section_pair_is_not_found(
153 153 self, repo_stub, settings_util):
154 154 settings_util.create_repo_rhodecode_ui(
155 155 repo_stub, 'section', 'wrong value', key='wrong key')
156 156
157 157 model = SettingsModel(repo=repo_stub.repo_name)
158 158 result = model.get_ui_by_section_and_key('section', 'test key')
159 159 assert result is None
160 160
161 161
162 162 class TestGlobalGetUiBySectionAndKey(object):
163 163 def test_ui_settings_are_returned_when_section_and_key_are_found(
164 164 self, settings_util):
165 165 section = 'test section'
166 166 value = 'test value'
167 167 key = 'test key'
168 168
169 169 settings_util.create_rhodecode_ui(
170 170 'wrong section', 'wrong value', key='wrong key')
171 171 setting = settings_util.create_rhodecode_ui(section, value, key=key)
172 172 key = setting.ui_key
173 173
174 174 model = SettingsModel()
175 175 result = model.get_ui_by_section_and_key(section, key)
176 176 assert result.ui_value == value
177 177 assert result.ui_section == section
178 178 assert result.ui_active is True
179 179
180 180 def test_none_is_returned_when_key_section_pair_is_not_found(
181 181 self, settings_util):
182 182 settings_util.create_rhodecode_ui(
183 183 'section', 'wrong value', key='wrong key')
184 184 model = SettingsModel()
185 185 result = model.get_ui_by_section_and_key('section', 'test key')
186 186 assert result is None
187 187
188 188
189 189 class TestRepoGetUi(object):
190 190 def test_non_empty_list_is_returned_when_ui_settings_found(
191 191 self, repo_stub, settings_util, fake_ui_values):
192 192 for ui in fake_ui_values:
193 193 settings_util.create_repo_rhodecode_ui(
194 194 repo_stub, ui.section, ui.value, key=ui.key)
195 195 # Create few global settings to check that only repo ones are
196 196 # displayed
197 197 settings_util.create_rhodecode_ui(ui.section, ui.value, key=ui.key)
198 198
199 199 model = SettingsModel(repo=repo_stub.repo_name)
200 200 result = model.get_ui()
201 201 assert sorted(result) == sorted(fake_ui_values)
202 202
203 203 def test_settings_filtered_by_section(
204 204 self, repo_stub, settings_util, fake_ui_values):
205 205 for ui in fake_ui_values:
206 206 settings_util.create_repo_rhodecode_ui(
207 207 repo_stub, ui.section, ui.value, key=ui.key)
208 208
209 209 model = SettingsModel(repo=repo_stub.repo_name)
210 210 result = model.get_ui(section=fake_ui_values[0].section)
211 211 expected_result = [
212 212 s for s in fake_ui_values
213 213 if s.section == fake_ui_values[0].section]
214 214 assert sorted(result) == sorted(expected_result)
215 215
216 216 def test_settings_filtered_by_key(
217 217 self, repo_stub, settings_util, fake_ui_values):
218 218 for ui in fake_ui_values:
219 219 settings_util.create_repo_rhodecode_ui(
220 220 repo_stub, ui.section, ui.value, key=ui.key)
221 221
222 222 model = SettingsModel(repo=repo_stub.repo_name)
223 223 result = model.get_ui(key=fake_ui_values[0].key)
224 224 expected_result = [
225 225 s for s in fake_ui_values if s.key == fake_ui_values[0].key]
226 226 assert sorted(result) == sorted(expected_result)
227 227
228 228 def test_empty_list_is_returned_when_ui_settings_are_not_found(
229 229 self, repo_stub, settings_util):
230 230 for i in range(10):
231 231 settings_util.create_rhodecode_ui(
232 232 'section{}'.format(i), 'value{}'.format(i),
233 233 key='key{}'.format(i), active=True)
234 234
235 235 model = SettingsModel(repo=repo_stub.repo_name)
236 236 result = model.get_ui()
237 237 assert result == []
238 238
239 239
240 240 class TestGlobalGetUi(object):
241 241 def test_non_empty_list_is_returned_when_ui_settings_found(
242 242 self, backend_stub, settings_util, fake_ui_values):
243 243 repo = backend_stub.create_repo()
244 244 for ui in fake_ui_values:
245 245 settings_util.create_rhodecode_ui(ui.section, ui.value, key=ui.key)
246 246 # Create few repo settings to check that only global ones are
247 247 # displayed
248 248 settings_util.create_repo_rhodecode_ui(
249 249 repo, ui.section, ui.value, key=ui.key)
250 250
251 251 model = SettingsModel()
252 252 result = model.get_ui()
253 253 for ui in fake_ui_values:
254 254 assert ui in result
255 255
256 256 def test_settings_filtered_by_key(self, settings_util, fake_ui_values):
257 257 for ui in fake_ui_values:
258 258 settings_util.create_rhodecode_ui(ui.section, ui.value, key=ui.key)
259 259 expected_result = [
260 260 s for s in fake_ui_values if s.key == fake_ui_values[0].key]
261 261
262 262 model = SettingsModel()
263 263 result = model.get_ui(key=fake_ui_values[0].key)
264 264 assert sorted(result) == sorted(expected_result)
265 265
266 266 def test_settings_filtered_by_section(self, settings_util, fake_ui_values):
267 267 for ui in fake_ui_values:
268 268 settings_util.create_rhodecode_ui(ui.section, ui.value, key=ui.key)
269 269 expected_result = [
270 270 s for s in fake_ui_values
271 271 if s.section == fake_ui_values[0].section]
272 272
273 273 model = SettingsModel()
274 274 result = model.get_ui(section=fake_ui_values[0].section)
275 275 assert sorted(result) == sorted(expected_result)
276 276
277 277 def test_repo_settings_are_not_displayed(
278 278 self, backend_stub, settings_util, fake_ui_values):
279 279 repo = backend_stub.create_repo()
280 280 for ui in fake_ui_values:
281 281 settings_util.create_repo_rhodecode_ui(
282 282 repo, ui.section, ui.value, key=ui.key, active=ui.active)
283 283
284 284 model = SettingsModel()
285 285 result = model.get_ui()
286 286 for ui in fake_ui_values:
287 287 assert ui not in result
288 288
289 289
290 290 class TestRepoGetBuiltInHooks(object):
291 291 def test_only_builtin_hooks_are_returned(self, repo_stub, settings_util):
292 292 section = 'hooks'
293 293 valid_keys = SettingsModel.BUILTIN_HOOKS
294 294 invalid_keys = ('fake_hook', )
295 295 keys = valid_keys + invalid_keys
296 296
297 297 for key in keys:
298 298 settings_util.create_repo_rhodecode_ui(
299 299 repo_stub, section, 'test value', key=key)
300 300
301 301 model = SettingsModel(repo=repo_stub.repo_name)
302 302 result = model.get_builtin_hooks()
303 303
304 304 assert len(result) == len(valid_keys)
305 305 for entry in result:
306 306 assert entry.ui_key in valid_keys
307 307
308 308
309 309 class TestGlobalGetBuiltInHooks(object):
310 310 def test_only_builtin_hooks_are_returned(self, settings_util):
311 311 section = 'hooks'
312 312 valid_keys = ('valid_key1', 'valid_key2')
313 313 invalid_keys = ('fake_hook', )
314 314 keys = valid_keys + invalid_keys
315 315
316 316 for key in keys:
317 317 settings_util.create_rhodecode_ui(section, 'test value', key=key)
318 318
319 319 model = SettingsModel()
320 320 with mock.patch.object(model, 'BUILTIN_HOOKS', valid_keys):
321 321 result = model.get_builtin_hooks()
322 322
323 323 assert len(result) == len(valid_keys)
324 324 for entry in result:
325 325 assert entry.ui_key in valid_keys
326 326
327 327
328 328 class TestRepoGetCustomHooks(object):
329 329 def test_only_custom_hooks_are_returned(self, repo_stub, settings_util):
330 330 section = 'hooks'
331 331 valid_keys = ('custom', )
332 332 invalid_keys = SettingsModel.BUILTIN_HOOKS
333 333 keys = valid_keys + invalid_keys
334 334
335 335 for key in keys:
336 336 settings_util.create_repo_rhodecode_ui(
337 337 repo_stub, section, 'test value', key=key)
338 338
339 339 model = SettingsModel(repo=repo_stub.repo_name)
340 340 result = model.get_custom_hooks()
341 341
342 342 assert len(result) == len(valid_keys)
343 343 for entry in result:
344 344 assert entry.ui_key in valid_keys
345 345
346 346
347 347 class TestGlobalGetCustomHooks(object):
348 348 def test_only_custom_hooks_are_returned(self, settings_util):
349 349 section = 'hooks'
350 350 valid_keys = ('valid_key1', 'valid_key2')
351 351 invalid_keys = ('fake_hook', )
352 352 keys = valid_keys + invalid_keys
353 353
354 354 for key in keys:
355 355 settings_util.create_rhodecode_ui(section, 'test value', key=key)
356 356
357 357 model = SettingsModel()
358 358 with mock.patch.object(model, 'BUILTIN_HOOKS', invalid_keys):
359 359 result = model.get_custom_hooks()
360 360 for entry in result:
361 361 assert entry.ui_key not in invalid_keys
362 362
363 363
364 364 class TestRepoCreateUiSectionValue(object):
365 365 @pytest.mark.parametrize("additional_kwargs", [
366 366 {'key': 'abcde'},
367 367 {'active': False},
368 368 {}
369 369 ])
370 370 def test_ui_section_value_is_created(
371 371 self, repo_stub, additional_kwargs):
372 372 model = SettingsModel(repo=repo_stub.repo_name)
373 373 section = 'test section'
374 374 value = 'test value'
375 375 result = model.create_ui_section_value(section, value)
376 376 key = result.ui_key
377 377 Session().commit()
378 378
379 379 setting = model.get_ui_by_key(key)
380 380 try:
381 381 assert setting == result
382 382 assert isinstance(setting, RepoRhodeCodeUi)
383 383 finally:
384 384 Session().delete(result)
385 385 Session().commit()
386 386
387 387
388 388 class TestGlobalCreateUiSectionValue(object):
389 389 @pytest.mark.parametrize("additional_kwargs", [
390 390 {'key': 'abcde'},
391 391 {'active': False},
392 392 {}
393 393 ])
394 394 def test_ui_section_value_is_created_with_autogenerated_key(
395 395 self, backend_stub, additional_kwargs):
396 396 model = SettingsModel()
397 397 section = 'test section'
398 398 value = 'test value'
399 399 result = model.create_ui_section_value(
400 400 section, value, **additional_kwargs)
401 401 key = result.ui_key
402 402 Session().commit()
403 403
404 404 setting = model.get_ui_by_key(key)
405 405 try:
406 406 assert setting == result
407 407 assert isinstance(setting, RhodeCodeUi)
408 408 finally:
409 409 Session().delete(result)
410 410 Session().commit()
411 411
412 412
413 413 class TestRepoCreateOrUpdateHook(object):
414 414 def test_hook_created(self, repo_stub):
415 415 model = SettingsModel(repo=repo_stub.repo_name)
416 416 key = 'test_key'
417 417 value = 'test value'
418 418 result = model.create_or_update_hook(key, value)
419 419 Session().commit()
420 420
421 421 setting = model.get_ui_by_section_and_key('hooks', key)
422 422 try:
423 423 assert setting == result
424 424 assert isinstance(setting, RepoRhodeCodeUi)
425 425 finally:
426 426 Session().delete(result)
427 427 Session().commit()
428 428
429 429 def test_hook_updated(self, repo_stub, settings_util):
430 430 section = 'hooks'
431 431 key = 'test_key'
432 432
433 433 settings_util.create_repo_rhodecode_ui(
434 434 repo_stub, section, 'old value', key=key)
435 435
436 436 model = SettingsModel(repo=repo_stub.repo_name)
437 437 value = 'test value'
438 438 model.create_or_update_hook(key, value)
439 439 Session().commit()
440 440
441 441 setting = model.get_ui_by_section_and_key('hooks', key)
442 442 assert setting.ui_value == value
443 443
444 444
445 445 class TestGlobalCreateOrUpdateHook(object):
446 446 def test_hook_created(self):
447 447 model = SettingsModel()
448 448 key = 'test_key'
449 449 value = 'test value'
450 450 result = model.create_or_update_hook(key, value)
451 451 Session().commit()
452 452
453 453 setting = model.get_ui_by_section_and_key('hooks', key)
454 454 try:
455 455 assert setting == result
456 456 assert isinstance(setting, RhodeCodeUi)
457 457 finally:
458 458 Session().delete(result)
459 459 Session().commit()
460 460
461 461 def test_hook_updated(self, settings_util):
462 462 section = 'hooks'
463 463 key = 'test_key'
464 464
465 465 settings_util.create_rhodecode_ui(section, 'old value', key=key)
466 466
467 467 model = SettingsModel()
468 468 value = 'test value'
469 469 model.create_or_update_hook(key, value)
470 470 Session().commit()
471 471
472 472 setting = model.get_ui_by_section_and_key('hooks', key)
473 473 assert setting.ui_value == value
474 474
475 475
476 476 class TestDeleteUiValue(object):
477 477 def test_delete_ui_when_repo_is_set(self, repo_stub, settings_util):
478 478 model = SettingsModel(repo=repo_stub.repo_name)
479 479 result = settings_util.create_repo_rhodecode_ui(
480 480 repo_stub, 'section', None, cleanup=False)
481 481
482 482 key = result.ui_key
483 483 model.delete_ui(result.ui_id)
484 484 Session().commit()
485 485
486 486 setting = model.get_ui_by_key(key)
487 487 assert setting is None
488 488
489 489 @pytest.mark.parametrize('id_', (None, 123))
490 490 def test_raises_exception_when_id_is_not_specified(self, id_):
491 491 model = SettingsModel()
492 492 with pytest.raises(SettingNotFound) as exc_info:
493 493 model.delete_ui(id_)
494 assert exc_info.value.message == 'Setting `{}` is not found'.format(id_)
494 assert str(exc_info.value) == 'Setting `{}` is not found'.format(id_)
495 495
496 496 def test_delete_ui_when_repo_is_not_set(self, settings_util):
497 497 model = SettingsModel()
498 498 result = settings_util.create_rhodecode_ui(
499 499 'section', None, cleanup=False)
500 500
501 501 key = result.ui_key
502 502 model.delete_ui(result.ui_id)
503 503 Session().commit()
504 504
505 505 setting = model.get_ui_by_key(key)
506 506 assert setting is None
507 507
508 508
509 509 class TestRepoGetSettingByName(object):
510 510 @pytest.mark.parametrize("name, value, type_, expected_value", [
511 511 ('test_unicode', 'Straße', 'unicode', 'Straße'),
512 512 ('test_int', '1234', 'int', 1234),
513 513 ('test_bool', 'True', 'bool', True),
514 514 ('test_list', 'a,b,c', 'list', ['a', 'b', 'c'])
515 515 ])
516 516 def test_setting_is_returned_when_name_is_found(
517 517 self, repo_stub, settings_util, name, value, type_,
518 518 expected_value):
519 519 settings_util.create_repo_rhodecode_setting(
520 520 repo_stub, name, value, type_)
521 521
522 522 model = SettingsModel(repo=repo_stub.repo_name)
523 523 setting = model.get_setting_by_name(name)
524 524 assert setting.app_settings_type == type_
525 525 actual_value = setting.app_settings_value
526 526 if type_ == 'unicode':
527 527 actual_value = safe_str(actual_value)
528 528 assert actual_value == expected_value
529 529
530 530 def test_returns_none_if_the_setting_does_not_exist(self, repo_stub):
531 531 model = SettingsModel(repo=repo_stub.repo_name)
532 532 setting = model.get_setting_by_name('abcde')
533 533 assert setting is None
534 534
535 535
536 536 class TestGlobalGetSettingByName(object):
537 537 @pytest.mark.parametrize("name, value, type_, expected_value", [
538 538 ('test_unicode', 'Straße', 'unicode', 'Straße'),
539 539 ('test_int', '1234', 'int', 1234),
540 540 ('test_bool', 'True', 'bool', True),
541 541 ('test_list', 'a,b,c', 'list', ['a', 'b', 'c'])
542 542 ])
543 543 def test_setting_is_returned_when_name_is_found(
544 544 self, settings_util, name, value, type_, expected_value):
545 545 settings_util.create_rhodecode_setting(name, value, type_)
546 546
547 547 model = SettingsModel()
548 548 setting = model.get_setting_by_name(name)
549 549 assert setting.app_settings_type == type_
550 550 actual_value = setting.app_settings_value
551 551 if type_ == 'unicode':
552 552 actual_value = safe_str(actual_value)
553 553 assert actual_value == expected_value
554 554
555 555 def test_returns_none_if_the_setting_does_not_exist(self):
556 556 model = SettingsModel()
557 557 setting = model.get_setting_by_name('abcde')
558 558 assert setting is None
559 559
560 560
561 561 class TestRepoGetAllSettings(object):
562 562 def test_settings_are_found(self, repo_stub, settings_util):
563 563 initial_settings = {
564 564 'test_setting_{}'.format(i): 'value' for i in range(10)}
565 565 settings = [
566 566 settings_util.create_repo_rhodecode_setting(
567 567 repo_stub, name, initial_settings[name], 'unicode')
568 568 for name in initial_settings
569 569 ]
570 570 model = SettingsModel(repo=repo_stub.repo_name)
571 571
572 572 settings = model.get_all_settings()
573 573 expected_settings = {
574 574 'rhodecode_' + name: initial_settings[name]
575 575 for name in initial_settings
576 576 }
577 577
578 578 assert len(settings) == 10
579 579 assert expected_settings == settings
580 580
581 581 def test_settings_are_not_found(self, repo_stub):
582 582 model = SettingsModel(repo=repo_stub.repo_name)
583 583 setting = model.get_all_settings()
584 584 assert setting == {}
585 585
586 586
587 587 class TestGlobalGetAllSettings(object):
588 588 def test_settings_are_found(self, settings_util):
589 589 initial_settings = {
590 590 'test_setting_{}'.format(i): 'value' for i in range(10)}
591 591 settings = [
592 592 settings_util.create_rhodecode_setting(
593 593 name, initial_settings[name], 'unicode')
594 594 for name in initial_settings
595 595 ]
596 596 model = SettingsModel()
597 597
598 598 settings = model.get_all_settings()
599 599 expected_settings = {
600 600 'rhodecode_' + name: initial_settings[name]
601 601 for name in initial_settings
602 602 }
603 603
604 604 filtered_settings = {
605 605 name: settings[name]
606 606 for name in settings if name.startswith('rhodecode_test_setting')
607 607 }
608 608 assert len(filtered_settings) == 10
609 609 assert expected_settings == filtered_settings
610 610
611 611 def test_settings_are_not_found(self, repo_stub):
612 612 model = SettingsModel(repo=repo_stub.repo_name)
613 613 setting = model.get_all_settings()
614 614 assert setting == {}
615 615
616 616
617 617 class TestRepoCreateOrUpdateSetting(object):
618 618 def test_setting_is_created(self, repo_stub):
619 619 model = SettingsModel(repo=repo_stub.repo_name)
620 620 name = 'test_setting'
621 621 value = 'test_value'
622 622 model.create_or_update_setting(name, val=value)
623 623
624 624 setting = model.get_setting_by_name(name)
625 625 try:
626 626 assert setting.app_settings_name == name
627 627 assert setting.app_settings_value == value
628 628 assert setting.app_settings_type == 'unicode'
629 629 assert isinstance(setting, RepoRhodeCodeSetting)
630 630 finally:
631 631 Session().delete(setting)
632 632 Session().commit()
633 633
634 634 def test_setting_is_updated(self, repo_stub, settings_util):
635 635 model = SettingsModel(repo=repo_stub.repo_name)
636 636 name = 'test_setting'
637 637 value = 'test_value'
638 638 settings_util.create_repo_rhodecode_setting(
639 639 repo_stub, name, value, 'unicode', cleanup=False)
640 640
641 641 updated_value = 'test_value_2'
642 642 model.create_or_update_setting(name, val=updated_value)
643 643
644 644 setting = model.get_setting_by_name(name)
645 645 try:
646 646 assert setting.app_settings_name == name
647 647 assert setting.app_settings_value == updated_value
648 648 assert setting.app_settings_type == 'unicode'
649 649 assert isinstance(setting, RepoRhodeCodeSetting)
650 650 finally:
651 651 Session().delete(setting)
652 652 Session().commit()
653 653
654 654
655 655 class TestGlobalCreateOrUpdateSetting(object):
656 656 def test_setting_is_created(self):
657 657 model = SettingsModel()
658 658 name = 'test_setting'
659 659 value = 'test_value'
660 660 model.create_or_update_setting(name, val=value)
661 661
662 662 setting = model.get_setting_by_name(name)
663 663 try:
664 664 assert setting.app_settings_name == name
665 665 assert setting.app_settings_value == value
666 666 assert setting.app_settings_type == 'unicode'
667 667 assert isinstance(setting, RhodeCodeSetting)
668 668 finally:
669 669 Session().delete(setting)
670 670 Session().commit()
671 671
672 672 def test_setting_is_updated(self, settings_util):
673 673 model = SettingsModel()
674 674 name = 'test_setting'
675 675 value = 'test_value'
676 676 settings_util.create_rhodecode_setting(
677 677 name, value, 'unicode', cleanup=False)
678 678
679 679 updated_value = 'test_value_2'
680 680 model.create_or_update_setting(name, val=updated_value)
681 681
682 682 setting = model.get_setting_by_name(name)
683 683 try:
684 684 assert setting.app_settings_name == name
685 685 assert setting.app_settings_value == updated_value
686 686 assert setting.app_settings_type == 'unicode'
687 687 assert isinstance(setting, RhodeCodeSetting)
688 688 finally:
689 689 Session().delete(setting)
690 690 Session().commit()
691 691
692 692
693 693 class TestRepoGetAuthSettings(object):
694 694 def test_settings_prefixed_with_auth_are_retured(
695 695 self, repo_stub, settings_util):
696 696 model = SettingsModel(repo=repo_stub.repo_name)
697 697 valid_settings = ('auth_test1', 'auth_test2')
698 698 invalid_settings = ('test1', 'test2')
699 699 fake_value = 'test_value'
700 700
701 701 for name in valid_settings + invalid_settings:
702 702 settings_util.create_repo_rhodecode_setting(
703 703 repo_stub, name, fake_value, 'unicode')
704 704
705 705 auth_settings = model.get_auth_settings()
706 706 assert auth_settings == {name: fake_value for name in valid_settings}
707 707
708 708
709 709 class TestGlobalGetAuthSettings(object):
710 710 def test_settings_prefixed_with_auth_are_retured(self, settings_util):
711 711 model = SettingsModel()
712 712 valid_settings = ('auth_test1', 'auth_test2')
713 713 invalid_settings = ('test1', 'test2')
714 714 fake_value = 'test_value'
715 715
716 716 for name in valid_settings + invalid_settings:
717 717 settings_util.create_rhodecode_setting(name, fake_value, 'unicode')
718 718
719 719 auth_settings = model.get_auth_settings()
720 720 for name in auth_settings:
721 721 assert name not in invalid_settings
722 722 if name in valid_settings:
723 723 assert auth_settings[name] == fake_value
724 724
725 725
726 726 class TestGetAuthPlugins(object):
727 727 def test_get_setting_by_name_is_called(self):
728 728 model = SettingsModel()
729 729
730 730 fake_value = 'some value'
731 731 result_mock = mock.Mock()
732 732 result_mock.app_settings_value = fake_value
733 733
734 734 get_setting_patch = mock.patch.object(
735 735 model, 'get_setting_by_name', return_value=result_mock)
736 736
737 737 with get_setting_patch as get_setting_mock:
738 738 result = model.get_auth_plugins()
739 739
740 740 get_setting_mock.assert_called_once_with('auth_plugins')
741 741 assert result == fake_value
742 742
743 743
744 744 class TestDefaultRepoSettings(object):
745 745 DEFAULT_SETTINGS_NAMES = ['default_a{}'.format(i) for i in range(10)]
746 746 CUSTOM_SETTINGS_NAMES = ['setting_b_{}'.format(i) for i in range(10)]
747 747
748 748 def test_returns_global_settings_prefixed_with_default(
749 749 self, settings_util):
750 750 self._create_values(settings_util)
751 751 model = SettingsModel()
752 752 result = model.get_default_repo_settings()
753 753 self._assert_prefixed_settings(result)
754 754
755 755 def test_returns_global_settings_without_default_prefix(
756 756 self, settings_util):
757 757 self._create_values(settings_util)
758 758 model = SettingsModel()
759 759 result = model.get_default_repo_settings(strip_prefix=True)
760 760 self._assert_non_prefixed_settings(result)
761 761
762 762 def test_returns_per_repo_settings_prefixed_with_default(
763 763 self, repo_stub, settings_util):
764 764 model = SettingsModel(repo=repo_stub)
765 765 self._create_values(settings_util, repo=repo_stub)
766 766 result = model.get_default_repo_settings()
767 767 self._assert_prefixed_settings(result)
768 768
769 769 def test_returns_per_repo_settings_without_default_prefix(
770 770 self, repo_stub, settings_util):
771 771 model = SettingsModel(repo=repo_stub)
772 772 self._create_values(settings_util, repo=repo_stub)
773 773 result = model.get_default_repo_settings(strip_prefix=True)
774 774 self._assert_non_prefixed_settings(result)
775 775
776 776 def _create_values(self, settings_util, repo=None):
777 777 for name in self.DEFAULT_SETTINGS_NAMES + self.CUSTOM_SETTINGS_NAMES:
778 778 if not repo:
779 779 settings_util.create_rhodecode_setting(
780 780 name, 'value', 'unicode')
781 781 else:
782 782 settings_util.create_repo_rhodecode_setting(
783 783 repo, name, 'value', 'unicode')
784 784
785 785 def _assert_prefixed_settings(self, result):
786 786 for setting in self.DEFAULT_SETTINGS_NAMES:
787 787 assert setting in result
788 788 assert result[setting] == 'value'
789 789
790 790 for setting in self.CUSTOM_SETTINGS_NAMES:
791 791 assert setting not in result
792 792
793 793 def _assert_non_prefixed_settings(self, result):
794 794 for setting in self.DEFAULT_SETTINGS_NAMES:
795 795 setting = setting.replace('default_', '')
796 796 assert setting in result
797 797 assert result[setting] == 'value'
798 798
799 799
800 800 @pytest.fixture
801 801 def fake_ui_values():
802 802 return [
803 803 UiSetting(
804 804 'section{}'.format(i % 2), 'key{}'.format(i),
805 805 'value{}'.format(i), True)
806 806 for i in range(10)
807 807 ]
@@ -1,1084 +1,1082 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 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 42 'rhodecode_use_outdated_comments': True,
43 43 'rhodecode_hg_use_rebase_for_merging': True,
44 44 'rhodecode_hg_close_branch_before_merging': True,
45 45 'rhodecode_git_use_rebase_for_merging': True,
46 46 'rhodecode_git_close_branch_before_merging': True,
47 47 'rhodecode_diff_cache': True,
48 48 }
49 49
50 50
51 51 class TestInheritGlobalSettingsProperty(object):
52 52 def test_get_raises_exception_when_repository_not_specified(self):
53 53 model = VcsSettingsModel()
54 54 with pytest.raises(Exception) as exc_info:
55 55 model.inherit_global_settings
56 assert exc_info.value.message == 'Repository is not specified'
56 assert str(exc_info.value) == 'Repository is not specified'
57 57
58 58 def test_true_is_returned_when_value_is_not_found(self, repo_stub):
59 59 model = VcsSettingsModel(repo=repo_stub.repo_name)
60 60 assert model.inherit_global_settings is True
61 61
62 62 def test_value_is_returned(self, repo_stub, settings_util):
63 63 model = VcsSettingsModel(repo=repo_stub.repo_name)
64 64 settings_util.create_repo_rhodecode_setting(
65 65 repo_stub, VcsSettingsModel.INHERIT_SETTINGS, False, 'bool')
66 66 assert model.inherit_global_settings is False
67 67
68 68 def test_value_is_set(self, repo_stub):
69 69 model = VcsSettingsModel(repo=repo_stub.repo_name)
70 70 model.inherit_global_settings = False
71 71 setting = model.repo_settings.get_setting_by_name(
72 72 VcsSettingsModel.INHERIT_SETTINGS)
73 73 try:
74 74 assert setting.app_settings_type == 'bool'
75 75 assert setting.app_settings_value is False
76 76 finally:
77 77 Session().delete(setting)
78 78 Session().commit()
79 79
80 80 def test_set_raises_exception_when_repository_not_specified(self):
81 81 model = VcsSettingsModel()
82 82 with pytest.raises(Exception) as exc_info:
83 83 model.inherit_global_settings = False
84 assert exc_info.value.message == 'Repository is not specified'
84 assert str(exc_info.value) == 'Repository is not specified'
85 85
86 86
87 87 class TestVcsSettingsModel(object):
88 88 def test_global_svn_branch_patterns(self):
89 89 model = VcsSettingsModel()
90 90 expected_result = {'test': 'test'}
91 91 with mock.patch.object(model, 'global_settings') as settings_mock:
92 92 get_settings = settings_mock.get_ui_by_section
93 93 get_settings.return_value = expected_result
94 94 settings_mock.return_value = expected_result
95 95 result = model.get_global_svn_branch_patterns()
96 96
97 97 get_settings.assert_called_once_with(model.SVN_BRANCH_SECTION)
98 98 assert expected_result == result
99 99
100 100 def test_repo_svn_branch_patterns(self):
101 101 model = VcsSettingsModel()
102 102 expected_result = {'test': 'test'}
103 103 with mock.patch.object(model, 'repo_settings') as settings_mock:
104 104 get_settings = settings_mock.get_ui_by_section
105 105 get_settings.return_value = expected_result
106 106 settings_mock.return_value = expected_result
107 107 result = model.get_repo_svn_branch_patterns()
108 108
109 109 get_settings.assert_called_once_with(model.SVN_BRANCH_SECTION)
110 110 assert expected_result == result
111 111
112 112 def test_repo_svn_branch_patterns_raises_exception_when_repo_is_not_set(
113 113 self):
114 114 model = VcsSettingsModel()
115 115 with pytest.raises(Exception) as exc_info:
116 116 model.get_repo_svn_branch_patterns()
117 assert exc_info.value.message == 'Repository is not specified'
117 assert str(exc_info.value) == 'Repository is not specified'
118 118
119 119 def test_global_svn_tag_patterns(self):
120 120 model = VcsSettingsModel()
121 121 expected_result = {'test': 'test'}
122 122 with mock.patch.object(model, 'global_settings') as settings_mock:
123 123 get_settings = settings_mock.get_ui_by_section
124 124 get_settings.return_value = expected_result
125 125 settings_mock.return_value = expected_result
126 126 result = model.get_global_svn_tag_patterns()
127 127
128 128 get_settings.assert_called_once_with(model.SVN_TAG_SECTION)
129 129 assert expected_result == result
130 130
131 131 def test_repo_svn_tag_patterns(self):
132 132 model = VcsSettingsModel()
133 133 expected_result = {'test': 'test'}
134 134 with mock.patch.object(model, 'repo_settings') as settings_mock:
135 135 get_settings = settings_mock.get_ui_by_section
136 136 get_settings.return_value = expected_result
137 137 settings_mock.return_value = expected_result
138 138 result = model.get_repo_svn_tag_patterns()
139 139
140 140 get_settings.assert_called_once_with(model.SVN_TAG_SECTION)
141 141 assert expected_result == result
142 142
143 143 def test_repo_svn_tag_patterns_raises_exception_when_repo_is_not_set(self):
144 144 model = VcsSettingsModel()
145 145 with pytest.raises(Exception) as exc_info:
146 146 model.get_repo_svn_tag_patterns()
147 assert exc_info.value.message == 'Repository is not specified'
147 assert str(exc_info.value) == 'Repository is not specified'
148 148
149 149 def test_get_global_settings(self):
150 150 expected_result = {'test': 'test'}
151 151 model = VcsSettingsModel()
152 152 with mock.patch.object(model, '_collect_all_settings') as collect_mock:
153 153 collect_mock.return_value = expected_result
154 154 result = model.get_global_settings()
155 155
156 156 collect_mock.assert_called_once_with(global_=True)
157 157 assert result == expected_result
158 158
159 159 def test_get_repo_settings(self, repo_stub):
160 160 model = VcsSettingsModel(repo=repo_stub.repo_name)
161 161 expected_result = {'test': 'test'}
162 162 with mock.patch.object(model, '_collect_all_settings') as collect_mock:
163 163 collect_mock.return_value = expected_result
164 164 result = model.get_repo_settings()
165 165
166 166 collect_mock.assert_called_once_with(global_=False)
167 167 assert result == expected_result
168 168
169 169 @pytest.mark.parametrize('settings, global_', [
170 170 ('global_settings', True),
171 171 ('repo_settings', False)
172 172 ])
173 173 def test_collect_all_settings(self, settings, global_):
174 174 model = VcsSettingsModel()
175 175 result_mock = self._mock_result()
176 176
177 177 settings_patch = mock.patch.object(model, settings)
178 178 with settings_patch as settings_mock:
179 179 settings_mock.get_ui_by_section_and_key.return_value = result_mock
180 180 settings_mock.get_setting_by_name.return_value = result_mock
181 181 result = model._collect_all_settings(global_=global_)
182 182
183 183 ui_settings = model.HG_SETTINGS + model.GIT_SETTINGS + model.HOOKS_SETTINGS
184 184 self._assert_get_settings_calls(
185 185 settings_mock, ui_settings, model.GENERAL_SETTINGS)
186 186 self._assert_collect_all_settings_result(
187 187 ui_settings, model.GENERAL_SETTINGS, result)
188 188
189 189 @pytest.mark.parametrize('settings, global_', [
190 190 ('global_settings', True),
191 191 ('repo_settings', False)
192 192 ])
193 193 def test_collect_all_settings_without_empty_value(self, settings, global_):
194 194 model = VcsSettingsModel()
195 195
196 196 settings_patch = mock.patch.object(model, settings)
197 197 with settings_patch as settings_mock:
198 198 settings_mock.get_ui_by_section_and_key.return_value = None
199 199 settings_mock.get_setting_by_name.return_value = None
200 200 result = model._collect_all_settings(global_=global_)
201 201
202 202 assert result == {}
203 203
204 204 def _mock_result(self):
205 205 result_mock = mock.Mock()
206 206 result_mock.ui_value = 'ui_value'
207 207 result_mock.ui_active = True
208 208 result_mock.app_settings_value = 'setting_value'
209 209 return result_mock
210 210
211 211 def _assert_get_settings_calls(
212 212 self, settings_mock, ui_settings, general_settings):
213 213 assert (
214 214 settings_mock.get_ui_by_section_and_key.call_count ==
215 215 len(ui_settings))
216 216 assert (
217 217 settings_mock.get_setting_by_name.call_count ==
218 218 len(general_settings))
219 219
220 220 for section, key in ui_settings:
221 221 expected_call = mock.call(section, key)
222 222 assert (
223 223 expected_call in
224 224 settings_mock.get_ui_by_section_and_key.call_args_list)
225 225
226 226 for name in general_settings:
227 227 expected_call = mock.call(name)
228 228 assert (
229 229 expected_call in
230 230 settings_mock.get_setting_by_name.call_args_list)
231 231
232 232 def _assert_collect_all_settings_result(
233 233 self, ui_settings, general_settings, result):
234 234 expected_result = {}
235 235 for section, key in ui_settings:
236 236 key = '{}_{}'.format(section, key.replace('.', '_'))
237 237
238 238 if section in ('extensions', 'hooks'):
239 239 value = True
240 240 elif key in ['vcs_git_lfs_enabled']:
241 241 value = True
242 242 else:
243 243 value = 'ui_value'
244 244 expected_result[key] = value
245 245
246 246 for name in general_settings:
247 247 key = 'rhodecode_' + name
248 248 expected_result[key] = 'setting_value'
249 249
250 250 assert expected_result == result
251 251
252 252
253 253 class TestCreateOrUpdateRepoHookSettings(object):
254 254 def test_create_when_no_repo_object_found(self, repo_stub):
255 255 model = VcsSettingsModel(repo=repo_stub.repo_name)
256 256
257 257 self._create_settings(model, HOOKS_FORM_DATA)
258 258
259 259 cleanup = []
260 260 try:
261 261 for section, key in model.HOOKS_SETTINGS:
262 262 ui = model.repo_settings.get_ui_by_section_and_key(
263 263 section, key)
264 264 assert ui.ui_active is True
265 265 cleanup.append(ui)
266 266 finally:
267 267 for ui in cleanup:
268 268 Session().delete(ui)
269 269 Session().commit()
270 270
271 271 def test_create_raises_exception_when_data_incomplete(self, repo_stub):
272 272 model = VcsSettingsModel(repo=repo_stub.repo_name)
273 273
274 274 deleted_key = 'hooks_changegroup_repo_size'
275 275 data = HOOKS_FORM_DATA.copy()
276 276 data.pop(deleted_key)
277 277
278 278 with pytest.raises(ValueError) as exc_info:
279 279 model.create_or_update_repo_hook_settings(data)
280 assert (
281 exc_info.value.message ==
282 'The given data does not contain {} key'.format(deleted_key))
280 msg = 'The given data does not contain {} key'.format(deleted_key)
281 assert str(exc_info.value) == msg
283 282
284 283 def test_update_when_repo_object_found(self, repo_stub, settings_util):
285 284 model = VcsSettingsModel(repo=repo_stub.repo_name)
286 285 for section, key in model.HOOKS_SETTINGS:
287 286 settings_util.create_repo_rhodecode_ui(
288 287 repo_stub, section, None, key=key, active=False)
289 288 model.create_or_update_repo_hook_settings(HOOKS_FORM_DATA)
290 289 for section, key in model.HOOKS_SETTINGS:
291 290 ui = model.repo_settings.get_ui_by_section_and_key(section, key)
292 291 assert ui.ui_active is True
293 292
294 293 def _create_settings(self, model, data):
295 294 global_patch = mock.patch.object(model, 'global_settings')
296 295 global_setting = mock.Mock()
297 296 global_setting.ui_value = 'Test value'
298 297 with global_patch as global_mock:
299 298 global_mock.get_ui_by_section_and_key.return_value = global_setting
300 299 model.create_or_update_repo_hook_settings(HOOKS_FORM_DATA)
301 300
302 301
303 302 class TestUpdateGlobalHookSettings(object):
304 303 def test_update_raises_exception_when_data_incomplete(self):
305 304 model = VcsSettingsModel()
306 305
307 306 deleted_key = 'hooks_changegroup_repo_size'
308 307 data = HOOKS_FORM_DATA.copy()
309 308 data.pop(deleted_key)
310 309
311 310 with pytest.raises(ValueError) as exc_info:
312 311 model.update_global_hook_settings(data)
313 assert (
314 exc_info.value.message ==
315 'The given data does not contain {} key'.format(deleted_key))
312 msg = 'The given data does not contain {} key'.format(deleted_key)
313 assert str(exc_info.value) == msg
316 314
317 315 def test_update_global_hook_settings(self, settings_util):
318 316 model = VcsSettingsModel()
319 317 setting_mock = mock.MagicMock()
320 318 setting_mock.ui_active = False
321 319 get_settings_patcher = mock.patch.object(
322 320 model.global_settings, 'get_ui_by_section_and_key',
323 321 return_value=setting_mock)
324 322 session_patcher = mock.patch('rhodecode.model.settings.Session')
325 323 with get_settings_patcher as get_settings_mock, session_patcher:
326 324 model.update_global_hook_settings(HOOKS_FORM_DATA)
327 325 assert setting_mock.ui_active is True
328 326 assert get_settings_mock.call_count == 3
329 327
330 328
331 329 class TestCreateOrUpdateRepoGeneralSettings(object):
332 330 def test_calls_create_or_update_general_settings(self, repo_stub):
333 331 model = VcsSettingsModel(repo=repo_stub.repo_name)
334 332 create_patch = mock.patch.object(
335 333 model, '_create_or_update_general_settings')
336 334 with create_patch as create_mock:
337 335 model.create_or_update_repo_pr_settings(GENERAL_FORM_DATA)
338 336 create_mock.assert_called_once_with(
339 337 model.repo_settings, GENERAL_FORM_DATA)
340 338
341 339 def test_raises_exception_when_repository_is_not_specified(self):
342 340 model = VcsSettingsModel()
343 341 with pytest.raises(Exception) as exc_info:
344 342 model.create_or_update_repo_pr_settings(GENERAL_FORM_DATA)
345 assert exc_info.value.message == 'Repository is not specified'
343 assert str(exc_info.value) == 'Repository is not specified'
346 344
347 345
348 346 class TestCreateOrUpdatGlobalGeneralSettings(object):
349 347 def test_calls_create_or_update_general_settings(self):
350 348 model = VcsSettingsModel()
351 349 create_patch = mock.patch.object(
352 350 model, '_create_or_update_general_settings')
353 351 with create_patch as create_mock:
354 352 model.create_or_update_global_pr_settings(GENERAL_FORM_DATA)
355 353 create_mock.assert_called_once_with(
356 354 model.global_settings, GENERAL_FORM_DATA)
357 355
358 356
359 357 class TestCreateOrUpdateGeneralSettings(object):
360 358 def test_create_when_no_repo_settings_found(self, repo_stub):
361 359 model = VcsSettingsModel(repo=repo_stub.repo_name)
362 360 model._create_or_update_general_settings(
363 361 model.repo_settings, GENERAL_FORM_DATA)
364 362
365 363 cleanup = []
366 364 try:
367 365 for name in model.GENERAL_SETTINGS:
368 366 setting = model.repo_settings.get_setting_by_name(name)
369 367 assert setting.app_settings_value is True
370 368 cleanup.append(setting)
371 369 finally:
372 370 for setting in cleanup:
373 371 Session().delete(setting)
374 372 Session().commit()
375 373
376 374 def test_create_raises_exception_when_data_incomplete(self, repo_stub):
377 375 model = VcsSettingsModel(repo=repo_stub.repo_name)
378 376
379 377 deleted_key = 'rhodecode_pr_merge_enabled'
380 378 data = GENERAL_FORM_DATA.copy()
381 379 data.pop(deleted_key)
382 380
383 381 with pytest.raises(ValueError) as exc_info:
384 382 model._create_or_update_general_settings(model.repo_settings, data)
385 assert (
386 exc_info.value.message ==
387 'The given data does not contain {} key'.format(deleted_key))
383
384 msg = 'The given data does not contain {} key'.format(deleted_key)
385 assert str(exc_info.value) == msg
388 386
389 387 def test_update_when_repo_setting_found(self, repo_stub, settings_util):
390 388 model = VcsSettingsModel(repo=repo_stub.repo_name)
391 389 for name in model.GENERAL_SETTINGS:
392 390 settings_util.create_repo_rhodecode_setting(
393 391 repo_stub, name, False, 'bool')
394 392
395 393 model._create_or_update_general_settings(
396 394 model.repo_settings, GENERAL_FORM_DATA)
397 395
398 396 for name in model.GENERAL_SETTINGS:
399 397 setting = model.repo_settings.get_setting_by_name(name)
400 398 assert setting.app_settings_value is True
401 399
402 400
403 401 class TestCreateRepoSvnSettings(object):
404 402 def test_calls_create_svn_settings(self, repo_stub):
405 403 model = VcsSettingsModel(repo=repo_stub.repo_name)
406 404 with mock.patch.object(model, '_create_svn_settings') as create_mock:
407 405 model.create_repo_svn_settings(SVN_FORM_DATA)
408 406 create_mock.assert_called_once_with(model.repo_settings, SVN_FORM_DATA)
409 407
410 408 def test_raises_exception_when_repository_is_not_specified(self):
411 409 model = VcsSettingsModel()
412 410 with pytest.raises(Exception) as exc_info:
413 411 model.create_repo_svn_settings(SVN_FORM_DATA)
414 assert exc_info.value.message == 'Repository is not specified'
412 assert str(exc_info.value) == 'Repository is not specified'
415 413
416 414
417 415 class TestCreateSvnSettings(object):
418 416 def test_create(self, repo_stub):
419 417 model = VcsSettingsModel(repo=repo_stub.repo_name)
420 418 model._create_svn_settings(model.repo_settings, SVN_FORM_DATA)
421 419 Session().commit()
422 420
423 421 branch_ui = model.repo_settings.get_ui_by_section(
424 422 model.SVN_BRANCH_SECTION)
425 423 tag_ui = model.repo_settings.get_ui_by_section(
426 424 model.SVN_TAG_SECTION)
427 425
428 426 try:
429 427 assert len(branch_ui) == 1
430 428 assert len(tag_ui) == 1
431 429 finally:
432 430 Session().delete(branch_ui[0])
433 431 Session().delete(tag_ui[0])
434 432 Session().commit()
435 433
436 434 def test_create_tag(self, repo_stub):
437 435 model = VcsSettingsModel(repo=repo_stub.repo_name)
438 436 data = SVN_FORM_DATA.copy()
439 437 data.pop('new_svn_branch')
440 438 model._create_svn_settings(model.repo_settings, data)
441 439 Session().commit()
442 440
443 441 branch_ui = model.repo_settings.get_ui_by_section(
444 442 model.SVN_BRANCH_SECTION)
445 443 tag_ui = model.repo_settings.get_ui_by_section(
446 444 model.SVN_TAG_SECTION)
447 445
448 446 try:
449 447 assert len(branch_ui) == 0
450 448 assert len(tag_ui) == 1
451 449 finally:
452 450 Session().delete(tag_ui[0])
453 451 Session().commit()
454 452
455 453 def test_create_nothing_when_no_svn_settings_specified(self, repo_stub):
456 454 model = VcsSettingsModel(repo=repo_stub.repo_name)
457 455 model._create_svn_settings(model.repo_settings, {})
458 456 Session().commit()
459 457
460 458 branch_ui = model.repo_settings.get_ui_by_section(
461 459 model.SVN_BRANCH_SECTION)
462 460 tag_ui = model.repo_settings.get_ui_by_section(
463 461 model.SVN_TAG_SECTION)
464 462
465 463 assert len(branch_ui) == 0
466 464 assert len(tag_ui) == 0
467 465
468 466 def test_create_nothing_when_empty_settings_specified(self, repo_stub):
469 467 model = VcsSettingsModel(repo=repo_stub.repo_name)
470 468 data = {
471 469 'new_svn_branch': '',
472 470 'new_svn_tag': ''
473 471 }
474 472 model._create_svn_settings(model.repo_settings, data)
475 473 Session().commit()
476 474
477 475 branch_ui = model.repo_settings.get_ui_by_section(
478 476 model.SVN_BRANCH_SECTION)
479 477 tag_ui = model.repo_settings.get_ui_by_section(
480 478 model.SVN_TAG_SECTION)
481 479
482 480 assert len(branch_ui) == 0
483 481 assert len(tag_ui) == 0
484 482
485 483
486 484 class TestCreateOrUpdateUi(object):
487 485 def test_create(self, repo_stub):
488 486 model = VcsSettingsModel(repo=repo_stub.repo_name)
489 487 model._create_or_update_ui(
490 488 model.repo_settings, 'test-section', 'test-key', active=False,
491 489 value='False')
492 490 Session().commit()
493 491
494 492 created_ui = model.repo_settings.get_ui_by_section_and_key(
495 493 'test-section', 'test-key')
496 494
497 495 try:
498 496 assert created_ui.ui_active is False
499 497 assert str2bool(created_ui.ui_value) is False
500 498 finally:
501 499 Session().delete(created_ui)
502 500 Session().commit()
503 501
504 502 def test_update(self, repo_stub, settings_util):
505 503 model = VcsSettingsModel(repo=repo_stub.repo_name)
506 504
507 505 largefiles, phases, evolve = model.HG_SETTINGS
508 506
509 507 section = 'test-section'
510 508 key = 'test-key'
511 509 settings_util.create_repo_rhodecode_ui(
512 510 repo_stub, section, 'True', key=key, active=True)
513 511
514 512 model._create_or_update_ui(
515 513 model.repo_settings, section, key, active=False, value='False')
516 514 Session().commit()
517 515
518 516 created_ui = model.repo_settings.get_ui_by_section_and_key(
519 517 section, key)
520 518 assert created_ui.ui_active is False
521 519 assert str2bool(created_ui.ui_value) is False
522 520
523 521
524 522 class TestCreateOrUpdateRepoHgSettings(object):
525 523 FORM_DATA = {
526 524 'extensions_largefiles': False,
527 525 'extensions_evolve': False,
528 526 'phases_publish': False
529 527 }
530 528
531 529 def test_creates_repo_hg_settings_when_data_is_correct(self, repo_stub):
532 530 model = VcsSettingsModel(repo=repo_stub.repo_name)
533 531 with mock.patch.object(model, '_create_or_update_ui') as create_mock:
534 532 model.create_or_update_repo_hg_settings(self.FORM_DATA)
535 533 expected_calls = [
536 534 mock.call(model.repo_settings, 'extensions', 'largefiles',
537 535 active=False, value=''),
538 536 mock.call(model.repo_settings, 'extensions', 'evolve',
539 537 active=False, value=''),
540 538 mock.call(model.repo_settings, 'phases', 'publish', value='False'),
541 539 ]
542 540 assert expected_calls == create_mock.call_args_list
543 541
544 542 @pytest.mark.parametrize('field_to_remove', FORM_DATA.keys())
545 543 def test_key_is_not_found(self, repo_stub, field_to_remove):
546 544 model = VcsSettingsModel(repo=repo_stub.repo_name)
547 545 data = self.FORM_DATA.copy()
548 546 data.pop(field_to_remove)
549 547 with pytest.raises(ValueError) as exc_info:
550 548 model.create_or_update_repo_hg_settings(data)
551 549 expected_message = 'The given data does not contain {} key'.format(
552 550 field_to_remove)
553 assert exc_info.value.message == expected_message
551 assert str(exc_info.value) == expected_message
554 552
555 553 def test_create_raises_exception_when_repository_not_specified(self):
556 554 model = VcsSettingsModel()
557 555 with pytest.raises(Exception) as exc_info:
558 556 model.create_or_update_repo_hg_settings(self.FORM_DATA)
559 assert exc_info.value.message == 'Repository is not specified'
557 assert str(exc_info.value) == 'Repository is not specified'
560 558
561 559
562 560 class TestUpdateGlobalSslSetting(object):
563 561 def test_updates_global_hg_settings(self):
564 562 model = VcsSettingsModel()
565 563 with mock.patch.object(model, '_create_or_update_ui') as create_mock:
566 564 model.update_global_ssl_setting('False')
567 565 create_mock.assert_called_once_with(
568 566 model.global_settings, 'web', 'push_ssl', value='False')
569 567
570 568
571 569 class TestUpdateGlobalPathSetting(object):
572 570 def test_updates_global_path_settings(self):
573 571 model = VcsSettingsModel()
574 572 with mock.patch.object(model, '_create_or_update_ui') as create_mock:
575 573 model.update_global_path_setting('False')
576 574 create_mock.assert_called_once_with(
577 575 model.global_settings, 'paths', '/', value='False')
578 576
579 577
580 578 class TestCreateOrUpdateGlobalHgSettings(object):
581 579 FORM_DATA = {
582 580 'extensions_largefiles': False,
583 581 'largefiles_usercache': '/example/largefiles-store',
584 582 'phases_publish': False,
585 583 'extensions_hgsubversion': False,
586 584 'extensions_evolve': False
587 585 }
588 586
589 587 def test_creates_repo_hg_settings_when_data_is_correct(self):
590 588 model = VcsSettingsModel()
591 589 with mock.patch.object(model, '_create_or_update_ui') as create_mock:
592 590 model.create_or_update_global_hg_settings(self.FORM_DATA)
593 591 expected_calls = [
594 592 mock.call(model.global_settings, 'extensions', 'largefiles',
595 593 active=False, value=''),
596 594 mock.call(model.global_settings, 'largefiles', 'usercache',
597 595 value='/example/largefiles-store'),
598 596 mock.call(model.global_settings, 'phases', 'publish',
599 597 value='False'),
600 598 mock.call(model.global_settings, 'extensions', 'hgsubversion',
601 599 active=False),
602 600 mock.call(model.global_settings, 'extensions', 'evolve',
603 601 active=False, value='')
604 602 ]
605 603 assert expected_calls == create_mock.call_args_list
606 604
607 605 @pytest.mark.parametrize('field_to_remove', FORM_DATA.keys())
608 606 def test_key_is_not_found(self, repo_stub, field_to_remove):
609 607 model = VcsSettingsModel(repo=repo_stub.repo_name)
610 608 data = self.FORM_DATA.copy()
611 609 data.pop(field_to_remove)
612 610 with pytest.raises(Exception) as exc_info:
613 611 model.create_or_update_global_hg_settings(data)
614 612 expected_message = 'The given data does not contain {} key'.format(
615 613 field_to_remove)
616 assert exc_info.value.message == expected_message
614 assert str(exc_info.value) == expected_message
617 615
618 616
619 617 class TestCreateOrUpdateGlobalGitSettings(object):
620 618 FORM_DATA = {
621 619 'vcs_git_lfs_enabled': False,
622 620 'vcs_git_lfs_store_location': '/example/lfs-store',
623 621 }
624 622
625 623 def test_creates_repo_hg_settings_when_data_is_correct(self):
626 624 model = VcsSettingsModel()
627 625 with mock.patch.object(model, '_create_or_update_ui') as create_mock:
628 626 model.create_or_update_global_git_settings(self.FORM_DATA)
629 627 expected_calls = [
630 628 mock.call(model.global_settings, 'vcs_git_lfs', 'enabled',
631 629 active=False, value=False),
632 630 mock.call(model.global_settings, 'vcs_git_lfs', 'store_location',
633 631 value='/example/lfs-store'),
634 632 ]
635 633 assert expected_calls == create_mock.call_args_list
636 634
637 635
638 636 class TestDeleteRepoSvnPattern(object):
639 637 def test_success_when_repo_is_set(self, backend_svn, settings_util):
640 638 repo = backend_svn.create_repo()
641 639 repo_name = repo.repo_name
642 640
643 641 model = VcsSettingsModel(repo=repo_name)
644 642 entry = settings_util.create_repo_rhodecode_ui(
645 643 repo, VcsSettingsModel.SVN_BRANCH_SECTION, 'svn-branch')
646 644 Session().commit()
647 645
648 646 model.delete_repo_svn_pattern(entry.ui_id)
649 647
650 648 def test_fail_when_delete_id_from_other_repo(self, backend_svn):
651 649 repo_name = backend_svn.repo_name
652 650 model = VcsSettingsModel(repo=repo_name)
653 651 delete_ui_patch = mock.patch.object(model.repo_settings, 'delete_ui')
654 652 with delete_ui_patch as delete_ui_mock:
655 653 model.delete_repo_svn_pattern(123)
656 654 delete_ui_mock.assert_called_once_with(-1)
657 655
658 656 def test_raises_exception_when_repository_is_not_specified(self):
659 657 model = VcsSettingsModel()
660 658 with pytest.raises(Exception) as exc_info:
661 659 model.delete_repo_svn_pattern(123)
662 assert exc_info.value.message == 'Repository is not specified'
660 assert str(exc_info.value) == 'Repository is not specified'
663 661
664 662
665 663 class TestDeleteGlobalSvnPattern(object):
666 664 def test_delete_global_svn_pattern_calls_delete_ui(self):
667 665 model = VcsSettingsModel()
668 666 delete_ui_patch = mock.patch.object(model.global_settings, 'delete_ui')
669 667 with delete_ui_patch as delete_ui_mock:
670 668 model.delete_global_svn_pattern(123)
671 669 delete_ui_mock.assert_called_once_with(123)
672 670
673 671
674 672 class TestFilterUiSettings(object):
675 673 def test_settings_are_filtered(self):
676 674 model = VcsSettingsModel()
677 675 repo_settings = [
678 676 UiSetting('extensions', 'largefiles', '', True),
679 677 UiSetting('phases', 'publish', 'True', True),
680 678 UiSetting('hooks', 'changegroup.repo_size', 'hook', True),
681 679 UiSetting('hooks', 'changegroup.push_logger', 'hook', True),
682 680 UiSetting('hooks', 'outgoing.pull_logger', 'hook', True),
683 681 UiSetting(
684 682 'vcs_svn_branch', '84223c972204fa545ca1b22dac7bef5b68d7442d',
685 683 'test_branch', True),
686 684 UiSetting(
687 685 'vcs_svn_tag', '84229c972204fa545ca1b22dac7bef5b68d7442d',
688 686 'test_tag', True),
689 687 ]
690 688 non_repo_settings = [
691 689 UiSetting('largefiles', 'usercache', '/example/largefiles-store', True),
692 690 UiSetting('test', 'outgoing.pull_logger', 'hook', True),
693 691 UiSetting('hooks', 'test2', 'hook', True),
694 692 UiSetting(
695 693 'vcs_svn_repo', '84229c972204fa545ca1b22dac7bef5b68d7442d',
696 694 'test_tag', True),
697 695 ]
698 696 settings = repo_settings + non_repo_settings
699 697 filtered_settings = model._filter_ui_settings(settings)
700 698 assert sorted(filtered_settings) == sorted(repo_settings)
701 699
702 700
703 701 class TestFilterGeneralSettings(object):
704 702 def test_settings_are_filtered(self):
705 703 model = VcsSettingsModel()
706 704 settings = {
707 705 'rhodecode_abcde': 'value1',
708 706 'rhodecode_vwxyz': 'value2',
709 707 }
710 708 general_settings = {
711 709 'rhodecode_{}'.format(key): 'value'
712 710 for key in VcsSettingsModel.GENERAL_SETTINGS
713 711 }
714 712 settings.update(general_settings)
715 713
716 714 filtered_settings = model._filter_general_settings(general_settings)
717 715 assert sorted(filtered_settings) == sorted(general_settings)
718 716
719 717
720 718 class TestGetRepoUiSettings(object):
721 719 def test_global_uis_are_returned_when_no_repo_uis_found(
722 720 self, repo_stub):
723 721 model = VcsSettingsModel(repo=repo_stub.repo_name)
724 722 result = model.get_repo_ui_settings()
725 723 svn_sections = (
726 724 VcsSettingsModel.SVN_TAG_SECTION,
727 725 VcsSettingsModel.SVN_BRANCH_SECTION)
728 726 expected_result = [
729 727 s for s in model.global_settings.get_ui()
730 728 if s.section not in svn_sections]
731 729 assert sorted(result) == sorted(expected_result)
732 730
733 731 def test_repo_uis_are_overriding_global_uis(
734 732 self, repo_stub, settings_util):
735 733 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
736 734 settings_util.create_repo_rhodecode_ui(
737 735 repo_stub, section, 'repo', key=key, active=False)
738 736 model = VcsSettingsModel(repo=repo_stub.repo_name)
739 737 result = model.get_repo_ui_settings()
740 738 for setting in result:
741 739 locator = (setting.section, setting.key)
742 740 if locator in VcsSettingsModel.HOOKS_SETTINGS:
743 741 assert setting.value == 'repo'
744 742
745 743 assert setting.active is False
746 744
747 745 def test_global_svn_patterns_are_not_in_list(
748 746 self, repo_stub, settings_util):
749 747 svn_sections = (
750 748 VcsSettingsModel.SVN_TAG_SECTION,
751 749 VcsSettingsModel.SVN_BRANCH_SECTION)
752 750 for section in svn_sections:
753 751 settings_util.create_rhodecode_ui(
754 752 section, 'repo', key='deadbeef' + section, active=False)
755 753 model = VcsSettingsModel(repo=repo_stub.repo_name)
756 754 result = model.get_repo_ui_settings()
757 755 for setting in result:
758 756 assert setting.section not in svn_sections
759 757
760 758 def test_repo_uis_filtered_by_section_are_returned(
761 759 self, repo_stub, settings_util):
762 760 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
763 761 settings_util.create_repo_rhodecode_ui(
764 762 repo_stub, section, 'repo', key=key, active=False)
765 763 model = VcsSettingsModel(repo=repo_stub.repo_name)
766 764 section, key = VcsSettingsModel.HOOKS_SETTINGS[0]
767 765 result = model.get_repo_ui_settings(section=section)
768 766 for setting in result:
769 767 assert setting.section == section
770 768
771 769 def test_repo_uis_filtered_by_key_are_returned(
772 770 self, repo_stub, settings_util):
773 771 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
774 772 settings_util.create_repo_rhodecode_ui(
775 773 repo_stub, section, 'repo', key=key, active=False)
776 774 model = VcsSettingsModel(repo=repo_stub.repo_name)
777 775 section, key = VcsSettingsModel.HOOKS_SETTINGS[0]
778 776 result = model.get_repo_ui_settings(key=key)
779 777 for setting in result:
780 778 assert setting.key == key
781 779
782 780 def test_raises_exception_when_repository_is_not_specified(self):
783 781 model = VcsSettingsModel()
784 782 with pytest.raises(Exception) as exc_info:
785 783 model.get_repo_ui_settings()
786 assert exc_info.value.message == 'Repository is not specified'
784 assert str(exc_info.value) == 'Repository is not specified'
787 785
788 786
789 787 class TestGetRepoGeneralSettings(object):
790 788 def test_global_settings_are_returned_when_no_repo_settings_found(
791 789 self, repo_stub):
792 790 model = VcsSettingsModel(repo=repo_stub.repo_name)
793 791 result = model.get_repo_general_settings()
794 792 expected_result = model.global_settings.get_all_settings()
795 793 assert sorted(result) == sorted(expected_result)
796 794
797 795 def test_repo_uis_are_overriding_global_uis(
798 796 self, repo_stub, settings_util):
799 797 for key in VcsSettingsModel.GENERAL_SETTINGS:
800 798 settings_util.create_repo_rhodecode_setting(
801 799 repo_stub, key, 'abcde', type_='unicode')
802 800 model = VcsSettingsModel(repo=repo_stub.repo_name)
803 801 result = model.get_repo_ui_settings()
804 802 for key in result:
805 803 if key in VcsSettingsModel.GENERAL_SETTINGS:
806 804 assert result[key] == 'abcde'
807 805
808 806 def test_raises_exception_when_repository_is_not_specified(self):
809 807 model = VcsSettingsModel()
810 808 with pytest.raises(Exception) as exc_info:
811 809 model.get_repo_general_settings()
812 assert exc_info.value.message == 'Repository is not specified'
810 assert str(exc_info.value) == 'Repository is not specified'
813 811
814 812
815 813 class TestGetGlobalGeneralSettings(object):
816 814 def test_global_settings_are_returned(self, repo_stub):
817 815 model = VcsSettingsModel()
818 816 result = model.get_global_general_settings()
819 817 expected_result = model.global_settings.get_all_settings()
820 818 assert sorted(result) == sorted(expected_result)
821 819
822 820 def test_repo_uis_are_not_overriding_global_uis(
823 821 self, repo_stub, settings_util):
824 822 for key in VcsSettingsModel.GENERAL_SETTINGS:
825 823 settings_util.create_repo_rhodecode_setting(
826 824 repo_stub, key, 'abcde', type_='unicode')
827 825 model = VcsSettingsModel(repo=repo_stub.repo_name)
828 826 result = model.get_global_general_settings()
829 827 expected_result = model.global_settings.get_all_settings()
830 828 assert sorted(result) == sorted(expected_result)
831 829
832 830
833 831 class TestGetGlobalUiSettings(object):
834 832 def test_global_uis_are_returned(self, repo_stub):
835 833 model = VcsSettingsModel()
836 834 result = model.get_global_ui_settings()
837 835 expected_result = model.global_settings.get_ui()
838 836 assert sorted(result) == sorted(expected_result)
839 837
840 838 def test_repo_uis_are_not_overriding_global_uis(
841 839 self, repo_stub, settings_util):
842 840 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
843 841 settings_util.create_repo_rhodecode_ui(
844 842 repo_stub, section, 'repo', key=key, active=False)
845 843 model = VcsSettingsModel(repo=repo_stub.repo_name)
846 844 result = model.get_global_ui_settings()
847 845 expected_result = model.global_settings.get_ui()
848 846 assert sorted(result) == sorted(expected_result)
849 847
850 848 def test_ui_settings_filtered_by_section(
851 849 self, repo_stub, settings_util):
852 850 model = VcsSettingsModel(repo=repo_stub.repo_name)
853 851 section, key = VcsSettingsModel.HOOKS_SETTINGS[0]
854 852 result = model.get_global_ui_settings(section=section)
855 853 expected_result = model.global_settings.get_ui(section=section)
856 854 assert sorted(result) == sorted(expected_result)
857 855
858 856 def test_ui_settings_filtered_by_key(
859 857 self, repo_stub, settings_util):
860 858 model = VcsSettingsModel(repo=repo_stub.repo_name)
861 859 section, key = VcsSettingsModel.HOOKS_SETTINGS[0]
862 860 result = model.get_global_ui_settings(key=key)
863 861 expected_result = model.global_settings.get_ui(key=key)
864 862 assert sorted(result) == sorted(expected_result)
865 863
866 864
867 865 class TestGetGeneralSettings(object):
868 866 def test_global_settings_are_returned_when_inherited_is_true(
869 867 self, repo_stub, settings_util):
870 868 model = VcsSettingsModel(repo=repo_stub.repo_name)
871 869 model.inherit_global_settings = True
872 870 for key in VcsSettingsModel.GENERAL_SETTINGS:
873 871 settings_util.create_repo_rhodecode_setting(
874 872 repo_stub, key, 'abcde', type_='unicode')
875 873 result = model.get_general_settings()
876 874 expected_result = model.get_global_general_settings()
877 875 assert sorted(result) == sorted(expected_result)
878 876
879 877 def test_repo_settings_are_returned_when_inherited_is_false(
880 878 self, repo_stub, settings_util):
881 879 model = VcsSettingsModel(repo=repo_stub.repo_name)
882 880 model.inherit_global_settings = False
883 881 for key in VcsSettingsModel.GENERAL_SETTINGS:
884 882 settings_util.create_repo_rhodecode_setting(
885 883 repo_stub, key, 'abcde', type_='unicode')
886 884 result = model.get_general_settings()
887 885 expected_result = model.get_repo_general_settings()
888 886 assert sorted(result) == sorted(expected_result)
889 887
890 888 def test_global_settings_are_returned_when_no_repository_specified(self):
891 889 model = VcsSettingsModel()
892 890 result = model.get_general_settings()
893 891 expected_result = model.get_global_general_settings()
894 892 assert sorted(result) == sorted(expected_result)
895 893
896 894
897 895 class TestGetUiSettings(object):
898 896 def test_global_settings_are_returned_when_inherited_is_true(
899 897 self, repo_stub, settings_util):
900 898 model = VcsSettingsModel(repo=repo_stub.repo_name)
901 899 model.inherit_global_settings = True
902 900 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
903 901 settings_util.create_repo_rhodecode_ui(
904 902 repo_stub, section, 'repo', key=key, active=True)
905 903 result = model.get_ui_settings()
906 904 expected_result = model.get_global_ui_settings()
907 905 assert sorted(result) == sorted(expected_result)
908 906
909 907 def test_repo_settings_are_returned_when_inherited_is_false(
910 908 self, repo_stub, settings_util):
911 909 model = VcsSettingsModel(repo=repo_stub.repo_name)
912 910 model.inherit_global_settings = False
913 911 for section, key in VcsSettingsModel.HOOKS_SETTINGS:
914 912 settings_util.create_repo_rhodecode_ui(
915 913 repo_stub, section, 'repo', key=key, active=True)
916 914 result = model.get_ui_settings()
917 915 expected_result = model.get_repo_ui_settings()
918 916 assert sorted(result) == sorted(expected_result)
919 917
920 918 def test_repo_settings_filtered_by_section_and_key(self, repo_stub):
921 919 model = VcsSettingsModel(repo=repo_stub.repo_name)
922 920 model.inherit_global_settings = False
923 921 args = ('section', 'key')
924 922 with mock.patch.object(model, 'get_repo_ui_settings') as settings_mock:
925 923 model.get_ui_settings(*args)
926 924 settings_mock.assert_called_once_with(*args)
927 925
928 926 def test_global_settings_filtered_by_section_and_key(self):
929 927 model = VcsSettingsModel()
930 928 args = ('section', 'key')
931 929 with mock.patch.object(model, 'get_global_ui_settings') as (
932 930 settings_mock):
933 931 model.get_ui_settings(*args)
934 932 settings_mock.assert_called_once_with(*args)
935 933
936 934 def test_global_settings_are_returned_when_no_repository_specified(self):
937 935 model = VcsSettingsModel()
938 936 result = model.get_ui_settings()
939 937 expected_result = model.get_global_ui_settings()
940 938 assert sorted(result) == sorted(expected_result)
941 939
942 940
943 941 class TestGetSvnPatterns(object):
944 942 def test_repo_settings_filtered_by_section_and_key(self, repo_stub):
945 943 model = VcsSettingsModel(repo=repo_stub.repo_name)
946 944 args = ('section', )
947 945 with mock.patch.object(model, 'get_repo_ui_settings') as settings_mock:
948 946 model.get_svn_patterns(*args)
949 947 settings_mock.assert_called_once_with(*args)
950 948
951 949 def test_global_settings_filtered_by_section_and_key(self):
952 950 model = VcsSettingsModel()
953 951 args = ('section', )
954 952 with mock.patch.object(model, 'get_global_ui_settings') as (
955 953 settings_mock):
956 954 model.get_svn_patterns(*args)
957 955 settings_mock.assert_called_once_with(*args)
958 956
959 957
960 958 class TestGetReposLocation(object):
961 959 def test_returns_repos_location(self, repo_stub):
962 960 model = VcsSettingsModel()
963 961
964 962 result_mock = mock.Mock()
965 963 result_mock.ui_value = '/tmp'
966 964
967 965 with mock.patch.object(model, 'global_settings') as settings_mock:
968 966 settings_mock.get_ui_by_key.return_value = result_mock
969 967 result = model.get_repos_location()
970 968
971 969 settings_mock.get_ui_by_key.assert_called_once_with('/')
972 970 assert result == '/tmp'
973 971
974 972
975 973 class TestCreateOrUpdateRepoSettings(object):
976 974 FORM_DATA = {
977 975 'inherit_global_settings': False,
978 976 'hooks_changegroup_repo_size': False,
979 977 'hooks_changegroup_push_logger': False,
980 978 'hooks_outgoing_pull_logger': False,
981 979 'extensions_largefiles': False,
982 980 'extensions_evolve': False,
983 981 'largefiles_usercache': '/example/largefiles-store',
984 982 'vcs_git_lfs_enabled': False,
985 983 'vcs_git_lfs_store_location': '/',
986 984 'phases_publish': 'False',
987 985 'rhodecode_pr_merge_enabled': False,
988 986 'rhodecode_use_outdated_comments': False,
989 987 'new_svn_branch': '',
990 988 'new_svn_tag': ''
991 989 }
992 990
993 991 def test_get_raises_exception_when_repository_not_specified(self):
994 992 model = VcsSettingsModel()
995 993 with pytest.raises(Exception) as exc_info:
996 994 model.create_or_update_repo_settings(data=self.FORM_DATA)
997 assert exc_info.value.message == 'Repository is not specified'
995 assert str(exc_info.value) == 'Repository is not specified'
998 996
999 997 def test_only_svn_settings_are_updated_when_type_is_svn(self, backend_svn):
1000 998 repo = backend_svn.create_repo()
1001 999 model = VcsSettingsModel(repo=repo)
1002 1000 with self._patch_model(model) as mocks:
1003 1001 model.create_or_update_repo_settings(
1004 1002 data=self.FORM_DATA, inherit_global_settings=False)
1005 1003 mocks['create_repo_svn_settings'].assert_called_once_with(
1006 1004 self.FORM_DATA)
1007 1005 non_called_methods = (
1008 1006 'create_or_update_repo_hook_settings',
1009 1007 'create_or_update_repo_pr_settings',
1010 1008 'create_or_update_repo_hg_settings')
1011 1009 for method in non_called_methods:
1012 1010 assert mocks[method].call_count == 0
1013 1011
1014 1012 def test_non_svn_settings_are_updated_when_type_is_hg(self, backend_hg):
1015 1013 repo = backend_hg.create_repo()
1016 1014 model = VcsSettingsModel(repo=repo)
1017 1015 with self._patch_model(model) as mocks:
1018 1016 model.create_or_update_repo_settings(
1019 1017 data=self.FORM_DATA, inherit_global_settings=False)
1020 1018
1021 1019 assert mocks['create_repo_svn_settings'].call_count == 0
1022 1020 called_methods = (
1023 1021 'create_or_update_repo_hook_settings',
1024 1022 'create_or_update_repo_pr_settings',
1025 1023 'create_or_update_repo_hg_settings')
1026 1024 for method in called_methods:
1027 1025 mocks[method].assert_called_once_with(self.FORM_DATA)
1028 1026
1029 1027 def test_non_svn_and_hg_settings_are_updated_when_type_is_git(
1030 1028 self, backend_git):
1031 1029 repo = backend_git.create_repo()
1032 1030 model = VcsSettingsModel(repo=repo)
1033 1031 with self._patch_model(model) as mocks:
1034 1032 model.create_or_update_repo_settings(
1035 1033 data=self.FORM_DATA, inherit_global_settings=False)
1036 1034
1037 1035 assert mocks['create_repo_svn_settings'].call_count == 0
1038 1036 called_methods = (
1039 1037 'create_or_update_repo_hook_settings',
1040 1038 'create_or_update_repo_pr_settings')
1041 1039 non_called_methods = (
1042 1040 'create_repo_svn_settings',
1043 1041 'create_or_update_repo_hg_settings'
1044 1042 )
1045 1043 for method in called_methods:
1046 1044 mocks[method].assert_called_once_with(self.FORM_DATA)
1047 1045 for method in non_called_methods:
1048 1046 assert mocks[method].call_count == 0
1049 1047
1050 1048 def test_no_methods_are_called_when_settings_are_inherited(
1051 1049 self, backend):
1052 1050 repo = backend.create_repo()
1053 1051 model = VcsSettingsModel(repo=repo)
1054 1052 with self._patch_model(model) as mocks:
1055 1053 model.create_or_update_repo_settings(
1056 1054 data=self.FORM_DATA, inherit_global_settings=True)
1057 1055 for method_name in mocks:
1058 1056 assert mocks[method_name].call_count == 0
1059 1057
1060 1058 def test_cache_is_marked_for_invalidation(self, repo_stub):
1061 1059 model = VcsSettingsModel(repo=repo_stub)
1062 1060 invalidation_patcher = mock.patch(
1063 1061 'rhodecode.model.scm.ScmModel.mark_for_invalidation')
1064 1062 with invalidation_patcher as invalidation_mock:
1065 1063 model.create_or_update_repo_settings(
1066 1064 data=self.FORM_DATA, inherit_global_settings=True)
1067 1065 invalidation_mock.assert_called_once_with(
1068 1066 repo_stub.repo_name, delete=True)
1069 1067
1070 1068 def test_inherit_flag_is_saved(self, repo_stub):
1071 1069 model = VcsSettingsModel(repo=repo_stub)
1072 1070 model.inherit_global_settings = True
1073 1071 with self._patch_model(model):
1074 1072 model.create_or_update_repo_settings(
1075 1073 data=self.FORM_DATA, inherit_global_settings=False)
1076 1074 assert model.inherit_global_settings is False
1077 1075
1078 1076 def _patch_model(self, model):
1079 1077 return mock.patch.multiple(
1080 1078 model,
1081 1079 create_repo_svn_settings=mock.DEFAULT,
1082 1080 create_or_update_repo_hook_settings=mock.DEFAULT,
1083 1081 create_or_update_repo_pr_settings=mock.DEFAULT,
1084 1082 create_or_update_repo_hg_settings=mock.DEFAULT)
General Comments 0
You need to be logged in to leave comments. Login now