##// END OF EJS Templates
svn-support: Fix tests for subversion support....
Martin Bornhold -
r1021:d36782c6 default
parent child Browse files
Show More
@@ -1,151 +1,97 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2016-2016 RhodeCode GmbH
3 # Copyright (C) 2016-2016 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21
21
22 import codecs
23 import mock
22 import mock
23 import pytest
24 import re
24 import re
25 import shutil
26 import tempfile
27
25
28 from pyramid import testing
26 from pyramid import testing
29
27
30 from rhodecode.svn_support import config_keys, utils
28 from rhodecode.svn_support import utils
31
29
32
30
33 class TestModDavSvnConfig(object):
31 class TestModDavSvnConfig(object):
34
32
35 @classmethod
33 @classmethod
36 def setup_class(cls):
34 def setup_class(cls):
37 # Make mako renderer available in tests.
35 # Make mako renderer available in tests.
38 config = testing.setUp()
36 config = testing.setUp()
39 config.include('pyramid_mako')
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 cls.location_root = u'/location/root/ç¡Àâ'
39 cls.location_root = u'/location/root/ç¡Àâ'
45 cls.parent_path_root = u'/parent/path/ç¡Àâ'
40 cls.parent_path_root = u'/parent/path/ç¡Àâ'
46 cls._dummy_realm = u'Dummy Realm (Àâüç¡)'
41 cls.realm = u'Dummy Realm (Àâüç¡)'
47
42
48 @classmethod
43 @classmethod
49 def teardown_class(cls):
44 def get_repo_group_mocks(cls, count=1):
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):
65 repo_groups = []
45 repo_groups = []
66 for num in range(0, count):
46 for num in range(0, count):
67 full_path = u'/path/to/RepâGrâúp-°¡ {}'.format(num)
47 full_path = u'/path/to/RepâGrâúp-°¡ {}'.format(num)
68 repo_group_mock = mock.MagicMock()
48 repo_group_mock = mock.MagicMock()
69 repo_group_mock.full_path = full_path
49 repo_group_mock.full_path = full_path
70 repo_group_mock.full_path_splitted = full_path.split('/')
50 repo_group_mock.full_path_splitted = full_path.split('/')
71 repo_groups.append(repo_group_mock)
51 repo_groups.append(repo_group_mock)
72 return repo_groups
52 return repo_groups
73
53
74 def assert_root_location_directive(self, config):
54 def assert_root_location_directive(self, config):
75 pattern = u'<Location "{location}">'.format(
55 pattern = u'<Location "{location}">'.format(
76 location=self.location_root)
56 location=self.location_root)
77 assert len(re.findall(pattern, config)) == 1
57 assert len(re.findall(pattern, config)) == 1
78
58
79 def assert_group_location_directive(self, config, group_path):
59 def assert_group_location_directive(self, config, group_path):
80 pattern = u'<Location "{location}{group_path}">'.format(
60 pattern = u'<Location "{location}{group_path}">'.format(
81 location=self.location_root, group_path=group_path)
61 location=self.location_root, group_path=group_path)
82 assert len(re.findall(pattern, config)) == 1
62 assert len(re.findall(pattern, config)) == 1
83
63
84 @mock.patch('rhodecode.svn_support.utils.get_rhodecode_realm')
64 def test_render_mod_dav_svn_config(self):
85 @mock.patch('rhodecode.svn_support.utils.RepoGroup')
65 repo_groups = self.get_repo_group_mocks(count=10)
86 def test_generate_mod_dav_svn_config(self, RepoGroupMock, GetRealmMock):
66 generated_config = utils._render_mod_dav_svn_config(
87 # Setup mock objects.
67 parent_path_root=self.parent_path_root,
88 GetRealmMock.return_value = self._dummy_realm
68 list_parent_path=True,
89 num_groups = 3
69 location_root=self.location_root,
90 RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups(
70 repo_groups=repo_groups,
91 count=num_groups)
71 realm=self.realm
92
72 )
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
103 # Assert that one location directive exists for each repository group.
73 # Assert that one location directive exists for each repository group.
104 for group in self.get_repo_groups(count=num_groups):
74 for group in repo_groups:
105 self.assert_group_location_directive(content, group.full_path)
75 self.assert_group_location_directive(
76 generated_config, group.full_path)
106
77
107 # Assert that the root location directive exists.
78 # Assert that the root location directive exists.
108 self.assert_root_location_directive(content)
79 self.assert_root_location_directive(generated_config)
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()
127
80
128 # Make assertions.
81 @pytest.mark.parametrize('list_parent_path', [True, False])
129 assert not re.search('SVNListParentPath\s+Off', content)
82 def test_list_parent_path(self, list_parent_path):
130 assert re.search('SVNListParentPath\s+On', content)
83 generated_config = utils._render_mod_dav_svn_config(
131
84 parent_path_root=self.parent_path_root,
132 @mock.patch('rhodecode.svn_support.utils.get_rhodecode_realm')
85 list_parent_path=list_parent_path,
133 @mock.patch('rhodecode.svn_support.utils.RepoGroup')
86 location_root=self.location_root,
134 def test_list_parent_path_off(self, RepoGroupMock, GetRealmMock):
87 repo_groups=self.get_repo_group_mocks(count=10),
135 # Setup mock objects.
88 realm=self.realm
136 GetRealmMock.return_value = self._dummy_realm
89 )
137 RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups()
138
90
139 # Execute the method under test.
91 # Assert that correct configuration directive is present.
140 settings = self.get_settings()
92 if list_parent_path:
141 settings[config_keys.list_parent_path] = False
93 assert not re.search('SVNListParentPath\s+Off', generated_config)
142 utils.generate_mod_dav_svn_config(
94 assert re.search('SVNListParentPath\s+On', generated_config)
143 settings=settings, parent_path_root=self.parent_path_root)
95 else:
144
96 assert re.search('SVNListParentPath\s+Off', generated_config)
145 # Read generated file.
97 assert not re.search('SVNListParentPath\s+On', generated_config)
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)
@@ -1,86 +1,87 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2016-2016 RhodeCode GmbH
3 # Copyright (C) 2016-2016 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import codecs
21 import codecs
22 import logging
22 import logging
23 import os
23 import os
24 from pyramid.renderers import render
24 from pyramid.renderers import render
25
25
26 from rhodecode.events import trigger
26 from rhodecode.events import trigger
27 from rhodecode.lib.utils import get_rhodecode_realm, get_rhodecode_base_path
27 from rhodecode.lib.utils import get_rhodecode_realm, get_rhodecode_base_path
28 from rhodecode.model.db import RepoGroup
28 from rhodecode.model.db import RepoGroup
29
29
30 from . import config_keys
30 from . import config_keys
31 from .events import ModDavSvnConfigChange
31 from .events import ModDavSvnConfigChange
32
32
33
33
34 log = logging.getLogger(__name__)
34 log = logging.getLogger(__name__)
35
35
36
36
37 def generate_mod_dav_svn_config(registry):
37 def generate_mod_dav_svn_config(registry):
38 """
38 """
39 Generate the configuration file for use with subversion's mod_dav_svn
39 Generate the configuration file for use with subversion's mod_dav_svn
40 module. The configuration has to contain a <Location> block for each
40 module. The configuration has to contain a <Location> block for each
41 available repository group because the mod_dav_svn module does not support
41 available repository group because the mod_dav_svn module does not support
42 repositories organized in sub folders.
42 repositories organized in sub folders.
43 """
43 """
44 settings = registry.settings
44 settings = registry.settings
45 config = _render_mod_dav_svn_config(
45 config = _render_mod_dav_svn_config(
46 parent_path_root=get_rhodecode_base_path(),
46 parent_path_root=get_rhodecode_base_path(),
47 list_parent_path=settings[config_keys.list_parent_path],
47 list_parent_path=settings[config_keys.list_parent_path],
48 location_root=settings[config_keys.location_root],
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 _write_mod_dav_svn_config(config, settings[config_keys.config_file_path])
51 _write_mod_dav_svn_config(config, settings[config_keys.config_file_path])
51
52
52 # Trigger an event on mod dav svn configuration change.
53 # Trigger an event on mod dav svn configuration change.
53 trigger(ModDavSvnConfigChange(), registry)
54 trigger(ModDavSvnConfigChange(), registry)
54
55
55
56
56 def _render_mod_dav_svn_config(
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 Render mod_dav_svn configuration to string.
60 Render mod_dav_svn configuration to string.
60 """
61 """
61 repo_group_paths = []
62 repo_group_paths = []
62 for repo_group in repo_groups:
63 for repo_group in repo_groups:
63 group_path = repo_group.full_path_splitted
64 group_path = repo_group.full_path_splitted
64 location = os.path.join(location_root, *group_path)
65 location = os.path.join(location_root, *group_path)
65 parent_path = os.path.join(parent_path_root, *group_path)
66 parent_path = os.path.join(parent_path_root, *group_path)
66 repo_group_paths.append((location, parent_path))
67 repo_group_paths.append((location, parent_path))
67
68
68 context = {
69 context = {
69 'location_root': location_root,
70 'location_root': location_root,
70 'parent_path_root': parent_path_root,
71 'parent_path_root': parent_path_root,
71 'repo_group_paths': repo_group_paths,
72 'repo_group_paths': repo_group_paths,
72 'svn_list_parent_path': list_parent_path,
73 'svn_list_parent_path': list_parent_path,
73 'rhodecode_realm': get_rhodecode_realm(),
74 'rhodecode_realm': realm,
74 }
75 }
75
76
76 # Render the configuration template to string.
77 # Render the configuration template to string.
77 template = 'rhodecode:svn_support/templates/mod-dav-svn.conf.mako'
78 template = 'rhodecode:svn_support/templates/mod-dav-svn.conf.mako'
78 return render(template, context)
79 return render(template, context)
79
80
80
81
81 def _write_mod_dav_svn_config(config, filepath):
82 def _write_mod_dav_svn_config(config, filepath):
82 """
83 """
83 Write mod_dav_svn config to file.
84 Write mod_dav_svn config to file.
84 """
85 """
85 with codecs.open(filepath, 'w', encoding='utf-8') as f:
86 with codecs.open(filepath, 'w', encoding='utf-8') as f:
86 f.write(config)
87 f.write(config)
General Comments 0
You need to be logged in to leave comments. Login now