Show More
@@ -0,0 +1,170 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | ||||
|
3 | # Copyright (C) 2016-2016 RhodeCode GmbH | |||
|
4 | # | |||
|
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 | |||
|
7 | # (only), as published by the Free Software Foundation. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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/>. | |||
|
16 | # | |||
|
17 | # This program is dual-licensed. If you wish to learn more about the | |||
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |||
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |||
|
20 | ||||
|
21 | ||||
|
22 | import pytest | |||
|
23 | ||||
|
24 | from rhodecode.config.middleware import ( | |||
|
25 | _sanitize_vcs_settings, _bool_setting, _string_setting, _list_setting) | |||
|
26 | ||||
|
27 | ||||
|
28 | class TestHelperFunctions(object): | |||
|
29 | @pytest.mark.parametrize('raw, expected', [ | |||
|
30 | ('true', True), | |||
|
31 | ('yes', True), | |||
|
32 | ('on', True), | |||
|
33 | ('false', False), | |||
|
34 | ('no', False), | |||
|
35 | ('off', False), | |||
|
36 | ('invalid-bool-value', False), | |||
|
37 | ('invalid-β«ΓΈΓΈ@-βΓ₯@Β¨β¬', False), | |||
|
38 | ]) | |||
|
39 | def test_bool_setting_helper(self, raw, expected): | |||
|
40 | key = 'dummy-key' | |||
|
41 | settings = {key: raw} | |||
|
42 | _bool_setting(settings, key, None) | |||
|
43 | assert settings[key] is expected | |||
|
44 | ||||
|
45 | @pytest.mark.parametrize('raw, expected', [ | |||
|
46 | ('', ''), | |||
|
47 | ('test-string', 'test-string'), | |||
|
48 | ('CaSe-TeSt', 'case-test'), | |||
|
49 | ('test-string-Γ§ΖΒ©β¬', 'test-string-Γ§ΖΒ©β¬'), | |||
|
50 | ]) | |||
|
51 | def test_string_setting_helper(self, raw, expected): | |||
|
52 | key = 'dummy-key' | |||
|
53 | settings = {key: raw} | |||
|
54 | _string_setting(settings, key, None) | |||
|
55 | assert settings[key] == expected | |||
|
56 | ||||
|
57 | @pytest.mark.parametrize('raw, expected', [ | |||
|
58 | ('', []), | |||
|
59 | ('test', ['test']), | |||
|
60 | ('CaSe-TeSt', ['CaSe-TeSt']), | |||
|
61 | ('hg git svn', ['hg', 'git', 'svn']), | |||
|
62 | ('hg,git,svn', ['hg', 'git', 'svn']), | |||
|
63 | ('hg, git, svn', ['hg', 'git', 'svn']), | |||
|
64 | ('hg\ngit\nsvn', ['hg', 'git', 'svn']), | |||
|
65 | ('hg\n git\n svn', ['hg', 'git', 'svn']), | |||
|
66 | ('test-string-Γ§ΖΒ©β¬', ['test-string-Γ§ΖΒ©β¬']), | |||
|
67 | ]) | |||
|
68 | def test_list_setting_helper(self, raw, expected): | |||
|
69 | key = 'dummy-key' | |||
|
70 | settings = {key: raw} | |||
|
71 | _list_setting(settings, key, None) | |||
|
72 | assert settings[key] == expected | |||
|
73 | ||||
|
74 | ||||
|
75 | class TestSanitizeVcsSettings(object): | |||
|
76 | _bool_settings = [ | |||
|
77 | ('vcs.hooks.direct_calls', False), | |||
|
78 | ('vcs.server.enable', True), | |||
|
79 | ('vcs.start_server', False), | |||
|
80 | ('startup.import_repos', False), | |||
|
81 | ] | |||
|
82 | ||||
|
83 | _string_settings = [ | |||
|
84 | ('vcs.svn.compatible_version', ''), | |||
|
85 | ('git_rev_filter', '--all'), | |||
|
86 | ('vcs.hooks.protocol', 'pyro4'), | |||
|
87 | ('vcs.server', ''), | |||
|
88 | ('vcs.server.log_level', 'debug'), | |||
|
89 | ('vcs.server.protocol', 'pyro4'), | |||
|
90 | ] | |||
|
91 | ||||
|
92 | _list_settings = [ | |||
|
93 | ('vcs.backends', 'hg git'), | |||
|
94 | ] | |||
|
95 | ||||
|
96 | @pytest.mark.parametrize('key, default', _list_settings) | |||
|
97 | def test_list_setting_spacesep_list(self, key, default): | |||
|
98 | test_list = ['test', 'list', 'values', 'for', key] | |||
|
99 | input_value = ' '.join(test_list) | |||
|
100 | settings = {key: input_value} | |||
|
101 | _sanitize_vcs_settings(settings) | |||
|
102 | assert settings[key] == test_list | |||
|
103 | ||||
|
104 | @pytest.mark.parametrize('key, default', _list_settings) | |||
|
105 | def test_list_setting_newlinesep_list(self, key, default): | |||
|
106 | test_list = ['test', 'list', 'values', 'for', key] | |||
|
107 | input_value = '\n'.join(test_list) | |||
|
108 | settings = {key: input_value} | |||
|
109 | _sanitize_vcs_settings(settings) | |||
|
110 | assert settings[key] == test_list | |||
|
111 | ||||
|
112 | @pytest.mark.parametrize('key, default', _list_settings) | |||
|
113 | def test_list_setting_commasep_list(self, key, default): | |||
|
114 | test_list = ['test', 'list', 'values', 'for', key] | |||
|
115 | input_value = ','.join(test_list) | |||
|
116 | settings = {key: input_value} | |||
|
117 | _sanitize_vcs_settings(settings) | |||
|
118 | assert settings[key] == test_list | |||
|
119 | ||||
|
120 | @pytest.mark.parametrize('key, default', _list_settings) | |||
|
121 | def test_list_setting_comma_and_space_sep_list(self, key, default): | |||
|
122 | test_list = ['test', 'list', 'values', 'for', key] | |||
|
123 | input_value = ', '.join(test_list) | |||
|
124 | settings = {key: input_value} | |||
|
125 | _sanitize_vcs_settings(settings) | |||
|
126 | assert settings[key] == test_list | |||
|
127 | ||||
|
128 | @pytest.mark.parametrize('key, default', _string_settings) | |||
|
129 | def test_string_setting_string(self, key, default): | |||
|
130 | test_value = 'test-string-for-{}'.format(key) | |||
|
131 | settings = {key: test_value} | |||
|
132 | _sanitize_vcs_settings(settings) | |||
|
133 | assert settings[key] == test_value | |||
|
134 | ||||
|
135 | @pytest.mark.parametrize('key, default', _string_settings) | |||
|
136 | def test_string_setting_default(self, key, default): | |||
|
137 | settings = {} | |||
|
138 | _sanitize_vcs_settings(settings) | |||
|
139 | assert settings[key] == default | |||
|
140 | ||||
|
141 | @pytest.mark.parametrize('key, default', _string_settings) | |||
|
142 | def test_string_setting_lowercase(self, key, default): | |||
|
143 | test_value = 'Test-String-For-{}'.format(key) | |||
|
144 | settings = {key: test_value} | |||
|
145 | _sanitize_vcs_settings(settings) | |||
|
146 | assert settings[key] == test_value.lower() | |||
|
147 | ||||
|
148 | @pytest.mark.parametrize('key, default', _bool_settings) | |||
|
149 | def test_bool_setting_true(self, key, default): | |||
|
150 | settings = {key: 'true'} | |||
|
151 | _sanitize_vcs_settings(settings) | |||
|
152 | assert settings[key] is True | |||
|
153 | ||||
|
154 | @pytest.mark.parametrize('key, default', _bool_settings) | |||
|
155 | def test_bool_setting_false(self, key, default): | |||
|
156 | settings = {key: 'false'} | |||
|
157 | _sanitize_vcs_settings(settings) | |||
|
158 | assert settings[key] is False | |||
|
159 | ||||
|
160 | @pytest.mark.parametrize('key, default', _bool_settings) | |||
|
161 | def test_bool_setting_invalid_string(self, key, default): | |||
|
162 | settings = {key: 'no-bool-val-string'} | |||
|
163 | _sanitize_vcs_settings(settings) | |||
|
164 | assert settings[key] is False | |||
|
165 | ||||
|
166 | @pytest.mark.parametrize('key, default', _bool_settings) | |||
|
167 | def test_bool_setting_default(self, key, default): | |||
|
168 | settings = {} | |||
|
169 | _sanitize_vcs_settings(settings) | |||
|
170 | assert settings[key] is default |
General Comments 0
You need to be logged in to leave comments.
Login now