Show More
@@ -1,814 +1,821 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2017 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2017 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 os |
|
21 | import os | |
22 | import hashlib |
|
22 | import hashlib | |
23 | import logging |
|
23 | import logging | |
24 | from collections import namedtuple |
|
24 | from collections import namedtuple | |
25 | from functools import wraps |
|
25 | from functools import wraps | |
26 |
|
26 | |||
27 | from rhodecode.lib import caches |
|
27 | from rhodecode.lib import caches | |
28 | from rhodecode.lib.utils2 import ( |
|
28 | from rhodecode.lib.utils2 import ( | |
29 | Optional, AttributeDict, safe_str, remove_prefix, str2bool) |
|
29 | Optional, AttributeDict, safe_str, remove_prefix, str2bool) | |
30 | from rhodecode.lib.vcs.backends import base |
|
30 | from rhodecode.lib.vcs.backends import base | |
31 | from rhodecode.model import BaseModel |
|
31 | from rhodecode.model import BaseModel | |
32 | from rhodecode.model.db import ( |
|
32 | from rhodecode.model.db import ( | |
33 | RepoRhodeCodeUi, RepoRhodeCodeSetting, RhodeCodeUi, RhodeCodeSetting) |
|
33 | RepoRhodeCodeUi, RepoRhodeCodeSetting, RhodeCodeUi, RhodeCodeSetting) | |
34 | from rhodecode.model.meta import Session |
|
34 | from rhodecode.model.meta import Session | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | log = logging.getLogger(__name__) |
|
37 | log = logging.getLogger(__name__) | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | UiSetting = namedtuple( |
|
40 | UiSetting = namedtuple( | |
41 | 'UiSetting', ['section', 'key', 'value', 'active']) |
|
41 | 'UiSetting', ['section', 'key', 'value', 'active']) | |
42 |
|
42 | |||
43 | SOCIAL_PLUGINS_LIST = ['github', 'bitbucket', 'twitter', 'google'] |
|
43 | SOCIAL_PLUGINS_LIST = ['github', 'bitbucket', 'twitter', 'google'] | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | class SettingNotFound(Exception): |
|
46 | class SettingNotFound(Exception): | |
47 | def __init__(self): |
|
47 | def __init__(self, setting_id): | |
48 | super(SettingNotFound, self).__init__('Setting is not found') |
|
48 | msg = 'Setting `{}` is not found'.format(setting_id) | |
|
49 | super(SettingNotFound, self).__init__(msg) | |||
49 |
|
50 | |||
50 |
|
51 | |||
51 | class SettingsModel(BaseModel): |
|
52 | class SettingsModel(BaseModel): | |
52 | BUILTIN_HOOKS = ( |
|
53 | BUILTIN_HOOKS = ( | |
53 | RhodeCodeUi.HOOK_REPO_SIZE, RhodeCodeUi.HOOK_PUSH, |
|
54 | RhodeCodeUi.HOOK_REPO_SIZE, RhodeCodeUi.HOOK_PUSH, | |
54 | RhodeCodeUi.HOOK_PRE_PUSH, RhodeCodeUi.HOOK_PRETX_PUSH, |
|
55 | RhodeCodeUi.HOOK_PRE_PUSH, RhodeCodeUi.HOOK_PRETX_PUSH, | |
55 | RhodeCodeUi.HOOK_PULL, RhodeCodeUi.HOOK_PRE_PULL, |
|
56 | RhodeCodeUi.HOOK_PULL, RhodeCodeUi.HOOK_PRE_PULL, | |
56 | RhodeCodeUi.HOOK_PUSH_KEY,) |
|
57 | RhodeCodeUi.HOOK_PUSH_KEY,) | |
57 | HOOKS_SECTION = 'hooks' |
|
58 | HOOKS_SECTION = 'hooks' | |
58 |
|
59 | |||
59 | def __init__(self, sa=None, repo=None): |
|
60 | def __init__(self, sa=None, repo=None): | |
60 | self.repo = repo |
|
61 | self.repo = repo | |
61 | self.UiDbModel = RepoRhodeCodeUi if repo else RhodeCodeUi |
|
62 | self.UiDbModel = RepoRhodeCodeUi if repo else RhodeCodeUi | |
62 | self.SettingsDbModel = ( |
|
63 | self.SettingsDbModel = ( | |
63 | RepoRhodeCodeSetting if repo else RhodeCodeSetting) |
|
64 | RepoRhodeCodeSetting if repo else RhodeCodeSetting) | |
64 | super(SettingsModel, self).__init__(sa) |
|
65 | super(SettingsModel, self).__init__(sa) | |
65 |
|
66 | |||
66 | def get_ui_by_key(self, key): |
|
67 | def get_ui_by_key(self, key): | |
67 | q = self.UiDbModel.query() |
|
68 | q = self.UiDbModel.query() | |
68 | q = q.filter(self.UiDbModel.ui_key == key) |
|
69 | q = q.filter(self.UiDbModel.ui_key == key) | |
69 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
70 | q = self._filter_by_repo(RepoRhodeCodeUi, q) | |
70 | return q.scalar() |
|
71 | return q.scalar() | |
71 |
|
72 | |||
72 | def get_ui_by_section(self, section): |
|
73 | def get_ui_by_section(self, section): | |
73 | q = self.UiDbModel.query() |
|
74 | q = self.UiDbModel.query() | |
74 | q = q.filter(self.UiDbModel.ui_section == section) |
|
75 | q = q.filter(self.UiDbModel.ui_section == section) | |
75 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
76 | q = self._filter_by_repo(RepoRhodeCodeUi, q) | |
76 | return q.all() |
|
77 | return q.all() | |
77 |
|
78 | |||
78 | def get_ui_by_section_and_key(self, section, key): |
|
79 | def get_ui_by_section_and_key(self, section, key): | |
79 | q = self.UiDbModel.query() |
|
80 | q = self.UiDbModel.query() | |
80 | q = q.filter(self.UiDbModel.ui_section == section) |
|
81 | q = q.filter(self.UiDbModel.ui_section == section) | |
81 | q = q.filter(self.UiDbModel.ui_key == key) |
|
82 | q = q.filter(self.UiDbModel.ui_key == key) | |
82 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
83 | q = self._filter_by_repo(RepoRhodeCodeUi, q) | |
83 | return q.scalar() |
|
84 | return q.scalar() | |
84 |
|
85 | |||
85 | def get_ui(self, section=None, key=None): |
|
86 | def get_ui(self, section=None, key=None): | |
86 | q = self.UiDbModel.query() |
|
87 | q = self.UiDbModel.query() | |
87 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
88 | q = self._filter_by_repo(RepoRhodeCodeUi, q) | |
88 |
|
89 | |||
89 | if section: |
|
90 | if section: | |
90 | q = q.filter(self.UiDbModel.ui_section == section) |
|
91 | q = q.filter(self.UiDbModel.ui_section == section) | |
91 | if key: |
|
92 | if key: | |
92 | q = q.filter(self.UiDbModel.ui_key == key) |
|
93 | q = q.filter(self.UiDbModel.ui_key == key) | |
93 |
|
94 | |||
94 | # TODO: mikhail: add caching |
|
95 | # TODO: mikhail: add caching | |
95 | result = [ |
|
96 | result = [ | |
96 | UiSetting( |
|
97 | UiSetting( | |
97 | section=safe_str(r.ui_section), key=safe_str(r.ui_key), |
|
98 | section=safe_str(r.ui_section), key=safe_str(r.ui_key), | |
98 | value=safe_str(r.ui_value), active=r.ui_active |
|
99 | value=safe_str(r.ui_value), active=r.ui_active | |
99 | ) |
|
100 | ) | |
100 | for r in q.all() |
|
101 | for r in q.all() | |
101 | ] |
|
102 | ] | |
102 | return result |
|
103 | return result | |
103 |
|
104 | |||
104 | def get_builtin_hooks(self): |
|
105 | def get_builtin_hooks(self): | |
105 | q = self.UiDbModel.query() |
|
106 | q = self.UiDbModel.query() | |
106 | q = q.filter(self.UiDbModel.ui_key.in_(self.BUILTIN_HOOKS)) |
|
107 | q = q.filter(self.UiDbModel.ui_key.in_(self.BUILTIN_HOOKS)) | |
107 | return self._get_hooks(q) |
|
108 | return self._get_hooks(q) | |
108 |
|
109 | |||
109 | def get_custom_hooks(self): |
|
110 | def get_custom_hooks(self): | |
110 | q = self.UiDbModel.query() |
|
111 | q = self.UiDbModel.query() | |
111 | q = q.filter(~self.UiDbModel.ui_key.in_(self.BUILTIN_HOOKS)) |
|
112 | q = q.filter(~self.UiDbModel.ui_key.in_(self.BUILTIN_HOOKS)) | |
112 | return self._get_hooks(q) |
|
113 | return self._get_hooks(q) | |
113 |
|
114 | |||
114 | def create_ui_section_value(self, section, val, key=None, active=True): |
|
115 | def create_ui_section_value(self, section, val, key=None, active=True): | |
115 | new_ui = self.UiDbModel() |
|
116 | new_ui = self.UiDbModel() | |
116 | new_ui.ui_section = section |
|
117 | new_ui.ui_section = section | |
117 | new_ui.ui_value = val |
|
118 | new_ui.ui_value = val | |
118 | new_ui.ui_active = active |
|
119 | new_ui.ui_active = active | |
119 |
|
120 | |||
120 | if self.repo: |
|
121 | if self.repo: | |
121 | repo = self._get_repo(self.repo) |
|
122 | repo = self._get_repo(self.repo) | |
122 | repository_id = repo.repo_id |
|
123 | repository_id = repo.repo_id | |
123 | new_ui.repository_id = repository_id |
|
124 | new_ui.repository_id = repository_id | |
124 |
|
125 | |||
125 | if not key: |
|
126 | if not key: | |
126 | # keys are unique so they need appended info |
|
127 | # keys are unique so they need appended info | |
127 | if self.repo: |
|
128 | if self.repo: | |
128 | key = hashlib.sha1( |
|
129 | key = hashlib.sha1( | |
129 | '{}{}{}'.format(section, val, repository_id)).hexdigest() |
|
130 | '{}{}{}'.format(section, val, repository_id)).hexdigest() | |
130 | else: |
|
131 | else: | |
131 | key = hashlib.sha1('{}{}'.format(section, val)).hexdigest() |
|
132 | key = hashlib.sha1('{}{}'.format(section, val)).hexdigest() | |
132 |
|
133 | |||
133 | new_ui.ui_key = key |
|
134 | new_ui.ui_key = key | |
134 |
|
135 | |||
135 | Session().add(new_ui) |
|
136 | Session().add(new_ui) | |
136 | return new_ui |
|
137 | return new_ui | |
137 |
|
138 | |||
138 | def create_or_update_hook(self, key, value): |
|
139 | def create_or_update_hook(self, key, value): | |
139 | ui = ( |
|
140 | ui = ( | |
140 | self.get_ui_by_section_and_key(self.HOOKS_SECTION, key) or |
|
141 | self.get_ui_by_section_and_key(self.HOOKS_SECTION, key) or | |
141 | self.UiDbModel()) |
|
142 | self.UiDbModel()) | |
142 | ui.ui_section = self.HOOKS_SECTION |
|
143 | ui.ui_section = self.HOOKS_SECTION | |
143 | ui.ui_active = True |
|
144 | ui.ui_active = True | |
144 | ui.ui_key = key |
|
145 | ui.ui_key = key | |
145 | ui.ui_value = value |
|
146 | ui.ui_value = value | |
146 |
|
147 | |||
147 | if self.repo: |
|
148 | if self.repo: | |
148 | repo = self._get_repo(self.repo) |
|
149 | repo = self._get_repo(self.repo) | |
149 | repository_id = repo.repo_id |
|
150 | repository_id = repo.repo_id | |
150 | ui.repository_id = repository_id |
|
151 | ui.repository_id = repository_id | |
151 |
|
152 | |||
152 | Session().add(ui) |
|
153 | Session().add(ui) | |
153 | return ui |
|
154 | return ui | |
154 |
|
155 | |||
155 | def delete_ui(self, id_): |
|
156 | def delete_ui(self, id_): | |
156 | ui = self.UiDbModel.get(id_) |
|
157 | ui = self.UiDbModel.get(id_) | |
157 | if not ui: |
|
158 | if not ui: | |
158 | raise SettingNotFound() |
|
159 | raise SettingNotFound(id_) | |
159 | Session().delete(ui) |
|
160 | Session().delete(ui) | |
160 |
|
161 | |||
161 | def get_setting_by_name(self, name): |
|
162 | def get_setting_by_name(self, name): | |
162 | q = self._get_settings_query() |
|
163 | q = self._get_settings_query() | |
163 | q = q.filter(self.SettingsDbModel.app_settings_name == name) |
|
164 | q = q.filter(self.SettingsDbModel.app_settings_name == name) | |
164 | return q.scalar() |
|
165 | return q.scalar() | |
165 |
|
166 | |||
166 | def create_or_update_setting( |
|
167 | def create_or_update_setting( | |
167 | self, name, val=Optional(''), type_=Optional('unicode')): |
|
168 | self, name, val=Optional(''), type_=Optional('unicode')): | |
168 | """ |
|
169 | """ | |
169 | Creates or updates RhodeCode setting. If updates is triggered it will |
|
170 | Creates or updates RhodeCode setting. If updates is triggered it will | |
170 | only update parameters that are explicityl set Optional instance will |
|
171 | only update parameters that are explicityl set Optional instance will | |
171 | be skipped |
|
172 | be skipped | |
172 |
|
173 | |||
173 | :param name: |
|
174 | :param name: | |
174 | :param val: |
|
175 | :param val: | |
175 | :param type_: |
|
176 | :param type_: | |
176 | :return: |
|
177 | :return: | |
177 | """ |
|
178 | """ | |
178 |
|
179 | |||
179 | res = self.get_setting_by_name(name) |
|
180 | res = self.get_setting_by_name(name) | |
180 | repo = self._get_repo(self.repo) if self.repo else None |
|
181 | repo = self._get_repo(self.repo) if self.repo else None | |
181 |
|
182 | |||
182 | if not res: |
|
183 | if not res: | |
183 | val = Optional.extract(val) |
|
184 | val = Optional.extract(val) | |
184 | type_ = Optional.extract(type_) |
|
185 | type_ = Optional.extract(type_) | |
185 |
|
186 | |||
186 | args = ( |
|
187 | args = ( | |
187 | (repo.repo_id, name, val, type_) |
|
188 | (repo.repo_id, name, val, type_) | |
188 | if repo else (name, val, type_)) |
|
189 | if repo else (name, val, type_)) | |
189 | res = self.SettingsDbModel(*args) |
|
190 | res = self.SettingsDbModel(*args) | |
190 |
|
191 | |||
191 | else: |
|
192 | else: | |
192 | if self.repo: |
|
193 | if self.repo: | |
193 | res.repository_id = repo.repo_id |
|
194 | res.repository_id = repo.repo_id | |
194 |
|
195 | |||
195 | res.app_settings_name = name |
|
196 | res.app_settings_name = name | |
196 | if not isinstance(type_, Optional): |
|
197 | if not isinstance(type_, Optional): | |
197 | # update if set |
|
198 | # update if set | |
198 | res.app_settings_type = type_ |
|
199 | res.app_settings_type = type_ | |
199 | if not isinstance(val, Optional): |
|
200 | if not isinstance(val, Optional): | |
200 | # update if set |
|
201 | # update if set | |
201 | res.app_settings_value = val |
|
202 | res.app_settings_value = val | |
202 |
|
203 | |||
203 | Session().add(res) |
|
204 | Session().add(res) | |
204 | return res |
|
205 | return res | |
205 |
|
206 | |||
206 | def invalidate_settings_cache(self): |
|
207 | def invalidate_settings_cache(self): | |
207 | namespace = 'rhodecode_settings' |
|
208 | namespace = 'rhodecode_settings' | |
208 | cache_manager = caches.get_cache_manager('sql_cache_short', namespace) |
|
209 | cache_manager = caches.get_cache_manager('sql_cache_short', namespace) | |
209 | caches.clear_cache_manager(cache_manager) |
|
210 | caches.clear_cache_manager(cache_manager) | |
210 |
|
211 | |||
211 | def get_all_settings(self, cache=False): |
|
212 | def get_all_settings(self, cache=False): | |
212 |
|
213 | |||
213 | def _compute(): |
|
214 | def _compute(): | |
214 | q = self._get_settings_query() |
|
215 | q = self._get_settings_query() | |
215 | if not q: |
|
216 | if not q: | |
216 | raise Exception('Could not get application settings !') |
|
217 | raise Exception('Could not get application settings !') | |
217 |
|
218 | |||
218 | settings = { |
|
219 | settings = { | |
219 | 'rhodecode_' + result.app_settings_name: result.app_settings_value |
|
220 | 'rhodecode_' + result.app_settings_name: result.app_settings_value | |
220 | for result in q |
|
221 | for result in q | |
221 | } |
|
222 | } | |
222 | return settings |
|
223 | return settings | |
223 |
|
224 | |||
224 | if cache: |
|
225 | if cache: | |
225 | log.debug('Fetching app settings using cache') |
|
226 | log.debug('Fetching app settings using cache') | |
226 | repo = self._get_repo(self.repo) if self.repo else None |
|
227 | repo = self._get_repo(self.repo) if self.repo else None | |
227 | namespace = 'rhodecode_settings' |
|
228 | namespace = 'rhodecode_settings' | |
228 | cache_manager = caches.get_cache_manager( |
|
229 | cache_manager = caches.get_cache_manager( | |
229 | 'sql_cache_short', namespace) |
|
230 | 'sql_cache_short', namespace) | |
230 | _cache_key = ( |
|
231 | _cache_key = ( | |
231 | "get_repo_{}_settings".format(repo.repo_id) |
|
232 | "get_repo_{}_settings".format(repo.repo_id) | |
232 | if repo else "get_app_settings") |
|
233 | if repo else "get_app_settings") | |
233 |
|
234 | |||
234 | return cache_manager.get(_cache_key, createfunc=_compute) |
|
235 | return cache_manager.get(_cache_key, createfunc=_compute) | |
235 |
|
236 | |||
236 | else: |
|
237 | else: | |
237 | return _compute() |
|
238 | return _compute() | |
238 |
|
239 | |||
239 | def get_auth_settings(self): |
|
240 | def get_auth_settings(self): | |
240 | q = self._get_settings_query() |
|
241 | q = self._get_settings_query() | |
241 | q = q.filter( |
|
242 | q = q.filter( | |
242 | self.SettingsDbModel.app_settings_name.startswith('auth_')) |
|
243 | self.SettingsDbModel.app_settings_name.startswith('auth_')) | |
243 | rows = q.all() |
|
244 | rows = q.all() | |
244 | auth_settings = { |
|
245 | auth_settings = { | |
245 | row.app_settings_name: row.app_settings_value for row in rows} |
|
246 | row.app_settings_name: row.app_settings_value for row in rows} | |
246 | return auth_settings |
|
247 | return auth_settings | |
247 |
|
248 | |||
248 | def get_auth_plugins(self): |
|
249 | def get_auth_plugins(self): | |
249 | auth_plugins = self.get_setting_by_name("auth_plugins") |
|
250 | auth_plugins = self.get_setting_by_name("auth_plugins") | |
250 | return auth_plugins.app_settings_value |
|
251 | return auth_plugins.app_settings_value | |
251 |
|
252 | |||
252 | def get_default_repo_settings(self, strip_prefix=False): |
|
253 | def get_default_repo_settings(self, strip_prefix=False): | |
253 | q = self._get_settings_query() |
|
254 | q = self._get_settings_query() | |
254 | q = q.filter( |
|
255 | q = q.filter( | |
255 | self.SettingsDbModel.app_settings_name.startswith('default_')) |
|
256 | self.SettingsDbModel.app_settings_name.startswith('default_')) | |
256 | rows = q.all() |
|
257 | rows = q.all() | |
257 |
|
258 | |||
258 | result = {} |
|
259 | result = {} | |
259 | for row in rows: |
|
260 | for row in rows: | |
260 | key = row.app_settings_name |
|
261 | key = row.app_settings_name | |
261 | if strip_prefix: |
|
262 | if strip_prefix: | |
262 | key = remove_prefix(key, prefix='default_') |
|
263 | key = remove_prefix(key, prefix='default_') | |
263 | result.update({key: row.app_settings_value}) |
|
264 | result.update({key: row.app_settings_value}) | |
264 | return result |
|
265 | return result | |
265 |
|
266 | |||
266 | def get_repo(self): |
|
267 | def get_repo(self): | |
267 | repo = self._get_repo(self.repo) |
|
268 | repo = self._get_repo(self.repo) | |
268 | if not repo: |
|
269 | if not repo: | |
269 | raise Exception( |
|
270 | raise Exception( | |
270 | 'Repository `{}` cannot be found inside the database'.format( |
|
271 | 'Repository `{}` cannot be found inside the database'.format( | |
271 | self.repo)) |
|
272 | self.repo)) | |
272 | return repo |
|
273 | return repo | |
273 |
|
274 | |||
274 | def _filter_by_repo(self, model, query): |
|
275 | def _filter_by_repo(self, model, query): | |
275 | if self.repo: |
|
276 | if self.repo: | |
276 | repo = self.get_repo() |
|
277 | repo = self.get_repo() | |
277 | query = query.filter(model.repository_id == repo.repo_id) |
|
278 | query = query.filter(model.repository_id == repo.repo_id) | |
278 | return query |
|
279 | return query | |
279 |
|
280 | |||
280 | def _get_hooks(self, query): |
|
281 | def _get_hooks(self, query): | |
281 | query = query.filter(self.UiDbModel.ui_section == self.HOOKS_SECTION) |
|
282 | query = query.filter(self.UiDbModel.ui_section == self.HOOKS_SECTION) | |
282 | query = self._filter_by_repo(RepoRhodeCodeUi, query) |
|
283 | query = self._filter_by_repo(RepoRhodeCodeUi, query) | |
283 | return query.all() |
|
284 | return query.all() | |
284 |
|
285 | |||
285 | def _get_settings_query(self): |
|
286 | def _get_settings_query(self): | |
286 | q = self.SettingsDbModel.query() |
|
287 | q = self.SettingsDbModel.query() | |
287 | return self._filter_by_repo(RepoRhodeCodeSetting, q) |
|
288 | return self._filter_by_repo(RepoRhodeCodeSetting, q) | |
288 |
|
289 | |||
289 | def list_enabled_social_plugins(self, settings): |
|
290 | def list_enabled_social_plugins(self, settings): | |
290 | enabled = [] |
|
291 | enabled = [] | |
291 | for plug in SOCIAL_PLUGINS_LIST: |
|
292 | for plug in SOCIAL_PLUGINS_LIST: | |
292 | if str2bool(settings.get('rhodecode_auth_{}_enabled'.format(plug) |
|
293 | if str2bool(settings.get('rhodecode_auth_{}_enabled'.format(plug) | |
293 | )): |
|
294 | )): | |
294 | enabled.append(plug) |
|
295 | enabled.append(plug) | |
295 | return enabled |
|
296 | return enabled | |
296 |
|
297 | |||
297 |
|
298 | |||
298 | def assert_repo_settings(func): |
|
299 | def assert_repo_settings(func): | |
299 | @wraps(func) |
|
300 | @wraps(func) | |
300 | def _wrapper(self, *args, **kwargs): |
|
301 | def _wrapper(self, *args, **kwargs): | |
301 | if not self.repo_settings: |
|
302 | if not self.repo_settings: | |
302 | raise Exception('Repository is not specified') |
|
303 | raise Exception('Repository is not specified') | |
303 | return func(self, *args, **kwargs) |
|
304 | return func(self, *args, **kwargs) | |
304 | return _wrapper |
|
305 | return _wrapper | |
305 |
|
306 | |||
306 |
|
307 | |||
307 | class IssueTrackerSettingsModel(object): |
|
308 | class IssueTrackerSettingsModel(object): | |
308 | INHERIT_SETTINGS = 'inherit_issue_tracker_settings' |
|
309 | INHERIT_SETTINGS = 'inherit_issue_tracker_settings' | |
309 | SETTINGS_PREFIX = 'issuetracker_' |
|
310 | SETTINGS_PREFIX = 'issuetracker_' | |
310 |
|
311 | |||
311 | def __init__(self, sa=None, repo=None): |
|
312 | def __init__(self, sa=None, repo=None): | |
312 | self.global_settings = SettingsModel(sa=sa) |
|
313 | self.global_settings = SettingsModel(sa=sa) | |
313 | self.repo_settings = SettingsModel(sa=sa, repo=repo) if repo else None |
|
314 | self.repo_settings = SettingsModel(sa=sa, repo=repo) if repo else None | |
314 |
|
315 | |||
315 | @property |
|
316 | @property | |
316 | def inherit_global_settings(self): |
|
317 | def inherit_global_settings(self): | |
317 | if not self.repo_settings: |
|
318 | if not self.repo_settings: | |
318 | return True |
|
319 | return True | |
319 | setting = self.repo_settings.get_setting_by_name(self.INHERIT_SETTINGS) |
|
320 | setting = self.repo_settings.get_setting_by_name(self.INHERIT_SETTINGS) | |
320 | return setting.app_settings_value if setting else True |
|
321 | return setting.app_settings_value if setting else True | |
321 |
|
322 | |||
322 | @inherit_global_settings.setter |
|
323 | @inherit_global_settings.setter | |
323 | def inherit_global_settings(self, value): |
|
324 | def inherit_global_settings(self, value): | |
324 | if self.repo_settings: |
|
325 | if self.repo_settings: | |
325 | settings = self.repo_settings.create_or_update_setting( |
|
326 | settings = self.repo_settings.create_or_update_setting( | |
326 | self.INHERIT_SETTINGS, value, type_='bool') |
|
327 | self.INHERIT_SETTINGS, value, type_='bool') | |
327 | Session().add(settings) |
|
328 | Session().add(settings) | |
328 |
|
329 | |||
329 | def _get_keyname(self, key, uid, prefix=''): |
|
330 | def _get_keyname(self, key, uid, prefix=''): | |
330 | return '{0}{1}{2}_{3}'.format( |
|
331 | return '{0}{1}{2}_{3}'.format( | |
331 | prefix, self.SETTINGS_PREFIX, key, uid) |
|
332 | prefix, self.SETTINGS_PREFIX, key, uid) | |
332 |
|
333 | |||
333 | def _make_dict_for_settings(self, qs): |
|
334 | def _make_dict_for_settings(self, qs): | |
334 | prefix_match = self._get_keyname('pat', '', 'rhodecode_') |
|
335 | prefix_match = self._get_keyname('pat', '', 'rhodecode_') | |
335 |
|
336 | |||
336 | issuetracker_entries = {} |
|
337 | issuetracker_entries = {} | |
337 | # create keys |
|
338 | # create keys | |
338 | for k, v in qs.items(): |
|
339 | for k, v in qs.items(): | |
339 | if k.startswith(prefix_match): |
|
340 | if k.startswith(prefix_match): | |
340 | uid = k[len(prefix_match):] |
|
341 | uid = k[len(prefix_match):] | |
341 | issuetracker_entries[uid] = None |
|
342 | issuetracker_entries[uid] = None | |
342 |
|
343 | |||
343 | # populate |
|
344 | # populate | |
344 | for uid in issuetracker_entries: |
|
345 | for uid in issuetracker_entries: | |
345 | issuetracker_entries[uid] = AttributeDict({ |
|
346 | issuetracker_entries[uid] = AttributeDict({ | |
346 | 'pat': qs.get(self._get_keyname('pat', uid, 'rhodecode_')), |
|
347 | 'pat': qs.get(self._get_keyname('pat', uid, 'rhodecode_')), | |
347 | 'url': qs.get(self._get_keyname('url', uid, 'rhodecode_')), |
|
348 | 'url': qs.get(self._get_keyname('url', uid, 'rhodecode_')), | |
348 | 'pref': qs.get(self._get_keyname('pref', uid, 'rhodecode_')), |
|
349 | 'pref': qs.get(self._get_keyname('pref', uid, 'rhodecode_')), | |
349 | 'desc': qs.get(self._get_keyname('desc', uid, 'rhodecode_')), |
|
350 | 'desc': qs.get(self._get_keyname('desc', uid, 'rhodecode_')), | |
350 | }) |
|
351 | }) | |
351 | return issuetracker_entries |
|
352 | return issuetracker_entries | |
352 |
|
353 | |||
353 | def get_global_settings(self, cache=False): |
|
354 | def get_global_settings(self, cache=False): | |
354 | """ |
|
355 | """ | |
355 | Returns list of global issue tracker settings |
|
356 | Returns list of global issue tracker settings | |
356 | """ |
|
357 | """ | |
357 | defaults = self.global_settings.get_all_settings(cache=cache) |
|
358 | defaults = self.global_settings.get_all_settings(cache=cache) | |
358 | settings = self._make_dict_for_settings(defaults) |
|
359 | settings = self._make_dict_for_settings(defaults) | |
359 | return settings |
|
360 | return settings | |
360 |
|
361 | |||
361 | def get_repo_settings(self, cache=False): |
|
362 | def get_repo_settings(self, cache=False): | |
362 | """ |
|
363 | """ | |
363 | Returns list of issue tracker settings per repository |
|
364 | Returns list of issue tracker settings per repository | |
364 | """ |
|
365 | """ | |
365 | if not self.repo_settings: |
|
366 | if not self.repo_settings: | |
366 | raise Exception('Repository is not specified') |
|
367 | raise Exception('Repository is not specified') | |
367 | all_settings = self.repo_settings.get_all_settings(cache=cache) |
|
368 | all_settings = self.repo_settings.get_all_settings(cache=cache) | |
368 | settings = self._make_dict_for_settings(all_settings) |
|
369 | settings = self._make_dict_for_settings(all_settings) | |
369 | return settings |
|
370 | return settings | |
370 |
|
371 | |||
371 | def get_settings(self, cache=False): |
|
372 | def get_settings(self, cache=False): | |
372 | if self.inherit_global_settings: |
|
373 | if self.inherit_global_settings: | |
373 | return self.get_global_settings(cache=cache) |
|
374 | return self.get_global_settings(cache=cache) | |
374 | else: |
|
375 | else: | |
375 | return self.get_repo_settings(cache=cache) |
|
376 | return self.get_repo_settings(cache=cache) | |
376 |
|
377 | |||
377 | def delete_entries(self, uid): |
|
378 | def delete_entries(self, uid): | |
378 | if self.repo_settings: |
|
379 | if self.repo_settings: | |
379 | all_patterns = self.get_repo_settings() |
|
380 | all_patterns = self.get_repo_settings() | |
380 | settings_model = self.repo_settings |
|
381 | settings_model = self.repo_settings | |
381 | else: |
|
382 | else: | |
382 | all_patterns = self.get_global_settings() |
|
383 | all_patterns = self.get_global_settings() | |
383 | settings_model = self.global_settings |
|
384 | settings_model = self.global_settings | |
384 | entries = all_patterns.get(uid) |
|
385 | entries = all_patterns.get(uid) | |
385 |
|
386 | |||
386 | for del_key in entries: |
|
387 | for del_key in entries: | |
387 | setting_name = self._get_keyname(del_key, uid) |
|
388 | setting_name = self._get_keyname(del_key, uid) | |
388 | entry = settings_model.get_setting_by_name(setting_name) |
|
389 | entry = settings_model.get_setting_by_name(setting_name) | |
389 | if entry: |
|
390 | if entry: | |
390 | Session().delete(entry) |
|
391 | Session().delete(entry) | |
391 |
|
392 | |||
392 | Session().commit() |
|
393 | Session().commit() | |
393 |
|
394 | |||
394 | def create_or_update_setting( |
|
395 | def create_or_update_setting( | |
395 | self, name, val=Optional(''), type_=Optional('unicode')): |
|
396 | self, name, val=Optional(''), type_=Optional('unicode')): | |
396 | if self.repo_settings: |
|
397 | if self.repo_settings: | |
397 | setting = self.repo_settings.create_or_update_setting( |
|
398 | setting = self.repo_settings.create_or_update_setting( | |
398 | name, val, type_) |
|
399 | name, val, type_) | |
399 | else: |
|
400 | else: | |
400 | setting = self.global_settings.create_or_update_setting( |
|
401 | setting = self.global_settings.create_or_update_setting( | |
401 | name, val, type_) |
|
402 | name, val, type_) | |
402 | return setting |
|
403 | return setting | |
403 |
|
404 | |||
404 |
|
405 | |||
405 | class VcsSettingsModel(object): |
|
406 | class VcsSettingsModel(object): | |
406 |
|
407 | |||
407 | INHERIT_SETTINGS = 'inherit_vcs_settings' |
|
408 | INHERIT_SETTINGS = 'inherit_vcs_settings' | |
408 | GENERAL_SETTINGS = ( |
|
409 | GENERAL_SETTINGS = ( | |
409 | 'use_outdated_comments', |
|
410 | 'use_outdated_comments', | |
410 | 'pr_merge_enabled', |
|
411 | 'pr_merge_enabled', | |
411 | 'hg_use_rebase_for_merging', |
|
412 | 'hg_use_rebase_for_merging', | |
412 | 'hg_close_branch_before_merging', |
|
413 | 'hg_close_branch_before_merging', | |
413 | 'git_use_rebase_for_merging', |
|
414 | 'git_use_rebase_for_merging', | |
414 | 'git_close_branch_before_merging') |
|
415 | 'git_close_branch_before_merging') | |
415 |
|
416 | |||
416 | HOOKS_SETTINGS = ( |
|
417 | HOOKS_SETTINGS = ( | |
417 | ('hooks', 'changegroup.repo_size'), |
|
418 | ('hooks', 'changegroup.repo_size'), | |
418 | ('hooks', 'changegroup.push_logger'), |
|
419 | ('hooks', 'changegroup.push_logger'), | |
419 | ('hooks', 'outgoing.pull_logger'),) |
|
420 | ('hooks', 'outgoing.pull_logger'),) | |
420 | HG_SETTINGS = ( |
|
421 | HG_SETTINGS = ( | |
421 | ('extensions', 'largefiles'), |
|
422 | ('extensions', 'largefiles'), | |
422 | ('phases', 'publish'), |
|
423 | ('phases', 'publish'), | |
423 | ('extensions', 'evolve'),) |
|
424 | ('extensions', 'evolve'),) | |
424 | GIT_SETTINGS = ( |
|
425 | GIT_SETTINGS = ( | |
425 | ('vcs_git_lfs', 'enabled'),) |
|
426 | ('vcs_git_lfs', 'enabled'),) | |
426 | GLOBAL_HG_SETTINGS = ( |
|
427 | GLOBAL_HG_SETTINGS = ( | |
427 | ('extensions', 'largefiles'), |
|
428 | ('extensions', 'largefiles'), | |
428 | ('largefiles', 'usercache'), |
|
429 | ('largefiles', 'usercache'), | |
429 | ('phases', 'publish'), |
|
430 | ('phases', 'publish'), | |
430 | ('extensions', 'hgsubversion'), |
|
431 | ('extensions', 'hgsubversion'), | |
431 | ('extensions', 'evolve'),) |
|
432 | ('extensions', 'evolve'),) | |
432 | GLOBAL_GIT_SETTINGS = ( |
|
433 | GLOBAL_GIT_SETTINGS = ( | |
433 | ('vcs_git_lfs', 'enabled'), |
|
434 | ('vcs_git_lfs', 'enabled'), | |
434 | ('vcs_git_lfs', 'store_location')) |
|
435 | ('vcs_git_lfs', 'store_location')) | |
435 | GLOBAL_SVN_SETTINGS = ( |
|
436 | GLOBAL_SVN_SETTINGS = ( | |
436 | ('vcs_svn_proxy', 'http_requests_enabled'), |
|
437 | ('vcs_svn_proxy', 'http_requests_enabled'), | |
437 | ('vcs_svn_proxy', 'http_server_url')) |
|
438 | ('vcs_svn_proxy', 'http_server_url')) | |
438 |
|
439 | |||
439 | SVN_BRANCH_SECTION = 'vcs_svn_branch' |
|
440 | SVN_BRANCH_SECTION = 'vcs_svn_branch' | |
440 | SVN_TAG_SECTION = 'vcs_svn_tag' |
|
441 | SVN_TAG_SECTION = 'vcs_svn_tag' | |
441 | SSL_SETTING = ('web', 'push_ssl') |
|
442 | SSL_SETTING = ('web', 'push_ssl') | |
442 | PATH_SETTING = ('paths', '/') |
|
443 | PATH_SETTING = ('paths', '/') | |
443 |
|
444 | |||
444 | def __init__(self, sa=None, repo=None): |
|
445 | def __init__(self, sa=None, repo=None): | |
445 | self.global_settings = SettingsModel(sa=sa) |
|
446 | self.global_settings = SettingsModel(sa=sa) | |
446 | self.repo_settings = SettingsModel(sa=sa, repo=repo) if repo else None |
|
447 | self.repo_settings = SettingsModel(sa=sa, repo=repo) if repo else None | |
447 | self._ui_settings = ( |
|
448 | self._ui_settings = ( | |
448 | self.HG_SETTINGS + self.GIT_SETTINGS + self.HOOKS_SETTINGS) |
|
449 | self.HG_SETTINGS + self.GIT_SETTINGS + self.HOOKS_SETTINGS) | |
449 | self._svn_sections = (self.SVN_BRANCH_SECTION, self.SVN_TAG_SECTION) |
|
450 | self._svn_sections = (self.SVN_BRANCH_SECTION, self.SVN_TAG_SECTION) | |
450 |
|
451 | |||
451 | @property |
|
452 | @property | |
452 | @assert_repo_settings |
|
453 | @assert_repo_settings | |
453 | def inherit_global_settings(self): |
|
454 | def inherit_global_settings(self): | |
454 | setting = self.repo_settings.get_setting_by_name(self.INHERIT_SETTINGS) |
|
455 | setting = self.repo_settings.get_setting_by_name(self.INHERIT_SETTINGS) | |
455 | return setting.app_settings_value if setting else True |
|
456 | return setting.app_settings_value if setting else True | |
456 |
|
457 | |||
457 | @inherit_global_settings.setter |
|
458 | @inherit_global_settings.setter | |
458 | @assert_repo_settings |
|
459 | @assert_repo_settings | |
459 | def inherit_global_settings(self, value): |
|
460 | def inherit_global_settings(self, value): | |
460 | self.repo_settings.create_or_update_setting( |
|
461 | self.repo_settings.create_or_update_setting( | |
461 | self.INHERIT_SETTINGS, value, type_='bool') |
|
462 | self.INHERIT_SETTINGS, value, type_='bool') | |
462 |
|
463 | |||
463 | def get_global_svn_branch_patterns(self): |
|
464 | def get_global_svn_branch_patterns(self): | |
464 | return self.global_settings.get_ui_by_section(self.SVN_BRANCH_SECTION) |
|
465 | return self.global_settings.get_ui_by_section(self.SVN_BRANCH_SECTION) | |
465 |
|
466 | |||
466 | @assert_repo_settings |
|
467 | @assert_repo_settings | |
467 | def get_repo_svn_branch_patterns(self): |
|
468 | def get_repo_svn_branch_patterns(self): | |
468 | return self.repo_settings.get_ui_by_section(self.SVN_BRANCH_SECTION) |
|
469 | return self.repo_settings.get_ui_by_section(self.SVN_BRANCH_SECTION) | |
469 |
|
470 | |||
470 | def get_global_svn_tag_patterns(self): |
|
471 | def get_global_svn_tag_patterns(self): | |
471 | return self.global_settings.get_ui_by_section(self.SVN_TAG_SECTION) |
|
472 | return self.global_settings.get_ui_by_section(self.SVN_TAG_SECTION) | |
472 |
|
473 | |||
473 | @assert_repo_settings |
|
474 | @assert_repo_settings | |
474 | def get_repo_svn_tag_patterns(self): |
|
475 | def get_repo_svn_tag_patterns(self): | |
475 | return self.repo_settings.get_ui_by_section(self.SVN_TAG_SECTION) |
|
476 | return self.repo_settings.get_ui_by_section(self.SVN_TAG_SECTION) | |
476 |
|
477 | |||
477 | def get_global_settings(self): |
|
478 | def get_global_settings(self): | |
478 | return self._collect_all_settings(global_=True) |
|
479 | return self._collect_all_settings(global_=True) | |
479 |
|
480 | |||
480 | @assert_repo_settings |
|
481 | @assert_repo_settings | |
481 | def get_repo_settings(self): |
|
482 | def get_repo_settings(self): | |
482 | return self._collect_all_settings(global_=False) |
|
483 | return self._collect_all_settings(global_=False) | |
483 |
|
484 | |||
484 | @assert_repo_settings |
|
485 | @assert_repo_settings | |
485 | def create_or_update_repo_settings( |
|
486 | def create_or_update_repo_settings( | |
486 | self, data, inherit_global_settings=False): |
|
487 | self, data, inherit_global_settings=False): | |
487 | from rhodecode.model.scm import ScmModel |
|
488 | from rhodecode.model.scm import ScmModel | |
488 |
|
489 | |||
489 | self.inherit_global_settings = inherit_global_settings |
|
490 | self.inherit_global_settings = inherit_global_settings | |
490 |
|
491 | |||
491 | repo = self.repo_settings.get_repo() |
|
492 | repo = self.repo_settings.get_repo() | |
492 | if not inherit_global_settings: |
|
493 | if not inherit_global_settings: | |
493 | if repo.repo_type == 'svn': |
|
494 | if repo.repo_type == 'svn': | |
494 | self.create_repo_svn_settings(data) |
|
495 | self.create_repo_svn_settings(data) | |
495 | else: |
|
496 | else: | |
496 | self.create_or_update_repo_hook_settings(data) |
|
497 | self.create_or_update_repo_hook_settings(data) | |
497 | self.create_or_update_repo_pr_settings(data) |
|
498 | self.create_or_update_repo_pr_settings(data) | |
498 |
|
499 | |||
499 | if repo.repo_type == 'hg': |
|
500 | if repo.repo_type == 'hg': | |
500 | self.create_or_update_repo_hg_settings(data) |
|
501 | self.create_or_update_repo_hg_settings(data) | |
501 |
|
502 | |||
502 | if repo.repo_type == 'git': |
|
503 | if repo.repo_type == 'git': | |
503 | self.create_or_update_repo_git_settings(data) |
|
504 | self.create_or_update_repo_git_settings(data) | |
504 |
|
505 | |||
505 | ScmModel().mark_for_invalidation(repo.repo_name, delete=True) |
|
506 | ScmModel().mark_for_invalidation(repo.repo_name, delete=True) | |
506 |
|
507 | |||
507 | @assert_repo_settings |
|
508 | @assert_repo_settings | |
508 | def create_or_update_repo_hook_settings(self, data): |
|
509 | def create_or_update_repo_hook_settings(self, data): | |
509 | for section, key in self.HOOKS_SETTINGS: |
|
510 | for section, key in self.HOOKS_SETTINGS: | |
510 | data_key = self._get_form_ui_key(section, key) |
|
511 | data_key = self._get_form_ui_key(section, key) | |
511 | if data_key not in data: |
|
512 | if data_key not in data: | |
512 | raise ValueError( |
|
513 | raise ValueError( | |
513 | 'The given data does not contain {} key'.format(data_key)) |
|
514 | 'The given data does not contain {} key'.format(data_key)) | |
514 |
|
515 | |||
515 | active = data.get(data_key) |
|
516 | active = data.get(data_key) | |
516 | repo_setting = self.repo_settings.get_ui_by_section_and_key( |
|
517 | repo_setting = self.repo_settings.get_ui_by_section_and_key( | |
517 | section, key) |
|
518 | section, key) | |
518 | if not repo_setting: |
|
519 | if not repo_setting: | |
519 | global_setting = self.global_settings.\ |
|
520 | global_setting = self.global_settings.\ | |
520 | get_ui_by_section_and_key(section, key) |
|
521 | get_ui_by_section_and_key(section, key) | |
521 | self.repo_settings.create_ui_section_value( |
|
522 | self.repo_settings.create_ui_section_value( | |
522 | section, global_setting.ui_value, key=key, active=active) |
|
523 | section, global_setting.ui_value, key=key, active=active) | |
523 | else: |
|
524 | else: | |
524 | repo_setting.ui_active = active |
|
525 | repo_setting.ui_active = active | |
525 | Session().add(repo_setting) |
|
526 | Session().add(repo_setting) | |
526 |
|
527 | |||
527 | def update_global_hook_settings(self, data): |
|
528 | def update_global_hook_settings(self, data): | |
528 | for section, key in self.HOOKS_SETTINGS: |
|
529 | for section, key in self.HOOKS_SETTINGS: | |
529 | data_key = self._get_form_ui_key(section, key) |
|
530 | data_key = self._get_form_ui_key(section, key) | |
530 | if data_key not in data: |
|
531 | if data_key not in data: | |
531 | raise ValueError( |
|
532 | raise ValueError( | |
532 | 'The given data does not contain {} key'.format(data_key)) |
|
533 | 'The given data does not contain {} key'.format(data_key)) | |
533 | active = data.get(data_key) |
|
534 | active = data.get(data_key) | |
534 | repo_setting = self.global_settings.get_ui_by_section_and_key( |
|
535 | repo_setting = self.global_settings.get_ui_by_section_and_key( | |
535 | section, key) |
|
536 | section, key) | |
536 | repo_setting.ui_active = active |
|
537 | repo_setting.ui_active = active | |
537 | Session().add(repo_setting) |
|
538 | Session().add(repo_setting) | |
538 |
|
539 | |||
539 | @assert_repo_settings |
|
540 | @assert_repo_settings | |
540 | def create_or_update_repo_pr_settings(self, data): |
|
541 | def create_or_update_repo_pr_settings(self, data): | |
541 | return self._create_or_update_general_settings( |
|
542 | return self._create_or_update_general_settings( | |
542 | self.repo_settings, data) |
|
543 | self.repo_settings, data) | |
543 |
|
544 | |||
544 | def create_or_update_global_pr_settings(self, data): |
|
545 | def create_or_update_global_pr_settings(self, data): | |
545 | return self._create_or_update_general_settings( |
|
546 | return self._create_or_update_general_settings( | |
546 | self.global_settings, data) |
|
547 | self.global_settings, data) | |
547 |
|
548 | |||
548 | @assert_repo_settings |
|
549 | @assert_repo_settings | |
549 | def create_repo_svn_settings(self, data): |
|
550 | def create_repo_svn_settings(self, data): | |
550 | return self._create_svn_settings(self.repo_settings, data) |
|
551 | return self._create_svn_settings(self.repo_settings, data) | |
551 |
|
552 | |||
552 | @assert_repo_settings |
|
553 | @assert_repo_settings | |
553 | def create_or_update_repo_hg_settings(self, data): |
|
554 | def create_or_update_repo_hg_settings(self, data): | |
554 | largefiles, phases, evolve = \ |
|
555 | largefiles, phases, evolve = \ | |
555 | self.HG_SETTINGS |
|
556 | self.HG_SETTINGS | |
556 | largefiles_key, phases_key, evolve_key = \ |
|
557 | largefiles_key, phases_key, evolve_key = \ | |
557 | self._get_settings_keys(self.HG_SETTINGS, data) |
|
558 | self._get_settings_keys(self.HG_SETTINGS, data) | |
558 |
|
559 | |||
559 | self._create_or_update_ui( |
|
560 | self._create_or_update_ui( | |
560 | self.repo_settings, *largefiles, value='', |
|
561 | self.repo_settings, *largefiles, value='', | |
561 | active=data[largefiles_key]) |
|
562 | active=data[largefiles_key]) | |
562 | self._create_or_update_ui( |
|
563 | self._create_or_update_ui( | |
563 | self.repo_settings, *evolve, value='', |
|
564 | self.repo_settings, *evolve, value='', | |
564 | active=data[evolve_key]) |
|
565 | active=data[evolve_key]) | |
565 | self._create_or_update_ui( |
|
566 | self._create_or_update_ui( | |
566 | self.repo_settings, *phases, value=safe_str(data[phases_key])) |
|
567 | self.repo_settings, *phases, value=safe_str(data[phases_key])) | |
567 |
|
568 | |||
568 | def create_or_update_global_hg_settings(self, data): |
|
569 | def create_or_update_global_hg_settings(self, data): | |
569 | largefiles, largefiles_store, phases, hgsubversion, evolve \ |
|
570 | largefiles, largefiles_store, phases, hgsubversion, evolve \ | |
570 | = self.GLOBAL_HG_SETTINGS |
|
571 | = self.GLOBAL_HG_SETTINGS | |
571 | largefiles_key, largefiles_store_key, phases_key, subversion_key, evolve_key \ |
|
572 | largefiles_key, largefiles_store_key, phases_key, subversion_key, evolve_key \ | |
572 | = self._get_settings_keys(self.GLOBAL_HG_SETTINGS, data) |
|
573 | = self._get_settings_keys(self.GLOBAL_HG_SETTINGS, data) | |
573 |
|
574 | |||
574 | self._create_or_update_ui( |
|
575 | self._create_or_update_ui( | |
575 | self.global_settings, *largefiles, value='', |
|
576 | self.global_settings, *largefiles, value='', | |
576 | active=data[largefiles_key]) |
|
577 | active=data[largefiles_key]) | |
577 | self._create_or_update_ui( |
|
578 | self._create_or_update_ui( | |
578 | self.global_settings, *largefiles_store, |
|
579 | self.global_settings, *largefiles_store, | |
579 | value=data[largefiles_store_key]) |
|
580 | value=data[largefiles_store_key]) | |
580 | self._create_or_update_ui( |
|
581 | self._create_or_update_ui( | |
581 | self.global_settings, *phases, value=safe_str(data[phases_key])) |
|
582 | self.global_settings, *phases, value=safe_str(data[phases_key])) | |
582 | self._create_or_update_ui( |
|
583 | self._create_or_update_ui( | |
583 | self.global_settings, *hgsubversion, active=data[subversion_key]) |
|
584 | self.global_settings, *hgsubversion, active=data[subversion_key]) | |
584 | self._create_or_update_ui( |
|
585 | self._create_or_update_ui( | |
585 | self.global_settings, *evolve, value='', |
|
586 | self.global_settings, *evolve, value='', | |
586 | active=data[evolve_key]) |
|
587 | active=data[evolve_key]) | |
587 |
|
588 | |||
588 | def create_or_update_repo_git_settings(self, data): |
|
589 | def create_or_update_repo_git_settings(self, data): | |
589 | # NOTE(marcink): # comma make unpack work properly |
|
590 | # NOTE(marcink): # comma make unpack work properly | |
590 | lfs_enabled, \ |
|
591 | lfs_enabled, \ | |
591 | = self.GIT_SETTINGS |
|
592 | = self.GIT_SETTINGS | |
592 |
|
593 | |||
593 | lfs_enabled_key, \ |
|
594 | lfs_enabled_key, \ | |
594 | = self._get_settings_keys(self.GIT_SETTINGS, data) |
|
595 | = self._get_settings_keys(self.GIT_SETTINGS, data) | |
595 |
|
596 | |||
596 | self._create_or_update_ui( |
|
597 | self._create_or_update_ui( | |
597 | self.repo_settings, *lfs_enabled, value=data[lfs_enabled_key], |
|
598 | self.repo_settings, *lfs_enabled, value=data[lfs_enabled_key], | |
598 | active=data[lfs_enabled_key]) |
|
599 | active=data[lfs_enabled_key]) | |
599 |
|
600 | |||
600 | def create_or_update_global_git_settings(self, data): |
|
601 | def create_or_update_global_git_settings(self, data): | |
601 | lfs_enabled, lfs_store_location \ |
|
602 | lfs_enabled, lfs_store_location \ | |
602 | = self.GLOBAL_GIT_SETTINGS |
|
603 | = self.GLOBAL_GIT_SETTINGS | |
603 | lfs_enabled_key, lfs_store_location_key \ |
|
604 | lfs_enabled_key, lfs_store_location_key \ | |
604 | = self._get_settings_keys(self.GLOBAL_GIT_SETTINGS, data) |
|
605 | = self._get_settings_keys(self.GLOBAL_GIT_SETTINGS, data) | |
605 |
|
606 | |||
606 | self._create_or_update_ui( |
|
607 | self._create_or_update_ui( | |
607 | self.global_settings, *lfs_enabled, value=data[lfs_enabled_key], |
|
608 | self.global_settings, *lfs_enabled, value=data[lfs_enabled_key], | |
608 | active=data[lfs_enabled_key]) |
|
609 | active=data[lfs_enabled_key]) | |
609 | self._create_or_update_ui( |
|
610 | self._create_or_update_ui( | |
610 | self.global_settings, *lfs_store_location, |
|
611 | self.global_settings, *lfs_store_location, | |
611 | value=data[lfs_store_location_key]) |
|
612 | value=data[lfs_store_location_key]) | |
612 |
|
613 | |||
613 | def create_or_update_global_svn_settings(self, data): |
|
614 | def create_or_update_global_svn_settings(self, data): | |
614 | # branch/tags patterns |
|
615 | # branch/tags patterns | |
615 | self._create_svn_settings(self.global_settings, data) |
|
616 | self._create_svn_settings(self.global_settings, data) | |
616 |
|
617 | |||
617 | http_requests_enabled, http_server_url = self.GLOBAL_SVN_SETTINGS |
|
618 | http_requests_enabled, http_server_url = self.GLOBAL_SVN_SETTINGS | |
618 | http_requests_enabled_key, http_server_url_key = self._get_settings_keys( |
|
619 | http_requests_enabled_key, http_server_url_key = self._get_settings_keys( | |
619 | self.GLOBAL_SVN_SETTINGS, data) |
|
620 | self.GLOBAL_SVN_SETTINGS, data) | |
620 |
|
621 | |||
621 | self._create_or_update_ui( |
|
622 | self._create_or_update_ui( | |
622 | self.global_settings, *http_requests_enabled, |
|
623 | self.global_settings, *http_requests_enabled, | |
623 | value=safe_str(data[http_requests_enabled_key])) |
|
624 | value=safe_str(data[http_requests_enabled_key])) | |
624 | self._create_or_update_ui( |
|
625 | self._create_or_update_ui( | |
625 | self.global_settings, *http_server_url, |
|
626 | self.global_settings, *http_server_url, | |
626 | value=data[http_server_url_key]) |
|
627 | value=data[http_server_url_key]) | |
627 |
|
628 | |||
628 | def update_global_ssl_setting(self, value): |
|
629 | def update_global_ssl_setting(self, value): | |
629 | self._create_or_update_ui( |
|
630 | self._create_or_update_ui( | |
630 | self.global_settings, *self.SSL_SETTING, value=value) |
|
631 | self.global_settings, *self.SSL_SETTING, value=value) | |
631 |
|
632 | |||
632 | def update_global_path_setting(self, value): |
|
633 | def update_global_path_setting(self, value): | |
633 | self._create_or_update_ui( |
|
634 | self._create_or_update_ui( | |
634 | self.global_settings, *self.PATH_SETTING, value=value) |
|
635 | self.global_settings, *self.PATH_SETTING, value=value) | |
635 |
|
636 | |||
636 | @assert_repo_settings |
|
637 | @assert_repo_settings | |
637 | def delete_repo_svn_pattern(self, id_): |
|
638 | def delete_repo_svn_pattern(self, id_): | |
638 |
self.repo_settings.delet |
|
639 | ui = self.repo_settings.UiDbModel.get(id_) | |
|
640 | if ui and ui.repository.repo_name == self.repo_settings.repo: | |||
|
641 | # only delete if it's the same repo as initialized settings | |||
|
642 | self.repo_settings.delete_ui(id_) | |||
|
643 | else: | |||
|
644 | # raise error as if we wouldn't find this option | |||
|
645 | self.repo_settings.delete_ui(-1) | |||
639 |
|
646 | |||
640 | def delete_global_svn_pattern(self, id_): |
|
647 | def delete_global_svn_pattern(self, id_): | |
641 | self.global_settings.delete_ui(id_) |
|
648 | self.global_settings.delete_ui(id_) | |
642 |
|
649 | |||
643 | @assert_repo_settings |
|
650 | @assert_repo_settings | |
644 | def get_repo_ui_settings(self, section=None, key=None): |
|
651 | def get_repo_ui_settings(self, section=None, key=None): | |
645 | global_uis = self.global_settings.get_ui(section, key) |
|
652 | global_uis = self.global_settings.get_ui(section, key) | |
646 | repo_uis = self.repo_settings.get_ui(section, key) |
|
653 | repo_uis = self.repo_settings.get_ui(section, key) | |
647 | filtered_repo_uis = self._filter_ui_settings(repo_uis) |
|
654 | filtered_repo_uis = self._filter_ui_settings(repo_uis) | |
648 | filtered_repo_uis_keys = [ |
|
655 | filtered_repo_uis_keys = [ | |
649 | (s.section, s.key) for s in filtered_repo_uis] |
|
656 | (s.section, s.key) for s in filtered_repo_uis] | |
650 |
|
657 | |||
651 | def _is_global_ui_filtered(ui): |
|
658 | def _is_global_ui_filtered(ui): | |
652 | return ( |
|
659 | return ( | |
653 | (ui.section, ui.key) in filtered_repo_uis_keys |
|
660 | (ui.section, ui.key) in filtered_repo_uis_keys | |
654 | or ui.section in self._svn_sections) |
|
661 | or ui.section in self._svn_sections) | |
655 |
|
662 | |||
656 | filtered_global_uis = [ |
|
663 | filtered_global_uis = [ | |
657 | ui for ui in global_uis if not _is_global_ui_filtered(ui)] |
|
664 | ui for ui in global_uis if not _is_global_ui_filtered(ui)] | |
658 |
|
665 | |||
659 | return filtered_global_uis + filtered_repo_uis |
|
666 | return filtered_global_uis + filtered_repo_uis | |
660 |
|
667 | |||
661 | def get_global_ui_settings(self, section=None, key=None): |
|
668 | def get_global_ui_settings(self, section=None, key=None): | |
662 | return self.global_settings.get_ui(section, key) |
|
669 | return self.global_settings.get_ui(section, key) | |
663 |
|
670 | |||
664 | def get_ui_settings_as_config_obj(self, section=None, key=None): |
|
671 | def get_ui_settings_as_config_obj(self, section=None, key=None): | |
665 | config = base.Config() |
|
672 | config = base.Config() | |
666 |
|
673 | |||
667 | ui_settings = self.get_ui_settings(section=section, key=key) |
|
674 | ui_settings = self.get_ui_settings(section=section, key=key) | |
668 |
|
675 | |||
669 | for entry in ui_settings: |
|
676 | for entry in ui_settings: | |
670 | config.set(entry.section, entry.key, entry.value) |
|
677 | config.set(entry.section, entry.key, entry.value) | |
671 |
|
678 | |||
672 | return config |
|
679 | return config | |
673 |
|
680 | |||
674 | def get_ui_settings(self, section=None, key=None): |
|
681 | def get_ui_settings(self, section=None, key=None): | |
675 | if not self.repo_settings or self.inherit_global_settings: |
|
682 | if not self.repo_settings or self.inherit_global_settings: | |
676 | return self.get_global_ui_settings(section, key) |
|
683 | return self.get_global_ui_settings(section, key) | |
677 | else: |
|
684 | else: | |
678 | return self.get_repo_ui_settings(section, key) |
|
685 | return self.get_repo_ui_settings(section, key) | |
679 |
|
686 | |||
680 | def get_svn_patterns(self, section=None): |
|
687 | def get_svn_patterns(self, section=None): | |
681 | if not self.repo_settings: |
|
688 | if not self.repo_settings: | |
682 | return self.get_global_ui_settings(section) |
|
689 | return self.get_global_ui_settings(section) | |
683 | else: |
|
690 | else: | |
684 | return self.get_repo_ui_settings(section) |
|
691 | return self.get_repo_ui_settings(section) | |
685 |
|
692 | |||
686 | @assert_repo_settings |
|
693 | @assert_repo_settings | |
687 | def get_repo_general_settings(self): |
|
694 | def get_repo_general_settings(self): | |
688 | global_settings = self.global_settings.get_all_settings() |
|
695 | global_settings = self.global_settings.get_all_settings() | |
689 | repo_settings = self.repo_settings.get_all_settings() |
|
696 | repo_settings = self.repo_settings.get_all_settings() | |
690 | filtered_repo_settings = self._filter_general_settings(repo_settings) |
|
697 | filtered_repo_settings = self._filter_general_settings(repo_settings) | |
691 | global_settings.update(filtered_repo_settings) |
|
698 | global_settings.update(filtered_repo_settings) | |
692 | return global_settings |
|
699 | return global_settings | |
693 |
|
700 | |||
694 | def get_global_general_settings(self): |
|
701 | def get_global_general_settings(self): | |
695 | return self.global_settings.get_all_settings() |
|
702 | return self.global_settings.get_all_settings() | |
696 |
|
703 | |||
697 | def get_general_settings(self): |
|
704 | def get_general_settings(self): | |
698 | if not self.repo_settings or self.inherit_global_settings: |
|
705 | if not self.repo_settings or self.inherit_global_settings: | |
699 | return self.get_global_general_settings() |
|
706 | return self.get_global_general_settings() | |
700 | else: |
|
707 | else: | |
701 | return self.get_repo_general_settings() |
|
708 | return self.get_repo_general_settings() | |
702 |
|
709 | |||
703 | def get_repos_location(self): |
|
710 | def get_repos_location(self): | |
704 | return self.global_settings.get_ui_by_key('/').ui_value |
|
711 | return self.global_settings.get_ui_by_key('/').ui_value | |
705 |
|
712 | |||
706 | def _filter_ui_settings(self, settings): |
|
713 | def _filter_ui_settings(self, settings): | |
707 | filtered_settings = [ |
|
714 | filtered_settings = [ | |
708 | s for s in settings if self._should_keep_setting(s)] |
|
715 | s for s in settings if self._should_keep_setting(s)] | |
709 | return filtered_settings |
|
716 | return filtered_settings | |
710 |
|
717 | |||
711 | def _should_keep_setting(self, setting): |
|
718 | def _should_keep_setting(self, setting): | |
712 | keep = ( |
|
719 | keep = ( | |
713 | (setting.section, setting.key) in self._ui_settings or |
|
720 | (setting.section, setting.key) in self._ui_settings or | |
714 | setting.section in self._svn_sections) |
|
721 | setting.section in self._svn_sections) | |
715 | return keep |
|
722 | return keep | |
716 |
|
723 | |||
717 | def _filter_general_settings(self, settings): |
|
724 | def _filter_general_settings(self, settings): | |
718 | keys = ['rhodecode_{}'.format(key) for key in self.GENERAL_SETTINGS] |
|
725 | keys = ['rhodecode_{}'.format(key) for key in self.GENERAL_SETTINGS] | |
719 | return { |
|
726 | return { | |
720 | k: settings[k] |
|
727 | k: settings[k] | |
721 | for k in settings if k in keys} |
|
728 | for k in settings if k in keys} | |
722 |
|
729 | |||
723 | def _collect_all_settings(self, global_=False): |
|
730 | def _collect_all_settings(self, global_=False): | |
724 | settings = self.global_settings if global_ else self.repo_settings |
|
731 | settings = self.global_settings if global_ else self.repo_settings | |
725 | result = {} |
|
732 | result = {} | |
726 |
|
733 | |||
727 | for section, key in self._ui_settings: |
|
734 | for section, key in self._ui_settings: | |
728 | ui = settings.get_ui_by_section_and_key(section, key) |
|
735 | ui = settings.get_ui_by_section_and_key(section, key) | |
729 | result_key = self._get_form_ui_key(section, key) |
|
736 | result_key = self._get_form_ui_key(section, key) | |
730 |
|
737 | |||
731 | if ui: |
|
738 | if ui: | |
732 | if section in ('hooks', 'extensions'): |
|
739 | if section in ('hooks', 'extensions'): | |
733 | result[result_key] = ui.ui_active |
|
740 | result[result_key] = ui.ui_active | |
734 | elif result_key in ['vcs_git_lfs_enabled']: |
|
741 | elif result_key in ['vcs_git_lfs_enabled']: | |
735 | result[result_key] = ui.ui_active |
|
742 | result[result_key] = ui.ui_active | |
736 | else: |
|
743 | else: | |
737 | result[result_key] = ui.ui_value |
|
744 | result[result_key] = ui.ui_value | |
738 |
|
745 | |||
739 | for name in self.GENERAL_SETTINGS: |
|
746 | for name in self.GENERAL_SETTINGS: | |
740 | setting = settings.get_setting_by_name(name) |
|
747 | setting = settings.get_setting_by_name(name) | |
741 | if setting: |
|
748 | if setting: | |
742 | result_key = 'rhodecode_{}'.format(name) |
|
749 | result_key = 'rhodecode_{}'.format(name) | |
743 | result[result_key] = setting.app_settings_value |
|
750 | result[result_key] = setting.app_settings_value | |
744 |
|
751 | |||
745 | return result |
|
752 | return result | |
746 |
|
753 | |||
747 | def _get_form_ui_key(self, section, key): |
|
754 | def _get_form_ui_key(self, section, key): | |
748 | return '{section}_{key}'.format( |
|
755 | return '{section}_{key}'.format( | |
749 | section=section, key=key.replace('.', '_')) |
|
756 | section=section, key=key.replace('.', '_')) | |
750 |
|
757 | |||
751 | def _create_or_update_ui( |
|
758 | def _create_or_update_ui( | |
752 | self, settings, section, key, value=None, active=None): |
|
759 | self, settings, section, key, value=None, active=None): | |
753 | ui = settings.get_ui_by_section_and_key(section, key) |
|
760 | ui = settings.get_ui_by_section_and_key(section, key) | |
754 | if not ui: |
|
761 | if not ui: | |
755 | active = True if active is None else active |
|
762 | active = True if active is None else active | |
756 | settings.create_ui_section_value( |
|
763 | settings.create_ui_section_value( | |
757 | section, value, key=key, active=active) |
|
764 | section, value, key=key, active=active) | |
758 | else: |
|
765 | else: | |
759 | if active is not None: |
|
766 | if active is not None: | |
760 | ui.ui_active = active |
|
767 | ui.ui_active = active | |
761 | if value is not None: |
|
768 | if value is not None: | |
762 | ui.ui_value = value |
|
769 | ui.ui_value = value | |
763 | Session().add(ui) |
|
770 | Session().add(ui) | |
764 |
|
771 | |||
765 | def _create_svn_settings(self, settings, data): |
|
772 | def _create_svn_settings(self, settings, data): | |
766 | svn_settings = { |
|
773 | svn_settings = { | |
767 | 'new_svn_branch': self.SVN_BRANCH_SECTION, |
|
774 | 'new_svn_branch': self.SVN_BRANCH_SECTION, | |
768 | 'new_svn_tag': self.SVN_TAG_SECTION |
|
775 | 'new_svn_tag': self.SVN_TAG_SECTION | |
769 | } |
|
776 | } | |
770 | for key in svn_settings: |
|
777 | for key in svn_settings: | |
771 | if data.get(key): |
|
778 | if data.get(key): | |
772 | settings.create_ui_section_value(svn_settings[key], data[key]) |
|
779 | settings.create_ui_section_value(svn_settings[key], data[key]) | |
773 |
|
780 | |||
774 | def _create_or_update_general_settings(self, settings, data): |
|
781 | def _create_or_update_general_settings(self, settings, data): | |
775 | for name in self.GENERAL_SETTINGS: |
|
782 | for name in self.GENERAL_SETTINGS: | |
776 | data_key = 'rhodecode_{}'.format(name) |
|
783 | data_key = 'rhodecode_{}'.format(name) | |
777 | if data_key not in data: |
|
784 | if data_key not in data: | |
778 | raise ValueError( |
|
785 | raise ValueError( | |
779 | 'The given data does not contain {} key'.format(data_key)) |
|
786 | 'The given data does not contain {} key'.format(data_key)) | |
780 | setting = settings.create_or_update_setting( |
|
787 | setting = settings.create_or_update_setting( | |
781 | name, data[data_key], 'bool') |
|
788 | name, data[data_key], 'bool') | |
782 | Session().add(setting) |
|
789 | Session().add(setting) | |
783 |
|
790 | |||
784 | def _get_settings_keys(self, settings, data): |
|
791 | def _get_settings_keys(self, settings, data): | |
785 | data_keys = [self._get_form_ui_key(*s) for s in settings] |
|
792 | data_keys = [self._get_form_ui_key(*s) for s in settings] | |
786 | for data_key in data_keys: |
|
793 | for data_key in data_keys: | |
787 | if data_key not in data: |
|
794 | if data_key not in data: | |
788 | raise ValueError( |
|
795 | raise ValueError( | |
789 | 'The given data does not contain {} key'.format(data_key)) |
|
796 | 'The given data does not contain {} key'.format(data_key)) | |
790 | return data_keys |
|
797 | return data_keys | |
791 |
|
798 | |||
792 | def create_largeobjects_dirs_if_needed(self, repo_store_path): |
|
799 | def create_largeobjects_dirs_if_needed(self, repo_store_path): | |
793 | """ |
|
800 | """ | |
794 | This is subscribed to the `pyramid.events.ApplicationCreated` event. It |
|
801 | This is subscribed to the `pyramid.events.ApplicationCreated` event. It | |
795 | does a repository scan if enabled in the settings. |
|
802 | does a repository scan if enabled in the settings. | |
796 | """ |
|
803 | """ | |
797 |
|
804 | |||
798 | from rhodecode.lib.vcs.backends.hg import largefiles_store |
|
805 | from rhodecode.lib.vcs.backends.hg import largefiles_store | |
799 | from rhodecode.lib.vcs.backends.git import lfs_store |
|
806 | from rhodecode.lib.vcs.backends.git import lfs_store | |
800 |
|
807 | |||
801 | paths = [ |
|
808 | paths = [ | |
802 | largefiles_store(repo_store_path), |
|
809 | largefiles_store(repo_store_path), | |
803 | lfs_store(repo_store_path)] |
|
810 | lfs_store(repo_store_path)] | |
804 |
|
811 | |||
805 | for path in paths: |
|
812 | for path in paths: | |
806 | if os.path.isdir(path): |
|
813 | if os.path.isdir(path): | |
807 | continue |
|
814 | continue | |
808 | if os.path.isfile(path): |
|
815 | if os.path.isfile(path): | |
809 | continue |
|
816 | continue | |
810 | # not a file nor dir, we try to create it |
|
817 | # not a file nor dir, we try to create it | |
811 | try: |
|
818 | try: | |
812 | os.makedirs(path) |
|
819 | os.makedirs(path) | |
813 | except Exception: |
|
820 | except Exception: | |
814 | log.warning('Failed to create largefiles dir:%s', path) |
|
821 | log.warning('Failed to create largefiles dir:%s', path) |
@@ -1,807 +1,807 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2017 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2017 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' |
|
494 | assert exc_info.value.message == '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,1072 +1,1083 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2017 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2017 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 | } |
|
47 | } | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | class TestInheritGlobalSettingsProperty(object): |
|
50 | class TestInheritGlobalSettingsProperty(object): | |
51 | def test_get_raises_exception_when_repository_not_specified(self): |
|
51 | def test_get_raises_exception_when_repository_not_specified(self): | |
52 | model = VcsSettingsModel() |
|
52 | model = VcsSettingsModel() | |
53 | with pytest.raises(Exception) as exc_info: |
|
53 | with pytest.raises(Exception) as exc_info: | |
54 | model.inherit_global_settings |
|
54 | model.inherit_global_settings | |
55 | assert exc_info.value.message == 'Repository is not specified' |
|
55 | assert exc_info.value.message == 'Repository is not specified' | |
56 |
|
56 | |||
57 | def test_true_is_returned_when_value_is_not_found(self, repo_stub): |
|
57 | def test_true_is_returned_when_value_is_not_found(self, repo_stub): | |
58 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
58 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
59 | assert model.inherit_global_settings is True |
|
59 | assert model.inherit_global_settings is True | |
60 |
|
60 | |||
61 | def test_value_is_returned(self, repo_stub, settings_util): |
|
61 | def test_value_is_returned(self, repo_stub, settings_util): | |
62 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
62 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
63 | settings_util.create_repo_rhodecode_setting( |
|
63 | settings_util.create_repo_rhodecode_setting( | |
64 | repo_stub, VcsSettingsModel.INHERIT_SETTINGS, False, 'bool') |
|
64 | repo_stub, VcsSettingsModel.INHERIT_SETTINGS, False, 'bool') | |
65 | assert model.inherit_global_settings is False |
|
65 | assert model.inherit_global_settings is False | |
66 |
|
66 | |||
67 | def test_value_is_set(self, repo_stub): |
|
67 | def test_value_is_set(self, repo_stub): | |
68 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
68 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
69 | model.inherit_global_settings = False |
|
69 | model.inherit_global_settings = False | |
70 | setting = model.repo_settings.get_setting_by_name( |
|
70 | setting = model.repo_settings.get_setting_by_name( | |
71 | VcsSettingsModel.INHERIT_SETTINGS) |
|
71 | VcsSettingsModel.INHERIT_SETTINGS) | |
72 | try: |
|
72 | try: | |
73 | assert setting.app_settings_type == 'bool' |
|
73 | assert setting.app_settings_type == 'bool' | |
74 | assert setting.app_settings_value is False |
|
74 | assert setting.app_settings_value is False | |
75 | finally: |
|
75 | finally: | |
76 | Session().delete(setting) |
|
76 | Session().delete(setting) | |
77 | Session().commit() |
|
77 | Session().commit() | |
78 |
|
78 | |||
79 | def test_set_raises_exception_when_repository_not_specified(self): |
|
79 | def test_set_raises_exception_when_repository_not_specified(self): | |
80 | model = VcsSettingsModel() |
|
80 | model = VcsSettingsModel() | |
81 | with pytest.raises(Exception) as exc_info: |
|
81 | with pytest.raises(Exception) as exc_info: | |
82 | model.inherit_global_settings = False |
|
82 | model.inherit_global_settings = False | |
83 | assert exc_info.value.message == 'Repository is not specified' |
|
83 | assert exc_info.value.message == 'Repository is not specified' | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | class TestVcsSettingsModel(object): |
|
86 | class TestVcsSettingsModel(object): | |
87 | def test_global_svn_branch_patterns(self): |
|
87 | def test_global_svn_branch_patterns(self): | |
88 | model = VcsSettingsModel() |
|
88 | model = VcsSettingsModel() | |
89 | expected_result = {'test': 'test'} |
|
89 | expected_result = {'test': 'test'} | |
90 | with mock.patch.object(model, 'global_settings') as settings_mock: |
|
90 | with mock.patch.object(model, 'global_settings') as settings_mock: | |
91 | get_settings = settings_mock.get_ui_by_section |
|
91 | get_settings = settings_mock.get_ui_by_section | |
92 | get_settings.return_value = expected_result |
|
92 | get_settings.return_value = expected_result | |
93 | settings_mock.return_value = expected_result |
|
93 | settings_mock.return_value = expected_result | |
94 | result = model.get_global_svn_branch_patterns() |
|
94 | result = model.get_global_svn_branch_patterns() | |
95 |
|
95 | |||
96 | get_settings.assert_called_once_with(model.SVN_BRANCH_SECTION) |
|
96 | get_settings.assert_called_once_with(model.SVN_BRANCH_SECTION) | |
97 | assert expected_result == result |
|
97 | assert expected_result == result | |
98 |
|
98 | |||
99 | def test_repo_svn_branch_patterns(self): |
|
99 | def test_repo_svn_branch_patterns(self): | |
100 | model = VcsSettingsModel() |
|
100 | model = VcsSettingsModel() | |
101 | expected_result = {'test': 'test'} |
|
101 | expected_result = {'test': 'test'} | |
102 | with mock.patch.object(model, 'repo_settings') as settings_mock: |
|
102 | with mock.patch.object(model, 'repo_settings') as settings_mock: | |
103 | get_settings = settings_mock.get_ui_by_section |
|
103 | get_settings = settings_mock.get_ui_by_section | |
104 | get_settings.return_value = expected_result |
|
104 | get_settings.return_value = expected_result | |
105 | settings_mock.return_value = expected_result |
|
105 | settings_mock.return_value = expected_result | |
106 | result = model.get_repo_svn_branch_patterns() |
|
106 | result = model.get_repo_svn_branch_patterns() | |
107 |
|
107 | |||
108 | get_settings.assert_called_once_with(model.SVN_BRANCH_SECTION) |
|
108 | get_settings.assert_called_once_with(model.SVN_BRANCH_SECTION) | |
109 | assert expected_result == result |
|
109 | assert expected_result == result | |
110 |
|
110 | |||
111 | def test_repo_svn_branch_patterns_raises_exception_when_repo_is_not_set( |
|
111 | def test_repo_svn_branch_patterns_raises_exception_when_repo_is_not_set( | |
112 | self): |
|
112 | self): | |
113 | model = VcsSettingsModel() |
|
113 | model = VcsSettingsModel() | |
114 | with pytest.raises(Exception) as exc_info: |
|
114 | with pytest.raises(Exception) as exc_info: | |
115 | model.get_repo_svn_branch_patterns() |
|
115 | model.get_repo_svn_branch_patterns() | |
116 | assert exc_info.value.message == 'Repository is not specified' |
|
116 | assert exc_info.value.message == 'Repository is not specified' | |
117 |
|
117 | |||
118 | def test_global_svn_tag_patterns(self): |
|
118 | def test_global_svn_tag_patterns(self): | |
119 | model = VcsSettingsModel() |
|
119 | model = VcsSettingsModel() | |
120 | expected_result = {'test': 'test'} |
|
120 | expected_result = {'test': 'test'} | |
121 | with mock.patch.object(model, 'global_settings') as settings_mock: |
|
121 | with mock.patch.object(model, 'global_settings') as settings_mock: | |
122 | get_settings = settings_mock.get_ui_by_section |
|
122 | get_settings = settings_mock.get_ui_by_section | |
123 | get_settings.return_value = expected_result |
|
123 | get_settings.return_value = expected_result | |
124 | settings_mock.return_value = expected_result |
|
124 | settings_mock.return_value = expected_result | |
125 | result = model.get_global_svn_tag_patterns() |
|
125 | result = model.get_global_svn_tag_patterns() | |
126 |
|
126 | |||
127 | get_settings.assert_called_once_with(model.SVN_TAG_SECTION) |
|
127 | get_settings.assert_called_once_with(model.SVN_TAG_SECTION) | |
128 | assert expected_result == result |
|
128 | assert expected_result == result | |
129 |
|
129 | |||
130 | def test_repo_svn_tag_patterns(self): |
|
130 | def test_repo_svn_tag_patterns(self): | |
131 | model = VcsSettingsModel() |
|
131 | model = VcsSettingsModel() | |
132 | expected_result = {'test': 'test'} |
|
132 | expected_result = {'test': 'test'} | |
133 | with mock.patch.object(model, 'repo_settings') as settings_mock: |
|
133 | with mock.patch.object(model, 'repo_settings') as settings_mock: | |
134 | get_settings = settings_mock.get_ui_by_section |
|
134 | get_settings = settings_mock.get_ui_by_section | |
135 | get_settings.return_value = expected_result |
|
135 | get_settings.return_value = expected_result | |
136 | settings_mock.return_value = expected_result |
|
136 | settings_mock.return_value = expected_result | |
137 | result = model.get_repo_svn_tag_patterns() |
|
137 | result = model.get_repo_svn_tag_patterns() | |
138 |
|
138 | |||
139 | get_settings.assert_called_once_with(model.SVN_TAG_SECTION) |
|
139 | get_settings.assert_called_once_with(model.SVN_TAG_SECTION) | |
140 | assert expected_result == result |
|
140 | assert expected_result == result | |
141 |
|
141 | |||
142 | def test_repo_svn_tag_patterns_raises_exception_when_repo_is_not_set(self): |
|
142 | def test_repo_svn_tag_patterns_raises_exception_when_repo_is_not_set(self): | |
143 | model = VcsSettingsModel() |
|
143 | model = VcsSettingsModel() | |
144 | with pytest.raises(Exception) as exc_info: |
|
144 | with pytest.raises(Exception) as exc_info: | |
145 | model.get_repo_svn_tag_patterns() |
|
145 | model.get_repo_svn_tag_patterns() | |
146 | assert exc_info.value.message == 'Repository is not specified' |
|
146 | assert exc_info.value.message == 'Repository is not specified' | |
147 |
|
147 | |||
148 | def test_get_global_settings(self): |
|
148 | def test_get_global_settings(self): | |
149 | expected_result = {'test': 'test'} |
|
149 | expected_result = {'test': 'test'} | |
150 | model = VcsSettingsModel() |
|
150 | model = VcsSettingsModel() | |
151 | with mock.patch.object(model, '_collect_all_settings') as collect_mock: |
|
151 | with mock.patch.object(model, '_collect_all_settings') as collect_mock: | |
152 | collect_mock.return_value = expected_result |
|
152 | collect_mock.return_value = expected_result | |
153 | result = model.get_global_settings() |
|
153 | result = model.get_global_settings() | |
154 |
|
154 | |||
155 | collect_mock.assert_called_once_with(global_=True) |
|
155 | collect_mock.assert_called_once_with(global_=True) | |
156 | assert result == expected_result |
|
156 | assert result == expected_result | |
157 |
|
157 | |||
158 | def test_get_repo_settings(self, repo_stub): |
|
158 | def test_get_repo_settings(self, repo_stub): | |
159 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
159 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
160 | expected_result = {'test': 'test'} |
|
160 | expected_result = {'test': 'test'} | |
161 | with mock.patch.object(model, '_collect_all_settings') as collect_mock: |
|
161 | with mock.patch.object(model, '_collect_all_settings') as collect_mock: | |
162 | collect_mock.return_value = expected_result |
|
162 | collect_mock.return_value = expected_result | |
163 | result = model.get_repo_settings() |
|
163 | result = model.get_repo_settings() | |
164 |
|
164 | |||
165 | collect_mock.assert_called_once_with(global_=False) |
|
165 | collect_mock.assert_called_once_with(global_=False) | |
166 | assert result == expected_result |
|
166 | assert result == expected_result | |
167 |
|
167 | |||
168 | @pytest.mark.parametrize('settings, global_', [ |
|
168 | @pytest.mark.parametrize('settings, global_', [ | |
169 | ('global_settings', True), |
|
169 | ('global_settings', True), | |
170 | ('repo_settings', False) |
|
170 | ('repo_settings', False) | |
171 | ]) |
|
171 | ]) | |
172 | def test_collect_all_settings(self, settings, global_): |
|
172 | def test_collect_all_settings(self, settings, global_): | |
173 | model = VcsSettingsModel() |
|
173 | model = VcsSettingsModel() | |
174 | result_mock = self._mock_result() |
|
174 | result_mock = self._mock_result() | |
175 |
|
175 | |||
176 | settings_patch = mock.patch.object(model, settings) |
|
176 | settings_patch = mock.patch.object(model, settings) | |
177 | with settings_patch as settings_mock: |
|
177 | with settings_patch as settings_mock: | |
178 | settings_mock.get_ui_by_section_and_key.return_value = result_mock |
|
178 | settings_mock.get_ui_by_section_and_key.return_value = result_mock | |
179 | settings_mock.get_setting_by_name.return_value = result_mock |
|
179 | settings_mock.get_setting_by_name.return_value = result_mock | |
180 | result = model._collect_all_settings(global_=global_) |
|
180 | result = model._collect_all_settings(global_=global_) | |
181 |
|
181 | |||
182 | ui_settings = model.HG_SETTINGS + model.GIT_SETTINGS + model.HOOKS_SETTINGS |
|
182 | ui_settings = model.HG_SETTINGS + model.GIT_SETTINGS + model.HOOKS_SETTINGS | |
183 | self._assert_get_settings_calls( |
|
183 | self._assert_get_settings_calls( | |
184 | settings_mock, ui_settings, model.GENERAL_SETTINGS) |
|
184 | settings_mock, ui_settings, model.GENERAL_SETTINGS) | |
185 | self._assert_collect_all_settings_result( |
|
185 | self._assert_collect_all_settings_result( | |
186 | ui_settings, model.GENERAL_SETTINGS, result) |
|
186 | ui_settings, model.GENERAL_SETTINGS, result) | |
187 |
|
187 | |||
188 | @pytest.mark.parametrize('settings, global_', [ |
|
188 | @pytest.mark.parametrize('settings, global_', [ | |
189 | ('global_settings', True), |
|
189 | ('global_settings', True), | |
190 | ('repo_settings', False) |
|
190 | ('repo_settings', False) | |
191 | ]) |
|
191 | ]) | |
192 | def test_collect_all_settings_without_empty_value(self, settings, global_): |
|
192 | def test_collect_all_settings_without_empty_value(self, settings, global_): | |
193 | model = VcsSettingsModel() |
|
193 | model = VcsSettingsModel() | |
194 |
|
194 | |||
195 | settings_patch = mock.patch.object(model, settings) |
|
195 | settings_patch = mock.patch.object(model, settings) | |
196 | with settings_patch as settings_mock: |
|
196 | with settings_patch as settings_mock: | |
197 | settings_mock.get_ui_by_section_and_key.return_value = None |
|
197 | settings_mock.get_ui_by_section_and_key.return_value = None | |
198 | settings_mock.get_setting_by_name.return_value = None |
|
198 | settings_mock.get_setting_by_name.return_value = None | |
199 | result = model._collect_all_settings(global_=global_) |
|
199 | result = model._collect_all_settings(global_=global_) | |
200 |
|
200 | |||
201 | assert result == {} |
|
201 | assert result == {} | |
202 |
|
202 | |||
203 | def _mock_result(self): |
|
203 | def _mock_result(self): | |
204 | result_mock = mock.Mock() |
|
204 | result_mock = mock.Mock() | |
205 | result_mock.ui_value = 'ui_value' |
|
205 | result_mock.ui_value = 'ui_value' | |
206 | result_mock.ui_active = True |
|
206 | result_mock.ui_active = True | |
207 | result_mock.app_settings_value = 'setting_value' |
|
207 | result_mock.app_settings_value = 'setting_value' | |
208 | return result_mock |
|
208 | return result_mock | |
209 |
|
209 | |||
210 | def _assert_get_settings_calls( |
|
210 | def _assert_get_settings_calls( | |
211 | self, settings_mock, ui_settings, general_settings): |
|
211 | self, settings_mock, ui_settings, general_settings): | |
212 | assert ( |
|
212 | assert ( | |
213 | settings_mock.get_ui_by_section_and_key.call_count == |
|
213 | settings_mock.get_ui_by_section_and_key.call_count == | |
214 | len(ui_settings)) |
|
214 | len(ui_settings)) | |
215 | assert ( |
|
215 | assert ( | |
216 | settings_mock.get_setting_by_name.call_count == |
|
216 | settings_mock.get_setting_by_name.call_count == | |
217 | len(general_settings)) |
|
217 | len(general_settings)) | |
218 |
|
218 | |||
219 | for section, key in ui_settings: |
|
219 | for section, key in ui_settings: | |
220 | expected_call = mock.call(section, key) |
|
220 | expected_call = mock.call(section, key) | |
221 | assert ( |
|
221 | assert ( | |
222 | expected_call in |
|
222 | expected_call in | |
223 | settings_mock.get_ui_by_section_and_key.call_args_list) |
|
223 | settings_mock.get_ui_by_section_and_key.call_args_list) | |
224 |
|
224 | |||
225 | for name in general_settings: |
|
225 | for name in general_settings: | |
226 | expected_call = mock.call(name) |
|
226 | expected_call = mock.call(name) | |
227 | assert ( |
|
227 | assert ( | |
228 | expected_call in |
|
228 | expected_call in | |
229 | settings_mock.get_setting_by_name.call_args_list) |
|
229 | settings_mock.get_setting_by_name.call_args_list) | |
230 |
|
230 | |||
231 | def _assert_collect_all_settings_result( |
|
231 | def _assert_collect_all_settings_result( | |
232 | self, ui_settings, general_settings, result): |
|
232 | self, ui_settings, general_settings, result): | |
233 | expected_result = {} |
|
233 | expected_result = {} | |
234 | for section, key in ui_settings: |
|
234 | for section, key in ui_settings: | |
235 | key = '{}_{}'.format(section, key.replace('.', '_')) |
|
235 | key = '{}_{}'.format(section, key.replace('.', '_')) | |
236 |
|
236 | |||
237 | if section in ('extensions', 'hooks'): |
|
237 | if section in ('extensions', 'hooks'): | |
238 | value = True |
|
238 | value = True | |
239 | elif key in ['vcs_git_lfs_enabled']: |
|
239 | elif key in ['vcs_git_lfs_enabled']: | |
240 | value = True |
|
240 | value = True | |
241 | else: |
|
241 | else: | |
242 | value = 'ui_value' |
|
242 | value = 'ui_value' | |
243 | expected_result[key] = value |
|
243 | expected_result[key] = value | |
244 |
|
244 | |||
245 | for name in general_settings: |
|
245 | for name in general_settings: | |
246 | key = 'rhodecode_' + name |
|
246 | key = 'rhodecode_' + name | |
247 | expected_result[key] = 'setting_value' |
|
247 | expected_result[key] = 'setting_value' | |
248 |
|
248 | |||
249 | assert expected_result == result |
|
249 | assert expected_result == result | |
250 |
|
250 | |||
251 |
|
251 | |||
252 | class TestCreateOrUpdateRepoHookSettings(object): |
|
252 | class TestCreateOrUpdateRepoHookSettings(object): | |
253 | def test_create_when_no_repo_object_found(self, repo_stub): |
|
253 | def test_create_when_no_repo_object_found(self, repo_stub): | |
254 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
254 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
255 |
|
255 | |||
256 | self._create_settings(model, HOOKS_FORM_DATA) |
|
256 | self._create_settings(model, HOOKS_FORM_DATA) | |
257 |
|
257 | |||
258 | cleanup = [] |
|
258 | cleanup = [] | |
259 | try: |
|
259 | try: | |
260 | for section, key in model.HOOKS_SETTINGS: |
|
260 | for section, key in model.HOOKS_SETTINGS: | |
261 | ui = model.repo_settings.get_ui_by_section_and_key( |
|
261 | ui = model.repo_settings.get_ui_by_section_and_key( | |
262 | section, key) |
|
262 | section, key) | |
263 | assert ui.ui_active is True |
|
263 | assert ui.ui_active is True | |
264 | cleanup.append(ui) |
|
264 | cleanup.append(ui) | |
265 | finally: |
|
265 | finally: | |
266 | for ui in cleanup: |
|
266 | for ui in cleanup: | |
267 | Session().delete(ui) |
|
267 | Session().delete(ui) | |
268 | Session().commit() |
|
268 | Session().commit() | |
269 |
|
269 | |||
270 | def test_create_raises_exception_when_data_incomplete(self, repo_stub): |
|
270 | def test_create_raises_exception_when_data_incomplete(self, repo_stub): | |
271 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
271 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
272 |
|
272 | |||
273 | deleted_key = 'hooks_changegroup_repo_size' |
|
273 | deleted_key = 'hooks_changegroup_repo_size' | |
274 | data = HOOKS_FORM_DATA.copy() |
|
274 | data = HOOKS_FORM_DATA.copy() | |
275 | data.pop(deleted_key) |
|
275 | data.pop(deleted_key) | |
276 |
|
276 | |||
277 | with pytest.raises(ValueError) as exc_info: |
|
277 | with pytest.raises(ValueError) as exc_info: | |
278 | model.create_or_update_repo_hook_settings(data) |
|
278 | model.create_or_update_repo_hook_settings(data) | |
279 | assert ( |
|
279 | assert ( | |
280 | exc_info.value.message == |
|
280 | exc_info.value.message == | |
281 | 'The given data does not contain {} key'.format(deleted_key)) |
|
281 | 'The given data does not contain {} key'.format(deleted_key)) | |
282 |
|
282 | |||
283 | 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): | |
284 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
284 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
285 | for section, key in model.HOOKS_SETTINGS: |
|
285 | for section, key in model.HOOKS_SETTINGS: | |
286 | settings_util.create_repo_rhodecode_ui( |
|
286 | settings_util.create_repo_rhodecode_ui( | |
287 | repo_stub, section, None, key=key, active=False) |
|
287 | repo_stub, section, None, key=key, active=False) | |
288 | model.create_or_update_repo_hook_settings(HOOKS_FORM_DATA) |
|
288 | model.create_or_update_repo_hook_settings(HOOKS_FORM_DATA) | |
289 | for section, key in model.HOOKS_SETTINGS: |
|
289 | for section, key in model.HOOKS_SETTINGS: | |
290 | 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) | |
291 | assert ui.ui_active is True |
|
291 | assert ui.ui_active is True | |
292 |
|
292 | |||
293 | def _create_settings(self, model, data): |
|
293 | def _create_settings(self, model, data): | |
294 | global_patch = mock.patch.object(model, 'global_settings') |
|
294 | global_patch = mock.patch.object(model, 'global_settings') | |
295 | global_setting = mock.Mock() |
|
295 | global_setting = mock.Mock() | |
296 | global_setting.ui_value = 'Test value' |
|
296 | global_setting.ui_value = 'Test value' | |
297 | with global_patch as global_mock: |
|
297 | with global_patch as global_mock: | |
298 | 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 | |
299 | model.create_or_update_repo_hook_settings(HOOKS_FORM_DATA) |
|
299 | model.create_or_update_repo_hook_settings(HOOKS_FORM_DATA) | |
300 |
|
300 | |||
301 |
|
301 | |||
302 | class TestUpdateGlobalHookSettings(object): |
|
302 | class TestUpdateGlobalHookSettings(object): | |
303 | def test_update_raises_exception_when_data_incomplete(self): |
|
303 | def test_update_raises_exception_when_data_incomplete(self): | |
304 | model = VcsSettingsModel() |
|
304 | model = VcsSettingsModel() | |
305 |
|
305 | |||
306 | deleted_key = 'hooks_changegroup_repo_size' |
|
306 | deleted_key = 'hooks_changegroup_repo_size' | |
307 | data = HOOKS_FORM_DATA.copy() |
|
307 | data = HOOKS_FORM_DATA.copy() | |
308 | data.pop(deleted_key) |
|
308 | data.pop(deleted_key) | |
309 |
|
309 | |||
310 | with pytest.raises(ValueError) as exc_info: |
|
310 | with pytest.raises(ValueError) as exc_info: | |
311 | model.update_global_hook_settings(data) |
|
311 | model.update_global_hook_settings(data) | |
312 | assert ( |
|
312 | assert ( | |
313 | exc_info.value.message == |
|
313 | exc_info.value.message == | |
314 | 'The given data does not contain {} key'.format(deleted_key)) |
|
314 | 'The given data does not contain {} key'.format(deleted_key)) | |
315 |
|
315 | |||
316 | def test_update_global_hook_settings(self, settings_util): |
|
316 | def test_update_global_hook_settings(self, settings_util): | |
317 | model = VcsSettingsModel() |
|
317 | model = VcsSettingsModel() | |
318 | setting_mock = mock.MagicMock() |
|
318 | setting_mock = mock.MagicMock() | |
319 | setting_mock.ui_active = False |
|
319 | setting_mock.ui_active = False | |
320 | get_settings_patcher = mock.patch.object( |
|
320 | get_settings_patcher = mock.patch.object( | |
321 | model.global_settings, 'get_ui_by_section_and_key', |
|
321 | model.global_settings, 'get_ui_by_section_and_key', | |
322 | return_value=setting_mock) |
|
322 | return_value=setting_mock) | |
323 | session_patcher = mock.patch('rhodecode.model.settings.Session') |
|
323 | session_patcher = mock.patch('rhodecode.model.settings.Session') | |
324 | with get_settings_patcher as get_settings_mock, session_patcher: |
|
324 | with get_settings_patcher as get_settings_mock, session_patcher: | |
325 | model.update_global_hook_settings(HOOKS_FORM_DATA) |
|
325 | model.update_global_hook_settings(HOOKS_FORM_DATA) | |
326 | assert setting_mock.ui_active is True |
|
326 | assert setting_mock.ui_active is True | |
327 | assert get_settings_mock.call_count == 3 |
|
327 | assert get_settings_mock.call_count == 3 | |
328 |
|
328 | |||
329 |
|
329 | |||
330 | class TestCreateOrUpdateRepoGeneralSettings(object): |
|
330 | class TestCreateOrUpdateRepoGeneralSettings(object): | |
331 | def test_calls_create_or_update_general_settings(self, repo_stub): |
|
331 | def test_calls_create_or_update_general_settings(self, repo_stub): | |
332 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
332 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
333 | create_patch = mock.patch.object( |
|
333 | create_patch = mock.patch.object( | |
334 | model, '_create_or_update_general_settings') |
|
334 | model, '_create_or_update_general_settings') | |
335 | with create_patch as create_mock: |
|
335 | with create_patch as create_mock: | |
336 | model.create_or_update_repo_pr_settings(GENERAL_FORM_DATA) |
|
336 | model.create_or_update_repo_pr_settings(GENERAL_FORM_DATA) | |
337 | create_mock.assert_called_once_with( |
|
337 | create_mock.assert_called_once_with( | |
338 | model.repo_settings, GENERAL_FORM_DATA) |
|
338 | model.repo_settings, GENERAL_FORM_DATA) | |
339 |
|
339 | |||
340 | def test_raises_exception_when_repository_is_not_specified(self): |
|
340 | def test_raises_exception_when_repository_is_not_specified(self): | |
341 | model = VcsSettingsModel() |
|
341 | model = VcsSettingsModel() | |
342 | with pytest.raises(Exception) as exc_info: |
|
342 | with pytest.raises(Exception) as exc_info: | |
343 | model.create_or_update_repo_pr_settings(GENERAL_FORM_DATA) |
|
343 | model.create_or_update_repo_pr_settings(GENERAL_FORM_DATA) | |
344 | assert exc_info.value.message == 'Repository is not specified' |
|
344 | assert exc_info.value.message == 'Repository is not specified' | |
345 |
|
345 | |||
346 |
|
346 | |||
347 | class TestCreateOrUpdatGlobalGeneralSettings(object): |
|
347 | class TestCreateOrUpdatGlobalGeneralSettings(object): | |
348 | def test_calls_create_or_update_general_settings(self): |
|
348 | def test_calls_create_or_update_general_settings(self): | |
349 | model = VcsSettingsModel() |
|
349 | model = VcsSettingsModel() | |
350 | create_patch = mock.patch.object( |
|
350 | create_patch = mock.patch.object( | |
351 | model, '_create_or_update_general_settings') |
|
351 | model, '_create_or_update_general_settings') | |
352 | with create_patch as create_mock: |
|
352 | with create_patch as create_mock: | |
353 | model.create_or_update_global_pr_settings(GENERAL_FORM_DATA) |
|
353 | model.create_or_update_global_pr_settings(GENERAL_FORM_DATA) | |
354 | create_mock.assert_called_once_with( |
|
354 | create_mock.assert_called_once_with( | |
355 | model.global_settings, GENERAL_FORM_DATA) |
|
355 | model.global_settings, GENERAL_FORM_DATA) | |
356 |
|
356 | |||
357 |
|
357 | |||
358 | class TestCreateOrUpdateGeneralSettings(object): |
|
358 | class TestCreateOrUpdateGeneralSettings(object): | |
359 | def test_create_when_no_repo_settings_found(self, repo_stub): |
|
359 | def test_create_when_no_repo_settings_found(self, repo_stub): | |
360 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
360 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
361 | model._create_or_update_general_settings( |
|
361 | model._create_or_update_general_settings( | |
362 | model.repo_settings, GENERAL_FORM_DATA) |
|
362 | model.repo_settings, GENERAL_FORM_DATA) | |
363 |
|
363 | |||
364 | cleanup = [] |
|
364 | cleanup = [] | |
365 | try: |
|
365 | try: | |
366 | for name in model.GENERAL_SETTINGS: |
|
366 | for name in model.GENERAL_SETTINGS: | |
367 | setting = model.repo_settings.get_setting_by_name(name) |
|
367 | setting = model.repo_settings.get_setting_by_name(name) | |
368 | assert setting.app_settings_value is True |
|
368 | assert setting.app_settings_value is True | |
369 | cleanup.append(setting) |
|
369 | cleanup.append(setting) | |
370 | finally: |
|
370 | finally: | |
371 | for setting in cleanup: |
|
371 | for setting in cleanup: | |
372 | Session().delete(setting) |
|
372 | Session().delete(setting) | |
373 | Session().commit() |
|
373 | Session().commit() | |
374 |
|
374 | |||
375 | def test_create_raises_exception_when_data_incomplete(self, repo_stub): |
|
375 | def test_create_raises_exception_when_data_incomplete(self, repo_stub): | |
376 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
376 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
377 |
|
377 | |||
378 | deleted_key = 'rhodecode_pr_merge_enabled' |
|
378 | deleted_key = 'rhodecode_pr_merge_enabled' | |
379 | data = GENERAL_FORM_DATA.copy() |
|
379 | data = GENERAL_FORM_DATA.copy() | |
380 | data.pop(deleted_key) |
|
380 | data.pop(deleted_key) | |
381 |
|
381 | |||
382 | with pytest.raises(ValueError) as exc_info: |
|
382 | with pytest.raises(ValueError) as exc_info: | |
383 | model._create_or_update_general_settings(model.repo_settings, data) |
|
383 | model._create_or_update_general_settings(model.repo_settings, data) | |
384 | assert ( |
|
384 | assert ( | |
385 | exc_info.value.message == |
|
385 | exc_info.value.message == | |
386 | 'The given data does not contain {} key'.format(deleted_key)) |
|
386 | 'The given data does not contain {} key'.format(deleted_key)) | |
387 |
|
387 | |||
388 | def test_update_when_repo_setting_found(self, repo_stub, settings_util): |
|
388 | def test_update_when_repo_setting_found(self, repo_stub, settings_util): | |
389 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
389 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
390 | for name in model.GENERAL_SETTINGS: |
|
390 | for name in model.GENERAL_SETTINGS: | |
391 | settings_util.create_repo_rhodecode_setting( |
|
391 | settings_util.create_repo_rhodecode_setting( | |
392 | repo_stub, name, False, 'bool') |
|
392 | repo_stub, name, False, 'bool') | |
393 |
|
393 | |||
394 | model._create_or_update_general_settings( |
|
394 | model._create_or_update_general_settings( | |
395 | model.repo_settings, GENERAL_FORM_DATA) |
|
395 | model.repo_settings, GENERAL_FORM_DATA) | |
396 |
|
396 | |||
397 | for name in model.GENERAL_SETTINGS: |
|
397 | for name in model.GENERAL_SETTINGS: | |
398 | setting = model.repo_settings.get_setting_by_name(name) |
|
398 | setting = model.repo_settings.get_setting_by_name(name) | |
399 | assert setting.app_settings_value is True |
|
399 | assert setting.app_settings_value is True | |
400 |
|
400 | |||
401 |
|
401 | |||
402 | class TestCreateRepoSvnSettings(object): |
|
402 | class TestCreateRepoSvnSettings(object): | |
403 | def test_calls_create_svn_settings(self, repo_stub): |
|
403 | def test_calls_create_svn_settings(self, repo_stub): | |
404 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
404 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
405 | with mock.patch.object(model, '_create_svn_settings') as create_mock: |
|
405 | with mock.patch.object(model, '_create_svn_settings') as create_mock: | |
406 | model.create_repo_svn_settings(SVN_FORM_DATA) |
|
406 | model.create_repo_svn_settings(SVN_FORM_DATA) | |
407 | create_mock.assert_called_once_with(model.repo_settings, SVN_FORM_DATA) |
|
407 | create_mock.assert_called_once_with(model.repo_settings, SVN_FORM_DATA) | |
408 |
|
408 | |||
409 | def test_raises_exception_when_repository_is_not_specified(self): |
|
409 | def test_raises_exception_when_repository_is_not_specified(self): | |
410 | model = VcsSettingsModel() |
|
410 | model = VcsSettingsModel() | |
411 | with pytest.raises(Exception) as exc_info: |
|
411 | with pytest.raises(Exception) as exc_info: | |
412 | model.create_repo_svn_settings(SVN_FORM_DATA) |
|
412 | model.create_repo_svn_settings(SVN_FORM_DATA) | |
413 | assert exc_info.value.message == 'Repository is not specified' |
|
413 | assert exc_info.value.message == 'Repository is not specified' | |
414 |
|
414 | |||
415 |
|
415 | |||
416 | class TestCreateSvnSettings(object): |
|
416 | class TestCreateSvnSettings(object): | |
417 | def test_create(self, repo_stub): |
|
417 | def test_create(self, repo_stub): | |
418 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
418 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
419 | model._create_svn_settings(model.repo_settings, SVN_FORM_DATA) |
|
419 | model._create_svn_settings(model.repo_settings, SVN_FORM_DATA) | |
420 | Session().commit() |
|
420 | Session().commit() | |
421 |
|
421 | |||
422 | branch_ui = model.repo_settings.get_ui_by_section( |
|
422 | branch_ui = model.repo_settings.get_ui_by_section( | |
423 | model.SVN_BRANCH_SECTION) |
|
423 | model.SVN_BRANCH_SECTION) | |
424 | tag_ui = model.repo_settings.get_ui_by_section( |
|
424 | tag_ui = model.repo_settings.get_ui_by_section( | |
425 | model.SVN_TAG_SECTION) |
|
425 | model.SVN_TAG_SECTION) | |
426 |
|
426 | |||
427 | try: |
|
427 | try: | |
428 | assert len(branch_ui) == 1 |
|
428 | assert len(branch_ui) == 1 | |
429 | assert len(tag_ui) == 1 |
|
429 | assert len(tag_ui) == 1 | |
430 | finally: |
|
430 | finally: | |
431 | Session().delete(branch_ui[0]) |
|
431 | Session().delete(branch_ui[0]) | |
432 | Session().delete(tag_ui[0]) |
|
432 | Session().delete(tag_ui[0]) | |
433 | Session().commit() |
|
433 | Session().commit() | |
434 |
|
434 | |||
435 | def test_create_tag(self, repo_stub): |
|
435 | def test_create_tag(self, repo_stub): | |
436 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
436 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
437 | data = SVN_FORM_DATA.copy() |
|
437 | data = SVN_FORM_DATA.copy() | |
438 | data.pop('new_svn_branch') |
|
438 | data.pop('new_svn_branch') | |
439 | model._create_svn_settings(model.repo_settings, data) |
|
439 | model._create_svn_settings(model.repo_settings, data) | |
440 | Session().commit() |
|
440 | Session().commit() | |
441 |
|
441 | |||
442 | branch_ui = model.repo_settings.get_ui_by_section( |
|
442 | branch_ui = model.repo_settings.get_ui_by_section( | |
443 | model.SVN_BRANCH_SECTION) |
|
443 | model.SVN_BRANCH_SECTION) | |
444 | tag_ui = model.repo_settings.get_ui_by_section( |
|
444 | tag_ui = model.repo_settings.get_ui_by_section( | |
445 | model.SVN_TAG_SECTION) |
|
445 | model.SVN_TAG_SECTION) | |
446 |
|
446 | |||
447 | try: |
|
447 | try: | |
448 | assert len(branch_ui) == 0 |
|
448 | assert len(branch_ui) == 0 | |
449 | assert len(tag_ui) == 1 |
|
449 | assert len(tag_ui) == 1 | |
450 | finally: |
|
450 | finally: | |
451 | Session().delete(tag_ui[0]) |
|
451 | Session().delete(tag_ui[0]) | |
452 | Session().commit() |
|
452 | Session().commit() | |
453 |
|
453 | |||
454 | def test_create_nothing_when_no_svn_settings_specified(self, repo_stub): |
|
454 | def test_create_nothing_when_no_svn_settings_specified(self, repo_stub): | |
455 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
455 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
456 | model._create_svn_settings(model.repo_settings, {}) |
|
456 | model._create_svn_settings(model.repo_settings, {}) | |
457 | Session().commit() |
|
457 | Session().commit() | |
458 |
|
458 | |||
459 | branch_ui = model.repo_settings.get_ui_by_section( |
|
459 | branch_ui = model.repo_settings.get_ui_by_section( | |
460 | model.SVN_BRANCH_SECTION) |
|
460 | model.SVN_BRANCH_SECTION) | |
461 | tag_ui = model.repo_settings.get_ui_by_section( |
|
461 | tag_ui = model.repo_settings.get_ui_by_section( | |
462 | model.SVN_TAG_SECTION) |
|
462 | model.SVN_TAG_SECTION) | |
463 |
|
463 | |||
464 | assert len(branch_ui) == 0 |
|
464 | assert len(branch_ui) == 0 | |
465 | assert len(tag_ui) == 0 |
|
465 | assert len(tag_ui) == 0 | |
466 |
|
466 | |||
467 | def test_create_nothing_when_empty_settings_specified(self, repo_stub): |
|
467 | def test_create_nothing_when_empty_settings_specified(self, repo_stub): | |
468 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
468 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
469 | data = { |
|
469 | data = { | |
470 | 'new_svn_branch': '', |
|
470 | 'new_svn_branch': '', | |
471 | 'new_svn_tag': '' |
|
471 | 'new_svn_tag': '' | |
472 | } |
|
472 | } | |
473 | model._create_svn_settings(model.repo_settings, data) |
|
473 | model._create_svn_settings(model.repo_settings, data) | |
474 | Session().commit() |
|
474 | Session().commit() | |
475 |
|
475 | |||
476 | branch_ui = model.repo_settings.get_ui_by_section( |
|
476 | branch_ui = model.repo_settings.get_ui_by_section( | |
477 | model.SVN_BRANCH_SECTION) |
|
477 | model.SVN_BRANCH_SECTION) | |
478 | tag_ui = model.repo_settings.get_ui_by_section( |
|
478 | tag_ui = model.repo_settings.get_ui_by_section( | |
479 | model.SVN_TAG_SECTION) |
|
479 | model.SVN_TAG_SECTION) | |
480 |
|
480 | |||
481 | assert len(branch_ui) == 0 |
|
481 | assert len(branch_ui) == 0 | |
482 | assert len(tag_ui) == 0 |
|
482 | assert len(tag_ui) == 0 | |
483 |
|
483 | |||
484 |
|
484 | |||
485 | class TestCreateOrUpdateUi(object): |
|
485 | class TestCreateOrUpdateUi(object): | |
486 | def test_create(self, repo_stub): |
|
486 | def test_create(self, repo_stub): | |
487 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
487 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
488 | model._create_or_update_ui( |
|
488 | model._create_or_update_ui( | |
489 | model.repo_settings, 'test-section', 'test-key', active=False, |
|
489 | model.repo_settings, 'test-section', 'test-key', active=False, | |
490 | value='False') |
|
490 | value='False') | |
491 | Session().commit() |
|
491 | Session().commit() | |
492 |
|
492 | |||
493 | created_ui = model.repo_settings.get_ui_by_section_and_key( |
|
493 | created_ui = model.repo_settings.get_ui_by_section_and_key( | |
494 | 'test-section', 'test-key') |
|
494 | 'test-section', 'test-key') | |
495 |
|
495 | |||
496 | try: |
|
496 | try: | |
497 | assert created_ui.ui_active is False |
|
497 | assert created_ui.ui_active is False | |
498 | assert str2bool(created_ui.ui_value) is False |
|
498 | assert str2bool(created_ui.ui_value) is False | |
499 | finally: |
|
499 | finally: | |
500 | Session().delete(created_ui) |
|
500 | Session().delete(created_ui) | |
501 | Session().commit() |
|
501 | Session().commit() | |
502 |
|
502 | |||
503 | def test_update(self, repo_stub, settings_util): |
|
503 | def test_update(self, repo_stub, settings_util): | |
504 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
504 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
505 |
|
505 | |||
506 | largefiles, phases, evolve = model.HG_SETTINGS |
|
506 | largefiles, phases, evolve = model.HG_SETTINGS | |
507 |
|
507 | |||
508 | section = 'test-section' |
|
508 | section = 'test-section' | |
509 | key = 'test-key' |
|
509 | key = 'test-key' | |
510 | settings_util.create_repo_rhodecode_ui( |
|
510 | settings_util.create_repo_rhodecode_ui( | |
511 | repo_stub, section, 'True', key=key, active=True) |
|
511 | repo_stub, section, 'True', key=key, active=True) | |
512 |
|
512 | |||
513 | model._create_or_update_ui( |
|
513 | model._create_or_update_ui( | |
514 | model.repo_settings, section, key, active=False, value='False') |
|
514 | model.repo_settings, section, key, active=False, value='False') | |
515 | Session().commit() |
|
515 | Session().commit() | |
516 |
|
516 | |||
517 | created_ui = model.repo_settings.get_ui_by_section_and_key( |
|
517 | created_ui = model.repo_settings.get_ui_by_section_and_key( | |
518 | section, key) |
|
518 | section, key) | |
519 | assert created_ui.ui_active is False |
|
519 | assert created_ui.ui_active is False | |
520 | assert str2bool(created_ui.ui_value) is False |
|
520 | assert str2bool(created_ui.ui_value) is False | |
521 |
|
521 | |||
522 |
|
522 | |||
523 | class TestCreateOrUpdateRepoHgSettings(object): |
|
523 | class TestCreateOrUpdateRepoHgSettings(object): | |
524 | FORM_DATA = { |
|
524 | FORM_DATA = { | |
525 | 'extensions_largefiles': False, |
|
525 | 'extensions_largefiles': False, | |
526 | 'extensions_evolve': False, |
|
526 | 'extensions_evolve': False, | |
527 | 'phases_publish': False |
|
527 | 'phases_publish': False | |
528 | } |
|
528 | } | |
529 |
|
529 | |||
530 | def test_creates_repo_hg_settings_when_data_is_correct(self, repo_stub): |
|
530 | def test_creates_repo_hg_settings_when_data_is_correct(self, repo_stub): | |
531 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
531 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
532 | with mock.patch.object(model, '_create_or_update_ui') as create_mock: |
|
532 | with mock.patch.object(model, '_create_or_update_ui') as create_mock: | |
533 | model.create_or_update_repo_hg_settings(self.FORM_DATA) |
|
533 | model.create_or_update_repo_hg_settings(self.FORM_DATA) | |
534 | expected_calls = [ |
|
534 | expected_calls = [ | |
535 | mock.call(model.repo_settings, 'extensions', 'largefiles', |
|
535 | mock.call(model.repo_settings, 'extensions', 'largefiles', | |
536 | active=False, value=''), |
|
536 | active=False, value=''), | |
537 | mock.call(model.repo_settings, 'extensions', 'evolve', |
|
537 | mock.call(model.repo_settings, 'extensions', 'evolve', | |
538 | active=False, value=''), |
|
538 | active=False, value=''), | |
539 | mock.call(model.repo_settings, 'phases', 'publish', value='False'), |
|
539 | mock.call(model.repo_settings, 'phases', 'publish', value='False'), | |
540 | ] |
|
540 | ] | |
541 | assert expected_calls == create_mock.call_args_list |
|
541 | assert expected_calls == create_mock.call_args_list | |
542 |
|
542 | |||
543 | @pytest.mark.parametrize('field_to_remove', FORM_DATA.keys()) |
|
543 | @pytest.mark.parametrize('field_to_remove', FORM_DATA.keys()) | |
544 | def test_key_is_not_found(self, repo_stub, field_to_remove): |
|
544 | def test_key_is_not_found(self, repo_stub, field_to_remove): | |
545 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
545 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
546 | data = self.FORM_DATA.copy() |
|
546 | data = self.FORM_DATA.copy() | |
547 | data.pop(field_to_remove) |
|
547 | data.pop(field_to_remove) | |
548 | with pytest.raises(ValueError) as exc_info: |
|
548 | with pytest.raises(ValueError) as exc_info: | |
549 | model.create_or_update_repo_hg_settings(data) |
|
549 | model.create_or_update_repo_hg_settings(data) | |
550 | expected_message = 'The given data does not contain {} key'.format( |
|
550 | expected_message = 'The given data does not contain {} key'.format( | |
551 | field_to_remove) |
|
551 | field_to_remove) | |
552 | assert exc_info.value.message == expected_message |
|
552 | assert exc_info.value.message == expected_message | |
553 |
|
553 | |||
554 | def test_create_raises_exception_when_repository_not_specified(self): |
|
554 | def test_create_raises_exception_when_repository_not_specified(self): | |
555 | model = VcsSettingsModel() |
|
555 | model = VcsSettingsModel() | |
556 | with pytest.raises(Exception) as exc_info: |
|
556 | with pytest.raises(Exception) as exc_info: | |
557 | model.create_or_update_repo_hg_settings(self.FORM_DATA) |
|
557 | model.create_or_update_repo_hg_settings(self.FORM_DATA) | |
558 | assert exc_info.value.message == 'Repository is not specified' |
|
558 | assert exc_info.value.message == 'Repository is not specified' | |
559 |
|
559 | |||
560 |
|
560 | |||
561 | class TestUpdateGlobalSslSetting(object): |
|
561 | class TestUpdateGlobalSslSetting(object): | |
562 | def test_updates_global_hg_settings(self): |
|
562 | def test_updates_global_hg_settings(self): | |
563 | model = VcsSettingsModel() |
|
563 | model = VcsSettingsModel() | |
564 | with mock.patch.object(model, '_create_or_update_ui') as create_mock: |
|
564 | with mock.patch.object(model, '_create_or_update_ui') as create_mock: | |
565 | model.update_global_ssl_setting('False') |
|
565 | model.update_global_ssl_setting('False') | |
566 | create_mock.assert_called_once_with( |
|
566 | create_mock.assert_called_once_with( | |
567 | model.global_settings, 'web', 'push_ssl', value='False') |
|
567 | model.global_settings, 'web', 'push_ssl', value='False') | |
568 |
|
568 | |||
569 |
|
569 | |||
570 | class TestUpdateGlobalPathSetting(object): |
|
570 | class TestUpdateGlobalPathSetting(object): | |
571 | def test_updates_global_path_settings(self): |
|
571 | def test_updates_global_path_settings(self): | |
572 | model = VcsSettingsModel() |
|
572 | model = VcsSettingsModel() | |
573 | with mock.patch.object(model, '_create_or_update_ui') as create_mock: |
|
573 | with mock.patch.object(model, '_create_or_update_ui') as create_mock: | |
574 | model.update_global_path_setting('False') |
|
574 | model.update_global_path_setting('False') | |
575 | create_mock.assert_called_once_with( |
|
575 | create_mock.assert_called_once_with( | |
576 | model.global_settings, 'paths', '/', value='False') |
|
576 | model.global_settings, 'paths', '/', value='False') | |
577 |
|
577 | |||
578 |
|
578 | |||
579 | class TestCreateOrUpdateGlobalHgSettings(object): |
|
579 | class TestCreateOrUpdateGlobalHgSettings(object): | |
580 | FORM_DATA = { |
|
580 | FORM_DATA = { | |
581 | 'extensions_largefiles': False, |
|
581 | 'extensions_largefiles': False, | |
582 | 'largefiles_usercache': '/example/largefiles-store', |
|
582 | 'largefiles_usercache': '/example/largefiles-store', | |
583 | 'phases_publish': False, |
|
583 | 'phases_publish': False, | |
584 | 'extensions_hgsubversion': False, |
|
584 | 'extensions_hgsubversion': False, | |
585 | 'extensions_evolve': False |
|
585 | 'extensions_evolve': False | |
586 | } |
|
586 | } | |
587 |
|
587 | |||
588 | def test_creates_repo_hg_settings_when_data_is_correct(self): |
|
588 | def test_creates_repo_hg_settings_when_data_is_correct(self): | |
589 | model = VcsSettingsModel() |
|
589 | model = VcsSettingsModel() | |
590 | with mock.patch.object(model, '_create_or_update_ui') as create_mock: |
|
590 | with mock.patch.object(model, '_create_or_update_ui') as create_mock: | |
591 | model.create_or_update_global_hg_settings(self.FORM_DATA) |
|
591 | model.create_or_update_global_hg_settings(self.FORM_DATA) | |
592 | expected_calls = [ |
|
592 | expected_calls = [ | |
593 | mock.call(model.global_settings, 'extensions', 'largefiles', |
|
593 | mock.call(model.global_settings, 'extensions', 'largefiles', | |
594 | active=False, value=''), |
|
594 | active=False, value=''), | |
595 | mock.call(model.global_settings, 'largefiles', 'usercache', |
|
595 | mock.call(model.global_settings, 'largefiles', 'usercache', | |
596 | value='/example/largefiles-store'), |
|
596 | value='/example/largefiles-store'), | |
597 | mock.call(model.global_settings, 'phases', 'publish', |
|
597 | mock.call(model.global_settings, 'phases', 'publish', | |
598 | value='False'), |
|
598 | value='False'), | |
599 | mock.call(model.global_settings, 'extensions', 'hgsubversion', |
|
599 | mock.call(model.global_settings, 'extensions', 'hgsubversion', | |
600 | active=False), |
|
600 | active=False), | |
601 | mock.call(model.global_settings, 'extensions', 'evolve', |
|
601 | mock.call(model.global_settings, 'extensions', 'evolve', | |
602 | active=False, value='') |
|
602 | active=False, value='') | |
603 | ] |
|
603 | ] | |
604 | assert expected_calls == create_mock.call_args_list |
|
604 | assert expected_calls == create_mock.call_args_list | |
605 |
|
605 | |||
606 | @pytest.mark.parametrize('field_to_remove', FORM_DATA.keys()) |
|
606 | @pytest.mark.parametrize('field_to_remove', FORM_DATA.keys()) | |
607 | def test_key_is_not_found(self, repo_stub, field_to_remove): |
|
607 | def test_key_is_not_found(self, repo_stub, field_to_remove): | |
608 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
608 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
609 | data = self.FORM_DATA.copy() |
|
609 | data = self.FORM_DATA.copy() | |
610 | data.pop(field_to_remove) |
|
610 | data.pop(field_to_remove) | |
611 | with pytest.raises(Exception) as exc_info: |
|
611 | with pytest.raises(Exception) as exc_info: | |
612 | model.create_or_update_global_hg_settings(data) |
|
612 | model.create_or_update_global_hg_settings(data) | |
613 | expected_message = 'The given data does not contain {} key'.format( |
|
613 | expected_message = 'The given data does not contain {} key'.format( | |
614 | field_to_remove) |
|
614 | field_to_remove) | |
615 | assert exc_info.value.message == expected_message |
|
615 | assert exc_info.value.message == expected_message | |
616 |
|
616 | |||
617 |
|
617 | |||
618 | class TestCreateOrUpdateGlobalGitSettings(object): |
|
618 | class TestCreateOrUpdateGlobalGitSettings(object): | |
619 | FORM_DATA = { |
|
619 | FORM_DATA = { | |
620 | 'vcs_git_lfs_enabled': False, |
|
620 | 'vcs_git_lfs_enabled': False, | |
621 | 'vcs_git_lfs_store_location': '/example/lfs-store', |
|
621 | 'vcs_git_lfs_store_location': '/example/lfs-store', | |
622 | } |
|
622 | } | |
623 |
|
623 | |||
624 | def test_creates_repo_hg_settings_when_data_is_correct(self): |
|
624 | def test_creates_repo_hg_settings_when_data_is_correct(self): | |
625 | model = VcsSettingsModel() |
|
625 | model = VcsSettingsModel() | |
626 | with mock.patch.object(model, '_create_or_update_ui') as create_mock: |
|
626 | with mock.patch.object(model, '_create_or_update_ui') as create_mock: | |
627 | model.create_or_update_global_git_settings(self.FORM_DATA) |
|
627 | model.create_or_update_global_git_settings(self.FORM_DATA) | |
628 | expected_calls = [ |
|
628 | expected_calls = [ | |
629 | mock.call(model.global_settings, 'vcs_git_lfs', 'enabled', |
|
629 | mock.call(model.global_settings, 'vcs_git_lfs', 'enabled', | |
630 | active=False, value=False), |
|
630 | active=False, value=False), | |
631 | mock.call(model.global_settings, 'vcs_git_lfs', 'store_location', |
|
631 | mock.call(model.global_settings, 'vcs_git_lfs', 'store_location', | |
632 | value='/example/lfs-store'), |
|
632 | value='/example/lfs-store'), | |
633 | ] |
|
633 | ] | |
634 | assert expected_calls == create_mock.call_args_list |
|
634 | assert expected_calls == create_mock.call_args_list | |
635 |
|
635 | |||
636 |
|
636 | |||
637 | class TestDeleteRepoSvnPattern(object): |
|
637 | class TestDeleteRepoSvnPattern(object): | |
638 | def test_success_when_repo_is_set(self, backend_svn): |
|
638 | def test_success_when_repo_is_set(self, backend_svn, settings_util): | |
|
639 | repo = backend_svn.create_repo() | |||
|
640 | repo_name = repo.repo_name | |||
|
641 | ||||
|
642 | model = VcsSettingsModel(repo=repo_name) | |||
|
643 | entry = settings_util.create_repo_rhodecode_ui( | |||
|
644 | repo, VcsSettingsModel.SVN_BRANCH_SECTION, 'svn-branch') | |||
|
645 | Session().commit() | |||
|
646 | ||||
|
647 | model.delete_repo_svn_pattern(entry.ui_id) | |||
|
648 | ||||
|
649 | def test_fail_when_delete_id_from_other_repo(self, backend_svn): | |||
639 | repo_name = backend_svn.repo_name |
|
650 | repo_name = backend_svn.repo_name | |
640 | model = VcsSettingsModel(repo=repo_name) |
|
651 | model = VcsSettingsModel(repo=repo_name) | |
641 | delete_ui_patch = mock.patch.object(model.repo_settings, 'delete_ui') |
|
652 | delete_ui_patch = mock.patch.object(model.repo_settings, 'delete_ui') | |
642 | with delete_ui_patch as delete_ui_mock: |
|
653 | with delete_ui_patch as delete_ui_mock: | |
643 | model.delete_repo_svn_pattern(123) |
|
654 | model.delete_repo_svn_pattern(123) | |
644 |
delete_ui_mock.assert_called_once_with(1 |
|
655 | delete_ui_mock.assert_called_once_with(-1) | |
645 |
|
656 | |||
646 | def test_raises_exception_when_repository_is_not_specified(self): |
|
657 | def test_raises_exception_when_repository_is_not_specified(self): | |
647 | model = VcsSettingsModel() |
|
658 | model = VcsSettingsModel() | |
648 | with pytest.raises(Exception) as exc_info: |
|
659 | with pytest.raises(Exception) as exc_info: | |
649 | model.delete_repo_svn_pattern(123) |
|
660 | model.delete_repo_svn_pattern(123) | |
650 | assert exc_info.value.message == 'Repository is not specified' |
|
661 | assert exc_info.value.message == 'Repository is not specified' | |
651 |
|
662 | |||
652 |
|
663 | |||
653 | class TestDeleteGlobalSvnPattern(object): |
|
664 | class TestDeleteGlobalSvnPattern(object): | |
654 | def test_delete_global_svn_pattern_calls_delete_ui(self): |
|
665 | def test_delete_global_svn_pattern_calls_delete_ui(self): | |
655 | model = VcsSettingsModel() |
|
666 | model = VcsSettingsModel() | |
656 | delete_ui_patch = mock.patch.object(model.global_settings, 'delete_ui') |
|
667 | delete_ui_patch = mock.patch.object(model.global_settings, 'delete_ui') | |
657 | with delete_ui_patch as delete_ui_mock: |
|
668 | with delete_ui_patch as delete_ui_mock: | |
658 | model.delete_global_svn_pattern(123) |
|
669 | model.delete_global_svn_pattern(123) | |
659 | delete_ui_mock.assert_called_once_with(123) |
|
670 | delete_ui_mock.assert_called_once_with(123) | |
660 |
|
671 | |||
661 |
|
672 | |||
662 | class TestFilterUiSettings(object): |
|
673 | class TestFilterUiSettings(object): | |
663 | def test_settings_are_filtered(self): |
|
674 | def test_settings_are_filtered(self): | |
664 | model = VcsSettingsModel() |
|
675 | model = VcsSettingsModel() | |
665 | repo_settings = [ |
|
676 | repo_settings = [ | |
666 | UiSetting('extensions', 'largefiles', '', True), |
|
677 | UiSetting('extensions', 'largefiles', '', True), | |
667 | UiSetting('phases', 'publish', 'True', True), |
|
678 | UiSetting('phases', 'publish', 'True', True), | |
668 | UiSetting('hooks', 'changegroup.repo_size', 'hook', True), |
|
679 | UiSetting('hooks', 'changegroup.repo_size', 'hook', True), | |
669 | UiSetting('hooks', 'changegroup.push_logger', 'hook', True), |
|
680 | UiSetting('hooks', 'changegroup.push_logger', 'hook', True), | |
670 | UiSetting('hooks', 'outgoing.pull_logger', 'hook', True), |
|
681 | UiSetting('hooks', 'outgoing.pull_logger', 'hook', True), | |
671 | UiSetting( |
|
682 | UiSetting( | |
672 | 'vcs_svn_branch', '84223c972204fa545ca1b22dac7bef5b68d7442d', |
|
683 | 'vcs_svn_branch', '84223c972204fa545ca1b22dac7bef5b68d7442d', | |
673 | 'test_branch', True), |
|
684 | 'test_branch', True), | |
674 | UiSetting( |
|
685 | UiSetting( | |
675 | 'vcs_svn_tag', '84229c972204fa545ca1b22dac7bef5b68d7442d', |
|
686 | 'vcs_svn_tag', '84229c972204fa545ca1b22dac7bef5b68d7442d', | |
676 | 'test_tag', True), |
|
687 | 'test_tag', True), | |
677 | ] |
|
688 | ] | |
678 | non_repo_settings = [ |
|
689 | non_repo_settings = [ | |
679 | UiSetting('largefiles', 'usercache', '/example/largefiles-store', True), |
|
690 | UiSetting('largefiles', 'usercache', '/example/largefiles-store', True), | |
680 | UiSetting('test', 'outgoing.pull_logger', 'hook', True), |
|
691 | UiSetting('test', 'outgoing.pull_logger', 'hook', True), | |
681 | UiSetting('hooks', 'test2', 'hook', True), |
|
692 | UiSetting('hooks', 'test2', 'hook', True), | |
682 | UiSetting( |
|
693 | UiSetting( | |
683 | 'vcs_svn_repo', '84229c972204fa545ca1b22dac7bef5b68d7442d', |
|
694 | 'vcs_svn_repo', '84229c972204fa545ca1b22dac7bef5b68d7442d', | |
684 | 'test_tag', True), |
|
695 | 'test_tag', True), | |
685 | ] |
|
696 | ] | |
686 | settings = repo_settings + non_repo_settings |
|
697 | settings = repo_settings + non_repo_settings | |
687 | filtered_settings = model._filter_ui_settings(settings) |
|
698 | filtered_settings = model._filter_ui_settings(settings) | |
688 | assert sorted(filtered_settings) == sorted(repo_settings) |
|
699 | assert sorted(filtered_settings) == sorted(repo_settings) | |
689 |
|
700 | |||
690 |
|
701 | |||
691 | class TestFilterGeneralSettings(object): |
|
702 | class TestFilterGeneralSettings(object): | |
692 | def test_settings_are_filtered(self): |
|
703 | def test_settings_are_filtered(self): | |
693 | model = VcsSettingsModel() |
|
704 | model = VcsSettingsModel() | |
694 | settings = { |
|
705 | settings = { | |
695 | 'rhodecode_abcde': 'value1', |
|
706 | 'rhodecode_abcde': 'value1', | |
696 | 'rhodecode_vwxyz': 'value2', |
|
707 | 'rhodecode_vwxyz': 'value2', | |
697 | } |
|
708 | } | |
698 | general_settings = { |
|
709 | general_settings = { | |
699 | 'rhodecode_{}'.format(key): 'value' |
|
710 | 'rhodecode_{}'.format(key): 'value' | |
700 | for key in VcsSettingsModel.GENERAL_SETTINGS |
|
711 | for key in VcsSettingsModel.GENERAL_SETTINGS | |
701 | } |
|
712 | } | |
702 | settings.update(general_settings) |
|
713 | settings.update(general_settings) | |
703 |
|
714 | |||
704 | filtered_settings = model._filter_general_settings(general_settings) |
|
715 | filtered_settings = model._filter_general_settings(general_settings) | |
705 | assert sorted(filtered_settings) == sorted(general_settings) |
|
716 | assert sorted(filtered_settings) == sorted(general_settings) | |
706 |
|
717 | |||
707 |
|
718 | |||
708 | class TestGetRepoUiSettings(object): |
|
719 | class TestGetRepoUiSettings(object): | |
709 | def test_global_uis_are_returned_when_no_repo_uis_found( |
|
720 | def test_global_uis_are_returned_when_no_repo_uis_found( | |
710 | self, repo_stub): |
|
721 | self, repo_stub): | |
711 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
722 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
712 | result = model.get_repo_ui_settings() |
|
723 | result = model.get_repo_ui_settings() | |
713 | svn_sections = ( |
|
724 | svn_sections = ( | |
714 | VcsSettingsModel.SVN_TAG_SECTION, |
|
725 | VcsSettingsModel.SVN_TAG_SECTION, | |
715 | VcsSettingsModel.SVN_BRANCH_SECTION) |
|
726 | VcsSettingsModel.SVN_BRANCH_SECTION) | |
716 | expected_result = [ |
|
727 | expected_result = [ | |
717 | s for s in model.global_settings.get_ui() |
|
728 | s for s in model.global_settings.get_ui() | |
718 | if s.section not in svn_sections] |
|
729 | if s.section not in svn_sections] | |
719 | assert sorted(result) == sorted(expected_result) |
|
730 | assert sorted(result) == sorted(expected_result) | |
720 |
|
731 | |||
721 | def test_repo_uis_are_overriding_global_uis( |
|
732 | def test_repo_uis_are_overriding_global_uis( | |
722 | self, repo_stub, settings_util): |
|
733 | self, repo_stub, settings_util): | |
723 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: |
|
734 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: | |
724 | settings_util.create_repo_rhodecode_ui( |
|
735 | settings_util.create_repo_rhodecode_ui( | |
725 | repo_stub, section, 'repo', key=key, active=False) |
|
736 | repo_stub, section, 'repo', key=key, active=False) | |
726 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
737 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
727 | result = model.get_repo_ui_settings() |
|
738 | result = model.get_repo_ui_settings() | |
728 | for setting in result: |
|
739 | for setting in result: | |
729 | locator = (setting.section, setting.key) |
|
740 | locator = (setting.section, setting.key) | |
730 | if locator in VcsSettingsModel.HOOKS_SETTINGS: |
|
741 | if locator in VcsSettingsModel.HOOKS_SETTINGS: | |
731 | assert setting.value == 'repo' |
|
742 | assert setting.value == 'repo' | |
732 |
|
743 | |||
733 | assert setting.active is False |
|
744 | assert setting.active is False | |
734 |
|
745 | |||
735 | def test_global_svn_patterns_are_not_in_list( |
|
746 | def test_global_svn_patterns_are_not_in_list( | |
736 | self, repo_stub, settings_util): |
|
747 | self, repo_stub, settings_util): | |
737 | svn_sections = ( |
|
748 | svn_sections = ( | |
738 | VcsSettingsModel.SVN_TAG_SECTION, |
|
749 | VcsSettingsModel.SVN_TAG_SECTION, | |
739 | VcsSettingsModel.SVN_BRANCH_SECTION) |
|
750 | VcsSettingsModel.SVN_BRANCH_SECTION) | |
740 | for section in svn_sections: |
|
751 | for section in svn_sections: | |
741 | settings_util.create_rhodecode_ui( |
|
752 | settings_util.create_rhodecode_ui( | |
742 | section, 'repo', key='deadbeef' + section, active=False) |
|
753 | section, 'repo', key='deadbeef' + section, active=False) | |
743 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
754 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
744 | result = model.get_repo_ui_settings() |
|
755 | result = model.get_repo_ui_settings() | |
745 | for setting in result: |
|
756 | for setting in result: | |
746 | assert setting.section not in svn_sections |
|
757 | assert setting.section not in svn_sections | |
747 |
|
758 | |||
748 | def test_repo_uis_filtered_by_section_are_returned( |
|
759 | def test_repo_uis_filtered_by_section_are_returned( | |
749 | self, repo_stub, settings_util): |
|
760 | self, repo_stub, settings_util): | |
750 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: |
|
761 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: | |
751 | settings_util.create_repo_rhodecode_ui( |
|
762 | settings_util.create_repo_rhodecode_ui( | |
752 | repo_stub, section, 'repo', key=key, active=False) |
|
763 | repo_stub, section, 'repo', key=key, active=False) | |
753 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
764 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
754 | section, key = VcsSettingsModel.HOOKS_SETTINGS[0] |
|
765 | section, key = VcsSettingsModel.HOOKS_SETTINGS[0] | |
755 | result = model.get_repo_ui_settings(section=section) |
|
766 | result = model.get_repo_ui_settings(section=section) | |
756 | for setting in result: |
|
767 | for setting in result: | |
757 | assert setting.section == section |
|
768 | assert setting.section == section | |
758 |
|
769 | |||
759 | def test_repo_uis_filtered_by_key_are_returned( |
|
770 | def test_repo_uis_filtered_by_key_are_returned( | |
760 | self, repo_stub, settings_util): |
|
771 | self, repo_stub, settings_util): | |
761 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: |
|
772 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: | |
762 | settings_util.create_repo_rhodecode_ui( |
|
773 | settings_util.create_repo_rhodecode_ui( | |
763 | repo_stub, section, 'repo', key=key, active=False) |
|
774 | repo_stub, section, 'repo', key=key, active=False) | |
764 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
775 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
765 | section, key = VcsSettingsModel.HOOKS_SETTINGS[0] |
|
776 | section, key = VcsSettingsModel.HOOKS_SETTINGS[0] | |
766 | result = model.get_repo_ui_settings(key=key) |
|
777 | result = model.get_repo_ui_settings(key=key) | |
767 | for setting in result: |
|
778 | for setting in result: | |
768 | assert setting.key == key |
|
779 | assert setting.key == key | |
769 |
|
780 | |||
770 | def test_raises_exception_when_repository_is_not_specified(self): |
|
781 | def test_raises_exception_when_repository_is_not_specified(self): | |
771 | model = VcsSettingsModel() |
|
782 | model = VcsSettingsModel() | |
772 | with pytest.raises(Exception) as exc_info: |
|
783 | with pytest.raises(Exception) as exc_info: | |
773 | model.get_repo_ui_settings() |
|
784 | model.get_repo_ui_settings() | |
774 | assert exc_info.value.message == 'Repository is not specified' |
|
785 | assert exc_info.value.message == 'Repository is not specified' | |
775 |
|
786 | |||
776 |
|
787 | |||
777 | class TestGetRepoGeneralSettings(object): |
|
788 | class TestGetRepoGeneralSettings(object): | |
778 | def test_global_settings_are_returned_when_no_repo_settings_found( |
|
789 | def test_global_settings_are_returned_when_no_repo_settings_found( | |
779 | self, repo_stub): |
|
790 | self, repo_stub): | |
780 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
791 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
781 | result = model.get_repo_general_settings() |
|
792 | result = model.get_repo_general_settings() | |
782 | expected_result = model.global_settings.get_all_settings() |
|
793 | expected_result = model.global_settings.get_all_settings() | |
783 | assert sorted(result) == sorted(expected_result) |
|
794 | assert sorted(result) == sorted(expected_result) | |
784 |
|
795 | |||
785 | def test_repo_uis_are_overriding_global_uis( |
|
796 | def test_repo_uis_are_overriding_global_uis( | |
786 | self, repo_stub, settings_util): |
|
797 | self, repo_stub, settings_util): | |
787 | for key in VcsSettingsModel.GENERAL_SETTINGS: |
|
798 | for key in VcsSettingsModel.GENERAL_SETTINGS: | |
788 | settings_util.create_repo_rhodecode_setting( |
|
799 | settings_util.create_repo_rhodecode_setting( | |
789 | repo_stub, key, 'abcde', type_='unicode') |
|
800 | repo_stub, key, 'abcde', type_='unicode') | |
790 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
801 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
791 | result = model.get_repo_ui_settings() |
|
802 | result = model.get_repo_ui_settings() | |
792 | for key in result: |
|
803 | for key in result: | |
793 | if key in VcsSettingsModel.GENERAL_SETTINGS: |
|
804 | if key in VcsSettingsModel.GENERAL_SETTINGS: | |
794 | assert result[key] == 'abcde' |
|
805 | assert result[key] == 'abcde' | |
795 |
|
806 | |||
796 | def test_raises_exception_when_repository_is_not_specified(self): |
|
807 | def test_raises_exception_when_repository_is_not_specified(self): | |
797 | model = VcsSettingsModel() |
|
808 | model = VcsSettingsModel() | |
798 | with pytest.raises(Exception) as exc_info: |
|
809 | with pytest.raises(Exception) as exc_info: | |
799 | model.get_repo_general_settings() |
|
810 | model.get_repo_general_settings() | |
800 | assert exc_info.value.message == 'Repository is not specified' |
|
811 | assert exc_info.value.message == 'Repository is not specified' | |
801 |
|
812 | |||
802 |
|
813 | |||
803 | class TestGetGlobalGeneralSettings(object): |
|
814 | class TestGetGlobalGeneralSettings(object): | |
804 | def test_global_settings_are_returned(self, repo_stub): |
|
815 | def test_global_settings_are_returned(self, repo_stub): | |
805 | model = VcsSettingsModel() |
|
816 | model = VcsSettingsModel() | |
806 | result = model.get_global_general_settings() |
|
817 | result = model.get_global_general_settings() | |
807 | expected_result = model.global_settings.get_all_settings() |
|
818 | expected_result = model.global_settings.get_all_settings() | |
808 | assert sorted(result) == sorted(expected_result) |
|
819 | assert sorted(result) == sorted(expected_result) | |
809 |
|
820 | |||
810 | def test_repo_uis_are_not_overriding_global_uis( |
|
821 | def test_repo_uis_are_not_overriding_global_uis( | |
811 | self, repo_stub, settings_util): |
|
822 | self, repo_stub, settings_util): | |
812 | for key in VcsSettingsModel.GENERAL_SETTINGS: |
|
823 | for key in VcsSettingsModel.GENERAL_SETTINGS: | |
813 | settings_util.create_repo_rhodecode_setting( |
|
824 | settings_util.create_repo_rhodecode_setting( | |
814 | repo_stub, key, 'abcde', type_='unicode') |
|
825 | repo_stub, key, 'abcde', type_='unicode') | |
815 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
826 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
816 | result = model.get_global_general_settings() |
|
827 | result = model.get_global_general_settings() | |
817 | expected_result = model.global_settings.get_all_settings() |
|
828 | expected_result = model.global_settings.get_all_settings() | |
818 | assert sorted(result) == sorted(expected_result) |
|
829 | assert sorted(result) == sorted(expected_result) | |
819 |
|
830 | |||
820 |
|
831 | |||
821 | class TestGetGlobalUiSettings(object): |
|
832 | class TestGetGlobalUiSettings(object): | |
822 | def test_global_uis_are_returned(self, repo_stub): |
|
833 | def test_global_uis_are_returned(self, repo_stub): | |
823 | model = VcsSettingsModel() |
|
834 | model = VcsSettingsModel() | |
824 | result = model.get_global_ui_settings() |
|
835 | result = model.get_global_ui_settings() | |
825 | expected_result = model.global_settings.get_ui() |
|
836 | expected_result = model.global_settings.get_ui() | |
826 | assert sorted(result) == sorted(expected_result) |
|
837 | assert sorted(result) == sorted(expected_result) | |
827 |
|
838 | |||
828 | def test_repo_uis_are_not_overriding_global_uis( |
|
839 | def test_repo_uis_are_not_overriding_global_uis( | |
829 | self, repo_stub, settings_util): |
|
840 | self, repo_stub, settings_util): | |
830 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: |
|
841 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: | |
831 | settings_util.create_repo_rhodecode_ui( |
|
842 | settings_util.create_repo_rhodecode_ui( | |
832 | repo_stub, section, 'repo', key=key, active=False) |
|
843 | repo_stub, section, 'repo', key=key, active=False) | |
833 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
844 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
834 | result = model.get_global_ui_settings() |
|
845 | result = model.get_global_ui_settings() | |
835 | expected_result = model.global_settings.get_ui() |
|
846 | expected_result = model.global_settings.get_ui() | |
836 | assert sorted(result) == sorted(expected_result) |
|
847 | assert sorted(result) == sorted(expected_result) | |
837 |
|
848 | |||
838 | def test_ui_settings_filtered_by_section( |
|
849 | def test_ui_settings_filtered_by_section( | |
839 | self, repo_stub, settings_util): |
|
850 | self, repo_stub, settings_util): | |
840 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
851 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
841 | section, key = VcsSettingsModel.HOOKS_SETTINGS[0] |
|
852 | section, key = VcsSettingsModel.HOOKS_SETTINGS[0] | |
842 | result = model.get_global_ui_settings(section=section) |
|
853 | result = model.get_global_ui_settings(section=section) | |
843 | expected_result = model.global_settings.get_ui(section=section) |
|
854 | expected_result = model.global_settings.get_ui(section=section) | |
844 | assert sorted(result) == sorted(expected_result) |
|
855 | assert sorted(result) == sorted(expected_result) | |
845 |
|
856 | |||
846 | def test_ui_settings_filtered_by_key( |
|
857 | def test_ui_settings_filtered_by_key( | |
847 | self, repo_stub, settings_util): |
|
858 | self, repo_stub, settings_util): | |
848 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
859 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
849 | section, key = VcsSettingsModel.HOOKS_SETTINGS[0] |
|
860 | section, key = VcsSettingsModel.HOOKS_SETTINGS[0] | |
850 | result = model.get_global_ui_settings(key=key) |
|
861 | result = model.get_global_ui_settings(key=key) | |
851 | expected_result = model.global_settings.get_ui(key=key) |
|
862 | expected_result = model.global_settings.get_ui(key=key) | |
852 | assert sorted(result) == sorted(expected_result) |
|
863 | assert sorted(result) == sorted(expected_result) | |
853 |
|
864 | |||
854 |
|
865 | |||
855 | class TestGetGeneralSettings(object): |
|
866 | class TestGetGeneralSettings(object): | |
856 | def test_global_settings_are_returned_when_inherited_is_true( |
|
867 | def test_global_settings_are_returned_when_inherited_is_true( | |
857 | self, repo_stub, settings_util): |
|
868 | self, repo_stub, settings_util): | |
858 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
869 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
859 | model.inherit_global_settings = True |
|
870 | model.inherit_global_settings = True | |
860 | for key in VcsSettingsModel.GENERAL_SETTINGS: |
|
871 | for key in VcsSettingsModel.GENERAL_SETTINGS: | |
861 | settings_util.create_repo_rhodecode_setting( |
|
872 | settings_util.create_repo_rhodecode_setting( | |
862 | repo_stub, key, 'abcde', type_='unicode') |
|
873 | repo_stub, key, 'abcde', type_='unicode') | |
863 | result = model.get_general_settings() |
|
874 | result = model.get_general_settings() | |
864 | expected_result = model.get_global_general_settings() |
|
875 | expected_result = model.get_global_general_settings() | |
865 | assert sorted(result) == sorted(expected_result) |
|
876 | assert sorted(result) == sorted(expected_result) | |
866 |
|
877 | |||
867 | def test_repo_settings_are_returned_when_inherited_is_false( |
|
878 | def test_repo_settings_are_returned_when_inherited_is_false( | |
868 | self, repo_stub, settings_util): |
|
879 | self, repo_stub, settings_util): | |
869 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
880 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
870 | model.inherit_global_settings = False |
|
881 | model.inherit_global_settings = False | |
871 | for key in VcsSettingsModel.GENERAL_SETTINGS: |
|
882 | for key in VcsSettingsModel.GENERAL_SETTINGS: | |
872 | settings_util.create_repo_rhodecode_setting( |
|
883 | settings_util.create_repo_rhodecode_setting( | |
873 | repo_stub, key, 'abcde', type_='unicode') |
|
884 | repo_stub, key, 'abcde', type_='unicode') | |
874 | result = model.get_general_settings() |
|
885 | result = model.get_general_settings() | |
875 | expected_result = model.get_repo_general_settings() |
|
886 | expected_result = model.get_repo_general_settings() | |
876 | assert sorted(result) == sorted(expected_result) |
|
887 | assert sorted(result) == sorted(expected_result) | |
877 |
|
888 | |||
878 | def test_global_settings_are_returned_when_no_repository_specified(self): |
|
889 | def test_global_settings_are_returned_when_no_repository_specified(self): | |
879 | model = VcsSettingsModel() |
|
890 | model = VcsSettingsModel() | |
880 | result = model.get_general_settings() |
|
891 | result = model.get_general_settings() | |
881 | expected_result = model.get_global_general_settings() |
|
892 | expected_result = model.get_global_general_settings() | |
882 | assert sorted(result) == sorted(expected_result) |
|
893 | assert sorted(result) == sorted(expected_result) | |
883 |
|
894 | |||
884 |
|
895 | |||
885 | class TestGetUiSettings(object): |
|
896 | class TestGetUiSettings(object): | |
886 | def test_global_settings_are_returned_when_inherited_is_true( |
|
897 | def test_global_settings_are_returned_when_inherited_is_true( | |
887 | self, repo_stub, settings_util): |
|
898 | self, repo_stub, settings_util): | |
888 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
899 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
889 | model.inherit_global_settings = True |
|
900 | model.inherit_global_settings = True | |
890 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: |
|
901 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: | |
891 | settings_util.create_repo_rhodecode_ui( |
|
902 | settings_util.create_repo_rhodecode_ui( | |
892 | repo_stub, section, 'repo', key=key, active=True) |
|
903 | repo_stub, section, 'repo', key=key, active=True) | |
893 | result = model.get_ui_settings() |
|
904 | result = model.get_ui_settings() | |
894 | expected_result = model.get_global_ui_settings() |
|
905 | expected_result = model.get_global_ui_settings() | |
895 | assert sorted(result) == sorted(expected_result) |
|
906 | assert sorted(result) == sorted(expected_result) | |
896 |
|
907 | |||
897 | def test_repo_settings_are_returned_when_inherited_is_false( |
|
908 | def test_repo_settings_are_returned_when_inherited_is_false( | |
898 | self, repo_stub, settings_util): |
|
909 | self, repo_stub, settings_util): | |
899 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
910 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
900 | model.inherit_global_settings = False |
|
911 | model.inherit_global_settings = False | |
901 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: |
|
912 | for section, key in VcsSettingsModel.HOOKS_SETTINGS: | |
902 | settings_util.create_repo_rhodecode_ui( |
|
913 | settings_util.create_repo_rhodecode_ui( | |
903 | repo_stub, section, 'repo', key=key, active=True) |
|
914 | repo_stub, section, 'repo', key=key, active=True) | |
904 | result = model.get_ui_settings() |
|
915 | result = model.get_ui_settings() | |
905 | expected_result = model.get_repo_ui_settings() |
|
916 | expected_result = model.get_repo_ui_settings() | |
906 | assert sorted(result) == sorted(expected_result) |
|
917 | assert sorted(result) == sorted(expected_result) | |
907 |
|
918 | |||
908 | def test_repo_settings_filtered_by_section_and_key(self, repo_stub): |
|
919 | def test_repo_settings_filtered_by_section_and_key(self, repo_stub): | |
909 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
920 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
910 | model.inherit_global_settings = False |
|
921 | model.inherit_global_settings = False | |
911 | args = ('section', 'key') |
|
922 | args = ('section', 'key') | |
912 | with mock.patch.object(model, 'get_repo_ui_settings') as settings_mock: |
|
923 | with mock.patch.object(model, 'get_repo_ui_settings') as settings_mock: | |
913 | model.get_ui_settings(*args) |
|
924 | model.get_ui_settings(*args) | |
914 | settings_mock.assert_called_once_with(*args) |
|
925 | settings_mock.assert_called_once_with(*args) | |
915 |
|
926 | |||
916 | def test_global_settings_filtered_by_section_and_key(self): |
|
927 | def test_global_settings_filtered_by_section_and_key(self): | |
917 | model = VcsSettingsModel() |
|
928 | model = VcsSettingsModel() | |
918 | args = ('section', 'key') |
|
929 | args = ('section', 'key') | |
919 | with mock.patch.object(model, 'get_global_ui_settings') as ( |
|
930 | with mock.patch.object(model, 'get_global_ui_settings') as ( | |
920 | settings_mock): |
|
931 | settings_mock): | |
921 | model.get_ui_settings(*args) |
|
932 | model.get_ui_settings(*args) | |
922 | settings_mock.assert_called_once_with(*args) |
|
933 | settings_mock.assert_called_once_with(*args) | |
923 |
|
934 | |||
924 | def test_global_settings_are_returned_when_no_repository_specified(self): |
|
935 | def test_global_settings_are_returned_when_no_repository_specified(self): | |
925 | model = VcsSettingsModel() |
|
936 | model = VcsSettingsModel() | |
926 | result = model.get_ui_settings() |
|
937 | result = model.get_ui_settings() | |
927 | expected_result = model.get_global_ui_settings() |
|
938 | expected_result = model.get_global_ui_settings() | |
928 | assert sorted(result) == sorted(expected_result) |
|
939 | assert sorted(result) == sorted(expected_result) | |
929 |
|
940 | |||
930 |
|
941 | |||
931 | class TestGetSvnPatterns(object): |
|
942 | class TestGetSvnPatterns(object): | |
932 | def test_repo_settings_filtered_by_section_and_key(self, repo_stub): |
|
943 | def test_repo_settings_filtered_by_section_and_key(self, repo_stub): | |
933 | model = VcsSettingsModel(repo=repo_stub.repo_name) |
|
944 | model = VcsSettingsModel(repo=repo_stub.repo_name) | |
934 | args = ('section', ) |
|
945 | args = ('section', ) | |
935 | with mock.patch.object(model, 'get_repo_ui_settings') as settings_mock: |
|
946 | with mock.patch.object(model, 'get_repo_ui_settings') as settings_mock: | |
936 | model.get_svn_patterns(*args) |
|
947 | model.get_svn_patterns(*args) | |
937 | settings_mock.assert_called_once_with(*args) |
|
948 | settings_mock.assert_called_once_with(*args) | |
938 |
|
949 | |||
939 | def test_global_settings_filtered_by_section_and_key(self): |
|
950 | def test_global_settings_filtered_by_section_and_key(self): | |
940 | model = VcsSettingsModel() |
|
951 | model = VcsSettingsModel() | |
941 | args = ('section', ) |
|
952 | args = ('section', ) | |
942 | with mock.patch.object(model, 'get_global_ui_settings') as ( |
|
953 | with mock.patch.object(model, 'get_global_ui_settings') as ( | |
943 | settings_mock): |
|
954 | settings_mock): | |
944 | model.get_svn_patterns(*args) |
|
955 | model.get_svn_patterns(*args) | |
945 | settings_mock.assert_called_once_with(*args) |
|
956 | settings_mock.assert_called_once_with(*args) | |
946 |
|
957 | |||
947 |
|
958 | |||
948 | class TestGetReposLocation(object): |
|
959 | class TestGetReposLocation(object): | |
949 | def test_returns_repos_location(self, repo_stub): |
|
960 | def test_returns_repos_location(self, repo_stub): | |
950 | model = VcsSettingsModel() |
|
961 | model = VcsSettingsModel() | |
951 |
|
962 | |||
952 | result_mock = mock.Mock() |
|
963 | result_mock = mock.Mock() | |
953 | result_mock.ui_value = '/tmp' |
|
964 | result_mock.ui_value = '/tmp' | |
954 |
|
965 | |||
955 | with mock.patch.object(model, 'global_settings') as settings_mock: |
|
966 | with mock.patch.object(model, 'global_settings') as settings_mock: | |
956 | settings_mock.get_ui_by_key.return_value = result_mock |
|
967 | settings_mock.get_ui_by_key.return_value = result_mock | |
957 | result = model.get_repos_location() |
|
968 | result = model.get_repos_location() | |
958 |
|
969 | |||
959 | settings_mock.get_ui_by_key.assert_called_once_with('/') |
|
970 | settings_mock.get_ui_by_key.assert_called_once_with('/') | |
960 | assert result == '/tmp' |
|
971 | assert result == '/tmp' | |
961 |
|
972 | |||
962 |
|
973 | |||
963 | class TestCreateOrUpdateRepoSettings(object): |
|
974 | class TestCreateOrUpdateRepoSettings(object): | |
964 | FORM_DATA = { |
|
975 | FORM_DATA = { | |
965 | 'inherit_global_settings': False, |
|
976 | 'inherit_global_settings': False, | |
966 | 'hooks_changegroup_repo_size': False, |
|
977 | 'hooks_changegroup_repo_size': False, | |
967 | 'hooks_changegroup_push_logger': False, |
|
978 | 'hooks_changegroup_push_logger': False, | |
968 | 'hooks_outgoing_pull_logger': False, |
|
979 | 'hooks_outgoing_pull_logger': False, | |
969 | 'extensions_largefiles': False, |
|
980 | 'extensions_largefiles': False, | |
970 | 'extensions_evolve': False, |
|
981 | 'extensions_evolve': False, | |
971 | 'largefiles_usercache': '/example/largefiles-store', |
|
982 | 'largefiles_usercache': '/example/largefiles-store', | |
972 | 'vcs_git_lfs_enabled': False, |
|
983 | 'vcs_git_lfs_enabled': False, | |
973 | 'vcs_git_lfs_store_location': '/', |
|
984 | 'vcs_git_lfs_store_location': '/', | |
974 | 'phases_publish': 'False', |
|
985 | 'phases_publish': 'False', | |
975 | 'rhodecode_pr_merge_enabled': False, |
|
986 | 'rhodecode_pr_merge_enabled': False, | |
976 | 'rhodecode_use_outdated_comments': False, |
|
987 | 'rhodecode_use_outdated_comments': False, | |
977 | 'new_svn_branch': '', |
|
988 | 'new_svn_branch': '', | |
978 | 'new_svn_tag': '' |
|
989 | 'new_svn_tag': '' | |
979 | } |
|
990 | } | |
980 |
|
991 | |||
981 | def test_get_raises_exception_when_repository_not_specified(self): |
|
992 | def test_get_raises_exception_when_repository_not_specified(self): | |
982 | model = VcsSettingsModel() |
|
993 | model = VcsSettingsModel() | |
983 | with pytest.raises(Exception) as exc_info: |
|
994 | with pytest.raises(Exception) as exc_info: | |
984 | model.create_or_update_repo_settings(data=self.FORM_DATA) |
|
995 | model.create_or_update_repo_settings(data=self.FORM_DATA) | |
985 | assert exc_info.value.message == 'Repository is not specified' |
|
996 | assert exc_info.value.message == 'Repository is not specified' | |
986 |
|
997 | |||
987 | def test_only_svn_settings_are_updated_when_type_is_svn(self, backend_svn): |
|
998 | def test_only_svn_settings_are_updated_when_type_is_svn(self, backend_svn): | |
988 | repo = backend_svn.create_repo() |
|
999 | repo = backend_svn.create_repo() | |
989 | model = VcsSettingsModel(repo=repo) |
|
1000 | model = VcsSettingsModel(repo=repo) | |
990 | with self._patch_model(model) as mocks: |
|
1001 | with self._patch_model(model) as mocks: | |
991 | model.create_or_update_repo_settings( |
|
1002 | model.create_or_update_repo_settings( | |
992 | data=self.FORM_DATA, inherit_global_settings=False) |
|
1003 | data=self.FORM_DATA, inherit_global_settings=False) | |
993 | mocks['create_repo_svn_settings'].assert_called_once_with( |
|
1004 | mocks['create_repo_svn_settings'].assert_called_once_with( | |
994 | self.FORM_DATA) |
|
1005 | self.FORM_DATA) | |
995 | non_called_methods = ( |
|
1006 | non_called_methods = ( | |
996 | 'create_or_update_repo_hook_settings', |
|
1007 | 'create_or_update_repo_hook_settings', | |
997 | 'create_or_update_repo_pr_settings', |
|
1008 | 'create_or_update_repo_pr_settings', | |
998 | 'create_or_update_repo_hg_settings') |
|
1009 | 'create_or_update_repo_hg_settings') | |
999 | for method in non_called_methods: |
|
1010 | for method in non_called_methods: | |
1000 | assert mocks[method].call_count == 0 |
|
1011 | assert mocks[method].call_count == 0 | |
1001 |
|
1012 | |||
1002 | def test_non_svn_settings_are_updated_when_type_is_hg(self, backend_hg): |
|
1013 | def test_non_svn_settings_are_updated_when_type_is_hg(self, backend_hg): | |
1003 | repo = backend_hg.create_repo() |
|
1014 | repo = backend_hg.create_repo() | |
1004 | model = VcsSettingsModel(repo=repo) |
|
1015 | model = VcsSettingsModel(repo=repo) | |
1005 | with self._patch_model(model) as mocks: |
|
1016 | with self._patch_model(model) as mocks: | |
1006 | model.create_or_update_repo_settings( |
|
1017 | model.create_or_update_repo_settings( | |
1007 | data=self.FORM_DATA, inherit_global_settings=False) |
|
1018 | data=self.FORM_DATA, inherit_global_settings=False) | |
1008 |
|
1019 | |||
1009 | assert mocks['create_repo_svn_settings'].call_count == 0 |
|
1020 | assert mocks['create_repo_svn_settings'].call_count == 0 | |
1010 | called_methods = ( |
|
1021 | called_methods = ( | |
1011 | 'create_or_update_repo_hook_settings', |
|
1022 | 'create_or_update_repo_hook_settings', | |
1012 | 'create_or_update_repo_pr_settings', |
|
1023 | 'create_or_update_repo_pr_settings', | |
1013 | 'create_or_update_repo_hg_settings') |
|
1024 | 'create_or_update_repo_hg_settings') | |
1014 | for method in called_methods: |
|
1025 | for method in called_methods: | |
1015 | mocks[method].assert_called_once_with(self.FORM_DATA) |
|
1026 | mocks[method].assert_called_once_with(self.FORM_DATA) | |
1016 |
|
1027 | |||
1017 | def test_non_svn_and_hg_settings_are_updated_when_type_is_git( |
|
1028 | def test_non_svn_and_hg_settings_are_updated_when_type_is_git( | |
1018 | self, backend_git): |
|
1029 | self, backend_git): | |
1019 | repo = backend_git.create_repo() |
|
1030 | repo = backend_git.create_repo() | |
1020 | model = VcsSettingsModel(repo=repo) |
|
1031 | model = VcsSettingsModel(repo=repo) | |
1021 | with self._patch_model(model) as mocks: |
|
1032 | with self._patch_model(model) as mocks: | |
1022 | model.create_or_update_repo_settings( |
|
1033 | model.create_or_update_repo_settings( | |
1023 | data=self.FORM_DATA, inherit_global_settings=False) |
|
1034 | data=self.FORM_DATA, inherit_global_settings=False) | |
1024 |
|
1035 | |||
1025 | assert mocks['create_repo_svn_settings'].call_count == 0 |
|
1036 | assert mocks['create_repo_svn_settings'].call_count == 0 | |
1026 | called_methods = ( |
|
1037 | called_methods = ( | |
1027 | 'create_or_update_repo_hook_settings', |
|
1038 | 'create_or_update_repo_hook_settings', | |
1028 | 'create_or_update_repo_pr_settings') |
|
1039 | 'create_or_update_repo_pr_settings') | |
1029 | non_called_methods = ( |
|
1040 | non_called_methods = ( | |
1030 | 'create_repo_svn_settings', |
|
1041 | 'create_repo_svn_settings', | |
1031 | 'create_or_update_repo_hg_settings' |
|
1042 | 'create_or_update_repo_hg_settings' | |
1032 | ) |
|
1043 | ) | |
1033 | for method in called_methods: |
|
1044 | for method in called_methods: | |
1034 | mocks[method].assert_called_once_with(self.FORM_DATA) |
|
1045 | mocks[method].assert_called_once_with(self.FORM_DATA) | |
1035 | for method in non_called_methods: |
|
1046 | for method in non_called_methods: | |
1036 | assert mocks[method].call_count == 0 |
|
1047 | assert mocks[method].call_count == 0 | |
1037 |
|
1048 | |||
1038 | def test_no_methods_are_called_when_settings_are_inherited( |
|
1049 | def test_no_methods_are_called_when_settings_are_inherited( | |
1039 | self, backend): |
|
1050 | self, backend): | |
1040 | repo = backend.create_repo() |
|
1051 | repo = backend.create_repo() | |
1041 | model = VcsSettingsModel(repo=repo) |
|
1052 | model = VcsSettingsModel(repo=repo) | |
1042 | with self._patch_model(model) as mocks: |
|
1053 | with self._patch_model(model) as mocks: | |
1043 | model.create_or_update_repo_settings( |
|
1054 | model.create_or_update_repo_settings( | |
1044 | data=self.FORM_DATA, inherit_global_settings=True) |
|
1055 | data=self.FORM_DATA, inherit_global_settings=True) | |
1045 | for method_name in mocks: |
|
1056 | for method_name in mocks: | |
1046 | assert mocks[method_name].call_count == 0 |
|
1057 | assert mocks[method_name].call_count == 0 | |
1047 |
|
1058 | |||
1048 | def test_cache_is_marked_for_invalidation(self, repo_stub): |
|
1059 | def test_cache_is_marked_for_invalidation(self, repo_stub): | |
1049 | model = VcsSettingsModel(repo=repo_stub) |
|
1060 | model = VcsSettingsModel(repo=repo_stub) | |
1050 | invalidation_patcher = mock.patch( |
|
1061 | invalidation_patcher = mock.patch( | |
1051 | 'rhodecode.model.scm.ScmModel.mark_for_invalidation') |
|
1062 | 'rhodecode.model.scm.ScmModel.mark_for_invalidation') | |
1052 | with invalidation_patcher as invalidation_mock: |
|
1063 | with invalidation_patcher as invalidation_mock: | |
1053 | model.create_or_update_repo_settings( |
|
1064 | model.create_or_update_repo_settings( | |
1054 | data=self.FORM_DATA, inherit_global_settings=True) |
|
1065 | data=self.FORM_DATA, inherit_global_settings=True) | |
1055 | invalidation_mock.assert_called_once_with( |
|
1066 | invalidation_mock.assert_called_once_with( | |
1056 | repo_stub.repo_name, delete=True) |
|
1067 | repo_stub.repo_name, delete=True) | |
1057 |
|
1068 | |||
1058 | def test_inherit_flag_is_saved(self, repo_stub): |
|
1069 | def test_inherit_flag_is_saved(self, repo_stub): | |
1059 | model = VcsSettingsModel(repo=repo_stub) |
|
1070 | model = VcsSettingsModel(repo=repo_stub) | |
1060 | model.inherit_global_settings = True |
|
1071 | model.inherit_global_settings = True | |
1061 | with self._patch_model(model): |
|
1072 | with self._patch_model(model): | |
1062 | model.create_or_update_repo_settings( |
|
1073 | model.create_or_update_repo_settings( | |
1063 | data=self.FORM_DATA, inherit_global_settings=False) |
|
1074 | data=self.FORM_DATA, inherit_global_settings=False) | |
1064 | assert model.inherit_global_settings is False |
|
1075 | assert model.inherit_global_settings is False | |
1065 |
|
1076 | |||
1066 | def _patch_model(self, model): |
|
1077 | def _patch_model(self, model): | |
1067 | return mock.patch.multiple( |
|
1078 | return mock.patch.multiple( | |
1068 | model, |
|
1079 | model, | |
1069 | create_repo_svn_settings=mock.DEFAULT, |
|
1080 | create_repo_svn_settings=mock.DEFAULT, | |
1070 | create_or_update_repo_hook_settings=mock.DEFAULT, |
|
1081 | create_or_update_repo_hook_settings=mock.DEFAULT, | |
1071 | create_or_update_repo_pr_settings=mock.DEFAULT, |
|
1082 | create_or_update_repo_pr_settings=mock.DEFAULT, | |
1072 | create_or_update_repo_hg_settings=mock.DEFAULT) |
|
1083 | create_or_update_repo_hg_settings=mock.DEFAULT) |
General Comments 0
You need to be logged in to leave comments.
Login now