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