tests: Add some tests to check that events/extensions are triggered on hook execution.

This also includes tests to check that events/extensions are NOT triggered
on requests to shadow repositories.
This commit is contained in:
Martin Bornhold 2016-09-23 16:23:08 +02:00
parent 3c6a97202a
commit 69f6006e28

View file

@ -19,6 +19,7 @@
# and proprietary license terms, please see https://rhodecode.com/licenses/
import mock
import pytest
from rhodecode.lib import hooks_base, utils2
@ -52,3 +53,90 @@ def test_post_push_truncates_commits(user_regular, repo_stub):
hooks_base.action_logger.assert_called_with(
extras.username, expected_action, extras.repository, extras.ip,
commit=True)
def assert_called_with_mock(callable_, expected_mock_name):
mock_obj = callable_.call_args[0][0]
mock_name = mock_obj._mock_new_parent._mock_new_name
assert mock_name == expected_mock_name
@pytest.fixture
def hook_extras(user_regular, repo_stub):
extras = utils2.AttributeDict({
'ip': '127.0.0.1',
'username': user_regular.username,
'action': 'push',
'repository': repo_stub.repo_name,
'scm': '',
'config': '',
'server_url': 'http://example.com',
'make_lock': None,
'locked_by': [None],
'commit_ids': [],
'is_shadow_repo': False,
})
return extras
@pytest.mark.parametrize('func, extension, event', [
(hooks_base.pre_push, 'pre_push_extension', 'RepoPrePushEvent'),
(hooks_base.post_push, 'post_pull_extension', 'RepoPushEvent'),
(hooks_base.pre_pull, 'pre_pull_extension', 'RepoPrePullEvent'),
(hooks_base.post_pull, 'post_push_extension', 'RepoPullEvent'),
])
def test_hooks_propagate(func, extension, event, hook_extras):
"""
Tests that our hook code propagates to rhodecode extensions and triggers
the appropriate event.
"""
extension_mock = mock.Mock()
events_mock = mock.Mock()
patches = {
'Repository': mock.Mock(),
'events': events_mock,
extension: extension_mock,
}
# Clear shadow repo flag.
hook_extras.is_shadow_repo = False
# Execute hook function.
with mock.patch.multiple(hooks_base, **patches):
func(hook_extras)
# Assert that extensions are called and event was fired.
extension_mock.called_once()
assert_called_with_mock(events_mock.trigger, event)
@pytest.mark.parametrize('func, extension, event', [
(hooks_base.pre_push, 'pre_push_extension', 'RepoPrePushEvent'),
(hooks_base.post_push, 'post_pull_extension', 'RepoPushEvent'),
(hooks_base.pre_pull, 'pre_pull_extension', 'RepoPrePullEvent'),
(hooks_base.post_pull, 'post_push_extension', 'RepoPullEvent'),
])
def test_hooks_propagates_not_on_shadow(func, extension, event, hook_extras):
"""
If hooks are called by a request to a shadow repo we only want to run our
internal hooks code but not external ones like rhodecode extensions or
trigger an event.
"""
extension_mock = mock.Mock()
events_mock = mock.Mock()
patches = {
'Repository': mock.Mock(),
'events': events_mock,
extension: extension_mock,
}
# Set shadow repo flag.
hook_extras.is_shadow_repo = True
# Execute hook function.
with mock.patch.multiple(hooks_base, **patches):
func(hook_extras)
# Assert that extensions are *not* called and event was *not* fired.
assert not extension_mock.called
assert not events_mock.trigger.called