Show More
@@ -19,6 +19,7 b'' | |||||
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 mock |
|
21 | import mock | |
|
22 | import pytest | |||
22 |
|
23 | |||
23 | from rhodecode.lib import hooks_base, utils2 |
|
24 | from rhodecode.lib import hooks_base, utils2 | |
24 |
|
25 | |||
@@ -52,3 +53,90 b' def test_post_push_truncates_commits(use' | |||||
52 | hooks_base.action_logger.assert_called_with( |
|
53 | hooks_base.action_logger.assert_called_with( | |
53 | extras.username, expected_action, extras.repository, extras.ip, |
|
54 | extras.username, expected_action, extras.repository, extras.ip, | |
54 | commit=True) |
|
55 | commit=True) | |
|
56 | ||||
|
57 | ||||
|
58 | def assert_called_with_mock(callable_, expected_mock_name): | |||
|
59 | mock_obj = callable_.call_args[0][0] | |||
|
60 | mock_name = mock_obj._mock_new_parent._mock_new_name | |||
|
61 | assert mock_name == expected_mock_name | |||
|
62 | ||||
|
63 | ||||
|
64 | @pytest.fixture | |||
|
65 | def hook_extras(user_regular, repo_stub): | |||
|
66 | extras = utils2.AttributeDict({ | |||
|
67 | 'ip': '127.0.0.1', | |||
|
68 | 'username': user_regular.username, | |||
|
69 | 'action': 'push', | |||
|
70 | 'repository': repo_stub.repo_name, | |||
|
71 | 'scm': '', | |||
|
72 | 'config': '', | |||
|
73 | 'server_url': 'http://example.com', | |||
|
74 | 'make_lock': None, | |||
|
75 | 'locked_by': [None], | |||
|
76 | 'commit_ids': [], | |||
|
77 | 'is_shadow_repo': False, | |||
|
78 | }) | |||
|
79 | return extras | |||
|
80 | ||||
|
81 | ||||
|
82 | @pytest.mark.parametrize('func, extension, event', [ | |||
|
83 | (hooks_base.pre_push, 'pre_push_extension', 'RepoPrePushEvent'), | |||
|
84 | (hooks_base.post_push, 'post_pull_extension', 'RepoPushEvent'), | |||
|
85 | (hooks_base.pre_pull, 'pre_pull_extension', 'RepoPrePullEvent'), | |||
|
86 | (hooks_base.post_pull, 'post_push_extension', 'RepoPullEvent'), | |||
|
87 | ]) | |||
|
88 | def test_hooks_propagate(func, extension, event, hook_extras): | |||
|
89 | """ | |||
|
90 | Tests that our hook code propagates to rhodecode extensions and triggers | |||
|
91 | the appropriate event. | |||
|
92 | """ | |||
|
93 | extension_mock = mock.Mock() | |||
|
94 | events_mock = mock.Mock() | |||
|
95 | patches = { | |||
|
96 | 'Repository': mock.Mock(), | |||
|
97 | 'events': events_mock, | |||
|
98 | extension: extension_mock, | |||
|
99 | } | |||
|
100 | ||||
|
101 | # Clear shadow repo flag. | |||
|
102 | hook_extras.is_shadow_repo = False | |||
|
103 | ||||
|
104 | # Execute hook function. | |||
|
105 | with mock.patch.multiple(hooks_base, **patches): | |||
|
106 | func(hook_extras) | |||
|
107 | ||||
|
108 | # Assert that extensions are called and event was fired. | |||
|
109 | extension_mock.called_once() | |||
|
110 | assert_called_with_mock(events_mock.trigger, event) | |||
|
111 | ||||
|
112 | ||||
|
113 | @pytest.mark.parametrize('func, extension, event', [ | |||
|
114 | (hooks_base.pre_push, 'pre_push_extension', 'RepoPrePushEvent'), | |||
|
115 | (hooks_base.post_push, 'post_pull_extension', 'RepoPushEvent'), | |||
|
116 | (hooks_base.pre_pull, 'pre_pull_extension', 'RepoPrePullEvent'), | |||
|
117 | (hooks_base.post_pull, 'post_push_extension', 'RepoPullEvent'), | |||
|
118 | ]) | |||
|
119 | def test_hooks_propagates_not_on_shadow(func, extension, event, hook_extras): | |||
|
120 | """ | |||
|
121 | If hooks are called by a request to a shadow repo we only want to run our | |||
|
122 | internal hooks code but not external ones like rhodecode extensions or | |||
|
123 | trigger an event. | |||
|
124 | """ | |||
|
125 | extension_mock = mock.Mock() | |||
|
126 | events_mock = mock.Mock() | |||
|
127 | patches = { | |||
|
128 | 'Repository': mock.Mock(), | |||
|
129 | 'events': events_mock, | |||
|
130 | extension: extension_mock, | |||
|
131 | } | |||
|
132 | ||||
|
133 | # Set shadow repo flag. | |||
|
134 | hook_extras.is_shadow_repo = True | |||
|
135 | ||||
|
136 | # Execute hook function. | |||
|
137 | with mock.patch.multiple(hooks_base, **patches): | |||
|
138 | func(hook_extras) | |||
|
139 | ||||
|
140 | # Assert that extensions are *not* called and event was *not* fired. | |||
|
141 | assert not extension_mock.called | |||
|
142 | assert not events_mock.trigger.called |
General Comments 0
You need to be logged in to leave comments.
Login now