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