##// END OF EJS Templates
tests: Add some tests to check that events/extensions are triggered on hook execution....
Martin Bornhold -
r910:1b15cb87 default
parent child Browse files
Show More
@@ -1,54 +1,142 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 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
25
26
26 @mock.patch.multiple(
27 @mock.patch.multiple(
27 hooks_base,
28 hooks_base,
28 action_logger=mock.Mock(),
29 action_logger=mock.Mock(),
29 post_push_extension=mock.Mock(),
30 post_push_extension=mock.Mock(),
30 Repository=mock.Mock())
31 Repository=mock.Mock())
31 def test_post_push_truncates_commits(user_regular, repo_stub):
32 def test_post_push_truncates_commits(user_regular, repo_stub):
32 extras = {
33 extras = {
33 'ip': '127.0.0.1',
34 'ip': '127.0.0.1',
34 'username': user_regular.username,
35 'username': user_regular.username,
35 'action': 'push_local',
36 'action': 'push_local',
36 'repository': repo_stub.repo_name,
37 'repository': repo_stub.repo_name,
37 'scm': 'git',
38 'scm': 'git',
38 'config': '',
39 'config': '',
39 'server_url': 'http://example.com',
40 'server_url': 'http://example.com',
40 'make_lock': None,
41 'make_lock': None,
41 'locked_by': [None],
42 'locked_by': [None],
42 'commit_ids': ['abcde12345' * 4] * 30000,
43 'commit_ids': ['abcde12345' * 4] * 30000,
43 'is_shadow_repo': False,
44 'is_shadow_repo': False,
44 }
45 }
45 extras = utils2.AttributeDict(extras)
46 extras = utils2.AttributeDict(extras)
46
47
47 hooks_base.post_push(extras)
48 hooks_base.post_push(extras)
48
49
49 # Calculate appropriate action string here
50 # Calculate appropriate action string here
50 expected_action = 'push_local:%s' % ','.join(extras.commit_ids[:29000])
51 expected_action = 'push_local:%s' % ','.join(extras.commit_ids[:29000])
51
52
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