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