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