Show More
@@ -1,138 +1,146 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 | 22 | import mock |
|
23 | 23 | import re |
|
24 | 24 | import shutil |
|
25 | 25 | import tempfile |
|
26 | 26 | |
|
27 | 27 | from pyramid import testing |
|
28 | 28 | |
|
29 | from rhodecode.svn_support import config_keys | |
|
30 | from rhodecode.svn_support.utils import generate_mod_dav_svn_config | |
|
29 | from rhodecode.svn_support import config_keys, utils | |
|
31 | 30 | |
|
32 | 31 | |
|
33 | 32 | class TestModDavSvnConfig(object): |
|
34 | 33 | @classmethod |
|
35 | 34 | def setup_class(cls): |
|
36 | 35 | # Make mako renderer available in tests. |
|
37 | 36 | config = testing.setUp() |
|
38 | 37 | config.include('pyramid_mako') |
|
39 | 38 | |
|
40 | 39 | # Temporary directory holding the generated config files. |
|
41 | 40 | cls.tempdir = tempfile.mkdtemp(suffix='pytest-mod-dav-svn') |
|
42 | 41 | |
|
43 | # Regex pattern to match a location block in the generated config. | |
|
44 | cls.location_regex = ( | |
|
45 | '<Location {location}>\s+' | |
|
46 | 'DAV svn\s+' | |
|
47 | 'SVNParentPath {svn_parent_path}\s+' | |
|
48 | 'SVNListParentPath {svn_list_parent_path}\s+' | |
|
49 | 'Allow from all\s+' | |
|
50 | 'Order allow,deny\s+' | |
|
51 | '</Location>') | |
|
42 | cls.location_root = '/location/root' | |
|
43 | cls.parent_path_root = '/parent/path/root' | |
|
52 | 44 | |
|
53 | 45 | @classmethod |
|
54 | 46 | def teardown_class(cls): |
|
55 | 47 | testing.tearDown() |
|
56 | 48 | shutil.rmtree(cls.tempdir, ignore_errors=True) |
|
57 | 49 | |
|
58 | 50 | @classmethod |
|
59 | 51 | def get_settings(cls): |
|
60 | 52 | config_file_path = tempfile.mkstemp( |
|
61 | 53 | suffix='mod-dav-svn.conf', dir=cls.tempdir)[1] |
|
62 | 54 | return { |
|
63 | 55 | config_keys.config_file_path: config_file_path, |
|
64 |
config_keys.location_root: |
|
|
65 |
config_keys.parent_path_root: |
|
|
56 | config_keys.location_root: cls.location_root, | |
|
57 | config_keys.parent_path_root: cls.parent_path_root, | |
|
66 | 58 | config_keys.list_parent_path: True, |
|
67 | 59 | } |
|
68 | 60 | |
|
69 | 61 | @classmethod |
|
70 | 62 | def get_repo_groups(cls, count=1): |
|
71 | 63 | repo_groups = [] |
|
72 | 64 | for num in range(0, count): |
|
65 | full_path = '/path/to/RepoGroup{}'.format(num) | |
|
73 | 66 | repo_group_mock = mock.MagicMock() |
|
74 |
repo_group_mock.full_path = |
|
|
67 | repo_group_mock.full_path = full_path | |
|
68 | repo_group_mock.full_path_splitted = full_path.split('/') | |
|
75 | 69 | repo_groups.append(repo_group_mock) |
|
76 | 70 | return repo_groups |
|
77 | 71 | |
|
72 | def assert_root_location_directive(self, config): | |
|
73 | pattern = '<Location {location}>'.format(location=self.location_root) | |
|
74 | assert len(re.findall(pattern, config)) == 1 | |
|
75 | ||
|
76 | def assert_group_location_directive(self, config, group_path): | |
|
77 | pattern = '<Location {location}{group_path}>'.format( | |
|
78 | location=self.location_root, group_path=group_path) | |
|
79 | assert len(re.findall(pattern, config)) == 1 | |
|
80 | ||
|
78 | 81 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') |
|
79 | 82 | def test_generate_mod_dav_svn_config(self, RepoGroupMock): |
|
80 | 83 | num_groups = 3 |
|
81 | 84 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups( |
|
82 | 85 | count=num_groups) |
|
83 | 86 | |
|
84 | 87 | # Execute the method under test. |
|
85 | 88 | settings = self.get_settings() |
|
86 | generate_mod_dav_svn_config(settings) | |
|
89 | utils.generate_mod_dav_svn_config(settings) | |
|
87 | 90 | |
|
88 | 91 | # Read generated file. |
|
89 | 92 | with open(settings[config_keys.config_file_path], 'r') as file_: |
|
90 | 93 | content = file_.read() |
|
91 | 94 | |
|
92 |
# Assert that one location |
|
|
93 | repo_group_pattern = self.location_regex.format( | |
|
94 | location='/location/root/path/to/RepoGroup\d+', | |
|
95 | svn_parent_path='/parent/path/root/path/to/RepoGroup\d+', | |
|
96 | svn_list_parent_path='On') | |
|
97 | assert len(re.findall(repo_group_pattern, content)) == num_groups | |
|
95 | # Assert that one location directive exists for each repository group. | |
|
96 | for group in self.get_repo_groups(count=num_groups): | |
|
97 | self.assert_group_location_directive(content, group.full_path) | |
|
98 | 98 | |
|
99 |
# Assert that the root location |
|
|
100 | root_pattern = self.location_regex.format( | |
|
101 | location='/location/root/', | |
|
102 | svn_parent_path='/parent/path/root/', | |
|
103 | svn_list_parent_path='On') | |
|
104 | assert len(re.findall(root_pattern, content)) == 1 | |
|
99 | # Assert that the root location directive exists. | |
|
100 | self.assert_root_location_directive(content) | |
|
105 | 101 | |
|
106 | 102 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') |
|
107 | 103 | def test_list_parent_path_on(self, RepoGroupMock): |
|
108 | 104 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups() |
|
109 | 105 | |
|
110 | 106 | # Execute the method under test. |
|
111 | 107 | settings = self.get_settings() |
|
112 | 108 | settings[config_keys.list_parent_path] = True |
|
113 | generate_mod_dav_svn_config(settings) | |
|
109 | utils.generate_mod_dav_svn_config(settings) | |
|
114 | 110 | |
|
115 | 111 | # Read generated file. |
|
116 | 112 | with open(settings[config_keys.config_file_path], 'r') as file_: |
|
117 | 113 | content = file_.read() |
|
118 | 114 | |
|
119 | 115 | # Make assertions. |
|
120 | 116 | assert not re.search('SVNListParentPath\s+Off', content) |
|
121 | 117 | assert re.search('SVNListParentPath\s+On', content) |
|
122 | 118 | |
|
123 | 119 | @mock.patch('rhodecode.svn_support.utils.RepoGroup') |
|
124 | 120 | def test_list_parent_path_off(self, RepoGroupMock): |
|
125 | 121 | RepoGroupMock.get_all_repo_groups.return_value = self.get_repo_groups() |
|
126 | 122 | |
|
127 | 123 | # Execute the method under test. |
|
128 | 124 | settings = self.get_settings() |
|
129 | 125 | settings[config_keys.list_parent_path] = False |
|
130 | generate_mod_dav_svn_config(settings) | |
|
126 | utils.generate_mod_dav_svn_config(settings) | |
|
131 | 127 | |
|
132 | 128 | # Read generated file. |
|
133 | 129 | with open(settings[config_keys.config_file_path], 'r') as file_: |
|
134 | 130 | content = file_.read() |
|
135 | 131 | |
|
136 | 132 | # Make assertions. |
|
137 | 133 | assert re.search('SVNListParentPath\s+Off', content) |
|
138 | 134 | 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