##// END OF EJS Templates
tests: Fixed ecpected results.
Martin Bornhold -
r462:e1f66798 default
parent child Browse files
Show More
@@ -1,522 +1,525 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 re
22 22 import os
23 23
24 24 import mock
25 25 import pytest
26 26
27 27 from rhodecode.controllers import summary
28 28 from rhodecode.lib import vcs
29 29 from rhodecode.lib import helpers as h
30 30 from rhodecode.lib.compat import OrderedDict
31 31 from rhodecode.lib.vcs.exceptions import RepositoryRequirementError
32 32 from rhodecode.model.db import Repository
33 33 from rhodecode.model.meta import Session
34 34 from rhodecode.model.repo import RepoModel
35 35 from rhodecode.model.scm import ScmModel
36 36 from rhodecode.tests import (
37 37 TestController, url, HG_REPO, assert_session_flash, TESTS_TMP_PATH)
38 38 from rhodecode.tests.fixture import Fixture
39 39 from rhodecode.tests.utils import AssertResponse
40 40
41 41
42 42 fixture = Fixture()
43 43
44 44
45 45 class TestSummaryController(TestController):
46 46 def test_index(self, backend):
47 47 self.log_user()
48 48 repo_id = backend.repo.repo_id
49 49 repo_name = backend.repo_name
50 50 with mock.patch('rhodecode.lib.helpers.is_svn_without_proxy',
51 51 return_value=False):
52 52 response = self.app.get(url('summary_home', repo_name=repo_name))
53 53
54 54 # repo type
55 55 response.mustcontain(
56 56 '<i class="icon-%s">' % (backend.alias, )
57 57 )
58 58 # public/private
59 59 response.mustcontain(
60 60 """<i class="icon-unlock-alt">"""
61 61 )
62 62
63 63 # clone url...
64 64 response.mustcontain(
65 65 'id="clone_url" readonly="readonly"'
66 66 ' value="http://test_admin@test.example.com:80/%s"' % (repo_name, ))
67 67 response.mustcontain(
68 68 'id="clone_url_id" readonly="readonly"'
69 69 ' value="http://test_admin@test.example.com:80/_%s"' % (repo_id, ))
70 70
71 71 def test_index_svn_without_proxy(self, backend_svn):
72 72 self.log_user()
73 73 repo_id = backend_svn.repo.repo_id
74 74 repo_name = backend_svn.repo_name
75 75 response = self.app.get(url('summary_home', repo_name=repo_name))
76 76 # clone url...
77 77 response.mustcontain(
78 78 'id="clone_url" disabled'
79 79 ' value="http://test_admin@test.example.com:80/%s"' % (repo_name, ))
80 80 response.mustcontain(
81 81 'id="clone_url_id" disabled'
82 82 ' value="http://test_admin@test.example.com:80/_%s"' % (repo_id, ))
83 83
84 84 def test_index_with_trailing_slash(self, autologin_user, backend):
85 85 repo_id = backend.repo.repo_id
86 86 repo_name = backend.repo_name
87 87 with mock.patch('rhodecode.lib.helpers.is_svn_without_proxy',
88 88 return_value=False):
89 89 response = self.app.get(
90 90 url('summary_home', repo_name=repo_name) + '/',
91 91 status=200)
92 92
93 93 # clone url...
94 94 response.mustcontain(
95 95 'id="clone_url" readonly="readonly"'
96 96 ' value="http://test_admin@test.example.com:80/%s"' % (repo_name, ))
97 97 response.mustcontain(
98 98 'id="clone_url_id" readonly="readonly"'
99 99 ' value="http://test_admin@test.example.com:80/_%s"' % (repo_id, ))
100 100
101 101 def test_index_by_id(self, backend):
102 102 self.log_user()
103 103 repo_id = backend.repo.repo_id
104 104 response = self.app.get(url(
105 105 'summary_home', repo_name='_%s' % (repo_id,)))
106 106
107 107 # repo type
108 108 response.mustcontain(
109 109 '<i class="icon-%s">' % (backend.alias, )
110 110 )
111 111 # public/private
112 112 response.mustcontain(
113 113 """<i class="icon-unlock-alt">"""
114 114 )
115 115
116 116 def test_index_by_repo_having_id_path_in_name_hg(self):
117 117 self.log_user()
118 118 fixture.create_repo(name='repo_1')
119 119 response = self.app.get(url('summary_home', repo_name='repo_1'))
120 120
121 121 try:
122 122 response.mustcontain("repo_1")
123 123 finally:
124 124 RepoModel().delete(Repository.get_by_repo_name('repo_1'))
125 125 Session().commit()
126 126
127 127 def test_index_with_anonymous_access_disabled(self):
128 128 with fixture.anon_access(False):
129 129 response = self.app.get(url('summary_home', repo_name=HG_REPO),
130 130 status=302)
131 131 assert 'login' in response.location
132 132
133 133 def _enable_stats(self, repo):
134 134 r = Repository.get_by_repo_name(repo)
135 135 r.enable_statistics = True
136 136 Session().add(r)
137 137 Session().commit()
138 138
139 139 expected_trending = {
140 140 'hg': {
141 141 "py": {"count": 68, "desc": ["Python"]},
142 142 "rst": {"count": 16, "desc": ["Rst"]},
143 143 "css": {"count": 2, "desc": ["Css"]},
144 144 "sh": {"count": 2, "desc": ["Bash"]},
145 145 "bat": {"count": 1, "desc": ["Batch"]},
146 146 "cfg": {"count": 1, "desc": ["Ini"]},
147 147 "html": {"count": 1, "desc": ["EvoqueHtml", "Html"]},
148 148 "ini": {"count": 1, "desc": ["Ini"]},
149 149 "js": {"count": 1, "desc": ["Javascript"]},
150 150 "makefile": {"count": 1, "desc": ["Makefile", "Makefile"]}
151 151 },
152 152 'git': {
153 153 "py": {"count": 68, "desc": ["Python"]},
154 154 "rst": {"count": 16, "desc": ["Rst"]},
155 155 "css": {"count": 2, "desc": ["Css"]},
156 156 "sh": {"count": 2, "desc": ["Bash"]},
157 157 "bat": {"count": 1, "desc": ["Batch"]},
158 158 "cfg": {"count": 1, "desc": ["Ini"]},
159 159 "html": {"count": 1, "desc": ["EvoqueHtml", "Html"]},
160 160 "ini": {"count": 1, "desc": ["Ini"]},
161 161 "js": {"count": 1, "desc": ["Javascript"]},
162 162 "makefile": {"count": 1, "desc": ["Makefile", "Makefile"]}
163 163 },
164 164 'svn': {
165 165 "py": {"count": 75, "desc": ["Python"]},
166 166 "rst": {"count": 16, "desc": ["Rst"]},
167 167 "html": {"count": 11, "desc": ["EvoqueHtml", "Html"]},
168 168 "css": {"count": 2, "desc": ["Css"]},
169 169 "bat": {"count": 1, "desc": ["Batch"]},
170 170 "cfg": {"count": 1, "desc": ["Ini"]},
171 171 "ini": {"count": 1, "desc": ["Ini"]},
172 172 "js": {"count": 1, "desc": ["Javascript"]},
173 173 "makefile": {"count": 1, "desc": ["Makefile", "Makefile"]},
174 174 "sh": {"count": 1, "desc": ["Bash"]}
175 175 },
176 176 }
177 177
178 178 def test_repo_stats(self, backend, xhr_header):
179 179 self.log_user()
180 180 response = self.app.get(
181 181 url('repo_stats',
182 182 repo_name=backend.repo_name, commit_id='tip'),
183 183 extra_environ=xhr_header,
184 184 status=200)
185 185 assert re.match(r'6[\d\.]+ KiB', response.json['size'])
186 186
187 187 def test_repo_stats_code_stats_enabled(self, backend, xhr_header):
188 188 self.log_user()
189 189 repo_name = backend.repo_name
190 190
191 191 # codes stats
192 192 self._enable_stats(repo_name)
193 193 ScmModel().mark_for_invalidation(repo_name)
194 194
195 195 response = self.app.get(
196 196 url('repo_stats',
197 197 repo_name=backend.repo_name, commit_id='tip'),
198 198 extra_environ=xhr_header,
199 199 status=200)
200 200
201 201 expected_data = self.expected_trending[backend.alias]
202 202 returned_stats = response.json['code_stats']
203 203 for k, v in expected_data.items():
204 204 assert v == returned_stats[k]
205 205
206 206 def test_repo_refs_data(self, backend):
207 207 response = self.app.get(
208 208 url('repo_refs_data', repo_name=backend.repo_name),
209 209 status=200)
210 210
211 211 # Ensure that there is the correct amount of items in the result
212 212 repo = backend.repo.scm_instance()
213 213 data = response.json['results']
214 214 items = sum(len(section['children']) for section in data)
215 215 repo_refs = len(repo.branches) + len(repo.tags) + len(repo.bookmarks)
216 216 assert items == repo_refs
217 217
218 218 def test_index_shows_missing_requirements_message(
219 219 self, backend, autologin_user):
220 220 repo_name = backend.repo_name
221 221 scm_patcher = mock.patch.object(
222 222 Repository, 'scm_instance', side_effect=RepositoryRequirementError)
223 223
224 224 with scm_patcher:
225 225 response = self.app.get(url('summary_home', repo_name=repo_name))
226 226 assert_response = AssertResponse(response)
227 227 assert_response.element_contains(
228 228 '.main .alert-warning strong', 'Missing requirements')
229 229 assert_response.element_contains(
230 230 '.main .alert-warning',
231 231 'These commits cannot be displayed, because this repository'
232 232 ' uses the Mercurial largefiles extension, which was not enabled.')
233 233
234 234 def test_missing_requirements_page_does_not_contains_switch_to(
235 235 self, backend):
236 236 self.log_user()
237 237 repo_name = backend.repo_name
238 238 scm_patcher = mock.patch.object(
239 239 Repository, 'scm_instance', side_effect=RepositoryRequirementError)
240 240
241 241 with scm_patcher:
242 242 response = self.app.get(url('summary_home', repo_name=repo_name))
243 243 response.mustcontain(no='Switch To')
244 244
245 245
246 246 @pytest.mark.usefixtures('pylonsapp')
247 247 class TestSwitcherReferenceData:
248 248
249 249 def test_creates_reference_urls_based_on_name(self):
250 250 references = {
251 251 'name': 'commit_id',
252 252 }
253 253 controller = summary.SummaryController()
254 254 is_svn = False
255 255 result = controller._switcher_reference_data(
256 256 'repo_name', references, is_svn)
257 257 expected_url = h.url(
258 258 'files_home', repo_name='repo_name', revision='name',
259 259 at='name')
260 260 assert result[0]['files_url'] == expected_url
261 261
262 262 def test_urls_contain_commit_id_if_slash_in_name(self):
263 263 references = {
264 264 'name/with/slash': 'commit_id',
265 265 }
266 266 controller = summary.SummaryController()
267 267 is_svn = False
268 268 result = controller._switcher_reference_data(
269 269 'repo_name', references, is_svn)
270 270 expected_url = h.url(
271 271 'files_home', repo_name='repo_name', revision='commit_id',
272 272 at='name/with/slash')
273 273 assert result[0]['files_url'] == expected_url
274 274
275 275 def test_adds_reference_to_path_for_svn(self):
276 276 references = {
277 277 'name/with/slash': 'commit_id',
278 278 }
279 279 controller = summary.SummaryController()
280 280 is_svn = True
281 281 result = controller._switcher_reference_data(
282 282 'repo_name', references, is_svn)
283 283 expected_url = h.url(
284 284 'files_home', repo_name='repo_name', f_path='name/with/slash',
285 285 revision='commit_id', at='name/with/slash')
286 286 assert result[0]['files_url'] == expected_url
287 287
288 288
289 289 @pytest.mark.usefixtures('pylonsapp')
290 290 class TestCreateReferenceData:
291 291
292 292 @pytest.fixture
293 293 def example_refs(self):
294 294 section_1_refs = OrderedDict((('a', 'a_id'), ('b', 'b_id')))
295 295 example_refs = [
296 296 ('section_1', section_1_refs, 't1'),
297 297 ('section_2', {'c': 'c_id'}, 't2'),
298 298 ]
299 299 return example_refs
300 300
301 301 def test_generates_refs_based_on_commit_ids(self, example_refs):
302 302 repo = mock.Mock()
303 303 repo.name = 'test-repo'
304 304 repo.alias = 'git'
305 305 full_repo_name = 'pytest-repo-group/' + repo.name
306 306 controller = summary.SummaryController()
307 307
308 308 result = controller._create_reference_data(
309 309 repo, full_repo_name, example_refs)
310 310
311 expected_files_url = '/{}/files/'.format(full_repo_name)
311 312 expected_result = [
312 313 {
313 314 'children': [
314 315 {
315 316 'id': 'a', 'raw_id': 'a_id', 'text': 'a', 'type': 't1',
316 'files_url': '/test-repo/files/a/?at=a'
317 'files_url': expected_files_url + 'a/?at=a',
317 318 },
318 319 {
319 320 'id': 'b', 'raw_id': 'b_id', 'text': 'b', 'type': 't1',
320 'files_url': '/test-repo/files/b/?at=b'
321 'files_url': expected_files_url + 'b/?at=b',
321 322 }
322 323 ],
323 324 'text': 'section_1'
324 325 },
325 326 {
326 327 'children': [
327 328 {
328 329 'id': 'c', 'raw_id': 'c_id', 'text': 'c', 'type': 't2',
329 'files_url': '/test-repo/files/c/?at=c'
330 'files_url': expected_files_url + 'c/?at=c',
330 331 }
331 332 ],
332 333 'text': 'section_2'
333 334 }]
334 335 assert result == expected_result
335 336
336 337 def test_generates_refs_with_path_for_svn(self, example_refs):
337 338 repo = mock.Mock()
338 339 repo.name = 'test-repo'
339 340 repo.alias = 'svn'
340 341 full_repo_name = 'pytest-repo-group/' + repo.name
341 342 controller = summary.SummaryController()
342 343 result = controller._create_reference_data(
343 344 repo, full_repo_name, example_refs)
344 345
346 expected_files_url = '/{}/files/'.format(full_repo_name)
345 347 expected_result = [
346 348 {
347 349 'children': [
348 350 {
349 351 'id': 'a@a_id', 'raw_id': 'a_id',
350 352 'text': 'a', 'type': 't1',
351 'files_url': '/test-repo/files/a_id/a?at=a'
353 'files_url': expected_files_url + 'a_id/a?at=a',
352 354 },
353 355 {
354 356 'id': 'b@b_id', 'raw_id': 'b_id',
355 357 'text': 'b', 'type': 't1',
356 'files_url': '/test-repo/files/b_id/b?at=b'
358 'files_url': expected_files_url + 'b_id/b?at=b',
357 359 }
358 360 ],
359 361 'text': 'section_1'
360 362 },
361 363 {
362 364 'children': [
363 365 {
364 366 'id': 'c@c_id', 'raw_id': 'c_id',
365 367 'text': 'c', 'type': 't2',
366 'files_url': '/test-repo/files/c_id/c?at=c'
368 'files_url': expected_files_url + 'c_id/c?at=c',
367 369 }
368 370 ],
369 371 'text': 'section_2'
370 372 }
371 373 ]
372 374 assert result == expected_result
373 375
374 376
375 377 @pytest.mark.usefixtures("app")
376 378 class TestRepoLocation:
377 379
378 380 @pytest.mark.parametrize("suffix", [u'', u'Δ…Δ™Ε‚'], ids=['', 'non-ascii'])
379 381 def test_manual_delete(self, autologin_user, backend, suffix, csrf_token):
380 382 repo = backend.create_repo(name_suffix=suffix)
381 383 repo_name = repo.repo_name
382 384
383 385 # delete from file system
384 386 RepoModel()._delete_filesystem_repo(repo)
385 387
386 388 # test if the repo is still in the database
387 389 new_repo = RepoModel().get_by_repo_name(repo_name)
388 390 assert new_repo.repo_name == repo_name
389 391
390 392 # check if repo is not in the filesystem
391 393 assert not repo_on_filesystem(repo_name)
392 394 self.assert_repo_not_found_redirect(repo_name)
393 395
394 396 def assert_repo_not_found_redirect(self, repo_name):
395 397 # run the check page that triggers the other flash message
396 398 response = self.app.get(url('repo_check_home', repo_name=repo_name))
397 399 assert_session_flash(
398 400 response, 'The repository at %s cannot be located.' % repo_name)
399 401
400 402
401 403 def repo_on_filesystem(repo_name):
402 404 try:
403 405 vcs.get_repo(os.path.join(TESTS_TMP_PATH, repo_name))
404 406 return True
405 407 except Exception:
406 408 return False
407 409
408 410
409 411 class TestCreateFilesUrl(object):
410 412 def test_creates_non_svn_url(self):
411 413 controller = summary.SummaryController()
412 414 repo = mock.Mock()
413 415 repo.name = 'abcde'
414 416 full_repo_name = 'test-repo-group/' + repo.name
415 417 ref_name = 'branch1'
416 418 raw_id = 'deadbeef0123456789'
417 419 is_svn = False
418 420
419 421 with mock.patch.object(summary.h, 'url') as url_mock:
420 422 result = controller._create_files_url(
421 423 repo, full_repo_name, ref_name, raw_id, is_svn)
422 424 url_mock.assert_called_once_with(
423 425 'files_home', repo_name=full_repo_name, f_path='',
424 426 revision=ref_name, at=ref_name)
425 427 assert result == url_mock.return_value
426 428
427 429 def test_creates_svn_url(self):
428 430 controller = summary.SummaryController()
429 431 repo = mock.Mock()
430 432 repo.name = 'abcde'
431 433 full_repo_name = 'test-repo-group/' + repo.name
432 434 ref_name = 'branch1'
433 435 raw_id = 'deadbeef0123456789'
434 436 is_svn = True
435 437
436 438 with mock.patch.object(summary.h, 'url') as url_mock:
437 439 result = controller._create_files_url(
438 440 repo, full_repo_name, ref_name, raw_id, is_svn)
439 441 url_mock.assert_called_once_with(
440 442 'files_home', repo_name=full_repo_name, f_path=ref_name,
441 443 revision=raw_id, at=ref_name)
442 444 assert result == url_mock.return_value
443 445
444 446 def test_name_has_slashes(self):
445 447 controller = summary.SummaryController()
446 448 repo = mock.Mock()
447 449 repo.name = 'abcde'
448 450 full_repo_name = 'test-repo-group/' + repo.name
449 451 ref_name = 'branch1/branch2'
450 452 raw_id = 'deadbeef0123456789'
451 453 is_svn = False
452 454
453 455 with mock.patch.object(summary.h, 'url') as url_mock:
454 456 result = controller._create_files_url(
455 457 repo, full_repo_name, ref_name, raw_id, is_svn)
456 458 url_mock.assert_called_once_with(
457 459 'files_home', repo_name=full_repo_name, f_path='', revision=raw_id,
458 460 at=ref_name)
459 461 assert result == url_mock.return_value
460 462
461 463
462 464 class TestReferenceItems(object):
463 465 repo = mock.Mock()
464 466 repo.name = 'pytest-repo'
465 467 repo_full_name = 'pytest-repo-group/' + repo.name
466 468 ref_type = 'branch'
467 469 fake_url = '/abcde/'
468 470
469 471 @staticmethod
470 472 def _format_function(name, id_):
471 473 return 'format_function_{}_{}'.format(name, id_)
472 474
473 475 def test_creates_required_amount_of_items(self):
474 476 amount = 100
475 477 refs = {
476 478 'ref{}'.format(i): '{0:040d}'.format(i)
477 479 for i in range(amount)
478 480 }
479 481
480 482 controller = summary.SummaryController()
481 483
482 484 url_patcher = mock.patch.object(
483 485 controller, '_create_files_url')
484 486 svn_patcher = mock.patch.object(
485 487 summary.h, 'is_svn', return_value=False)
486 488
487 489 with url_patcher as url_mock, svn_patcher:
488 490 result = controller._create_reference_items(
489 491 self.repo, self.repo_full_name, refs, self.ref_type,
490 492 self._format_function)
491 493 assert len(result) == amount
492 494 assert url_mock.call_count == amount
493 495
494 496 def test_single_item_details(self):
495 497 ref_name = 'ref1'
496 498 ref_id = 'deadbeef'
497 499 refs = {
498 500 ref_name: ref_id
499 501 }
500 502
501 503 controller = summary.SummaryController()
502 504 url_patcher = mock.patch.object(
503 505 controller, '_create_files_url', return_value=self.fake_url)
504 506 svn_patcher = mock.patch.object(
505 507 summary.h, 'is_svn', return_value=False)
506 508
507 509 with url_patcher as url_mock, svn_patcher:
508 510 result = controller._create_reference_items(
509 511 self.repo, self.repo_full_name, refs, self.ref_type,
510 512 self._format_function)
511 513
512 url_mock.assert_called_once_with(self.repo, ref_name, ref_id, False)
514 url_mock.assert_called_once_with(
515 self.repo, self.repo_full_name, ref_name, ref_id, False)
513 516 expected_result = [
514 517 {
515 518 'text': ref_name,
516 519 'id': self._format_function(ref_name, ref_id),
517 520 'raw_id': ref_id,
518 521 'type': self.ref_type,
519 522 'files_url': self.fake_url
520 523 }
521 524 ]
522 525 assert result == expected_result
General Comments 0
You need to be logged in to leave comments. Login now