##// END OF EJS Templates
core: avoid using rhodecode.test packages inside main packages as tests are removed during build which can cause some problems in some edge case calls
core: avoid using rhodecode.test packages inside main packages as tests are removed during build which can cause some problems in some edge case calls

File last commit:

r5608:6d33e504 default
r5618:bdbdb63f default
Show More
test_webhook.py
195 lines | 6.7 KiB | text/x-python | PythonLexer
core: updated copyright to 2024
r5608 # Copyright (C) 2010-2024 RhodeCode GmbH
webhooks: added variables into the call URL. Fixes #4211
r938 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import pytest
integrations: restructure code and added better coverage.
r5123 import mock
from mock import patch
webhooks: added variables into the call URL. Fixes #4211
r938
from rhodecode import events
from rhodecode.lib.utils2 import AttributeDict
webhook: abstract webhook handler to base for easier re-usage.
r2585 from rhodecode.integrations.types.webhook import WebhookDataHandler
integrations: restructure code and added better coverage.
r5123 from rhodecode.tests import GIT_REPO
webhooks: added variables into the call URL. Fixes #4211
r938
pytest: use consistent way of creating a fixture by using pytest.fixture()
r3946 @pytest.fixture()
webhooks: added variables into the call URL. Fixes #4211
r938 def base_data():
return {
webhook: abstract webhook handler to base for easier re-usage.
r2585 'name': 'event',
webhooks: added variables into the call URL. Fixes #4211
r938 'repo': {
'repo_name': 'foo',
'repo_type': 'hg',
'repo_id': '12',
integrations: expose actor user_id, and username in webhook integration templates args....
r1709 'url': 'http://repo.url/foo',
webhook: enable support of extra repo variables as replacement in template url.
r1761 'extra_fields': {},
integrations: expose actor user_id, and username in webhook integration templates args....
r1709 },
'actor': {
'username': 'actor_name',
'user_id': 1
integrations: restructure code and added better coverage.
r5123 },
"pullrequest": {
"url": "https://example.com/pr1",
"pull_request_id": "1",
"title": "started pr",
"status": "new",
"commits_uid": ["1", "2"],
"shadow_url": "http://shadow-url",
},
"comment": {
"comment_id": 1,
"comment_text": "test-comment",
"status": "approved",
"comment_f_path": "text.py",
"comment_line_no": "1",
"comment_type": "note",
},
"commit": {
"commit_id": "efefef",
"commit_branch": "master",
"commit_message": "changed foo"
},
"push": {
"branches": "",
"commits": []
},
webhooks: added variables into the call URL. Fixes #4211
r938 }
def test_webhook_parse_url_invalid_event():
template_url = 'http://server.com/${repo_name}/build'
webhook: abstract webhook handler to base for easier re-usage.
r2585 handler = WebhookDataHandler(
template_url, {'exmaple-header': 'header-values'})
event = events.RepoDeleteEvent('')
webhooks: added variables into the call URL. Fixes #4211
r938 with pytest.raises(ValueError) as err:
webhook: abstract webhook handler to base for easier re-usage.
r2585 handler(event, {})
err = str(err.value)
events: added support for pull-request-comment and commit-comment events....
r4314 assert err == "event type `<class 'rhodecode.events.repo.RepoDeleteEvent'>` has no handler defined"
webhooks: added variables into the call URL. Fixes #4211
r938
@pytest.mark.parametrize('template,expected_urls', [
webhook: abstract webhook handler to base for easier re-usage.
r2585 ('http://server.com/${repo_name}/build',
['http://server.com/foo/build']),
('http://server.com/${repo_name}/${repo_type}',
['http://server.com/foo/hg']),
('http://${server}.com/${repo_name}/${repo_id}',
['http://${server}.com/foo/12']),
('http://server.com/${branch}/build',
['http://server.com/${branch}/build']),
webhooks: added variables into the call URL. Fixes #4211
r938 ])
def test_webook_parse_url_for_create_event(base_data, template, expected_urls):
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 headers = {'exmaple-header': 'header-values'}
webhook: abstract webhook handler to base for easier re-usage.
r2585 handler = WebhookDataHandler(template, headers)
webhooks: added variables into the call URL. Fixes #4211
r938 urls = handler(events.RepoCreateEvent(''), base_data)
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 assert urls == [
webhook: abstract webhook handler to base for easier re-usage.
r2585 (url, headers, base_data) for url in expected_urls]
webhooks: added variables into the call URL. Fixes #4211
r938
@pytest.mark.parametrize('template,expected_urls', [
webhook: abstract webhook handler to base for easier re-usage.
r2585 ('http://server.com/${repo_name}/${pull_request_id}',
['http://server.com/foo/999']),
('http://server.com/${repo_name}/${pull_request_url}',
webhook: quote URL variables to prevent url errors with special chars like # in pr title.
r3477 ['http://server.com/foo/http%3A//pr-url.com']),
('http://server.com/${repo_name}/${pull_request_url}/?TITLE=${pull_request_title}',
['http://server.com/foo/http%3A//pr-url.com/?TITLE=example-pr-title%20Ticket%20%23123']),
('http://server.com/${repo_name}/?SHADOW_URL=${pull_request_shadow_url}',
['http://server.com/foo/?SHADOW_URL=http%3A//pr-url.com/repository']),
webhooks: added variables into the call URL. Fixes #4211
r938 ])
webhook: quote URL variables to prevent url errors with special chars like # in pr title.
r3477 def test_webook_parse_url_for_pull_request_event(base_data, template, expected_urls):
integrations: webhook, allow to set a custom header. Fixes #5384
r2086
webhooks: added variables into the call URL. Fixes #4211
r938 base_data['pullrequest'] = {
'pull_request_id': 999,
'url': 'http://pr-url.com',
webhook: quote URL variables to prevent url errors with special chars like # in pr title.
r3477 'title': 'example-pr-title Ticket #123',
pull-request-events: expose pr title and uid for commits inside....
r2588 'commits_uid': 'abcdefg1234',
webhook: abstract webhook handler to base for easier re-usage.
r2585 'shadow_url': 'http://pr-url.com/repository'
webhooks: added variables into the call URL. Fixes #4211
r938 }
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 headers = {'exmaple-header': 'header-values'}
webhook: abstract webhook handler to base for easier re-usage.
r2585 handler = WebhookDataHandler(template, headers)
webhooks: added variables into the call URL. Fixes #4211
r938 urls = handler(events.PullRequestCreateEvent(
AttributeDict({'target_repo': 'foo'})), base_data)
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 assert urls == [
webhook: abstract webhook handler to base for easier re-usage.
r2585 (url, headers, base_data) for url in expected_urls]
webhooks: added variables into the call URL. Fixes #4211
r938
@pytest.mark.parametrize('template,expected_urls', [
webhook: abstract webhook handler to base for easier re-usage.
r2585 ('http://server.com/${branch}/build',
['http://server.com/stable/build',
'http://server.com/dev/build']),
('http://server.com/${branch}/${commit_id}',
['http://server.com/stable/stable-xxx',
'http://server.com/stable/stable-yyy',
'http://server.com/dev/dev-xxx',
'http://server.com/dev/dev-yyy']),
webhook: handle ${commit_id} variable independent from ${branch}....
r3041 ('http://server.com/${branch_head}',
['http://server.com/stable-yyy',
'http://server.com/dev-yyy']),
('http://server.com/${commit_id}',
['http://server.com/stable-xxx',
'http://server.com/stable-yyy',
'http://server.com/dev-xxx',
'http://server.com/dev-yyy']),
webhooks: added variables into the call URL. Fixes #4211
r938 ])
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 def test_webook_parse_url_for_push_event(
pylons: remove pylons as dependency...
r2351 baseapp, repo_push_event, base_data, template, expected_urls):
webhooks: added variables into the call URL. Fixes #4211
r938 base_data['push'] = {
'branches': [{'name': 'stable'}, {'name': 'dev'}],
'commits': [{'branch': 'stable', 'raw_id': 'stable-xxx'},
{'branch': 'stable', 'raw_id': 'stable-yyy'},
{'branch': 'dev', 'raw_id': 'dev-xxx'},
{'branch': 'dev', 'raw_id': 'dev-yyy'}]
}
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 headers = {'exmaple-header': 'header-values'}
webhook: abstract webhook handler to base for easier re-usage.
r2585 handler = WebhookDataHandler(template, headers)
webhooks: added variables into the call URL. Fixes #4211
r938 urls = handler(repo_push_event, base_data)
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 assert urls == [
webhook: abstract webhook handler to base for easier re-usage.
r2585 (url, headers, base_data) for url in expected_urls]
integrations: restructure code and added better coverage.
r5123
@pytest.mark.parametrize("event_type, args", [
(
events.RepoPushEvent,
(GIT_REPO, mock.MagicMock(name="pushed_commit_ids"), mock.MagicMock(name="extras")),
),
(
events.RepoCreateEvent,
(GIT_REPO,),
),
(
events.RepoCommitCommentEvent,
(GIT_REPO, mock.MagicMock(name="commit"), mock.MagicMock(name="comment")),
),
(
events.RepoCommitCommentEditEvent,
(GIT_REPO, mock.MagicMock(name="commit"), mock.MagicMock(name="comment")),
),
(
events.PullRequestEvent,
(mock.MagicMock(), ),
),
])
def test_webhook_data_handler(app, event_type: events.RhodecodeEvent, args, base_data):
handler = WebhookDataHandler(
template_url='http://server.com/${branch}/${commit_id}',
headers={'exmaple-header': 'header-values'}
)
handler(event_type(*args), base_data)