Show More
@@ -1,151 +1,97 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-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 | import codecs | |
|
23 | 22 | import mock |
|
23 | import pytest | |
|
24 | 24 | import re |
|
25 | import shutil | |
|
26 | import tempfile | |
|
27 | 25 | |
|
28 | 26 | from pyramid import testing |
|
29 | 27 | |
|
30 |
from rhodecode.svn_support import |
|
|
28 | from rhodecode.svn_support import utils | |
|
31 | 29 | |
|
32 | 30 | |
|
33 | 31 | class TestModDavSvnConfig(object): |
|
34 | 32 | |
|
35 | 33 | @classmethod |
|
36 | 34 | def setup_class(cls): |
|
37 | 35 | # Make mako renderer available in tests. |
|
38 | 36 | config = testing.setUp() |
|
39 | 37 | config.include('pyramid_mako') |
|
40 | 38 | |
|
41 | # Temporary directory holding the generated config files. | |
|
42 | cls.tempdir = tempfile.mkdtemp(suffix='pytest-mod-dav-svn') | |
|
43 | ||
|
44 | 39 | cls.location_root = u'/location/root/ç¡Àâ' |
|
45 | 40 | cls.parent_path_root = u'/parent/path/ç¡Àâ' |
|
46 |
cls. |
|
|
41 | cls.realm = u'Dummy Realm (Àâüç¡)' | |
|
47 | 42 | |
|
48 | 43 | @classmethod |
|
49 | def teardown_class(cls): | |
|
50 | testing.tearDown() | |
|
51 | shutil.rmtree(cls.tempdir, ignore_errors=True) | |
|
52 | ||
|
53 | @classmethod | |
|
54 | def get_settings(cls): | |
|
55 | config_file_path = tempfile.mkstemp( | |
|
56 | suffix='mod-dav-svn.conf', dir=cls.tempdir)[1] | |
|
57 | return { | |
|
58 | config_keys.config_file_path: config_file_path, | |
|
59 | config_keys.location_root: cls.location_root, | |
|
60 | config_keys.list_parent_path: True, | |
|
61 | } | |
|
62 | ||
|
63 | @classmethod | |
|
64 | def get_repo_groups(cls, count=1): | |
|
44 | def get_repo_group_mocks(cls, count=1): | |
|
65 | 45 | repo_groups = [] |
|
66 | 46 | for num in range(0, count): |
|
67 | 47 | full_path = u'/path/to/RepâGrâúp-°¡ {}'.format(num) |
|
68 | 48 | repo_group_mock = mock.MagicMock() |
|
69 | 49 | repo_group_mock.full_path = full_path |
|
70 | 50 | repo_group_mock.full_path_splitted = full_path.split('/') |
|
71 | 51 | repo_groups.append(repo_group_mock) |
|
72 | 52 | return repo_groups |
|
73 | 53 | |
|
74 | 54 | def assert_root_location_directive(self, config): |
|
75 | 55 | pattern = u'<Location "{location}">'.format( |
|
76 | 56 | location=self.location_root) |
|
77 | 57 | assert len(re.findall(pattern, config)) == 1 |
|
78 | 58 | |
|
79 | 59 | def assert_group_location_directive(self, config, group_path): |
|
80 | 60 | pattern = u'<Location "{location}{group_path}">'.format( |
|
81 | 61 | location=self.location_root, group_path=group_path) |
|
82 | 62 | assert len(re.findall(pattern, config)) == 1 |
|
83 | 63 | |
|
84 | @mock.patch('rhodecode.svn_support.utils.get_rhodecode_realm') | |
|
85 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') | |
|
86 | def test_generate_mod_dav_svn_config(self, RepoGroupMock, GetRealmMock): | |
|
87 | # Setup mock objects. | |
|
88 | GetRealmMock.return_value = self._dummy_realm | |
|
89 | num_groups = 3 | |
|
90 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups( | |
|
91 | count=num_groups) | |
|
92 | ||
|
93 | # Execute the method under test. | |
|
94 | settings = self.get_settings() | |
|
95 | utils.generate_mod_dav_svn_config( | |
|
96 | settings=settings, parent_path_root=self.parent_path_root) | |
|
97 | ||
|
98 | # Read generated file. | |
|
99 | path = settings[config_keys.config_file_path] | |
|
100 | with codecs.open(path, 'r', encoding='utf-8') as f: | |
|
101 | content = f.read() | |
|
102 | ||
|
64 | def test_render_mod_dav_svn_config(self): | |
|
65 | repo_groups = self.get_repo_group_mocks(count=10) | |
|
66 | generated_config = utils._render_mod_dav_svn_config( | |
|
67 | parent_path_root=self.parent_path_root, | |
|
68 | list_parent_path=True, | |
|
69 | location_root=self.location_root, | |
|
70 | repo_groups=repo_groups, | |
|
71 | realm=self.realm | |
|
72 | ) | |
|
103 | 73 | # Assert that one location directive exists for each repository group. |
|
104 |
for group in |
|
|
105 |
self.assert_group_location_directive( |
|
|
74 | for group in repo_groups: | |
|
75 | self.assert_group_location_directive( | |
|
76 | generated_config, group.full_path) | |
|
106 | 77 | |
|
107 | 78 | # Assert that the root location directive exists. |
|
108 |
self.assert_root_location_directive( |
|
|
109 | ||
|
110 | @mock.patch('rhodecode.svn_support.utils.get_rhodecode_realm') | |
|
111 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') | |
|
112 | def test_list_parent_path_on(self, RepoGroupMock, GetRealmMock): | |
|
113 | # Setup mock objects. | |
|
114 | GetRealmMock.return_value = self._dummy_realm | |
|
115 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups() | |
|
116 | ||
|
117 | # Execute the method under test. | |
|
118 | settings = self.get_settings() | |
|
119 | settings[config_keys.list_parent_path] = True | |
|
120 | utils.generate_mod_dav_svn_config( | |
|
121 | settings=settings, parent_path_root=self.parent_path_root) | |
|
122 | ||
|
123 | # Read generated file. | |
|
124 | path = settings[config_keys.config_file_path] | |
|
125 | with codecs.open(path, 'r', encoding='utf-8') as f: | |
|
126 | content = f.read() | |
|
79 | self.assert_root_location_directive(generated_config) | |
|
127 | 80 | |
|
128 | # Make assertions. | |
|
129 | assert not re.search('SVNListParentPath\s+Off', content) | |
|
130 | assert re.search('SVNListParentPath\s+On', content) | |
|
131 | ||
|
132 | @mock.patch('rhodecode.svn_support.utils.get_rhodecode_realm') | |
|
133 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') | |
|
134 | def test_list_parent_path_off(self, RepoGroupMock, GetRealmMock): | |
|
135 | # Setup mock objects. | |
|
136 | GetRealmMock.return_value = self._dummy_realm | |
|
137 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups() | |
|
81 | @pytest.mark.parametrize('list_parent_path', [True, False]) | |
|
82 | def test_list_parent_path(self, list_parent_path): | |
|
83 | generated_config = utils._render_mod_dav_svn_config( | |
|
84 | parent_path_root=self.parent_path_root, | |
|
85 | list_parent_path=list_parent_path, | |
|
86 | location_root=self.location_root, | |
|
87 | repo_groups=self.get_repo_group_mocks(count=10), | |
|
88 | realm=self.realm | |
|
89 | ) | |
|
138 | 90 | |
|
139 | # Execute the method under test. | |
|
140 | settings = self.get_settings() | |
|
141 | settings[config_keys.list_parent_path] = False | |
|
142 | utils.generate_mod_dav_svn_config( | |
|
143 | settings=settings, parent_path_root=self.parent_path_root) | |
|
144 | ||
|
145 | # Read generated file. | |
|
146 | with open(settings[config_keys.config_file_path], 'r') as file_: | |
|
147 | content = file_.read() | |
|
148 | ||
|
149 | # Make assertions. | |
|
150 | assert re.search('SVNListParentPath\s+Off', content) | |
|
151 | assert not re.search('SVNListParentPath\s+On', content) | |
|
91 | # Assert that correct configuration directive is present. | |
|
92 | if list_parent_path: | |
|
93 | assert not re.search('SVNListParentPath\s+Off', generated_config) | |
|
94 | assert re.search('SVNListParentPath\s+On', generated_config) | |
|
95 | else: | |
|
96 | assert re.search('SVNListParentPath\s+Off', generated_config) | |
|
97 | assert not re.search('SVNListParentPath\s+On', generated_config) |
@@ -1,86 +1,87 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-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 codecs |
|
22 | 22 | import logging |
|
23 | 23 | import os |
|
24 | 24 | from pyramid.renderers import render |
|
25 | 25 | |
|
26 | 26 | from rhodecode.events import trigger |
|
27 | 27 | from rhodecode.lib.utils import get_rhodecode_realm, get_rhodecode_base_path |
|
28 | 28 | from rhodecode.model.db import RepoGroup |
|
29 | 29 | |
|
30 | 30 | from . import config_keys |
|
31 | 31 | from .events import ModDavSvnConfigChange |
|
32 | 32 | |
|
33 | 33 | |
|
34 | 34 | log = logging.getLogger(__name__) |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | def generate_mod_dav_svn_config(registry): |
|
38 | 38 | """ |
|
39 | 39 | Generate the configuration file for use with subversion's mod_dav_svn |
|
40 | 40 | module. The configuration has to contain a <Location> block for each |
|
41 | 41 | available repository group because the mod_dav_svn module does not support |
|
42 | 42 | repositories organized in sub folders. |
|
43 | 43 | """ |
|
44 | 44 | settings = registry.settings |
|
45 | 45 | config = _render_mod_dav_svn_config( |
|
46 | 46 | parent_path_root=get_rhodecode_base_path(), |
|
47 | 47 | list_parent_path=settings[config_keys.list_parent_path], |
|
48 | 48 | location_root=settings[config_keys.location_root], |
|
49 |
repo_groups=RepoGroup.get_all_repo_groups() |
|
|
49 | repo_groups=RepoGroup.get_all_repo_groups(), | |
|
50 | realm=get_rhodecode_realm()) | |
|
50 | 51 | _write_mod_dav_svn_config(config, settings[config_keys.config_file_path]) |
|
51 | 52 | |
|
52 | 53 | # Trigger an event on mod dav svn configuration change. |
|
53 | 54 | trigger(ModDavSvnConfigChange(), registry) |
|
54 | 55 | |
|
55 | 56 | |
|
56 | 57 | def _render_mod_dav_svn_config( |
|
57 | parent_path_root, list_parent_path, location_root, repo_groups): | |
|
58 | parent_path_root, list_parent_path, location_root, repo_groups, realm): | |
|
58 | 59 | """ |
|
59 | 60 | Render mod_dav_svn configuration to string. |
|
60 | 61 | """ |
|
61 | 62 | repo_group_paths = [] |
|
62 | 63 | for repo_group in repo_groups: |
|
63 | 64 | group_path = repo_group.full_path_splitted |
|
64 | 65 | location = os.path.join(location_root, *group_path) |
|
65 | 66 | parent_path = os.path.join(parent_path_root, *group_path) |
|
66 | 67 | repo_group_paths.append((location, parent_path)) |
|
67 | 68 | |
|
68 | 69 | context = { |
|
69 | 70 | 'location_root': location_root, |
|
70 | 71 | 'parent_path_root': parent_path_root, |
|
71 | 72 | 'repo_group_paths': repo_group_paths, |
|
72 | 73 | 'svn_list_parent_path': list_parent_path, |
|
73 |
'rhodecode_realm': |
|
|
74 | 'rhodecode_realm': realm, | |
|
74 | 75 | } |
|
75 | 76 | |
|
76 | 77 | # Render the configuration template to string. |
|
77 | 78 | template = 'rhodecode:svn_support/templates/mod-dav-svn.conf.mako' |
|
78 | 79 | return render(template, context) |
|
79 | 80 | |
|
80 | 81 | |
|
81 | 82 | def _write_mod_dav_svn_config(config, filepath): |
|
82 | 83 | """ |
|
83 | 84 | Write mod_dav_svn config to file. |
|
84 | 85 | """ |
|
85 | 86 | with codecs.open(filepath, 'w', encoding='utf-8') as f: |
|
86 | 87 | f.write(config) |
General Comments 0
You need to be logged in to leave comments.
Login now