2016-09-20 22:51:11 +02:00
# -*- coding: utf-8 -*-
2018-05-10 19:34:01 +02:00
from typing import Dict
2017-04-22 02:34:53 +02:00
2017-11-16 00:43:10 +01:00
from mock import MagicMock , patch
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
class BeanstalkHookTests ( WebhookTestCase ) :
STREAM_NAME = ' commits '
2018-03-13 23:43:02 +01:00
URL_TEMPLATE = u " /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 ) ) """
2018-11-09 20:33:58 +01:00
self . api_stream_message ( self . TEST_USER_EMAIL , ' git_singlecommit ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = None )
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 :
2017-04-22 02:34:53 +02: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 ) ) """
2018-11-09 20:33:58 +01:00
self . api_stream_message ( self . TEST_USER_EMAIL , ' git_singlecommit ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = None )
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 ) ) """
2018-11-09 20:33:58 +01:00
self . api_stream_message ( self . TEST_USER_EMAIL , ' git_multiple_committers ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
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 :
2017-04-22 02:34:53 +02: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 ) ) """
2018-11-09 20:33:58 +01:00
self . api_stream_message ( self . TEST_USER_EMAIL , ' git_multiple_committers ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
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 ) ) """
2018-11-09 20:33:58 +01:00
self . api_stream_message ( self . TEST_USER_EMAIL , ' git_multiple ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = None )
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 :
2017-04-22 02:34:53 +02: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 ) ) """
2018-11-09 20:33:58 +01:00
self . api_stream_message ( self . TEST_USER_EMAIL , ' git_multiple ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = None )
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 "
2017-04-26 02:57:47 +02:00
expected_message = """ Leo Franchi [pushed](http://lfranchi-svn.beanstalkapp.com/work-test) 50 commits to branch master.
2016-10-07 15:30:28 +02:00
2016-10-12 23:27:24 +02:00
{ } [ and { } more commit ( s ) ] """ .format((commits_info * COMMITS_LIMIT), 50 - COMMITS_LIMIT)
2018-11-09 20:33:58 +01:00
self . api_stream_message ( self . TEST_USER_EMAIL , ' git_morethanlimitcommits ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
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 :
2017-04-22 02:34:53 +02:00
self . url = self . build_webhook_url ( branches = ' master,development ' )
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 "
2017-04-26 02:57:47 +02:00
expected_message = """ Leo Franchi [pushed](http://lfranchi-svn.beanstalkapp.com/work-test) 50 commits to branch master.
2017-04-22 02:34:53 +02:00
{ } [ and { } more commit ( s ) ] """ .format((commits_info * COMMITS_LIMIT), 50 - COMMITS_LIMIT)
2018-11-09 20:33:58 +01:00
self . api_stream_message ( self . TEST_USER_EMAIL , ' git_morethanlimitcommits ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = None )
2017-04-22 02:34:53 +02:00
2018-03-13 23:43:02 +01:00
@patch ( ' zerver.webhooks.beanstalk.view.check_send_webhook_message ' )
def test_git_single_filtered_by_branches_ignore ( self , check_send_webhook_message_mock : MagicMock ) - > None :
2017-04-22 02:34:53 +02:00
self . url = self . build_webhook_url ( branches = ' changes,development ' )
payload = self . get_body ( ' git_singlecommit ' )
2017-12-14 19:02:45 +01:00
result = self . api_post ( self . TEST_USER_EMAIL , 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 )
2018-03-13 23:43:02 +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 (
2018-03-13 23:43:02 +01:00
self , check_send_webhook_message_mock : MagicMock ) - > None :
2017-04-22 02:34:53 +02:00
self . url = self . build_webhook_url ( branches = ' changes,development ' )
payload = self . get_body ( ' git_multiple_committers ' )
2017-12-14 19:02:45 +01:00
result = self . api_post ( self . TEST_USER_EMAIL , 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 )
2018-03-13 23:43:02 +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 (
2018-03-13 23:43:02 +01:00
self , check_send_webhook_message_mock : MagicMock ) - > None :
2017-04-22 02:34:53 +02:00
self . url = self . build_webhook_url ( branches = ' changes,development ' )
payload = self . get_body ( ' git_multiple ' )
2017-12-14 19:02:45 +01:00
result = self . api_post ( self . TEST_USER_EMAIL , 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 )
2018-03-13 23:43:02 +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 (
2018-03-13 23:43:02 +01:00
self , check_send_webhook_message_mock : MagicMock ) - > None :
2017-04-22 02:34:53 +02:00
self . url = self . build_webhook_url ( branches = ' changes,development ' )
payload = self . get_body ( ' git_morethanlimitcommits ' )
2017-12-14 19:02:45 +01:00
result = self . api_post ( self . TEST_USER_EMAIL , 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 ! """
2018-11-09 20:33:58 +01:00
self . api_stream_message ( self . TEST_USER_EMAIL , ' svn_addremove ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = None )
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 """
2018-11-09 20:33:58 +01:00
self . api_stream_message ( self . TEST_USER_EMAIL , ' svn_changefile ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = None )
2016-09-20 22:51:11 +02:00
2018-05-10 19:34:01 +02:00
def get_body ( self , fixture_name : str ) - > Dict [ str , str ] :
2018-04-20 03:57:21 +02:00
return { ' payload ' : self . webhook_fixture_data ( ' beanstalk ' , fixture_name ) }