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