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