##// END OF EJS Templates
tests: fixed home tests with cached values
marcink -
r277:f422c79f default
parent child Browse files
Show More
@@ -1,379 +1,381 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 '&lt;img src=&#34;/image1&#34; onload=&#34;'
101 101 'alert(&#39;Hello, World!&#39;);&#34;&gt;')
102 102 response.mustcontain(
103 103 '&lt;img src=&#34;/image2&#34; onload=&#34;'
104 104 'alert(&#39;Hello, World!&#39;);&#34;&gt;')
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 show = SettingsModel().get_setting_by_name('show_version')
114 show.app_settings_value = state
115 Session().add(show)
113 sett = SettingsModel().create_or_update_setting(
114 'show_version', state, 'bool')
115 Session().add(sett)
116 116 Session().commit()
117 SettingsModel().invalidate_settings_cache()
118
117 119 response = self.app.get(url(controller='home', action='index'))
118 120 if state is True:
119 121 response.mustcontain(version_string)
120 122 if state is False:
121 123 response.mustcontain(no=[version_string])
122 124
123 125
124 126 class TestUserAutocompleteData(TestController):
125 127 def test_returns_list_of_users(self, user_util):
126 128 self.log_user()
127 129 user = user_util.create_user(is_active=True)
128 130 user_name = user.username
129 131 response = self.app.get(
130 132 url(controller='home', action='user_autocomplete_data'),
131 133 headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200)
132 134 result = json.loads(response.body)
133 135 values = [suggestion['value'] for suggestion in result['suggestions']]
134 136 assert user_name in values
135 137
136 138 def test_returns_inactive_users_when_active_flag_sent(self, user_util):
137 139 self.log_user()
138 140 user = user_util.create_user(is_active=False)
139 141 user_name = user.username
140 142 response = self.app.get(
141 143 url(controller='home', action='user_autocomplete_data',
142 144 user_groups='true', active='0'),
143 145 headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200)
144 146 result = json.loads(response.body)
145 147 values = [suggestion['value'] for suggestion in result['suggestions']]
146 148 assert user_name in values
147 149
148 150 def test_returns_groups_when_user_groups_sent(self, user_util):
149 151 self.log_user()
150 152 group = user_util.create_user_group(user_groups_active=True)
151 153 group_name = group.users_group_name
152 154 response = self.app.get(
153 155 url(controller='home', action='user_autocomplete_data',
154 156 user_groups='true'),
155 157 headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200)
156 158 result = json.loads(response.body)
157 159 values = [suggestion['value'] for suggestion in result['suggestions']]
158 160 assert group_name in values
159 161
160 162 def test_result_is_limited_when_query_is_sent(self):
161 163 self.log_user()
162 164 fake_result = [
163 165 {
164 166 'first_name': 'John',
165 167 'value_display': 'hello{} (John Smith)'.format(i),
166 168 'icon_link': '/images/user14.png',
167 169 'value': 'hello{}'.format(i),
168 170 'last_name': 'Smith',
169 171 'username': 'hello{}'.format(i),
170 172 'id': i,
171 173 'value_type': u'user'
172 174 }
173 175 for i in range(10)
174 176 ]
175 177 users_patcher = patch.object(
176 178 RepoModel, 'get_users', return_value=fake_result)
177 179 groups_patcher = patch.object(
178 180 RepoModel, 'get_user_groups', return_value=fake_result)
179 181
180 182 query = 'hello'
181 183 with users_patcher as users_mock, groups_patcher as groups_mock:
182 184 response = self.app.get(
183 185 url(controller='home', action='user_autocomplete_data',
184 186 user_groups='true', query=query),
185 187 headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200)
186 188
187 189 result = json.loads(response.body)
188 190 users_mock.assert_called_once_with(
189 191 name_contains=query, only_active=True)
190 192 groups_mock.assert_called_once_with(
191 193 name_contains=query, only_active=True)
192 194 assert len(result['suggestions']) == 20
193 195
194 196
195 197 def assert_and_get_content(result):
196 198 repos = []
197 199 groups = []
198 200 commits = []
199 201 for data in result:
200 202 for data_item in data['children']:
201 203 assert data_item['id']
202 204 assert data_item['text']
203 205 assert data_item['url']
204 206 if data_item['type'] == 'repo':
205 207 repos.append(data_item)
206 208 elif data_item['type'] == 'group':
207 209 groups.append(data_item)
208 210 elif data_item['type'] == 'commit':
209 211 commits.append(data_item)
210 212 else:
211 213 raise Exception('invalid type %s' % data_item['type'])
212 214
213 215 return repos, groups, commits
214 216
215 217
216 218 class TestGotoSwitcherData(TestController):
217 219 required_repos_with_groups = [
218 220 'abc',
219 221 'abc-fork',
220 222 'forks/abcd',
221 223 'abcd',
222 224 'abcde',
223 225 'a/abc',
224 226 'aa/abc',
225 227 'aaa/abc',
226 228 'aaaa/abc',
227 229 'repos_abc/aaa/abc',
228 230 'abc_repos/abc',
229 231 'abc_repos/abcd',
230 232 'xxx/xyz',
231 233 'forked-abc/a/abc'
232 234 ]
233 235
234 236 @pytest.fixture(autouse=True, scope='class')
235 237 def prepare(self, request, pylonsapp):
236 238 for repo_and_group in self.required_repos_with_groups:
237 239 # create structure of groups and return the last group
238 240
239 241 repo_group = map_groups(repo_and_group)
240 242
241 243 RepoModel()._create_repo(
242 244 repo_and_group, 'hg', 'test-ac', TEST_USER_ADMIN_LOGIN,
243 245 repo_group=getattr(repo_group, 'group_id', None))
244 246
245 247 Session().commit()
246 248
247 249 request.addfinalizer(self.cleanup)
248 250
249 251 def cleanup(self):
250 252 # first delete all repos
251 253 for repo_and_groups in self.required_repos_with_groups:
252 254 repo = Repository.get_by_repo_name(repo_and_groups)
253 255 if repo:
254 256 RepoModel().delete(repo)
255 257 Session().commit()
256 258
257 259 # then delete all empty groups
258 260 for repo_and_groups in self.required_repos_with_groups:
259 261 if '/' in repo_and_groups:
260 262 r_group = repo_and_groups.rsplit('/', 1)[0]
261 263 repo_group = RepoGroup.get_by_group_name(r_group)
262 264 if not repo_group:
263 265 continue
264 266 parents = repo_group.parents
265 267 RepoGroupModel().delete(repo_group, force_delete=True)
266 268 Session().commit()
267 269
268 270 for el in reversed(parents):
269 271 RepoGroupModel().delete(el, force_delete=True)
270 272 Session().commit()
271 273
272 274 def test_returns_list_of_repos_and_groups(self):
273 275 self.log_user()
274 276
275 277 response = self.app.get(
276 278 url(controller='home', action='goto_switcher_data'),
277 279 headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200)
278 280 result = json.loads(response.body)['results']
279 281
280 282 repos, groups, commits = assert_and_get_content(result)
281 283
282 284 assert len(repos) == len(Repository.get_all())
283 285 assert len(groups) == len(RepoGroup.get_all())
284 286 assert len(commits) == 0
285 287
286 288 def test_returns_list_of_repos_and_groups_filtered(self):
287 289 self.log_user()
288 290
289 291 response = self.app.get(
290 292 url(controller='home', action='goto_switcher_data'),
291 293 headers={'X-REQUESTED-WITH': 'XMLHttpRequest', },
292 294 params={'query': 'abc'}, status=200)
293 295 result = json.loads(response.body)['results']
294 296
295 297 repos, groups, commits = assert_and_get_content(result)
296 298
297 299 assert len(repos) == 13
298 300 assert len(groups) == 5
299 301 assert len(commits) == 0
300 302
301 303 def test_returns_list_of_properly_sorted_and_filtered(self):
302 304 self.log_user()
303 305
304 306 response = self.app.get(
305 307 url(controller='home', action='goto_switcher_data'),
306 308 headers={'X-REQUESTED-WITH': 'XMLHttpRequest', },
307 309 params={'query': 'abc'}, status=200)
308 310 result = json.loads(response.body)['results']
309 311
310 312 repos, groups, commits = assert_and_get_content(result)
311 313
312 314 test_repos = [x['text'] for x in repos[:4]]
313 315 assert ['abc', 'abcd', 'a/abc', 'abcde'] == test_repos
314 316
315 317 test_groups = [x['text'] for x in groups[:4]]
316 318 assert ['abc_repos', 'repos_abc',
317 319 'forked-abc', 'forked-abc/a'] == test_groups
318 320
319 321
320 322 class TestRepoListData(TestController):
321 323 def test_returns_list_of_repos_and_groups(self, user_util):
322 324 self.log_user()
323 325
324 326 response = self.app.get(
325 327 url(controller='home', action='repo_list_data'),
326 328 headers={'X-REQUESTED-WITH': 'XMLHttpRequest', }, status=200)
327 329 result = json.loads(response.body)['results']
328 330
329 331 repos, groups, commits = assert_and_get_content(result)
330 332
331 333 assert len(repos) == len(Repository.get_all())
332 334 assert len(groups) == 0
333 335 assert len(commits) == 0
334 336
335 337 def test_returns_list_of_repos_and_groups_filtered(self):
336 338 self.log_user()
337 339
338 340 response = self.app.get(
339 341 url(controller='home', action='repo_list_data'),
340 342 headers={'X-REQUESTED-WITH': 'XMLHttpRequest', },
341 343 params={'query': 'vcs_test_git'}, status=200)
342 344 result = json.loads(response.body)['results']
343 345
344 346 repos, groups, commits = assert_and_get_content(result)
345 347
346 348 assert len(repos) == len(Repository.query().filter(
347 349 Repository.repo_name.ilike('%vcs_test_git%')).all())
348 350 assert len(groups) == 0
349 351 assert len(commits) == 0
350 352
351 353 def test_returns_list_of_repos_and_groups_filtered_with_type(self):
352 354 self.log_user()
353 355
354 356 response = self.app.get(
355 357 url(controller='home', action='repo_list_data'),
356 358 headers={'X-REQUESTED-WITH': 'XMLHttpRequest', },
357 359 params={'query': 'vcs_test_git', 'repo_type': 'git'}, status=200)
358 360 result = json.loads(response.body)['results']
359 361
360 362 repos, groups, commits = assert_and_get_content(result)
361 363
362 364 assert len(repos) == len(Repository.query().filter(
363 365 Repository.repo_name.ilike('%vcs_test_git%')).all())
364 366 assert len(groups) == 0
365 367 assert len(commits) == 0
366 368
367 369 def test_returns_list_of_repos_non_ascii_query(self):
368 370 self.log_user()
369 371 response = self.app.get(
370 372 url(controller='home', action='repo_list_data'),
371 373 headers={'X-REQUESTED-WITH': 'XMLHttpRequest', },
372 374 params={'query': 'ć_vcs_test_ą', 'repo_type': 'git'}, status=200)
373 375 result = json.loads(response.body)['results']
374 376
375 377 repos, groups, commits = assert_and_get_content(result)
376 378
377 379 assert len(repos) == 0
378 380 assert len(groups) == 0
379 381 assert len(commits) == 0
General Comments 0
You need to be logged in to leave comments. Login now