2018-05-10 19:34:01 +02:00
from typing import Dict
2020-05-26 07:16:25 +02:00
from unittest . mock import MagicMock , patch
2017-11-16 00:43:10 +01:00
2016-11-10 19:30:09 +01:00
from zerver . lib . test_classes import WebhookTestCase
2017-11-16 00:43:10 +01:00
from zerver . lib . webhooks . git import COMMITS_LIMIT
2016-09-20 22:51:11 +02:00
2020-01-14 22:06:24 +01:00
2016-09-20 22:51:11 +02:00
class BeanstalkHookTests ( WebhookTestCase ) :
2021-02-12 08:20:45 +01:00
STREAM_NAME = " commits "
2020-04-09 21:51:58 +02:00
URL_TEMPLATE = " /api/v1/external/beanstalk?stream= {stream} "
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_git_single ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " work-test / master "
2017-04-26 02:57:47 +02:00
expected_message = """ Leo Franchi [pushed](http://lfranchi-svn.beanstalkapp.com/work-test) 1 commit to branch master.
2016-09-20 22:51:11 +02:00
2017-04-22 02:34:53 +02:00
* add some stuff ( [ e50508d ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / e50508df ) ) """
2021-02-12 08:19:30 +01:00
self . api_stream_message (
2021-02-12 08:20:45 +01:00
self . test_user , " git_singlecommit " , expected_topic , expected_message , content_type = None
2021-02-12 08:19:30 +01:00
)
2017-04-22 02:34:53 +02:00
2017-11-04 07:47:46 +01:00
def test_git_single_filtered_by_branches ( self ) - > None :
2021-02-12 08:20:45 +01:00
self . url = self . build_webhook_url ( branches = " master,development " )
2018-11-09 20:33:58 +01:00
expected_topic = " work-test / master "
2017-04-26 02:57:47 +02:00
expected_message = """ Leo Franchi [pushed](http://lfranchi-svn.beanstalkapp.com/work-test) 1 commit to branch master.
2017-04-22 02:34:53 +02:00
2017-03-15 10:15:03 +01:00
* add some stuff ( [ e50508d ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / e50508df ) ) """
2021-02-12 08:19:30 +01:00
self . api_stream_message (
2021-02-12 08:20:45 +01:00
self . test_user , " git_singlecommit " , expected_topic , expected_message , content_type = None
2021-02-12 08:19:30 +01:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_git_multiple_committers ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " work-test / master "
2017-04-26 00:19:47 +02:00
expected_message = """ Leo Franchi [pushed](http://lfranchi-svn.beanstalkapp.com/work-test) 3 commits to branch master. Commits by Leo Franchi (2) and Tomasz Kolek (1).
2017-04-05 09:12:19 +02:00
2017-04-22 02:34:53 +02:00
* Added new file ( [ edf529c ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / edf529c7 ) )
* Filled in new file with some stuff ( [ c2a191b ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / c2a191b9 ) )
* More work to fix some bugs ( [ 2009815 ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / 20098158 ) ) """
2021-02-12 08:19:30 +01:00
self . api_stream_message (
self . test_user ,
2021-02-12 08:20:45 +01:00
" git_multiple_committers " ,
2021-02-12 08:19:30 +01:00
expected_topic ,
expected_message ,
content_type = None ,
)
2017-04-22 02:34:53 +02:00
2017-11-04 07:47:46 +01:00
def test_git_multiple_committers_filtered_by_branches ( self ) - > None :
2021-02-12 08:20:45 +01:00
self . url = self . build_webhook_url ( branches = " master,development " )
2018-11-09 20:33:58 +01:00
expected_topic = " work-test / master "
2017-04-26 00:19:47 +02:00
expected_message = """ Leo Franchi [pushed](http://lfranchi-svn.beanstalkapp.com/work-test) 3 commits to branch master. Commits by Leo Franchi (2) and Tomasz Kolek (1).
2017-04-22 02:34:53 +02:00
2017-04-05 09:12:19 +02:00
* Added new file ( [ edf529c ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / edf529c7 ) )
* Filled in new file with some stuff ( [ c2a191b ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / c2a191b9 ) )
* More work to fix some bugs ( [ 2009815 ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / 20098158 ) ) """
2021-02-12 08:19:30 +01:00
self . api_stream_message (
self . test_user ,
2021-02-12 08:20:45 +01:00
" git_multiple_committers " ,
2021-02-12 08:19:30 +01:00
expected_topic ,
expected_message ,
content_type = None ,
)
2017-04-05 09:12:19 +02:00
2017-11-04 07:47:46 +01:00
def test_git_multiple ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " work-test / master "
2017-04-26 02:57:47 +02:00
expected_message = """ Leo Franchi [pushed](http://lfranchi-svn.beanstalkapp.com/work-test) 3 commits to branch master.
2016-09-20 22:51:11 +02:00
2017-04-22 02:34:53 +02:00
* Added new file ( [ edf529c ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / edf529c7 ) )
* Filled in new file with some stuff ( [ c2a191b ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / c2a191b9 ) )
* More work to fix some bugs ( [ 2009815 ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / 20098158 ) ) """
2021-02-12 08:19:30 +01:00
self . api_stream_message (
2021-02-12 08:20:45 +01:00
self . test_user , " git_multiple " , expected_topic , expected_message , content_type = None
2021-02-12 08:19:30 +01:00
)
2017-04-22 02:34:53 +02:00
2017-11-04 07:47:46 +01:00
def test_git_multiple_filtered_by_branches ( self ) - > None :
2021-02-12 08:20:45 +01:00
self . url = self . build_webhook_url ( branches = " master,development " )
2018-11-09 20:33:58 +01:00
expected_topic = " work-test / master "
2017-04-26 02:57:47 +02:00
expected_message = """ Leo Franchi [pushed](http://lfranchi-svn.beanstalkapp.com/work-test) 3 commits to branch master.
2017-04-22 02:34:53 +02:00
2017-03-15 10:15:03 +01:00
* Added new file ( [ edf529c ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / edf529c7 ) )
* Filled in new file with some stuff ( [ c2a191b ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / c2a191b9 ) )
* More work to fix some bugs ( [ 2009815 ] ( http : / / lfranchi - svn . beanstalkapp . com / work - test / changesets / 20098158 ) ) """
2021-02-12 08:19:30 +01:00
self . api_stream_message (
2021-02-12 08:20:45 +01:00
self . test_user , " git_multiple " , expected_topic , expected_message , content_type = None
2021-02-12 08:19:30 +01:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_git_more_than_limit ( self ) - > None :
2017-03-15 10:15:03 +01:00
commits_info = " * add some stuff ([e50508d](http://lfranchi-svn.beanstalkapp.com/work-test/changesets/e50508df)) \n "
2018-11-09 20:33:58 +01:00
expected_topic = " work-test / master "
2020-06-13 08:57:35 +02:00
expected_message = f """ Leo Franchi [pushed](http://lfranchi-svn.beanstalkapp.com/work-test) 50 commits to branch master.
2016-10-07 15:30:28 +02:00
2020-06-13 08:57:35 +02:00
{ ( commits_info * COMMITS_LIMIT ) } [ and { 50 - COMMITS_LIMIT } more commit ( s ) ] """
2021-02-12 08:19:30 +01:00
self . api_stream_message (
self . test_user ,
2021-02-12 08:20:45 +01:00
" git_morethanlimitcommits " ,
2021-02-12 08:19:30 +01:00
expected_topic ,
expected_message ,
content_type = None ,
)
2016-10-07 15:30:28 +02:00
2017-11-04 07:47:46 +01:00
def test_git_more_than_limit_filtered_by_branches ( self ) - > None :
2021-02-12 08:20:45 +01:00
self . url = self . build_webhook_url ( branches = " master,development " )
2017-04-22 02:34:53 +02:00
commits_info = " * add some stuff ([e50508d](http://lfranchi-svn.beanstalkapp.com/work-test/changesets/e50508df)) \n "
2018-11-09 20:33:58 +01:00
expected_topic = " work-test / master "
2020-06-13 08:57:35 +02:00
expected_message = f """ Leo Franchi [pushed](http://lfranchi-svn.beanstalkapp.com/work-test) 50 commits to branch master.
2017-04-22 02:34:53 +02:00
2020-06-13 08:57:35 +02:00
{ ( commits_info * COMMITS_LIMIT ) } [ and { 50 - COMMITS_LIMIT } more commit ( s ) ] """
2021-02-12 08:19:30 +01:00
self . api_stream_message (
self . test_user ,
2021-02-12 08:20:45 +01:00
" git_morethanlimitcommits " ,
2021-02-12 08:19:30 +01:00
expected_topic ,
expected_message ,
content_type = None ,
)
2017-04-22 02:34:53 +02:00
2021-02-12 08:20:45 +01:00
@patch ( " zerver.webhooks.beanstalk.view.check_send_webhook_message " )
2021-02-12 08:19:30 +01:00
def test_git_single_filtered_by_branches_ignore (
self , check_send_webhook_message_mock : MagicMock
) - > None :
2021-02-12 08:20:45 +01:00
self . url = self . build_webhook_url ( branches = " changes,development " )
payload = self . get_payload ( " git_singlecommit " )
2020-03-10 11:48:26 +01:00
result = self . api_post ( self . test_user , self . url , payload )
2018-03-13 23:43:02 +01:00
self . assertFalse ( check_send_webhook_message_mock . called )
2017-04-22 02:34:53 +02:00
self . assert_json_success ( result )
2021-02-12 08:20:45 +01:00
@patch ( " zerver.webhooks.beanstalk.view.check_send_webhook_message " )
2017-04-22 02:34:53 +02:00
def test_git_multiple_committers_filtered_by_branches_ignore (
2021-02-12 08:19:30 +01:00
self , check_send_webhook_message_mock : MagicMock
) - > None :
2021-02-12 08:20:45 +01:00
self . url = self . build_webhook_url ( branches = " changes,development " )
payload = self . get_payload ( " git_multiple_committers " )
2020-03-10 11:48:26 +01:00
result = self . api_post ( self . test_user , self . url , payload )
2018-03-13 23:43:02 +01:00
self . assertFalse ( check_send_webhook_message_mock . called )
2017-04-22 02:34:53 +02:00
self . assert_json_success ( result )
2021-02-12 08:20:45 +01:00
@patch ( " zerver.webhooks.beanstalk.view.check_send_webhook_message " )
2017-04-22 02:34:53 +02:00
def test_git_multiple_filtered_by_branches_ignore (
2021-02-12 08:19:30 +01:00
self , check_send_webhook_message_mock : MagicMock
) - > None :
2021-02-12 08:20:45 +01:00
self . url = self . build_webhook_url ( branches = " changes,development " )
payload = self . get_payload ( " git_multiple " )
2020-03-10 11:48:26 +01:00
result = self . api_post ( self . test_user , self . url , payload )
2018-03-13 23:43:02 +01:00
self . assertFalse ( check_send_webhook_message_mock . called )
2017-04-22 02:34:53 +02:00
self . assert_json_success ( result )
2021-02-12 08:20:45 +01:00
@patch ( " zerver.webhooks.beanstalk.view.check_send_webhook_message " )
2017-04-22 02:34:53 +02:00
def test_git_more_than_limit_filtered_by_branches_ignore (
2021-02-12 08:19:30 +01:00
self , check_send_webhook_message_mock : MagicMock
) - > None :
2021-02-12 08:20:45 +01:00
self . url = self . build_webhook_url ( branches = " changes,development " )
payload = self . get_payload ( " git_morethanlimitcommits " )
2020-03-10 11:48:26 +01:00
result = self . api_post ( self . test_user , self . url , payload )
2018-03-13 23:43:02 +01:00
self . assertFalse ( check_send_webhook_message_mock . called )
2017-04-22 02:34:53 +02:00
self . assert_json_success ( result )
2017-11-04 07:47:46 +01:00
def test_svn_addremove ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " svn r3 "
2016-09-20 22:51:11 +02:00
expected_message = """ Leo Franchi pushed [revision 3](http://lfranchi-svn.beanstalkapp.com/work-test/changesets/3):
> Removed a file and added another one ! """
2021-02-12 08:19:30 +01:00
self . api_stream_message (
2021-02-12 08:20:45 +01:00
self . test_user , " svn_addremove " , expected_topic , expected_message , content_type = None
2021-02-12 08:19:30 +01:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_svn_changefile ( self ) - > None :
2018-11-09 20:33:58 +01:00
expected_topic = " svn r2 "
2016-09-20 22:51:11 +02:00
expected_message = """ Leo Franchi pushed [revision 2](http://lfranchi-svn.beanstalkapp.com/work-test/changesets/2):
> Added some code """
2021-02-12 08:19:30 +01:00
self . api_stream_message (
2021-02-12 08:20:45 +01:00
self . test_user , " svn_changefile " , expected_topic , expected_message , content_type = None
2021-02-12 08:19:30 +01:00
)
2016-09-20 22:51:11 +02:00
2020-08-20 17:03:43 +02:00
def get_payload ( self , fixture_name : str ) - > Dict [ str , str ] :
2021-02-12 08:20:45 +01:00
return { " payload " : self . webhook_fixture_data ( " beanstalk " , fixture_name ) }