##// END OF EJS Templates
tests: Test sanitize helper function for integer settings.
Martin Bornhold -
r624:e0a65beb default
parent child Browse files
Show More
@@ -1,175 +1,202 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 pytest
23 23
24 24 from rhodecode.config.middleware import (
25 _sanitize_vcs_settings, _bool_setting, _string_setting, _list_setting)
25 _sanitize_vcs_settings, _bool_setting, _string_setting, _list_setting,
26 _int_setting)
26 27
27 28
28 29 class TestHelperFunctions(object):
29 30 @pytest.mark.parametrize('raw, expected', [
30 31 ('true', True), (u'true', True),
31 32 ('yes', True), (u'yes', True),
32 33 ('on', True), (u'on', True),
33 34 ('false', False), (u'false', False),
34 35 ('no', False), (u'no', False),
35 36 ('off', False), (u'off', False),
36 37 ('invalid-bool-value', False),
37 38 ('invalid-∫øø@-√Γ₯@¨€', False),
38 39 (u'invalid-∫øø@-√Γ₯@¨€', False),
39 40 ])
40 41 def test_bool_setting_helper(self, raw, expected):
41 42 key = 'dummy-key'
42 43 settings = {key: raw}
43 44 _bool_setting(settings, key, None)
44 45 assert settings[key] is expected
45 46
46 47 @pytest.mark.parametrize('raw, expected', [
47 48 ('', ''),
48 49 ('test-string', 'test-string'),
49 50 ('CaSe-TeSt', 'case-test'),
50 51 ('test-string-烩€', 'test-string-烩€'),
51 52 (u'test-string-烩€', u'test-string-烩€'),
52 53 ])
53 54 def test_string_setting_helper(self, raw, expected):
54 55 key = 'dummy-key'
55 56 settings = {key: raw}
56 57 _string_setting(settings, key, None)
57 58 assert settings[key] == expected
58 59
59 60 @pytest.mark.parametrize('raw, expected', [
60 61 ('', []),
61 62 ('test', ['test']),
62 63 ('CaSe-TeSt', ['CaSe-TeSt']),
63 64 ('test-string-烩€', ['test-string-烩€']),
64 65 (u'test-string-烩€', [u'test-string-烩€']),
65 66 ('hg git svn', ['hg', 'git', 'svn']),
66 67 ('hg,git,svn', ['hg', 'git', 'svn']),
67 68 ('hg, git, svn', ['hg', 'git', 'svn']),
68 69 ('hg\ngit\nsvn', ['hg', 'git', 'svn']),
69 70 (' hg\n git\n svn ', ['hg', 'git', 'svn']),
70 71 (', hg , git , svn , ', ['', 'hg', 'git', 'svn', '']),
71 72 ('cheese,free node,other', ['cheese', 'free node', 'other']),
72 73 ])
73 74 def test_list_setting_helper(self, raw, expected):
74 75 key = 'dummy-key'
75 76 settings = {key: raw}
76 77 _list_setting(settings, key, None)
77 78 assert settings[key] == expected
78 79
80 @pytest.mark.parametrize('raw, expected', [
81 ('0', 0),
82 ('-0', 0),
83 ('12345', 12345),
84 ('-12345', -12345),
85 (u'-12345', -12345),
86 ])
87 def test_int_setting_helper(self, raw, expected):
88 key = 'dummy-key'
89 settings = {key: raw}
90 _int_setting(settings, key, None)
91 assert settings[key] == expected
92
93 @pytest.mark.parametrize('raw', [
94 ('0xff'),
95 (''),
96 ('invalid-int'),
97 ('invalid-⁄~†'),
98 (u'invalid-⁄~†'),
99 ])
100 def test_int_setting_helper_invalid_input(self, raw):
101 key = 'dummy-key'
102 settings = {key: raw}
103 with pytest.raises(Exception):
104 _int_setting(settings, key, None)
105
79 106
80 107 class TestSanitizeVcsSettings(object):
81 108 _bool_settings = [
82 109 ('vcs.hooks.direct_calls', False),
83 110 ('vcs.server.enable', True),
84 111 ('vcs.start_server', False),
85 112 ('startup.import_repos', False),
86 113 ]
87 114
88 115 _string_settings = [
89 116 ('vcs.svn.compatible_version', ''),
90 117 ('git_rev_filter', '--all'),
91 118 ('vcs.hooks.protocol', 'pyro4'),
92 119 ('vcs.server', ''),
93 120 ('vcs.server.log_level', 'debug'),
94 121 ('vcs.server.protocol', 'pyro4'),
95 122 ]
96 123
97 124 _list_settings = [
98 125 ('vcs.backends', 'hg git'),
99 126 ]
100 127
101 128 @pytest.mark.parametrize('key, default', _list_settings)
102 129 def test_list_setting_spacesep_list(self, key, default):
103 130 test_list = ['test', 'list', 'values', 'for', key]
104 131 input_value = ' '.join(test_list)
105 132 settings = {key: input_value}
106 133 _sanitize_vcs_settings(settings)
107 134 assert settings[key] == test_list
108 135
109 136 @pytest.mark.parametrize('key, default', _list_settings)
110 137 def test_list_setting_newlinesep_list(self, key, default):
111 138 test_list = ['test', 'list', 'values', 'for', key]
112 139 input_value = '\n'.join(test_list)
113 140 settings = {key: input_value}
114 141 _sanitize_vcs_settings(settings)
115 142 assert settings[key] == test_list
116 143
117 144 @pytest.mark.parametrize('key, default', _list_settings)
118 145 def test_list_setting_commasep_list(self, key, default):
119 146 test_list = ['test', 'list', 'values', 'for', key]
120 147 input_value = ','.join(test_list)
121 148 settings = {key: input_value}
122 149 _sanitize_vcs_settings(settings)
123 150 assert settings[key] == test_list
124 151
125 152 @pytest.mark.parametrize('key, default', _list_settings)
126 153 def test_list_setting_comma_and_space_sep_list(self, key, default):
127 154 test_list = ['test', 'list', 'values', 'for', key]
128 155 input_value = ', '.join(test_list)
129 156 settings = {key: input_value}
130 157 _sanitize_vcs_settings(settings)
131 158 assert settings[key] == test_list
132 159
133 160 @pytest.mark.parametrize('key, default', _string_settings)
134 161 def test_string_setting_string(self, key, default):
135 162 test_value = 'test-string-for-{}'.format(key)
136 163 settings = {key: test_value}
137 164 _sanitize_vcs_settings(settings)
138 165 assert settings[key] == test_value
139 166
140 167 @pytest.mark.parametrize('key, default', _string_settings)
141 168 def test_string_setting_default(self, key, default):
142 169 settings = {}
143 170 _sanitize_vcs_settings(settings)
144 171 assert settings[key] == default
145 172
146 173 @pytest.mark.parametrize('key, default', _string_settings)
147 174 def test_string_setting_lowercase(self, key, default):
148 175 test_value = 'Test-String-For-{}'.format(key)
149 176 settings = {key: test_value}
150 177 _sanitize_vcs_settings(settings)
151 178 assert settings[key] == test_value.lower()
152 179
153 180 @pytest.mark.parametrize('key, default', _bool_settings)
154 181 def test_bool_setting_true(self, key, default):
155 182 settings = {key: 'true'}
156 183 _sanitize_vcs_settings(settings)
157 184 assert settings[key] is True
158 185
159 186 @pytest.mark.parametrize('key, default', _bool_settings)
160 187 def test_bool_setting_false(self, key, default):
161 188 settings = {key: 'false'}
162 189 _sanitize_vcs_settings(settings)
163 190 assert settings[key] is False
164 191
165 192 @pytest.mark.parametrize('key, default', _bool_settings)
166 193 def test_bool_setting_invalid_string(self, key, default):
167 194 settings = {key: 'no-bool-val-string'}
168 195 _sanitize_vcs_settings(settings)
169 196 assert settings[key] is False
170 197
171 198 @pytest.mark.parametrize('key, default', _bool_settings)
172 199 def test_bool_setting_default(self, key, default):
173 200 settings = {}
174 201 _sanitize_vcs_settings(settings)
175 202 assert settings[key] is default
General Comments 0
You need to be logged in to leave comments. Login now