Show More
@@ -1,308 +1,309 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import tempfile |
|
22 | 22 | |
|
23 | 23 | import mock |
|
24 | 24 | import pytest |
|
25 | 25 | |
|
26 | 26 | from rhodecode.lib.exceptions import AttachedForksError |
|
27 | 27 | from rhodecode.lib.utils import make_db_config |
|
28 | 28 | from rhodecode.model.db import Repository |
|
29 | 29 | from rhodecode.model.meta import Session |
|
30 | 30 | from rhodecode.model.repo import RepoModel |
|
31 | 31 | from rhodecode.model.scm import ScmModel |
|
32 | 32 | from rhodecode.lib.utils2 import safe_unicode |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | class TestRepoModel: |
|
36 | 36 | |
|
37 | 37 | def test_remove_repo(self, backend): |
|
38 | 38 | repo = backend.create_repo() |
|
39 | 39 | Session().commit() |
|
40 | 40 | RepoModel().delete(repo=repo) |
|
41 | 41 | Session().commit() |
|
42 | 42 | |
|
43 | 43 | repos = ScmModel().repo_scan() |
|
44 | 44 | |
|
45 | 45 | assert Repository.get_by_repo_name(repo_name=backend.repo_name) is None |
|
46 | 46 | assert repo.repo_name not in repos |
|
47 | 47 | |
|
48 | 48 | def test_remove_repo_raises_exc_when_attached_forks(self, backend): |
|
49 | 49 | repo = backend.create_repo() |
|
50 | 50 | Session().commit() |
|
51 | 51 | backend.create_fork() |
|
52 | 52 | Session().commit() |
|
53 | 53 | |
|
54 | 54 | with pytest.raises(AttachedForksError): |
|
55 | 55 | RepoModel().delete(repo=repo) |
|
56 | 56 | |
|
57 | 57 | def test_remove_repo_delete_forks(self, backend): |
|
58 | 58 | repo = backend.create_repo() |
|
59 | 59 | Session().commit() |
|
60 | 60 | |
|
61 | 61 | fork = backend.create_fork() |
|
62 | 62 | Session().commit() |
|
63 | 63 | |
|
64 | 64 | fork_of_fork = backend.create_fork() |
|
65 | 65 | Session().commit() |
|
66 | 66 | |
|
67 | 67 | RepoModel().delete(repo=repo, forks='delete') |
|
68 | 68 | Session().commit() |
|
69 | 69 | |
|
70 | 70 | assert Repository.get_by_repo_name(repo_name=repo.repo_name) is None |
|
71 | 71 | assert Repository.get_by_repo_name(repo_name=fork.repo_name) is None |
|
72 | 72 | assert ( |
|
73 | 73 | Repository.get_by_repo_name(repo_name=fork_of_fork.repo_name) |
|
74 | 74 | is None) |
|
75 | 75 | |
|
76 | 76 | def test_remove_repo_detach_forks(self, backend): |
|
77 | 77 | repo = backend.create_repo() |
|
78 | 78 | Session().commit() |
|
79 | 79 | |
|
80 | 80 | fork = backend.create_fork() |
|
81 | 81 | Session().commit() |
|
82 | 82 | |
|
83 | 83 | fork_of_fork = backend.create_fork() |
|
84 | 84 | Session().commit() |
|
85 | 85 | |
|
86 | 86 | RepoModel().delete(repo=repo, forks='detach') |
|
87 | 87 | Session().commit() |
|
88 | 88 | |
|
89 | 89 | assert Repository.get_by_repo_name(repo_name=repo.repo_name) is None |
|
90 | 90 | assert ( |
|
91 | 91 | Repository.get_by_repo_name(repo_name=fork.repo_name) is not None) |
|
92 | 92 | assert ( |
|
93 | 93 | Repository.get_by_repo_name(repo_name=fork_of_fork.repo_name) |
|
94 | 94 | is not None) |
|
95 | 95 | |
|
96 | 96 | @pytest.mark.parametrize("filename, expected", [ |
|
97 | 97 | ("README", True), |
|
98 | 98 | ("README.rst", False), |
|
99 | 99 | ]) |
|
100 | 100 | def test_filenode_is_link(self, vcsbackend, filename, expected): |
|
101 | 101 | repo = vcsbackend.repo |
|
102 | 102 | assert repo.get_commit().is_link(filename) is expected |
|
103 | 103 | |
|
104 | 104 | def test_get_commit(self, backend): |
|
105 | 105 | backend.repo.get_commit() |
|
106 | 106 | |
|
107 | 107 | def test_get_changeset_is_deprecated(self, backend): |
|
108 | 108 | repo = backend.repo |
|
109 | 109 | pytest.deprecated_call(repo.get_changeset) |
|
110 | 110 | |
|
111 | 111 | def test_clone_url_encrypted_value(self, backend): |
|
112 | 112 | repo = backend.create_repo() |
|
113 | 113 | Session().commit() |
|
114 | 114 | |
|
115 | 115 | repo.clone_url = 'https://marcink:qweqwe@code.rhodecode.com' |
|
116 | 116 | Session().add(repo) |
|
117 | 117 | Session().commit() |
|
118 | 118 | |
|
119 | 119 | assert repo.clone_url == 'https://marcink:qweqwe@code.rhodecode.com' |
|
120 | 120 | |
|
121 | 121 | @pytest.mark.backends("git", "svn") |
|
122 | 122 | def test_create_filesystem_repo_installs_hooks(self, tmpdir, backend): |
|
123 | 123 | hook_methods = { |
|
124 | 124 | 'git': 'install_git_hook', |
|
125 | 125 | 'svn': 'install_svn_hooks' |
|
126 | 126 | } |
|
127 | 127 | repo = backend.create_repo() |
|
128 | 128 | repo_name = repo.repo_name |
|
129 | 129 | model = RepoModel() |
|
130 | 130 | repo_location = tempfile.mkdtemp() |
|
131 | 131 | model.repos_path = repo_location |
|
132 | 132 | method = hook_methods[backend.alias] |
|
133 | 133 | with mock.patch.object(ScmModel, method) as hooks_mock: |
|
134 | 134 | model._create_filesystem_repo( |
|
135 | 135 | repo_name, backend.alias, repo_group='', clone_uri=None) |
|
136 | 136 | assert hooks_mock.call_count == 1 |
|
137 | 137 | hook_args, hook_kwargs = hooks_mock.call_args |
|
138 | 138 | assert hook_args[0].name == repo_name |
|
139 | 139 | |
|
140 | 140 | @pytest.mark.parametrize("use_global_config, repo_name_passed", [ |
|
141 | 141 | (True, False), |
|
142 | 142 | (False, True) |
|
143 | 143 | ]) |
|
144 | 144 | def test_per_repo_config_is_generated_during_filesystem_repo_creation( |
|
145 | 145 | self, tmpdir, backend, use_global_config, repo_name_passed): |
|
146 | 146 | repo_name = 'test-{}-repo-{}'.format(backend.alias, use_global_config) |
|
147 | 147 | config = make_db_config() |
|
148 | 148 | model = RepoModel() |
|
149 | 149 | with mock.patch('rhodecode.model.repo.make_db_config') as config_mock: |
|
150 | 150 | config_mock.return_value = config |
|
151 | 151 | model._create_filesystem_repo( |
|
152 | 152 | repo_name, backend.alias, repo_group='', clone_uri=None, |
|
153 | 153 | use_global_config=use_global_config) |
|
154 | 154 | expected_repo_name = repo_name if repo_name_passed else None |
|
155 | 155 | expected_call = mock.call(clear_session=False, repo=expected_repo_name) |
|
156 | 156 | assert expected_call in config_mock.call_args_list |
|
157 | 157 | |
|
158 | 158 | def test_update_commit_cache_with_config(serf, backend): |
|
159 | 159 | repo = backend.create_repo() |
|
160 | 160 | with mock.patch('rhodecode.model.db.Repository.scm_instance') as scm: |
|
161 | 161 | scm_instance = mock.Mock() |
|
162 | 162 | scm_instance.get_commit.return_value = { |
|
163 | 'raw_id': 40*'0' | |
|
163 | 'raw_id': 40*'0', | |
|
164 | 'revision': 1 | |
|
164 | 165 | } |
|
165 | 166 | scm.return_value = scm_instance |
|
166 | 167 | repo.update_commit_cache() |
|
167 | 168 | scm.assert_called_with(cache=False, config=None) |
|
168 | 169 | config = {'test': 'config'} |
|
169 | 170 | repo.update_commit_cache(config=config) |
|
170 | 171 | scm.assert_called_with( |
|
171 | 172 | cache=False, config=config) |
|
172 | 173 | |
|
173 | 174 | |
|
174 | 175 | class TestGetUsers(object): |
|
175 | 176 | def test_returns_active_users(self, backend, user_util): |
|
176 | 177 | for i in range(4): |
|
177 | 178 | is_active = i % 2 == 0 |
|
178 | 179 | user_util.create_user(active=is_active, lastname='Fake user') |
|
179 | 180 | |
|
180 | 181 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): |
|
181 | 182 | users = RepoModel().get_users() |
|
182 | 183 | fake_users = [u for u in users if u['last_name'] == 'Fake user'] |
|
183 | 184 | assert len(fake_users) == 2 |
|
184 | 185 | |
|
185 | 186 | expected_keys = ( |
|
186 | 187 | 'id', 'first_name', 'last_name', 'username', 'icon_link', |
|
187 | 188 | 'value_display', 'value', 'value_type') |
|
188 | 189 | for user in users: |
|
189 | 190 | assert user['value_type'] is 'user' |
|
190 | 191 | for key in expected_keys: |
|
191 | 192 | assert key in user |
|
192 | 193 | |
|
193 | 194 | def test_returns_user_filtered_by_last_name(self, backend, user_util): |
|
194 | 195 | keywords = ('aBc', u'ΓΌnicode') |
|
195 | 196 | for keyword in keywords: |
|
196 | 197 | for i in range(2): |
|
197 | 198 | user_util.create_user( |
|
198 | 199 | active=True, lastname=u'Fake {} user'.format(keyword)) |
|
199 | 200 | |
|
200 | 201 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): |
|
201 | 202 | keyword = keywords[1].lower() |
|
202 | 203 | users = RepoModel().get_users(name_contains=keyword) |
|
203 | 204 | |
|
204 | 205 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] |
|
205 | 206 | assert len(fake_users) == 2 |
|
206 | 207 | for user in fake_users: |
|
207 | 208 | assert user['last_name'] == safe_unicode('Fake ΓΌnicode user') |
|
208 | 209 | |
|
209 | 210 | def test_returns_user_filtered_by_first_name(self, backend, user_util): |
|
210 | 211 | created_users = [] |
|
211 | 212 | keywords = ('aBc', u'ΓΌnicode') |
|
212 | 213 | for keyword in keywords: |
|
213 | 214 | for i in range(2): |
|
214 | 215 | created_users.append(user_util.create_user( |
|
215 | 216 | active=True, lastname='Fake user', |
|
216 | 217 | firstname=u'Fake {} user'.format(keyword))) |
|
217 | 218 | |
|
218 | 219 | keyword = keywords[1].lower() |
|
219 | 220 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): |
|
220 | 221 | users = RepoModel().get_users(name_contains=keyword) |
|
221 | 222 | |
|
222 | 223 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] |
|
223 | 224 | assert len(fake_users) == 2 |
|
224 | 225 | for user in fake_users: |
|
225 | 226 | assert user['first_name'] == safe_unicode('Fake ΓΌnicode user') |
|
226 | 227 | |
|
227 | 228 | def test_returns_user_filtered_by_username(self, backend, user_util): |
|
228 | 229 | created_users = [] |
|
229 | 230 | for i in range(5): |
|
230 | 231 | created_users.append(user_util.create_user( |
|
231 | 232 | active=True, lastname='Fake user')) |
|
232 | 233 | |
|
233 | 234 | user_filter = created_users[-1].username[-2:] |
|
234 | 235 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): |
|
235 | 236 | users = RepoModel().get_users(name_contains=user_filter) |
|
236 | 237 | |
|
237 | 238 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] |
|
238 | 239 | assert len(fake_users) == 1 |
|
239 | 240 | assert fake_users[0]['username'] == created_users[-1].username |
|
240 | 241 | |
|
241 | 242 | def test_returns_limited_user_list(self, backend, user_util): |
|
242 | 243 | created_users = [] |
|
243 | 244 | for i in range(5): |
|
244 | 245 | created_users.append(user_util.create_user( |
|
245 | 246 | active=True, lastname='Fake user')) |
|
246 | 247 | |
|
247 | 248 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): |
|
248 | 249 | users = RepoModel().get_users(name_contains='Fake', limit=3) |
|
249 | 250 | |
|
250 | 251 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] |
|
251 | 252 | assert len(fake_users) == 3 |
|
252 | 253 | |
|
253 | 254 | |
|
254 | 255 | class TestGetUserGroups(object): |
|
255 | 256 | def test_returns_filtered_list(self, backend, user_util): |
|
256 | 257 | created_groups = [] |
|
257 | 258 | for i in range(4): |
|
258 | 259 | created_groups.append( |
|
259 | 260 | user_util.create_user_group(users_group_active=True)) |
|
260 | 261 | |
|
261 | 262 | group_filter = created_groups[-1].users_group_name[-2:] |
|
262 | 263 | with self._patch_user_group_list(): |
|
263 | 264 | groups = RepoModel().get_user_groups(group_filter) |
|
264 | 265 | |
|
265 | 266 | fake_groups = [ |
|
266 | 267 | u for u in groups if u['value'].startswith('test_returns')] |
|
267 | 268 | assert len(fake_groups) == 1 |
|
268 | 269 | assert fake_groups[0]['value'] == created_groups[-1].users_group_name |
|
269 | 270 | assert fake_groups[0]['value_display'].startswith( |
|
270 | 271 | 'Group: test_returns') |
|
271 | 272 | |
|
272 | 273 | def test_returns_limited_list(self, backend, user_util): |
|
273 | 274 | created_groups = [] |
|
274 | 275 | for i in range(3): |
|
275 | 276 | created_groups.append( |
|
276 | 277 | user_util.create_user_group(users_group_active=True)) |
|
277 | 278 | |
|
278 | 279 | with self._patch_user_group_list(): |
|
279 | 280 | groups = RepoModel().get_user_groups('test_returns') |
|
280 | 281 | |
|
281 | 282 | fake_groups = [ |
|
282 | 283 | u for u in groups if u['value'].startswith('test_returns')] |
|
283 | 284 | assert len(fake_groups) == 3 |
|
284 | 285 | |
|
285 | 286 | def test_returns_active_user_groups(self, backend, user_util): |
|
286 | 287 | for i in range(4): |
|
287 | 288 | is_active = i % 2 == 0 |
|
288 | 289 | user_util.create_user_group(users_group_active=is_active) |
|
289 | 290 | |
|
290 | 291 | with self._patch_user_group_list(): |
|
291 | 292 | groups = RepoModel().get_user_groups() |
|
292 | 293 | expected = ('id', 'icon_link', 'value_display', 'value', 'value_type') |
|
293 | 294 | for group in groups: |
|
294 | 295 | assert group['value_type'] is 'user_group' |
|
295 | 296 | for key in expected: |
|
296 | 297 | assert key in group |
|
297 | 298 | |
|
298 | 299 | fake_groups = [ |
|
299 | 300 | u for u in groups if u['value'].startswith('test_returns')] |
|
300 | 301 | assert len(fake_groups) == 2 |
|
301 | 302 | for user in fake_groups: |
|
302 | 303 | assert user['value_display'].startswith('Group: test_returns') |
|
303 | 304 | |
|
304 | 305 | def _patch_user_group_list(self): |
|
305 | 306 | def side_effect(group_list, perm_set): |
|
306 | 307 | return group_list |
|
307 | 308 | return mock.patch( |
|
308 | 309 | 'rhodecode.model.repo.UserGroupList', side_effect=side_effect) |
General Comments 0
You need to be logged in to leave comments.
Login now