Show More
@@ -1,284 +1,289 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 | """ |
|
22 | 22 | Home controller for RhodeCode Enterprise |
|
23 | 23 | """ |
|
24 | 24 | |
|
25 | 25 | import logging |
|
26 | 26 | import time |
|
27 | 27 | import re |
|
28 | 28 | |
|
29 | 29 | from pylons import tmpl_context as c, request, url, config |
|
30 | 30 | from pylons.i18n.translation import _ |
|
31 | 31 | from sqlalchemy.sql import func |
|
32 | 32 | |
|
33 | 33 | from rhodecode.lib.auth import ( |
|
34 | 34 | LoginRequired, HasPermissionAllDecorator, AuthUser, |
|
35 | 35 | HasRepoGroupPermissionAnyDecorator, XHRRequired) |
|
36 | 36 | from rhodecode.lib.base import BaseController, render |
|
37 | 37 | from rhodecode.lib.index import searcher_from_config |
|
38 | 38 | from rhodecode.lib.ext_json import json |
|
39 | 39 | from rhodecode.lib.utils import jsonify |
|
40 | from rhodecode.lib.utils2 import safe_unicode | |
|
40 | from rhodecode.lib.utils2 import safe_unicode, str2bool | |
|
41 | 41 | from rhodecode.model.db import Repository, RepoGroup |
|
42 | 42 | from rhodecode.model.repo import RepoModel |
|
43 | 43 | from rhodecode.model.repo_group import RepoGroupModel |
|
44 | 44 | from rhodecode.model.scm import RepoList, RepoGroupList |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | log = logging.getLogger(__name__) |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | class HomeController(BaseController): |
|
51 | 51 | def __before__(self): |
|
52 | 52 | super(HomeController, self).__before__() |
|
53 | 53 | |
|
54 | 54 | def ping(self): |
|
55 | 55 | """ |
|
56 | 56 | Ping, doesn't require login, good for checking out the platform |
|
57 | 57 | """ |
|
58 | 58 | instance_id = getattr(c, 'rhodecode_instanceid', '') |
|
59 | 59 | return 'pong[%s] => %s' % (instance_id, self.ip_addr,) |
|
60 | 60 | |
|
61 | 61 | @LoginRequired() |
|
62 | 62 | @HasPermissionAllDecorator('hg.admin') |
|
63 | 63 | def error_test(self): |
|
64 | 64 | """ |
|
65 | 65 | Test exception handling and emails on errors |
|
66 | 66 | """ |
|
67 | 67 | class TestException(Exception): |
|
68 | 68 | pass |
|
69 | 69 | |
|
70 | 70 | msg = ('RhodeCode Enterprise %s test exception. Generation time: %s' |
|
71 | 71 | % (c.rhodecode_name, time.time())) |
|
72 | 72 | raise TestException(msg) |
|
73 | 73 | |
|
74 | 74 | def _get_groups_and_repos(self, repo_group_id=None): |
|
75 | 75 | # repo groups groups |
|
76 | 76 | repo_group_list = RepoGroup.get_all_repo_groups(group_id=repo_group_id) |
|
77 | 77 | _perms = ['group.read', 'group.write', 'group.admin'] |
|
78 | 78 | repo_group_list_acl = RepoGroupList(repo_group_list, perm_set=_perms) |
|
79 | 79 | repo_group_data = RepoGroupModel().get_repo_groups_as_dict( |
|
80 | 80 | repo_group_list=repo_group_list_acl, admin=False) |
|
81 | 81 | |
|
82 | 82 | # repositories |
|
83 | 83 | repo_list = Repository.get_all_repos(group_id=repo_group_id) |
|
84 | 84 | _perms = ['repository.read', 'repository.write', 'repository.admin'] |
|
85 | 85 | repo_list_acl = RepoList(repo_list, perm_set=_perms) |
|
86 | 86 | repo_data = RepoModel().get_repos_as_dict( |
|
87 | 87 | repo_list=repo_list_acl, admin=False) |
|
88 | 88 | |
|
89 | 89 | return repo_data, repo_group_data |
|
90 | 90 | |
|
91 | 91 | @LoginRequired() |
|
92 | 92 | def index(self): |
|
93 | 93 | c.repo_group = None |
|
94 | 94 | |
|
95 | 95 | repo_data, repo_group_data = self._get_groups_and_repos() |
|
96 | 96 | # json used to render the grids |
|
97 | 97 | c.repos_data = json.dumps(repo_data) |
|
98 | 98 | c.repo_groups_data = json.dumps(repo_group_data) |
|
99 | 99 | |
|
100 | 100 | return render('/index.html') |
|
101 | 101 | |
|
102 | 102 | @LoginRequired() |
|
103 | 103 | @HasRepoGroupPermissionAnyDecorator('group.read', 'group.write', |
|
104 | 104 | 'group.admin') |
|
105 | 105 | def index_repo_group(self, group_name): |
|
106 | 106 | """GET /repo_group_name: Show a specific item""" |
|
107 | 107 | c.repo_group = RepoGroupModel()._get_repo_group(group_name) |
|
108 | 108 | repo_data, repo_group_data = self._get_groups_and_repos( |
|
109 | 109 | c.repo_group.group_id) |
|
110 | 110 | |
|
111 | 111 | # json used to render the grids |
|
112 | 112 | c.repos_data = json.dumps(repo_data) |
|
113 | 113 | c.repo_groups_data = json.dumps(repo_group_data) |
|
114 | 114 | |
|
115 | 115 | return render('index_repo_group.html') |
|
116 | 116 | |
|
117 | 117 | def _get_repo_list(self, name_contains=None, repo_type=None, limit=20): |
|
118 | 118 | query = Repository.query()\ |
|
119 | 119 | .order_by(func.length(Repository.repo_name))\ |
|
120 | 120 | .order_by(Repository.repo_name) |
|
121 | 121 | |
|
122 | 122 | if repo_type: |
|
123 | 123 | query = query.filter(Repository.repo_type == repo_type) |
|
124 | 124 | |
|
125 | 125 | if name_contains: |
|
126 | 126 | ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) |
|
127 | 127 | query = query.filter( |
|
128 | 128 | Repository.repo_name.ilike(ilike_expression)) |
|
129 | 129 | query = query.limit(limit) |
|
130 | 130 | |
|
131 | 131 | all_repos = query.all() |
|
132 | 132 | repo_iter = self.scm_model.get_repos(all_repos) |
|
133 | 133 | return [ |
|
134 | 134 | { |
|
135 | 135 | 'id': obj['name'], |
|
136 | 136 | 'text': obj['name'], |
|
137 | 137 | 'type': 'repo', |
|
138 | 138 | 'obj': obj['dbrepo'], |
|
139 | 139 | 'url': url('summary_home', repo_name=obj['name']) |
|
140 | 140 | } |
|
141 | 141 | for obj in repo_iter] |
|
142 | 142 | |
|
143 | 143 | def _get_repo_group_list(self, name_contains=None, limit=20): |
|
144 | 144 | query = RepoGroup.query()\ |
|
145 | 145 | .order_by(func.length(RepoGroup.group_name))\ |
|
146 | 146 | .order_by(RepoGroup.group_name) |
|
147 | 147 | |
|
148 | 148 | if name_contains: |
|
149 | 149 | ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) |
|
150 | 150 | query = query.filter( |
|
151 | 151 | RepoGroup.group_name.ilike(ilike_expression)) |
|
152 | 152 | query = query.limit(limit) |
|
153 | 153 | |
|
154 | 154 | all_groups = query.all() |
|
155 | 155 | repo_groups_iter = self.scm_model.get_repo_groups(all_groups) |
|
156 | 156 | return [ |
|
157 | 157 | { |
|
158 | 158 | 'id': obj.group_name, |
|
159 | 159 | 'text': obj.group_name, |
|
160 | 160 | 'type': 'group', |
|
161 | 161 | 'obj': {}, |
|
162 | 162 | 'url': url('repo_group_home', group_name=obj.group_name) |
|
163 | 163 | } |
|
164 | 164 | for obj in repo_groups_iter] |
|
165 | 165 | |
|
166 | 166 | def _get_hash_commit_list(self, hash_starts_with=None, limit=20): |
|
167 | 167 | if not hash_starts_with or len(hash_starts_with) < 3: |
|
168 | 168 | return [] |
|
169 | 169 | |
|
170 | 170 | commit_hashes = re.compile('([0-9a-f]{2,40})').findall(hash_starts_with) |
|
171 | 171 | |
|
172 | 172 | if len(commit_hashes) != 1: |
|
173 | 173 | return [] |
|
174 | 174 | |
|
175 | 175 | commit_hash_prefix = commit_hashes[0] |
|
176 | 176 | |
|
177 | 177 | auth_user = AuthUser( |
|
178 | 178 | user_id=c.rhodecode_user.user_id, ip_addr=self.ip_addr) |
|
179 | 179 | searcher = searcher_from_config(config) |
|
180 | 180 | result = searcher.search( |
|
181 | 181 | 'commit_id:%s*' % commit_hash_prefix, 'commit', auth_user) |
|
182 | 182 | |
|
183 | 183 | return [ |
|
184 | 184 | { |
|
185 | 185 | 'id': entry['commit_id'], |
|
186 | 186 | 'text': entry['commit_id'], |
|
187 | 187 | 'type': 'commit', |
|
188 | 188 | 'obj': {'repo': entry['repository']}, |
|
189 | 189 | 'url': url('changeset_home', |
|
190 | 190 | repo_name=entry['repository'], revision=entry['commit_id']) |
|
191 | 191 | } |
|
192 | 192 | for entry in result['results']] |
|
193 | 193 | |
|
194 | 194 | @LoginRequired() |
|
195 | 195 | @XHRRequired() |
|
196 | 196 | @jsonify |
|
197 | 197 | def goto_switcher_data(self): |
|
198 | 198 | query = request.GET.get('query') |
|
199 | 199 | log.debug('generating goto switcher list, query %s', query) |
|
200 | 200 | |
|
201 | 201 | res = [] |
|
202 | 202 | repo_groups = self._get_repo_group_list(query) |
|
203 | 203 | if repo_groups: |
|
204 | 204 | res.append({ |
|
205 | 205 | 'text': _('Groups'), |
|
206 | 206 | 'children': repo_groups |
|
207 | 207 | }) |
|
208 | 208 | |
|
209 | 209 | repos = self._get_repo_list(query) |
|
210 | 210 | if repos: |
|
211 | 211 | res.append({ |
|
212 | 212 | 'text': _('Repositories'), |
|
213 | 213 | 'children': repos |
|
214 | 214 | }) |
|
215 | 215 | |
|
216 | 216 | commits = self._get_hash_commit_list(query) |
|
217 | 217 | if commits: |
|
218 | 218 | unique_repos = {} |
|
219 | 219 | for commit in commits: |
|
220 | 220 | unique_repos.setdefault(commit['obj']['repo'], [] |
|
221 | 221 | ).append(commit) |
|
222 | 222 | |
|
223 | 223 | for repo in unique_repos: |
|
224 | 224 | res.append({ |
|
225 | 225 | 'text': _('Commits in %(repo)s') % {'repo': repo}, |
|
226 | 226 | 'children': unique_repos[repo] |
|
227 | 227 | }) |
|
228 | 228 | |
|
229 | 229 | data = { |
|
230 | 230 | 'more': False, |
|
231 | 231 | 'results': res |
|
232 | 232 | } |
|
233 | 233 | return data |
|
234 | 234 | |
|
235 | 235 | @LoginRequired() |
|
236 | 236 | @XHRRequired() |
|
237 | 237 | @jsonify |
|
238 | 238 | def repo_list_data(self): |
|
239 | 239 | query = request.GET.get('query') |
|
240 | 240 | repo_type = request.GET.get('repo_type') |
|
241 | 241 | log.debug('generating repo list, query:%s', query) |
|
242 | 242 | |
|
243 | 243 | res = [] |
|
244 | 244 | repos = self._get_repo_list(query, repo_type=repo_type) |
|
245 | 245 | if repos: |
|
246 | 246 | res.append({ |
|
247 | 247 | 'text': _('Repositories'), |
|
248 | 248 | 'children': repos |
|
249 | 249 | }) |
|
250 | 250 | |
|
251 | 251 | data = { |
|
252 | 252 | 'more': False, |
|
253 | 253 | 'results': res |
|
254 | 254 | } |
|
255 | 255 | return data |
|
256 | 256 | |
|
257 | 257 | @LoginRequired() |
|
258 | 258 | @XHRRequired() |
|
259 | 259 | @jsonify |
|
260 | 260 | def user_autocomplete_data(self): |
|
261 | 261 | query = request.GET.get('query') |
|
262 | active = str2bool(request.GET.get('active') or True) | |
|
262 | 263 | |
|
263 | 264 | repo_model = RepoModel() |
|
264 |
_users = repo_model.get_users( |
|
|
265 | _users = repo_model.get_users( | |
|
266 | name_contains=query, only_active=active) | |
|
265 | 267 | |
|
266 | 268 | if request.GET.get('user_groups'): |
|
267 | 269 | # extend with user groups |
|
268 |
_user_groups = repo_model.get_user_groups( |
|
|
270 | _user_groups = repo_model.get_user_groups( | |
|
271 | name_contains=query, only_active=active) | |
|
269 | 272 | _users = _users + _user_groups |
|
270 | 273 | |
|
271 | 274 | return {'suggestions': _users} |
|
272 | 275 | |
|
273 | 276 | @LoginRequired() |
|
274 | 277 | @XHRRequired() |
|
275 | 278 | @jsonify |
|
276 | 279 | def user_group_autocomplete_data(self): |
|
277 | 280 | query = request.GET.get('query') |
|
281 | active = str2bool(request.GET.get('active') or True) | |
|
278 | 282 | |
|
279 | 283 | repo_model = RepoModel() |
|
280 |
_user_groups = repo_model.get_user_groups( |
|
|
284 | _user_groups = repo_model.get_user_groups( | |
|
285 | name_contains=query, only_active=active) | |
|
281 | 286 | _user_groups = _user_groups |
|
282 | 287 | |
|
283 | 288 | return {'suggestions': _user_groups} |
|
284 | 289 |
@@ -1,365 +1,379 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 json |
|
22 | 22 | |
|
23 | 23 | from mock import patch |
|
24 | 24 | import pytest |
|
25 | 25 | |
|
26 | 26 | import rhodecode |
|
27 | 27 | from rhodecode.lib.utils import map_groups |
|
28 | 28 | from rhodecode.model.db import Repository, User, RepoGroup |
|
29 | 29 | from rhodecode.model.meta import Session |
|
30 | 30 | from rhodecode.model.repo import RepoModel |
|
31 | 31 | from rhodecode.model.repo_group import RepoGroupModel |
|
32 | 32 | from rhodecode.model.settings import SettingsModel |
|
33 | 33 | from rhodecode.tests import TestController, url, TEST_USER_ADMIN_LOGIN |
|
34 | 34 | from rhodecode.tests.fixture import Fixture |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | fixture = Fixture() |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | class TestHomeController(TestController): |
|
41 | 41 | |
|
42 | 42 | def test_index(self): |
|
43 | 43 | self.log_user() |
|
44 | 44 | response = self.app.get(url(controller='home', action='index')) |
|
45 | 45 | # if global permission is set |
|
46 | 46 | response.mustcontain('Add Repository') |
|
47 | 47 | |
|
48 | 48 | # search for objects inside the JavaScript JSON |
|
49 | 49 | for repo in Repository.getAll(): |
|
50 | 50 | response.mustcontain('"name_raw": "%s"' % repo.repo_name) |
|
51 | 51 | |
|
52 | 52 | def test_index_contains_backend_specific_details(self, backend): |
|
53 | 53 | self.log_user() |
|
54 | 54 | response = self.app.get(url(controller='home', action='index')) |
|
55 | 55 | tip = backend.repo.get_commit().raw_id |
|
56 | 56 | |
|
57 | 57 | # html in javascript variable: |
|
58 | 58 | response.mustcontain(r'<i class=\"icon-%s\"' % (backend.alias, )) |
|
59 | 59 | response.mustcontain(r'href=\"/%s\"' % (backend.repo_name, )) |
|
60 | 60 | |
|
61 | 61 | response.mustcontain("""/%s/changeset/%s""" % (backend.repo_name, tip)) |
|
62 | 62 | response.mustcontain("""Added a symlink""") |
|
63 | 63 | |
|
64 | 64 | def test_index_with_anonymous_access_disabled(self): |
|
65 | 65 | with fixture.anon_access(False): |
|
66 | 66 | response = self.app.get(url(controller='home', action='index'), |
|
67 | 67 | status=302) |
|
68 | 68 | assert 'login' in response.location |
|
69 | 69 | |
|
70 | 70 | def test_index_page_on_groups(self, autologin_user, repo_group): |
|
71 | 71 | response = self.app.get(url('repo_group_home', group_name='gr1')) |
|
72 | 72 | response.mustcontain("gr1/repo_in_group") |
|
73 | 73 | |
|
74 | 74 | def test_index_page_on_group_with_trailing_slash( |
|
75 | 75 | self, autologin_user, repo_group): |
|
76 | 76 | response = self.app.get(url('repo_group_home', group_name='gr1') + '/') |
|
77 | 77 | response.mustcontain("gr1/repo_in_group") |
|
78 | 78 | |
|
79 | 79 | @pytest.fixture(scope='class') |
|
80 | 80 | def repo_group(self, request): |
|
81 | 81 | gr = fixture.create_repo_group('gr1') |
|
82 | 82 | fixture.create_repo(name='gr1/repo_in_group', repo_group=gr) |
|
83 | 83 | |
|
84 | 84 | @request.addfinalizer |
|
85 | 85 | def cleanup(): |
|
86 | 86 | RepoModel().delete('gr1/repo_in_group') |
|
87 | 87 | RepoGroupModel().delete(repo_group='gr1', force_delete=True) |
|
88 | 88 | Session().commit() |
|
89 | 89 | |
|
90 | 90 | def test_index_with_name_with_tags(self, autologin_user): |
|
91 | 91 | user = User.get_by_username('test_admin') |
|
92 | 92 | user.name = '<img src="/image1" onload="alert(\'Hello, World!\');">' |
|
93 | 93 | user.lastname = ( |
|
94 | 94 | '<img src="/image2" onload="alert(\'Hello, World!\');">') |
|
95 | 95 | Session().add(user) |
|
96 | 96 | Session().commit() |
|
97 | 97 | |
|
98 | 98 | response = self.app.get(url(controller='home', action='index')) |
|
99 | 99 | response.mustcontain( |
|
100 | 100 | '<img src="/image1" onload="' |
|
101 | 101 | 'alert('Hello, World!');">') |
|
102 | 102 | response.mustcontain( |
|
103 | 103 | '<img src="/image2" onload="' |
|
104 | 104 | 'alert('Hello, World!');">') |
|
105 | 105 | |
|
106 | 106 | @pytest.mark.parametrize("name, state", [ |
|
107 | 107 | ('Disabled', False), |
|
108 | 108 | ('Enabled', True), |
|
109 | 109 | ]) |
|
110 | 110 | def test_index_show_version(self, autologin_user, name, state): |
|
111 | 111 | version_string = 'RhodeCode Enterprise %s' % rhodecode.__version__ |
|
112 | 112 | |
|
113 | 113 | show = SettingsModel().get_setting_by_name('show_version') |
|
114 | 114 | show.app_settings_value = state |
|
115 | 115 | Session().add(show) |
|
116 | 116 | Session().commit() |
|
117 | 117 | response = self.app.get(url(controller='home', action='index')) |
|
118 | 118 | if state is True: |
|
119 | 119 | response.mustcontain(version_string) |
|
120 | 120 | if state is False: |
|
121 | 121 | response.mustcontain(no=[version_string]) |
|
122 | 122 | |
|
123 | 123 | |
|
124 | 124 | class TestUserAutocompleteData(TestController): |
|
125 | 125 | def test_returns_list_of_users(self, user_util): |
|
126 | 126 | self.log_user() |
|
127 | 127 | user = user_util.create_user(is_active=True) |
|
128 | 128 | user_name = user.username |
|
129 | 129 | response = self.app.get( |
|
130 | 130 | url(controller='home', action='user_autocomplete_data'), |
|
131 | 131 | headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200) |
|
132 | 132 | result = json.loads(response.body) |
|
133 | 133 | values = [suggestion['value'] for suggestion in result['suggestions']] |
|
134 | 134 | assert user_name in values |
|
135 | 135 | |
|
136 | def test_returns_inactive_users_when_active_flag_sent(self, user_util): | |
|
137 | self.log_user() | |
|
138 | user = user_util.create_user(is_active=False) | |
|
139 | user_name = user.username | |
|
140 | response = self.app.get( | |
|
141 | url(controller='home', action='user_autocomplete_data', | |
|
142 | user_groups='true', active='0'), | |
|
143 | headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200) | |
|
144 | result = json.loads(response.body) | |
|
145 | values = [suggestion['value'] for suggestion in result['suggestions']] | |
|
146 | assert user_name in values | |
|
147 | ||
|
136 | 148 | def test_returns_groups_when_user_groups_sent(self, user_util): |
|
137 | 149 | self.log_user() |
|
138 | 150 | group = user_util.create_user_group(user_groups_active=True) |
|
139 | 151 | group_name = group.users_group_name |
|
140 | 152 | response = self.app.get( |
|
141 | 153 | url(controller='home', action='user_autocomplete_data', |
|
142 | 154 | user_groups='true'), |
|
143 | 155 | headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200) |
|
144 | 156 | result = json.loads(response.body) |
|
145 | 157 | values = [suggestion['value'] for suggestion in result['suggestions']] |
|
146 | 158 | assert group_name in values |
|
147 | 159 | |
|
148 | 160 | def test_result_is_limited_when_query_is_sent(self): |
|
149 | 161 | self.log_user() |
|
150 | 162 | fake_result = [ |
|
151 | 163 | { |
|
152 | 164 | 'first_name': 'John', |
|
153 | 165 | 'value_display': 'hello{} (John Smith)'.format(i), |
|
154 | 166 | 'icon_link': '/images/user14.png', |
|
155 | 167 | 'value': 'hello{}'.format(i), |
|
156 | 168 | 'last_name': 'Smith', |
|
157 | 169 | 'username': 'hello{}'.format(i), |
|
158 | 170 | 'id': i, |
|
159 | 171 | 'value_type': u'user' |
|
160 | 172 | } |
|
161 | 173 | for i in range(10) |
|
162 | 174 | ] |
|
163 | 175 | users_patcher = patch.object( |
|
164 | 176 | RepoModel, 'get_users', return_value=fake_result) |
|
165 | 177 | groups_patcher = patch.object( |
|
166 | 178 | RepoModel, 'get_user_groups', return_value=fake_result) |
|
167 | 179 | |
|
168 | 180 | query = 'hello' |
|
169 | 181 | with users_patcher as users_mock, groups_patcher as groups_mock: |
|
170 | 182 | response = self.app.get( |
|
171 | 183 | url(controller='home', action='user_autocomplete_data', |
|
172 | 184 | user_groups='true', query=query), |
|
173 | 185 | headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200) |
|
174 | 186 | |
|
175 | 187 | result = json.loads(response.body) |
|
176 |
users_mock.assert_called_once_with( |
|
|
177 | groups_mock.assert_called_once_with(name_contains=query) | |
|
188 | users_mock.assert_called_once_with( | |
|
189 | name_contains=query, only_active=True) | |
|
190 | groups_mock.assert_called_once_with( | |
|
191 | name_contains=query, only_active=True) | |
|
178 | 192 | assert len(result['suggestions']) == 20 |
|
179 | 193 | |
|
180 | 194 | |
|
181 | 195 | def assert_and_get_content(result): |
|
182 | 196 | repos = [] |
|
183 | 197 | groups = [] |
|
184 | 198 | commits = [] |
|
185 | 199 | for data in result: |
|
186 | 200 | for data_item in data['children']: |
|
187 | 201 | assert data_item['id'] |
|
188 | 202 | assert data_item['text'] |
|
189 | 203 | assert data_item['url'] |
|
190 | 204 | if data_item['type'] == 'repo': |
|
191 | 205 | repos.append(data_item) |
|
192 | 206 | elif data_item['type'] == 'group': |
|
193 | 207 | groups.append(data_item) |
|
194 | 208 | elif data_item['type'] == 'commit': |
|
195 | 209 | commits.append(data_item) |
|
196 | 210 | else: |
|
197 | 211 | raise Exception('invalid type %s' % data_item['type']) |
|
198 | 212 | |
|
199 | 213 | return repos, groups, commits |
|
200 | 214 | |
|
201 | 215 | |
|
202 | 216 | class TestGotoSwitcherData(TestController): |
|
203 | 217 | required_repos_with_groups = [ |
|
204 | 218 | 'abc', |
|
205 | 219 | 'abc-fork', |
|
206 | 220 | 'forks/abcd', |
|
207 | 221 | 'abcd', |
|
208 | 222 | 'abcde', |
|
209 | 223 | 'a/abc', |
|
210 | 224 | 'aa/abc', |
|
211 | 225 | 'aaa/abc', |
|
212 | 226 | 'aaaa/abc', |
|
213 | 227 | 'repos_abc/aaa/abc', |
|
214 | 228 | 'abc_repos/abc', |
|
215 | 229 | 'abc_repos/abcd', |
|
216 | 230 | 'xxx/xyz', |
|
217 | 231 | 'forked-abc/a/abc' |
|
218 | 232 | ] |
|
219 | 233 | |
|
220 | 234 | @pytest.fixture(autouse=True, scope='class') |
|
221 | 235 | def prepare(self, request, pylonsapp): |
|
222 | 236 | for repo_and_group in self.required_repos_with_groups: |
|
223 | 237 | # create structure of groups and return the last group |
|
224 | 238 | |
|
225 | 239 | repo_group = map_groups(repo_and_group) |
|
226 | 240 | |
|
227 | 241 | RepoModel()._create_repo( |
|
228 | 242 | repo_and_group, 'hg', 'test-ac', TEST_USER_ADMIN_LOGIN, |
|
229 | 243 | repo_group=getattr(repo_group, 'group_id', None)) |
|
230 | 244 | |
|
231 | 245 | Session().commit() |
|
232 | 246 | |
|
233 | 247 | request.addfinalizer(self.cleanup) |
|
234 | 248 | |
|
235 | 249 | def cleanup(self): |
|
236 | 250 | # first delete all repos |
|
237 | 251 | for repo_and_groups in self.required_repos_with_groups: |
|
238 | 252 | repo = Repository.get_by_repo_name(repo_and_groups) |
|
239 | 253 | if repo: |
|
240 | 254 | RepoModel().delete(repo) |
|
241 | 255 | Session().commit() |
|
242 | 256 | |
|
243 | 257 | # then delete all empty groups |
|
244 | 258 | for repo_and_groups in self.required_repos_with_groups: |
|
245 | 259 | if '/' in repo_and_groups: |
|
246 | 260 | r_group = repo_and_groups.rsplit('/', 1)[0] |
|
247 | 261 | repo_group = RepoGroup.get_by_group_name(r_group) |
|
248 | 262 | if not repo_group: |
|
249 | 263 | continue |
|
250 | 264 | parents = repo_group.parents |
|
251 | 265 | RepoGroupModel().delete(repo_group, force_delete=True) |
|
252 | 266 | Session().commit() |
|
253 | 267 | |
|
254 | 268 | for el in reversed(parents): |
|
255 | 269 | RepoGroupModel().delete(el, force_delete=True) |
|
256 | 270 | Session().commit() |
|
257 | 271 | |
|
258 | 272 | def test_returns_list_of_repos_and_groups(self): |
|
259 | 273 | self.log_user() |
|
260 | 274 | |
|
261 | 275 | response = self.app.get( |
|
262 | 276 | url(controller='home', action='goto_switcher_data'), |
|
263 | 277 | headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200) |
|
264 | 278 | result = json.loads(response.body)['results'] |
|
265 | 279 | |
|
266 | 280 | repos, groups, commits = assert_and_get_content(result) |
|
267 | 281 | |
|
268 | 282 | assert len(repos) == len(Repository.get_all()) |
|
269 | 283 | assert len(groups) == len(RepoGroup.get_all()) |
|
270 | 284 | assert len(commits) == 0 |
|
271 | 285 | |
|
272 | 286 | def test_returns_list_of_repos_and_groups_filtered(self): |
|
273 | 287 | self.log_user() |
|
274 | 288 | |
|
275 | 289 | response = self.app.get( |
|
276 | 290 | url(controller='home', action='goto_switcher_data'), |
|
277 | 291 | headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, |
|
278 | 292 | params={'query': 'abc'}, status=200) |
|
279 | 293 | result = json.loads(response.body)['results'] |
|
280 | 294 | |
|
281 | 295 | repos, groups, commits = assert_and_get_content(result) |
|
282 | 296 | |
|
283 | 297 | assert len(repos) == 13 |
|
284 | 298 | assert len(groups) == 5 |
|
285 | 299 | assert len(commits) == 0 |
|
286 | 300 | |
|
287 | 301 | def test_returns_list_of_properly_sorted_and_filtered(self): |
|
288 | 302 | self.log_user() |
|
289 | 303 | |
|
290 | 304 | response = self.app.get( |
|
291 | 305 | url(controller='home', action='goto_switcher_data'), |
|
292 | 306 | headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, |
|
293 | 307 | params={'query': 'abc'}, status=200) |
|
294 | 308 | result = json.loads(response.body)['results'] |
|
295 | 309 | |
|
296 | 310 | repos, groups, commits = assert_and_get_content(result) |
|
297 | 311 | |
|
298 | 312 | test_repos = [x['text'] for x in repos[:4]] |
|
299 | 313 | assert ['abc', 'abcd', 'a/abc', 'abcde'] == test_repos |
|
300 | 314 | |
|
301 | 315 | test_groups = [x['text'] for x in groups[:4]] |
|
302 | 316 | assert ['abc_repos', 'repos_abc', |
|
303 | 317 | 'forked-abc', 'forked-abc/a'] == test_groups |
|
304 | 318 | |
|
305 | 319 | |
|
306 | 320 | class TestRepoListData(TestController): |
|
307 | 321 | def test_returns_list_of_repos_and_groups(self, user_util): |
|
308 | 322 | self.log_user() |
|
309 | 323 | |
|
310 | 324 | response = self.app.get( |
|
311 | 325 | url(controller='home', action='repo_list_data'), |
|
312 | 326 | headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200) |
|
313 | 327 | result = json.loads(response.body)['results'] |
|
314 | 328 | |
|
315 | 329 | repos, groups, commits = assert_and_get_content(result) |
|
316 | 330 | |
|
317 | 331 | assert len(repos) == len(Repository.get_all()) |
|
318 | 332 | assert len(groups) == 0 |
|
319 | 333 | assert len(commits) == 0 |
|
320 | 334 | |
|
321 | 335 | def test_returns_list_of_repos_and_groups_filtered(self): |
|
322 | 336 | self.log_user() |
|
323 | 337 | |
|
324 | 338 | response = self.app.get( |
|
325 | 339 | url(controller='home', action='repo_list_data'), |
|
326 | 340 | headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, |
|
327 | 341 | params={'query': 'vcs_test_git'}, status=200) |
|
328 | 342 | result = json.loads(response.body)['results'] |
|
329 | 343 | |
|
330 | 344 | repos, groups, commits = assert_and_get_content(result) |
|
331 | 345 | |
|
332 | 346 | assert len(repos) == len(Repository.query().filter( |
|
333 | 347 | Repository.repo_name.ilike('%vcs_test_git%')).all()) |
|
334 | 348 | assert len(groups) == 0 |
|
335 | 349 | assert len(commits) == 0 |
|
336 | 350 | |
|
337 | 351 | def test_returns_list_of_repos_and_groups_filtered_with_type(self): |
|
338 | 352 | self.log_user() |
|
339 | 353 | |
|
340 | 354 | response = self.app.get( |
|
341 | 355 | url(controller='home', action='repo_list_data'), |
|
342 | 356 | headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, |
|
343 | 357 | params={'query': 'vcs_test_git', 'repo_type': 'git'}, status=200) |
|
344 | 358 | result = json.loads(response.body)['results'] |
|
345 | 359 | |
|
346 | 360 | repos, groups, commits = assert_and_get_content(result) |
|
347 | 361 | |
|
348 | 362 | assert len(repos) == len(Repository.query().filter( |
|
349 | 363 | Repository.repo_name.ilike('%vcs_test_git%')).all()) |
|
350 | 364 | assert len(groups) == 0 |
|
351 | 365 | assert len(commits) == 0 |
|
352 | 366 | |
|
353 | 367 | def test_returns_list_of_repos_non_ascii_query(self): |
|
354 | 368 | self.log_user() |
|
355 | 369 | response = self.app.get( |
|
356 | 370 | url(controller='home', action='repo_list_data'), |
|
357 | 371 | headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, |
|
358 | 372 | params={'query': 'ć_vcs_test_ą', 'repo_type': 'git'}, status=200) |
|
359 | 373 | result = json.loads(response.body)['results'] |
|
360 | 374 | |
|
361 | 375 | repos, groups, commits = assert_and_get_content(result) |
|
362 | 376 | |
|
363 | 377 | assert len(repos) == 0 |
|
364 | 378 | assert len(groups) == 0 |
|
365 | 379 | assert len(commits) == 0 |
General Comments 0
You need to be logged in to leave comments.
Login now