Show More
@@ -1,146 +1,151 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 | |||
22 | import mock |
|
23 | import mock | |
23 | import re |
|
24 | import re | |
24 | import shutil |
|
25 | import shutil | |
25 | import tempfile |
|
26 | import tempfile | |
26 |
|
27 | |||
27 | from pyramid import testing |
|
28 | from pyramid import testing | |
28 |
|
29 | |||
29 | from rhodecode.svn_support import config_keys, utils |
|
30 | from rhodecode.svn_support import config_keys, utils | |
30 |
|
31 | |||
31 |
|
32 | |||
32 | class TestModDavSvnConfig(object): |
|
33 | class TestModDavSvnConfig(object): | |
|
34 | ||||
33 | @classmethod |
|
35 | @classmethod | |
34 | def setup_class(cls): |
|
36 | def setup_class(cls): | |
35 | # Make mako renderer available in tests. |
|
37 | # Make mako renderer available in tests. | |
36 | config = testing.setUp() |
|
38 | config = testing.setUp() | |
37 | config.include('pyramid_mako') |
|
39 | config.include('pyramid_mako') | |
38 |
|
40 | |||
39 | # Temporary directory holding the generated config files. |
|
41 | # Temporary directory holding the generated config files. | |
40 | cls.tempdir = tempfile.mkdtemp(suffix='pytest-mod-dav-svn') |
|
42 | cls.tempdir = tempfile.mkdtemp(suffix='pytest-mod-dav-svn') | |
41 |
|
43 | |||
42 | cls.location_root = '/location/root' |
|
44 | cls.location_root = u'/location/root/ç¡Àâ' | |
43 |
cls.parent_path_root = '/parent/path/ |
|
45 | cls.parent_path_root = u'/parent/path/ç¡Àâ' | |
|
46 | cls._dummy_realm = u'Dummy Realm (Àâüç¡)' | |||
44 |
|
47 | |||
45 | @classmethod |
|
48 | @classmethod | |
46 | def teardown_class(cls): |
|
49 | def teardown_class(cls): | |
47 | testing.tearDown() |
|
50 | testing.tearDown() | |
48 | shutil.rmtree(cls.tempdir, ignore_errors=True) |
|
51 | shutil.rmtree(cls.tempdir, ignore_errors=True) | |
49 |
|
52 | |||
50 | @classmethod |
|
53 | @classmethod | |
51 | def get_settings(cls): |
|
54 | def get_settings(cls): | |
52 | config_file_path = tempfile.mkstemp( |
|
55 | config_file_path = tempfile.mkstemp( | |
53 | suffix='mod-dav-svn.conf', dir=cls.tempdir)[1] |
|
56 | suffix='mod-dav-svn.conf', dir=cls.tempdir)[1] | |
54 | return { |
|
57 | return { | |
55 | config_keys.config_file_path: config_file_path, |
|
58 | config_keys.config_file_path: config_file_path, | |
56 | config_keys.location_root: cls.location_root, |
|
59 | config_keys.location_root: cls.location_root, | |
57 | config_keys.parent_path_root: cls.parent_path_root, |
|
|||
58 | config_keys.list_parent_path: True, |
|
60 | config_keys.list_parent_path: True, | |
59 | } |
|
61 | } | |
60 |
|
62 | |||
61 | @classmethod |
|
63 | @classmethod | |
62 | def get_repo_groups(cls, count=1): |
|
64 | def get_repo_groups(cls, count=1): | |
63 | repo_groups = [] |
|
65 | repo_groups = [] | |
64 | for num in range(0, count): |
|
66 | for num in range(0, count): | |
65 |
full_path = '/path/to/Rep |
|
67 | full_path = u'/path/to/RepâGrâúp-°¡ {}'.format(num) | |
66 | repo_group_mock = mock.MagicMock() |
|
68 | repo_group_mock = mock.MagicMock() | |
67 | repo_group_mock.full_path = full_path |
|
69 | repo_group_mock.full_path = full_path | |
68 | repo_group_mock.full_path_splitted = full_path.split('/') |
|
70 | repo_group_mock.full_path_splitted = full_path.split('/') | |
69 | repo_groups.append(repo_group_mock) |
|
71 | repo_groups.append(repo_group_mock) | |
70 | return repo_groups |
|
72 | return repo_groups | |
71 |
|
73 | |||
72 | def assert_root_location_directive(self, config): |
|
74 | def assert_root_location_directive(self, config): | |
73 |
pattern = '<Location {location}>'.format( |
|
75 | pattern = u'<Location "{location}">'.format( | |
|
76 | location=self.location_root) | |||
74 | assert len(re.findall(pattern, config)) == 1 |
|
77 | assert len(re.findall(pattern, config)) == 1 | |
75 |
|
78 | |||
76 | def assert_group_location_directive(self, config, group_path): |
|
79 | def assert_group_location_directive(self, config, group_path): | |
77 | pattern = '<Location {location}{group_path}>'.format( |
|
80 | pattern = u'<Location "{location}{group_path}">'.format( | |
78 | location=self.location_root, group_path=group_path) |
|
81 | location=self.location_root, group_path=group_path) | |
79 | assert len(re.findall(pattern, config)) == 1 |
|
82 | assert len(re.findall(pattern, config)) == 1 | |
80 |
|
83 | |||
|
84 | @mock.patch('rhodecode.svn_support.utils.get_rhodecode_realm') | |||
81 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') |
|
85 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') | |
82 | def test_generate_mod_dav_svn_config(self, RepoGroupMock): |
|
86 | def test_generate_mod_dav_svn_config(self, RepoGroupMock, GetRealmMock): | |
|
87 | # Setup mock objects. | |||
|
88 | GetRealmMock.return_value = self._dummy_realm | |||
83 | num_groups = 3 |
|
89 | num_groups = 3 | |
84 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups( |
|
90 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups( | |
85 | count=num_groups) |
|
91 | count=num_groups) | |
86 |
|
92 | |||
87 | # Execute the method under test. |
|
93 | # Execute the method under test. | |
88 | settings = self.get_settings() |
|
94 | settings = self.get_settings() | |
89 |
utils.generate_mod_dav_svn_config( |
|
95 | utils.generate_mod_dav_svn_config( | |
|
96 | settings=settings, parent_path_root=self.parent_path_root) | |||
90 |
|
97 | |||
91 | # Read generated file. |
|
98 | # Read generated file. | |
92 |
|
|
99 | path = settings[config_keys.config_file_path] | |
93 | content = file_.read() |
|
100 | with codecs.open(path, 'r', encoding='utf-8') as f: | |
|
101 | content = f.read() | |||
94 |
|
102 | |||
95 | # Assert that one location directive exists for each repository group. |
|
103 | # Assert that one location directive exists for each repository group. | |
96 | for group in self.get_repo_groups(count=num_groups): |
|
104 | for group in self.get_repo_groups(count=num_groups): | |
97 | self.assert_group_location_directive(content, group.full_path) |
|
105 | self.assert_group_location_directive(content, group.full_path) | |
98 |
|
106 | |||
99 | # Assert that the root location directive exists. |
|
107 | # Assert that the root location directive exists. | |
100 | self.assert_root_location_directive(content) |
|
108 | self.assert_root_location_directive(content) | |
101 |
|
109 | |||
|
110 | @mock.patch('rhodecode.svn_support.utils.get_rhodecode_realm') | |||
102 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') |
|
111 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') | |
103 | def test_list_parent_path_on(self, RepoGroupMock): |
|
112 | def test_list_parent_path_on(self, RepoGroupMock, GetRealmMock): | |
|
113 | # Setup mock objects. | |||
|
114 | GetRealmMock.return_value = self._dummy_realm | |||
104 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups() |
|
115 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups() | |
105 |
|
116 | |||
106 | # Execute the method under test. |
|
117 | # Execute the method under test. | |
107 | settings = self.get_settings() |
|
118 | settings = self.get_settings() | |
108 | settings[config_keys.list_parent_path] = True |
|
119 | settings[config_keys.list_parent_path] = True | |
109 |
utils.generate_mod_dav_svn_config( |
|
120 | utils.generate_mod_dav_svn_config( | |
|
121 | settings=settings, parent_path_root=self.parent_path_root) | |||
110 |
|
122 | |||
111 | # Read generated file. |
|
123 | # Read generated file. | |
112 |
|
|
124 | path = settings[config_keys.config_file_path] | |
113 | content = file_.read() |
|
125 | with codecs.open(path, 'r', encoding='utf-8') as f: | |
|
126 | content = f.read() | |||
114 |
|
127 | |||
115 | # Make assertions. |
|
128 | # Make assertions. | |
116 | assert not re.search('SVNListParentPath\s+Off', content) |
|
129 | assert not re.search('SVNListParentPath\s+Off', content) | |
117 | assert re.search('SVNListParentPath\s+On', content) |
|
130 | assert re.search('SVNListParentPath\s+On', content) | |
118 |
|
131 | |||
|
132 | @mock.patch('rhodecode.svn_support.utils.get_rhodecode_realm') | |||
119 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') |
|
133 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') | |
120 | def test_list_parent_path_off(self, RepoGroupMock): |
|
134 | def test_list_parent_path_off(self, RepoGroupMock, GetRealmMock): | |
|
135 | # Setup mock objects. | |||
|
136 | GetRealmMock.return_value = self._dummy_realm | |||
121 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups() |
|
137 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups() | |
122 |
|
138 | |||
123 | # Execute the method under test. |
|
139 | # Execute the method under test. | |
124 | settings = self.get_settings() |
|
140 | settings = self.get_settings() | |
125 | settings[config_keys.list_parent_path] = False |
|
141 | settings[config_keys.list_parent_path] = False | |
126 |
utils.generate_mod_dav_svn_config( |
|
142 | utils.generate_mod_dav_svn_config( | |
|
143 | settings=settings, parent_path_root=self.parent_path_root) | |||
127 |
|
144 | |||
128 | # Read generated file. |
|
145 | # Read generated file. | |
129 | with open(settings[config_keys.config_file_path], 'r') as file_: |
|
146 | with open(settings[config_keys.config_file_path], 'r') as file_: | |
130 | content = file_.read() |
|
147 | content = file_.read() | |
131 |
|
148 | |||
132 | # Make assertions. |
|
149 | # Make assertions. | |
133 | assert re.search('SVNListParentPath\s+Off', content) |
|
150 | assert re.search('SVNListParentPath\s+Off', content) | |
134 | assert not re.search('SVNListParentPath\s+On', content) |
|
151 | assert not re.search('SVNListParentPath\s+On', content) | |
135 |
|
||||
136 | @mock.patch('rhodecode.svn_support.utils.log') |
|
|||
137 | def test_write_does_not_raise_on_error(self, LogMock): |
|
|||
138 | """ |
|
|||
139 | Writing the configuration to file should never raise exceptions. |
|
|||
140 | If e.g. path points to a place without write permissions. |
|
|||
141 | """ |
|
|||
142 | utils._write_mod_dav_svn_config( |
|
|||
143 | 'content', '/dev/null/not/existing/path') |
|
|||
144 |
|
||||
145 | # Assert that we log the exception. |
|
|||
146 | assert LogMock.exception.called |
|
General Comments 0
You need to be logged in to leave comments.
Login now