Show More
@@ -1,310 +1,417 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-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 | import base64 |
|
21 | import base64 | |
22 |
|
22 | |||
23 | import mock |
|
23 | import mock | |
24 | import pytest |
|
24 | import pytest | |
25 | import webtest.app |
|
25 | import webtest.app | |
26 |
|
26 | |||
27 | from rhodecode.lib.caching_query import FromCache |
|
27 | from rhodecode.lib.caching_query import FromCache | |
28 | from rhodecode.lib.hooks_daemon import DummyHooksCallbackDaemon |
|
28 | from rhodecode.lib.hooks_daemon import DummyHooksCallbackDaemon | |
29 | from rhodecode.lib.middleware import simplevcs |
|
29 | from rhodecode.lib.middleware import simplevcs | |
30 | from rhodecode.lib.middleware.https_fixup import HttpsFixup |
|
30 | from rhodecode.lib.middleware.https_fixup import HttpsFixup | |
31 | from rhodecode.lib.middleware.utils import scm_app |
|
31 | from rhodecode.lib.middleware.utils import scm_app | |
32 | from rhodecode.model.db import User, _hash_key |
|
32 | from rhodecode.model.db import User, _hash_key | |
33 | from rhodecode.model.meta import Session |
|
33 | from rhodecode.model.meta import Session | |
34 | from rhodecode.tests import ( |
|
34 | from rhodecode.tests import ( | |
35 | HG_REPO, TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS) |
|
35 | HG_REPO, TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS) | |
36 | from rhodecode.tests.lib.middleware import mock_scm_app |
|
36 | from rhodecode.tests.lib.middleware import mock_scm_app | |
37 | from rhodecode.tests.utils import set_anonymous_access |
|
37 | from rhodecode.tests.utils import set_anonymous_access | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | class StubVCSController(simplevcs.SimpleVCS): |
|
40 | class StubVCSController(simplevcs.SimpleVCS): | |
41 |
|
41 | |||
42 | SCM = 'hg' |
|
42 | SCM = 'hg' | |
43 | stub_response_body = tuple() |
|
43 | stub_response_body = tuple() | |
44 |
|
44 | |||
45 | def __init__(self, *args, **kwargs): |
|
45 | def __init__(self, *args, **kwargs): | |
46 | super(StubVCSController, self).__init__(*args, **kwargs) |
|
46 | super(StubVCSController, self).__init__(*args, **kwargs) | |
47 | self._action = 'pull' |
|
47 | self._action = 'pull' | |
48 | self._name = HG_REPO |
|
48 | self._name = HG_REPO | |
49 | self.set_repo_names(None) |
|
49 | self.set_repo_names(None) | |
50 |
|
50 | |||
51 | def _get_repository_name(self, environ): |
|
51 | def _get_repository_name(self, environ): | |
52 | return self._name |
|
52 | return self._name | |
53 |
|
53 | |||
54 | def _get_action(self, environ): |
|
54 | def _get_action(self, environ): | |
55 | return self._action |
|
55 | return self._action | |
56 |
|
56 | |||
57 | def _create_wsgi_app(self, repo_path, repo_name, config): |
|
57 | def _create_wsgi_app(self, repo_path, repo_name, config): | |
58 | def fake_app(environ, start_response): |
|
58 | def fake_app(environ, start_response): | |
59 | start_response('200 OK', []) |
|
59 | start_response('200 OK', []) | |
60 | return self.stub_response_body |
|
60 | return self.stub_response_body | |
61 | return fake_app |
|
61 | return fake_app | |
62 |
|
62 | |||
63 | def _create_config(self, extras, repo_name): |
|
63 | def _create_config(self, extras, repo_name): | |
64 | return None |
|
64 | return None | |
65 |
|
65 | |||
66 |
|
66 | |||
67 | @pytest.fixture |
|
67 | @pytest.fixture | |
68 | def vcscontroller(pylonsapp, config_stub): |
|
68 | def vcscontroller(pylonsapp, config_stub): | |
69 | config_stub.testing_securitypolicy() |
|
69 | config_stub.testing_securitypolicy() | |
70 | config_stub.include('rhodecode.authentication') |
|
70 | config_stub.include('rhodecode.authentication') | |
71 |
|
71 | |||
72 | set_anonymous_access(True) |
|
72 | set_anonymous_access(True) | |
73 | controller = StubVCSController(pylonsapp, pylonsapp.config, None) |
|
73 | controller = StubVCSController(pylonsapp, pylonsapp.config, None) | |
74 | app = HttpsFixup(controller, pylonsapp.config) |
|
74 | app = HttpsFixup(controller, pylonsapp.config) | |
75 | app = webtest.app.TestApp(app) |
|
75 | app = webtest.app.TestApp(app) | |
76 |
|
76 | |||
77 | _remove_default_user_from_query_cache() |
|
77 | _remove_default_user_from_query_cache() | |
78 |
|
78 | |||
79 | # Sanity checks that things are set up correctly |
|
79 | # Sanity checks that things are set up correctly | |
80 | app.get('/' + HG_REPO, status=200) |
|
80 | app.get('/' + HG_REPO, status=200) | |
81 |
|
81 | |||
82 | app.controller = controller |
|
82 | app.controller = controller | |
83 | return app |
|
83 | return app | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | def _remove_default_user_from_query_cache(): |
|
86 | def _remove_default_user_from_query_cache(): | |
87 | user = User.get_default_user(cache=True) |
|
87 | user = User.get_default_user(cache=True) | |
88 | query = Session().query(User).filter(User.username == user.username) |
|
88 | query = Session().query(User).filter(User.username == user.username) | |
89 | query = query.options(FromCache( |
|
89 | query = query.options(FromCache( | |
90 | "sql_cache_short", "get_user_%s" % _hash_key(user.username))) |
|
90 | "sql_cache_short", "get_user_%s" % _hash_key(user.username))) | |
91 | query.invalidate() |
|
91 | query.invalidate() | |
92 | Session().expire(user) |
|
92 | Session().expire(user) | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | @pytest.fixture |
|
95 | @pytest.fixture | |
96 | def disable_anonymous_user(request, pylonsapp): |
|
96 | def disable_anonymous_user(request, pylonsapp): | |
97 | set_anonymous_access(False) |
|
97 | set_anonymous_access(False) | |
98 |
|
98 | |||
99 | @request.addfinalizer |
|
99 | @request.addfinalizer | |
100 | def cleanup(): |
|
100 | def cleanup(): | |
101 | set_anonymous_access(True) |
|
101 | set_anonymous_access(True) | |
102 |
|
102 | |||
103 |
|
103 | |||
104 | def test_handles_exceptions_during_permissions_checks( |
|
104 | def test_handles_exceptions_during_permissions_checks( | |
105 | vcscontroller, disable_anonymous_user): |
|
105 | vcscontroller, disable_anonymous_user): | |
106 | user_and_pass = '%s:%s' % (TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS) |
|
106 | user_and_pass = '%s:%s' % (TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS) | |
107 | auth_password = base64.encodestring(user_and_pass).strip() |
|
107 | auth_password = base64.encodestring(user_and_pass).strip() | |
108 | extra_environ = { |
|
108 | extra_environ = { | |
109 | 'AUTH_TYPE': 'Basic', |
|
109 | 'AUTH_TYPE': 'Basic', | |
110 | 'HTTP_AUTHORIZATION': 'Basic %s' % auth_password, |
|
110 | 'HTTP_AUTHORIZATION': 'Basic %s' % auth_password, | |
111 | 'REMOTE_USER': TEST_USER_ADMIN_LOGIN, |
|
111 | 'REMOTE_USER': TEST_USER_ADMIN_LOGIN, | |
112 | } |
|
112 | } | |
113 |
|
113 | |||
114 | # Verify that things are hooked up correctly |
|
114 | # Verify that things are hooked up correctly | |
115 | vcscontroller.get('/', status=200, extra_environ=extra_environ) |
|
115 | vcscontroller.get('/', status=200, extra_environ=extra_environ) | |
116 |
|
116 | |||
117 | # Simulate trouble during permission checks |
|
117 | # Simulate trouble during permission checks | |
118 | with mock.patch('rhodecode.model.db.User.get_by_username', |
|
118 | with mock.patch('rhodecode.model.db.User.get_by_username', | |
119 | side_effect=Exception) as get_user: |
|
119 | side_effect=Exception) as get_user: | |
120 | # Verify that a correct 500 is returned and check that the expected |
|
120 | # Verify that a correct 500 is returned and check that the expected | |
121 | # code path was hit. |
|
121 | # code path was hit. | |
122 | vcscontroller.get('/', status=500, extra_environ=extra_environ) |
|
122 | vcscontroller.get('/', status=500, extra_environ=extra_environ) | |
123 | assert get_user.called |
|
123 | assert get_user.called | |
124 |
|
124 | |||
125 |
|
125 | |||
126 | def test_returns_forbidden_if_no_anonymous_access( |
|
126 | def test_returns_forbidden_if_no_anonymous_access( | |
127 | vcscontroller, disable_anonymous_user): |
|
127 | vcscontroller, disable_anonymous_user): | |
128 | vcscontroller.get('/', status=401) |
|
128 | vcscontroller.get('/', status=401) | |
129 |
|
129 | |||
130 |
|
130 | |||
131 | class StubFailVCSController(simplevcs.SimpleVCS): |
|
131 | class StubFailVCSController(simplevcs.SimpleVCS): | |
132 | def _handle_request(self, environ, start_response): |
|
132 | def _handle_request(self, environ, start_response): | |
133 | raise Exception("BOOM") |
|
133 | raise Exception("BOOM") | |
134 |
|
134 | |||
135 |
|
135 | |||
136 | @pytest.fixture(scope='module') |
|
136 | @pytest.fixture(scope='module') | |
137 | def fail_controller(pylonsapp): |
|
137 | def fail_controller(pylonsapp): | |
138 | controller = StubFailVCSController(pylonsapp, pylonsapp.config, None) |
|
138 | controller = StubFailVCSController(pylonsapp, pylonsapp.config, None) | |
139 | controller = HttpsFixup(controller, pylonsapp.config) |
|
139 | controller = HttpsFixup(controller, pylonsapp.config) | |
140 | controller = webtest.app.TestApp(controller) |
|
140 | controller = webtest.app.TestApp(controller) | |
141 | return controller |
|
141 | return controller | |
142 |
|
142 | |||
143 |
|
143 | |||
144 | def test_handles_exceptions_as_internal_server_error(fail_controller): |
|
144 | def test_handles_exceptions_as_internal_server_error(fail_controller): | |
145 | fail_controller.get('/', status=500) |
|
145 | fail_controller.get('/', status=500) | |
146 |
|
146 | |||
147 |
|
147 | |||
148 | def test_provides_traceback_for_appenlight(fail_controller): |
|
148 | def test_provides_traceback_for_appenlight(fail_controller): | |
149 | response = fail_controller.get( |
|
149 | response = fail_controller.get( | |
150 | '/', status=500, extra_environ={'appenlight.client': 'fake'}) |
|
150 | '/', status=500, extra_environ={'appenlight.client': 'fake'}) | |
151 | assert 'appenlight.__traceback' in response.request.environ |
|
151 | assert 'appenlight.__traceback' in response.request.environ | |
152 |
|
152 | |||
153 |
|
153 | |||
154 | def test_provides_utils_scm_app_as_scm_app_by_default(pylonsapp): |
|
154 | def test_provides_utils_scm_app_as_scm_app_by_default(pylonsapp): | |
155 | controller = StubVCSController(pylonsapp, pylonsapp.config, None) |
|
155 | controller = StubVCSController(pylonsapp, pylonsapp.config, None) | |
156 | assert controller.scm_app is scm_app |
|
156 | assert controller.scm_app is scm_app | |
157 |
|
157 | |||
158 |
|
158 | |||
159 | def test_allows_to_override_scm_app_via_config(pylonsapp): |
|
159 | def test_allows_to_override_scm_app_via_config(pylonsapp): | |
160 | config = pylonsapp.config.copy() |
|
160 | config = pylonsapp.config.copy() | |
161 | config['vcs.scm_app_implementation'] = ( |
|
161 | config['vcs.scm_app_implementation'] = ( | |
162 | 'rhodecode.tests.lib.middleware.mock_scm_app') |
|
162 | 'rhodecode.tests.lib.middleware.mock_scm_app') | |
163 | controller = StubVCSController(pylonsapp, config, None) |
|
163 | controller = StubVCSController(pylonsapp, config, None) | |
164 | assert controller.scm_app is mock_scm_app |
|
164 | assert controller.scm_app is mock_scm_app | |
165 |
|
165 | |||
166 |
|
166 | |||
167 | @pytest.mark.parametrize('query_string, expected', [ |
|
167 | @pytest.mark.parametrize('query_string, expected', [ | |
168 | ('cmd=stub_command', True), |
|
168 | ('cmd=stub_command', True), | |
169 | ('cmd=listkeys', False), |
|
169 | ('cmd=listkeys', False), | |
170 | ]) |
|
170 | ]) | |
171 | def test_should_check_locking(query_string, expected): |
|
171 | def test_should_check_locking(query_string, expected): | |
172 | result = simplevcs._should_check_locking(query_string) |
|
172 | result = simplevcs._should_check_locking(query_string) | |
173 | assert result == expected |
|
173 | assert result == expected | |
174 |
|
174 | |||
175 |
|
175 | |||
|
176 | @pytest.mark.backends('git', 'hg') | |||
|
177 | class TestShadowRepoExposure(object): | |||
|
178 | def test_pull_on_shadow_repo_propagates_to_wsgi_app(self, pylonsapp): | |||
|
179 | """ | |||
|
180 | Check that a pull action to a shadow repo is propagated to the | |||
|
181 | underlying wsgi app. | |||
|
182 | """ | |||
|
183 | controller = StubVCSController(pylonsapp, pylonsapp.config, None) | |||
|
184 | controller._check_ssl = mock.Mock() | |||
|
185 | controller.is_shadow_repo = True | |||
|
186 | controller._action = 'pull' | |||
|
187 | controller.stub_response_body = 'dummy body value' | |||
|
188 | environ_stub = { | |||
|
189 | 'HTTP_HOST': 'test.example.com', | |||
|
190 | 'REQUEST_METHOD': 'GET', | |||
|
191 | 'wsgi.url_scheme': 'http', | |||
|
192 | } | |||
|
193 | ||||
|
194 | response = controller(environ_stub, mock.Mock()) | |||
|
195 | response_body = ''.join(response) | |||
|
196 | ||||
|
197 | # Assert that we got the response from the wsgi app. | |||
|
198 | assert response_body == controller.stub_response_body | |||
|
199 | ||||
|
200 | def test_push_on_shadow_repo_raises(self, pylonsapp): | |||
|
201 | """ | |||
|
202 | Check that a push action to a shadow repo is aborted. | |||
|
203 | """ | |||
|
204 | controller = StubVCSController(pylonsapp, pylonsapp.config, None) | |||
|
205 | controller._check_ssl = mock.Mock() | |||
|
206 | controller.is_shadow_repo = True | |||
|
207 | controller._action = 'push' | |||
|
208 | controller.stub_response_body = 'dummy body value' | |||
|
209 | environ_stub = { | |||
|
210 | 'HTTP_HOST': 'test.example.com', | |||
|
211 | 'REQUEST_METHOD': 'GET', | |||
|
212 | 'wsgi.url_scheme': 'http', | |||
|
213 | } | |||
|
214 | ||||
|
215 | response = controller(environ_stub, mock.Mock()) | |||
|
216 | response_body = ''.join(response) | |||
|
217 | ||||
|
218 | assert response_body != controller.stub_response_body | |||
|
219 | # Assert that a 406 error is returned. | |||
|
220 | assert '406 Not Acceptable' in response_body | |||
|
221 | ||||
|
222 | def test_set_repo_names_no_shadow(self, pylonsapp): | |||
|
223 | """ | |||
|
224 | Check that the set_repo_names method sets all names to the one returned | |||
|
225 | by the _get_repository_name method on a request to a non shadow repo. | |||
|
226 | """ | |||
|
227 | environ_stub = {} | |||
|
228 | controller = StubVCSController(pylonsapp, pylonsapp.config, None) | |||
|
229 | controller._name = 'RepoGroup/MyRepo' | |||
|
230 | controller.set_repo_names(environ_stub) | |||
|
231 | assert not controller.is_shadow_repo | |||
|
232 | assert (controller.url_repo_name == | |||
|
233 | controller.acl_repo_name == | |||
|
234 | controller.vcs_repo_name == | |||
|
235 | controller._get_repository_name(environ_stub)) | |||
|
236 | ||||
|
237 | def test_set_repo_names_with_shadow(self, pylonsapp, pr_util): | |||
|
238 | """ | |||
|
239 | Check that the set_repo_names method sets correct names on a request | |||
|
240 | to a shadow repo. | |||
|
241 | """ | |||
|
242 | from rhodecode.model.pull_request import PullRequestModel | |||
|
243 | ||||
|
244 | pull_request = pr_util.create_pull_request() | |||
|
245 | shadow_url = '{target}/pull-request/{pr_id}/repository'.format( | |||
|
246 | target=pull_request.target_repo.repo_name, | |||
|
247 | pr_id=pull_request.pull_request_id) | |||
|
248 | controller = StubVCSController(pylonsapp, pylonsapp.config, None) | |||
|
249 | controller._name = shadow_url | |||
|
250 | controller.set_repo_names({}) | |||
|
251 | ||||
|
252 | # Get file system path to shadow repo for assertions. | |||
|
253 | workspace_id = PullRequestModel()._workspace_id(pull_request) | |||
|
254 | target_vcs = pull_request.target_repo.scm_instance() | |||
|
255 | vcs_repo_name = target_vcs._get_shadow_repository_path( | |||
|
256 | workspace_id) | |||
|
257 | ||||
|
258 | assert controller.vcs_repo_name == vcs_repo_name | |||
|
259 | assert controller.url_repo_name == shadow_url | |||
|
260 | assert controller.acl_repo_name == pull_request.target_repo.repo_name | |||
|
261 | assert controller.is_shadow_repo | |||
|
262 | ||||
|
263 | def test_set_repo_names_with_shadow_but_missing_pr( | |||
|
264 | self, pylonsapp, pr_util): | |||
|
265 | """ | |||
|
266 | Checks that the set_repo_names method enforces matching target repos | |||
|
267 | and pull request IDs. | |||
|
268 | """ | |||
|
269 | pull_request = pr_util.create_pull_request() | |||
|
270 | shadow_url = '{target}/pull-request/{pr_id}/repository'.format( | |||
|
271 | target=pull_request.target_repo.repo_name, | |||
|
272 | pr_id=999999999) | |||
|
273 | controller = StubVCSController(pylonsapp, pylonsapp.config, None) | |||
|
274 | controller._name = shadow_url | |||
|
275 | controller.set_repo_names({}) | |||
|
276 | ||||
|
277 | assert not controller.is_shadow_repo | |||
|
278 | assert (controller.url_repo_name == | |||
|
279 | controller.acl_repo_name == | |||
|
280 | controller.vcs_repo_name) | |||
|
281 | ||||
|
282 | ||||
176 | @mock.patch.multiple( |
|
283 | @mock.patch.multiple( | |
177 | 'Pyro4.config', SERVERTYPE='multiplex', POLLTIMEOUT=0.01) |
|
284 | 'Pyro4.config', SERVERTYPE='multiplex', POLLTIMEOUT=0.01) | |
178 | class TestGenerateVcsResponse: |
|
285 | class TestGenerateVcsResponse: | |
179 |
|
286 | |||
180 | def test_ensures_that_start_response_is_called_early_enough(self): |
|
287 | def test_ensures_that_start_response_is_called_early_enough(self): | |
181 | self.call_controller_with_response_body(iter(['a', 'b'])) |
|
288 | self.call_controller_with_response_body(iter(['a', 'b'])) | |
182 | assert self.start_response.called |
|
289 | assert self.start_response.called | |
183 |
|
290 | |||
184 | def test_invalidates_cache_after_body_is_consumed(self): |
|
291 | def test_invalidates_cache_after_body_is_consumed(self): | |
185 | result = self.call_controller_with_response_body(iter(['a', 'b'])) |
|
292 | result = self.call_controller_with_response_body(iter(['a', 'b'])) | |
186 | assert not self.was_cache_invalidated() |
|
293 | assert not self.was_cache_invalidated() | |
187 | # Consume the result |
|
294 | # Consume the result | |
188 | list(result) |
|
295 | list(result) | |
189 | assert self.was_cache_invalidated() |
|
296 | assert self.was_cache_invalidated() | |
190 |
|
297 | |||
191 | @mock.patch('rhodecode.lib.middleware.simplevcs.HTTPLockedRC') |
|
298 | @mock.patch('rhodecode.lib.middleware.simplevcs.HTTPLockedRC') | |
192 | def test_handles_locking_exception(self, http_locked_rc): |
|
299 | def test_handles_locking_exception(self, http_locked_rc): | |
193 | result = self.call_controller_with_response_body( |
|
300 | result = self.call_controller_with_response_body( | |
194 | self.raise_result_iter(vcs_kind='repo_locked')) |
|
301 | self.raise_result_iter(vcs_kind='repo_locked')) | |
195 | assert not http_locked_rc.called |
|
302 | assert not http_locked_rc.called | |
196 | # Consume the result |
|
303 | # Consume the result | |
197 | list(result) |
|
304 | list(result) | |
198 | assert http_locked_rc.called |
|
305 | assert http_locked_rc.called | |
199 |
|
306 | |||
200 | @mock.patch('rhodecode.lib.middleware.simplevcs.HTTPRequirementError') |
|
307 | @mock.patch('rhodecode.lib.middleware.simplevcs.HTTPRequirementError') | |
201 | def test_handles_requirement_exception(self, http_requirement): |
|
308 | def test_handles_requirement_exception(self, http_requirement): | |
202 | result = self.call_controller_with_response_body( |
|
309 | result = self.call_controller_with_response_body( | |
203 | self.raise_result_iter(vcs_kind='requirement')) |
|
310 | self.raise_result_iter(vcs_kind='requirement')) | |
204 | assert not http_requirement.called |
|
311 | assert not http_requirement.called | |
205 | # Consume the result |
|
312 | # Consume the result | |
206 | list(result) |
|
313 | list(result) | |
207 | assert http_requirement.called |
|
314 | assert http_requirement.called | |
208 |
|
315 | |||
209 | @mock.patch('rhodecode.lib.middleware.simplevcs.HTTPLockedRC') |
|
316 | @mock.patch('rhodecode.lib.middleware.simplevcs.HTTPLockedRC') | |
210 | def test_handles_locking_exception_in_app_call(self, http_locked_rc): |
|
317 | def test_handles_locking_exception_in_app_call(self, http_locked_rc): | |
211 | app_factory_patcher = mock.patch.object( |
|
318 | app_factory_patcher = mock.patch.object( | |
212 | StubVCSController, '_create_wsgi_app') |
|
319 | StubVCSController, '_create_wsgi_app') | |
213 | with app_factory_patcher as app_factory: |
|
320 | with app_factory_patcher as app_factory: | |
214 | app_factory().side_effect = self.vcs_exception() |
|
321 | app_factory().side_effect = self.vcs_exception() | |
215 | result = self.call_controller_with_response_body(['a']) |
|
322 | result = self.call_controller_with_response_body(['a']) | |
216 | list(result) |
|
323 | list(result) | |
217 | assert http_locked_rc.called |
|
324 | assert http_locked_rc.called | |
218 |
|
325 | |||
219 | def test_raises_unknown_exceptions(self): |
|
326 | def test_raises_unknown_exceptions(self): | |
220 | result = self.call_controller_with_response_body( |
|
327 | result = self.call_controller_with_response_body( | |
221 | self.raise_result_iter(vcs_kind='unknown')) |
|
328 | self.raise_result_iter(vcs_kind='unknown')) | |
222 | with pytest.raises(Exception): |
|
329 | with pytest.raises(Exception): | |
223 | list(result) |
|
330 | list(result) | |
224 |
|
331 | |||
225 | def test_prepare_callback_daemon_is_called(self): |
|
332 | def test_prepare_callback_daemon_is_called(self): | |
226 | def side_effect(extras): |
|
333 | def side_effect(extras): | |
227 | return DummyHooksCallbackDaemon(), extras |
|
334 | return DummyHooksCallbackDaemon(), extras | |
228 |
|
335 | |||
229 | prepare_patcher = mock.patch.object( |
|
336 | prepare_patcher = mock.patch.object( | |
230 | StubVCSController, '_prepare_callback_daemon') |
|
337 | StubVCSController, '_prepare_callback_daemon') | |
231 | with prepare_patcher as prepare_mock: |
|
338 | with prepare_patcher as prepare_mock: | |
232 | prepare_mock.side_effect = side_effect |
|
339 | prepare_mock.side_effect = side_effect | |
233 | self.call_controller_with_response_body(iter(['a', 'b'])) |
|
340 | self.call_controller_with_response_body(iter(['a', 'b'])) | |
234 | assert prepare_mock.called |
|
341 | assert prepare_mock.called | |
235 | assert prepare_mock.call_count == 1 |
|
342 | assert prepare_mock.call_count == 1 | |
236 |
|
343 | |||
237 | def call_controller_with_response_body(self, response_body): |
|
344 | def call_controller_with_response_body(self, response_body): | |
238 | settings = { |
|
345 | settings = { | |
239 | 'base_path': 'fake_base_path', |
|
346 | 'base_path': 'fake_base_path', | |
240 | 'vcs.hooks.protocol': 'http', |
|
347 | 'vcs.hooks.protocol': 'http', | |
241 | 'vcs.hooks.direct_calls': False, |
|
348 | 'vcs.hooks.direct_calls': False, | |
242 | } |
|
349 | } | |
243 | controller = StubVCSController(None, settings, None) |
|
350 | controller = StubVCSController(None, settings, None) | |
244 | controller._invalidate_cache = mock.Mock() |
|
351 | controller._invalidate_cache = mock.Mock() | |
245 | controller.stub_response_body = response_body |
|
352 | controller.stub_response_body = response_body | |
246 | self.start_response = mock.Mock() |
|
353 | self.start_response = mock.Mock() | |
247 | result = controller._generate_vcs_response( |
|
354 | result = controller._generate_vcs_response( | |
248 | environ={}, start_response=self.start_response, |
|
355 | environ={}, start_response=self.start_response, | |
249 | repo_path='fake_repo_path', |
|
356 | repo_path='fake_repo_path', | |
250 | repo_name='fake_repo_name', |
|
357 | repo_name='fake_repo_name', | |
251 | extras={}, action='push') |
|
358 | extras={}, action='push') | |
252 | self.controller = controller |
|
359 | self.controller = controller | |
253 | return result |
|
360 | return result | |
254 |
|
361 | |||
255 | def raise_result_iter(self, vcs_kind='repo_locked'): |
|
362 | def raise_result_iter(self, vcs_kind='repo_locked'): | |
256 | """ |
|
363 | """ | |
257 | Simulates an exception due to a vcs raised exception if kind vcs_kind |
|
364 | Simulates an exception due to a vcs raised exception if kind vcs_kind | |
258 | """ |
|
365 | """ | |
259 | raise self.vcs_exception(vcs_kind=vcs_kind) |
|
366 | raise self.vcs_exception(vcs_kind=vcs_kind) | |
260 | yield "never_reached" |
|
367 | yield "never_reached" | |
261 |
|
368 | |||
262 | def vcs_exception(self, vcs_kind='repo_locked'): |
|
369 | def vcs_exception(self, vcs_kind='repo_locked'): | |
263 | locked_exception = Exception('TEST_MESSAGE') |
|
370 | locked_exception = Exception('TEST_MESSAGE') | |
264 | locked_exception._vcs_kind = vcs_kind |
|
371 | locked_exception._vcs_kind = vcs_kind | |
265 | return locked_exception |
|
372 | return locked_exception | |
266 |
|
373 | |||
267 | def was_cache_invalidated(self): |
|
374 | def was_cache_invalidated(self): | |
268 | return self.controller._invalidate_cache.called |
|
375 | return self.controller._invalidate_cache.called | |
269 |
|
376 | |||
270 |
|
377 | |||
271 | class TestInitializeGenerator: |
|
378 | class TestInitializeGenerator: | |
272 |
|
379 | |||
273 | def test_drains_first_element(self): |
|
380 | def test_drains_first_element(self): | |
274 | gen = self.factory(['__init__', 1, 2]) |
|
381 | gen = self.factory(['__init__', 1, 2]) | |
275 | result = list(gen) |
|
382 | result = list(gen) | |
276 | assert result == [1, 2] |
|
383 | assert result == [1, 2] | |
277 |
|
384 | |||
278 | @pytest.mark.parametrize('values', [ |
|
385 | @pytest.mark.parametrize('values', [ | |
279 | [], |
|
386 | [], | |
280 | [1, 2], |
|
387 | [1, 2], | |
281 | ]) |
|
388 | ]) | |
282 | def test_raises_value_error(self, values): |
|
389 | def test_raises_value_error(self, values): | |
283 | with pytest.raises(ValueError): |
|
390 | with pytest.raises(ValueError): | |
284 | self.factory(values) |
|
391 | self.factory(values) | |
285 |
|
392 | |||
286 | @simplevcs.initialize_generator |
|
393 | @simplevcs.initialize_generator | |
287 | def factory(self, iterable): |
|
394 | def factory(self, iterable): | |
288 | for elem in iterable: |
|
395 | for elem in iterable: | |
289 | yield elem |
|
396 | yield elem | |
290 |
|
397 | |||
291 |
|
398 | |||
292 | class TestPrepareHooksDaemon(object): |
|
399 | class TestPrepareHooksDaemon(object): | |
293 | def test_calls_imported_prepare_callback_daemon(self, app_settings): |
|
400 | def test_calls_imported_prepare_callback_daemon(self, app_settings): | |
294 | expected_extras = {'extra1': 'value1'} |
|
401 | expected_extras = {'extra1': 'value1'} | |
295 | daemon = DummyHooksCallbackDaemon() |
|
402 | daemon = DummyHooksCallbackDaemon() | |
296 |
|
403 | |||
297 | controller = StubVCSController(None, app_settings, None) |
|
404 | controller = StubVCSController(None, app_settings, None) | |
298 | prepare_patcher = mock.patch.object( |
|
405 | prepare_patcher = mock.patch.object( | |
299 | simplevcs, 'prepare_callback_daemon', |
|
406 | simplevcs, 'prepare_callback_daemon', | |
300 | return_value=(daemon, expected_extras)) |
|
407 | return_value=(daemon, expected_extras)) | |
301 | with prepare_patcher as prepare_mock: |
|
408 | with prepare_patcher as prepare_mock: | |
302 | callback_daemon, extras = controller._prepare_callback_daemon( |
|
409 | callback_daemon, extras = controller._prepare_callback_daemon( | |
303 | expected_extras.copy()) |
|
410 | expected_extras.copy()) | |
304 | prepare_mock.assert_called_once_with( |
|
411 | prepare_mock.assert_called_once_with( | |
305 | expected_extras, |
|
412 | expected_extras, | |
306 | protocol=app_settings['vcs.hooks.protocol'], |
|
413 | protocol=app_settings['vcs.hooks.protocol'], | |
307 | use_direct_calls=app_settings['vcs.hooks.direct_calls']) |
|
414 | use_direct_calls=app_settings['vcs.hooks.direct_calls']) | |
308 |
|
415 | |||
309 | assert callback_daemon == daemon |
|
416 | assert callback_daemon == daemon | |
310 | assert extras == extras |
|
417 | assert extras == extras |
General Comments 0
You need to be logged in to leave comments.
Login now