##// END OF EJS Templates
tests: fix disable_vcs tests
dan -
r192:f246f4bc default
parent child Browse files
Show More
@@ -1,76 +1,76 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2015-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 Disable VCS pages when VCS Server is not available
23 23 """
24 24
25 25 import logging
26 26 import re
27 27 from pyramid.httpexceptions import HTTPBadGateway
28 28
29 29 log = logging.getLogger(__name__)
30 30
31 31
32 32 class VCSServerUnavailable(HTTPBadGateway):
33 33 """ HTTP Exception class for when VCS Server is unavailable """
34 34 code = 502
35 35 title = 'VCS Server Required'
36 36 explanation = 'A VCS Server is required for this action. There is currently no VCS Server configured.'
37 37
38 38 class DisableVCSPagesWrapper(object):
39 39 """
40 40 Pyramid view wrapper to disable all pages that require VCS Server to be
41 41 running, avoiding that errors explode to the user.
42 42
43 43 This Wrapper should be enabled only in case VCS Server is not available
44 44 for the instance.
45 45 """
46 46
47 47 VCS_NOT_REQUIRED = [
48 48 '^/$',
49 49 ('/_admin(?!/settings/mapping)(?!/my_account/repos)'
50 50 '(?!/create_repository)(?!/gists)(?!/notifications/)'
51 51 ),
52 52 ]
53 53 _REGEX_VCS_NOT_REQUIRED = [re.compile(path) for path in VCS_NOT_REQUIRED]
54 54
55 55 def _check_vcs_requirement(self, path_info):
56 56 """
57 57 Tries to match the current path to one of the safe URLs to be rendered.
58 58 Displays an error message in case
59 59 """
60 60 for regex in self._REGEX_VCS_NOT_REQUIRED:
61 61 safe_url = regex.match(path_info)
62 62 if safe_url:
63 63 return True
64 64
65 65 # Url is not safe to be rendered without VCS Server
66 66 log.debug('accessing: `%s` with VCS Server disabled', path_info)
67 67 return False
68 68
69 69 def __init__(self, handler):
70 70 self.handler = handler
71 71
72 72 def __call__(self, context, request):
73 if not self._check_vcs_requirement(request.environ['PATH_INFO']):
73 if not self._check_vcs_requirement(request.path):
74 74 raise VCSServerUnavailable('VCS Server is not available')
75 75
76 76 return self.handler(context, request)
@@ -1,54 +1,53 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 pytest
22 from rhodecode.lib.middleware.disable_vcs import DisableVCSPagesWrapper
22 from pyramid.response import Response
23 from pyramid.testing import DummyRequest
24 from rhodecode.lib.middleware.disable_vcs import (
25 DisableVCSPagesWrapper, VCSServerUnavailable)
23 26
24 27
25 @pytest.mark.parametrize('url, expected_url', [
26 ('/', '/'),
27 ('/_admin/settings', '/_admin/settings'),
28 ('/_admin/i_am_fine', '/_admin/i_am_fine'),
29 ('/_admin/settings/mappings', '/error/vcs_unavailable'),
30 ('/_admin/my_account/repos', '/error/vcs_unavailable'),
31 ('/_admin/create_repository', '/error/vcs_unavailable'),
32 ('/_admin/gists/1', '/error/vcs_unavailable'),
33 ('/_admin/notifications/1', '/error/vcs_unavailable'),
28 @pytest.mark.parametrize('url, should_raise', [
29 ('/', False),
30 ('/_admin/settings', False),
31 ('/_admin/i_am_fine', False),
32 ('/_admin/settings/mappings', True),
33 ('/_admin/my_account/repos', True),
34 ('/_admin/create_repository', True),
35 ('/_admin/gists/1', True),
36 ('/_admin/notifications/1', True),
34 37 ])
35 def test_vcs_disabled(url, expected_url):
36 app = DisableVCSPagesWrapper(app=SimpleApp())
37 assert expected_url == app(get_environ(url), None)
38
38 def test_vcs_disabled(url, should_raise):
39 wrapped_view = DisableVCSPagesWrapper(pyramid_view)
40 request = DummyRequest(path=url)
39 41
40 def get_environ(url):
41 """Construct a minimum WSGI environ based on the URL."""
42 environ = {
43 'PATH_INFO': url,
44 }
45 return environ
46
42 if should_raise:
43 with pytest.raises(VCSServerUnavailable):
44 response = wrapped_view(None, request)
45 else:
46 response = wrapped_view(None, request)
47 assert response.status_int == 200
47 48
48 class SimpleApp(object):
49 def pyramid_view(context, request):
49 50 """
50 A mock app to be used in the wrapper that returns the modified URL
51 from the middleware
51 A mock pyramid view to be used in the wrapper
52 52 """
53 def __call__(self, environ, start_response):
54 return environ['PATH_INFO']
53 return Response('success')
General Comments 0
You need to be logged in to leave comments. Login now