2016-09-20 22:51:11 +02:00
import ujson
from six import text_type
from typing import Dict , Optional
from zerver . models import Message
2016-10-12 23:27:24 +02:00
from zerver . lib . webhooks . git import COMMITS_LIMIT
2016-09-20 22:51:11 +02:00
from zerver . lib . test_helpers import WebhookTestCase
class GithubV1HookTests ( WebhookTestCase ) :
STREAM_NAME = None # type: Optional[text_type]
URL_TEMPLATE = u " /api/v1/external/github "
FIXTURE_DIR_NAME = ' github '
SEND_STREAM = False
BRANCHES = None # type: Optional[text_type]
2016-09-26 17:28:14 +02:00
push_content = u """ zbenjamin [pushed](https://github.com/zbenjamin/zulip-test/compare/4f9adc4777d5...b95449196980) to branch master
2016-09-20 22:51:11 +02:00
* [ 48 c329a ] ( https : / / github . com / zbenjamin / zulip - test / commit / 48 c329a0b68a9a379ff195ee3f1c1f4ab0b2a89e ) : Add baz
* [ 06 ebe5f ] ( https : / / github . com / zbenjamin / zulip - test / commit / 06 ebe5f472a32f6f31fd2a665f0c7442b69cce72 ) : Baz needs to be longer
2016-09-26 17:28:14 +02:00
* [ b954491 ] ( https : / / github . com / zbenjamin / zulip - test / commit / b95449196980507f08209bdfdc4f1d611689b7a8 ) : Final edit to baz , I swear """
2016-09-20 22:51:11 +02:00
def test_spam_branch_is_ignored ( self ) :
# type: () -> None
self . SEND_STREAM = True
self . STREAM_NAME = ' commits '
self . BRANCHES = ' dev,staging '
data = self . get_body ( ' push ' )
# We subscribe to the stream in this test, even though
# it won't get written, to avoid failing for the wrong
# reason.
self . subscribe_to_stream ( self . TEST_USER_EMAIL , self . STREAM_NAME )
prior_count = Message . objects . count ( )
result = self . client_post ( self . URL_TEMPLATE , data )
self . assert_json_success ( result )
after_count = Message . objects . count ( )
self . assertEqual ( prior_count , after_count )
def get_body ( self , fixture_name ) :
# type: (text_type) -> Dict[str, text_type]
api_key = self . get_api_key ( self . TEST_USER_EMAIL )
data = ujson . loads ( self . fixture_data ( self . FIXTURE_DIR_NAME , ' v1_ ' + fixture_name ) )
data . update ( { ' email ' : self . TEST_USER_EMAIL ,
' api-key ' : api_key ,
' payload ' : ujson . dumps ( data [ ' payload ' ] ) } )
if self . SEND_STREAM :
data [ ' stream ' ] = self . STREAM_NAME
if self . BRANCHES is not None :
data [ ' branches ' ] = self . BRANCHES
return data
def basic_test ( self , fixture_name , stream_name , expected_subject , expected_content , send_stream = False , branches = None ) :
# type: (text_type, text_type, text_type, text_type, bool, Optional[text_type]) -> None
self . STREAM_NAME = stream_name
self . SEND_STREAM = send_stream
self . BRANCHES = branches
self . send_and_test_stream_message ( fixture_name , expected_subject , expected_content , content_type = None )
def test_user_specified_branches ( self ) :
# type: () -> None
2016-10-05 19:26:09 +02:00
self . basic_test ( ' push ' , ' my_commits ' , ' zulip-test / master ' , self . push_content ,
2016-09-20 22:51:11 +02:00
send_stream = True , branches = " master,staging " )
def test_user_specified_stream ( self ) :
# type: () -> None
""" Around May 2013 the github webhook started to specify the stream.
Before then , the stream was hard coded to " commits " . """
2016-10-05 19:26:09 +02:00
self . basic_test ( ' push ' , ' my_commits ' , ' zulip-test / master ' , self . push_content ,
2016-09-20 22:51:11 +02:00
send_stream = True )
def test_legacy_hook ( self ) :
# type: () -> None
2016-10-05 19:26:09 +02:00
self . basic_test ( ' push ' , ' commits ' , ' zulip-test / master ' , self . push_content )
2016-09-20 22:51:11 +02:00
2016-10-07 15:49:35 +02:00
def test_push_multiple_commits ( self ) :
# type: () -> None
commit_info = " * [48c329a](https://github.com/zbenjamin/zulip-test/commit/48c329a0b68a9a379ff195ee3f1c1f4ab0b2a89e): Add baz \n "
expected_subject = " zbenjamin [pushed](https://github.com/zbenjamin/zulip-test/compare/4f9adc4777d5...b95449196980) to branch master \n \n {} [and {} more commit(s)] " . format (
2016-10-12 23:27:24 +02:00
commit_info * COMMITS_LIMIT ,
50 - COMMITS_LIMIT ,
2016-10-07 15:49:35 +02:00
)
self . basic_test ( ' push_commits_more_than_limit ' , ' commits ' , ' zulip-test / master ' , expected_subject )
2016-09-20 22:51:11 +02:00
def test_issues_opened ( self ) :
# type: () -> None
self . basic_test ( ' issues_opened ' , ' issues ' ,
2016-10-19 22:16:24 +02:00
" zulip-test / Issue #5 The frobnicator doesn ' t work " ,
2016-10-26 21:13:00 +02:00
" zbenjamin opened [Issue #5](https://github.com/zbenjamin/zulip-test/issues/5) \n \n ~~~ quote \n I tried changing the widgets, but I got: \r \n \r \n Permission denied: widgets are immutable \n ~~~ " )
2016-09-20 22:51:11 +02:00
def test_issue_comment ( self ) :
# type: () -> None
self . basic_test ( ' issue_comment ' , ' issues ' ,
2016-10-20 16:42:44 +02:00
" zulip-test / Issue #5 The frobnicator doesn ' t work " ,
2016-10-26 21:13:00 +02:00
" zbenjamin [commented](https://github.com/zbenjamin/zulip-test/issues/5#issuecomment-23374280) on [Issue #5](https://github.com/zbenjamin/zulip-test/issues/5) \n \n ~~~ quote \n Whoops, I did something wrong. \r \n \r \n I ' m sorry. \n ~~~ " )
2016-09-20 22:51:11 +02:00
def test_issues_closed ( self ) :
# type: () -> None
self . basic_test ( ' issues_closed ' , ' issues ' ,
2016-10-19 22:16:24 +02:00
" zulip-test / Issue #5 The frobnicator doesn ' t work " ,
2016-10-26 21:13:00 +02:00
" zbenjamin closed [Issue #5](https://github.com/zbenjamin/zulip-test/issues/5) " )
2016-09-20 22:51:11 +02:00
def test_pull_request_opened ( self ) :
# type: () -> None
self . basic_test ( ' pull_request_opened ' , ' commits ' ,
2016-10-11 18:58:04 +02:00
" zulip-test / PR #7 Counting is hard. " ,
2016-10-26 21:13:00 +02:00
" lfaraone opened [PR #7](https://github.com/zbenjamin/zulip-test/pull/7)(assigned to lfaraone) \n from `patch-2` to `master` \n \n ~~~ quote \n Omitted something I think? \n ~~~ " )
2016-09-20 22:51:11 +02:00
def test_pull_request_closed ( self ) :
# type: () -> None
self . basic_test ( ' pull_request_closed ' , ' commits ' ,
2016-10-11 18:58:04 +02:00
" zulip-test / PR #7 Counting is hard. " ,
2016-10-26 21:13:00 +02:00
" zbenjamin closed [PR #7](https://github.com/zbenjamin/zulip-test/pull/7) " )
2016-09-20 22:51:11 +02:00
def test_pull_request_synchronize ( self ) :
# type: () -> None
self . basic_test ( ' pull_request_synchronize ' , ' commits ' ,
2016-10-11 18:58:04 +02:00
" zulip-test / PR #13 Even more cowbell. " ,
2016-10-26 21:13:00 +02:00
" zbenjamin synchronized [PR #13](https://github.com/zbenjamin/zulip-test/pull/13) " )
2016-09-20 22:51:11 +02:00
def test_pull_request_comment ( self ) :
# type: () -> None
self . basic_test ( ' pull_request_comment ' , ' commits ' ,
2016-10-20 16:42:44 +02:00
" zulip-test / PR #9 Less cowbell. " ,
2016-10-26 21:13:00 +02:00
" zbenjamin [commented](https://github.com/zbenjamin/zulip-test/pull/9#issuecomment-24771110) on [PR #9](https://github.com/zbenjamin/zulip-test/pull/9) \n \n ~~~ quote \n Yeah, who really needs more cowbell than we already have? \n ~~~ " )
2016-09-20 22:51:11 +02:00
def test_pull_request_comment_user_specified_stream ( self ) :
# type: () -> None
self . basic_test ( ' pull_request_comment ' , ' my_commits ' ,
2016-10-20 16:42:44 +02:00
" zulip-test / PR #9 Less cowbell. " ,
2016-10-26 21:13:00 +02:00
" zbenjamin [commented](https://github.com/zbenjamin/zulip-test/pull/9#issuecomment-24771110) on [PR #9](https://github.com/zbenjamin/zulip-test/pull/9) \n \n ~~~ quote \n Yeah, who really needs more cowbell than we already have? \n ~~~ " ,
2016-09-20 22:51:11 +02:00
send_stream = True )
def test_commit_comment ( self ) :
# type: () -> None
self . basic_test ( ' commit_comment ' , ' commits ' ,
2016-10-26 21:29:23 +02:00
" zulip-test " ,
" zbenjamin [commented](https://github.com/zbenjamin/zulip-test/commit/7c994678d2f98797d299abed852d3ff9d0834533#commitcomment-4252302) on [Commit](https://github.com/zbenjamin/zulip-test/commit/7c994678d2f98797d299abed852d3ff9d0834533) \n \n ~~~ quote \n Are we sure this is enough cowbell? \n ~~~ " )
2016-09-20 22:51:11 +02:00
def test_commit_comment_line ( self ) :
# type: () -> None
self . basic_test ( ' commit_comment_line ' , ' commits ' ,
2016-10-26 21:29:23 +02:00
" zulip-test " ,
" zbenjamin [commented](https://github.com/zbenjamin/zulip-test/commit/7c994678d2f98797d299abed852d3ff9d0834533#commitcomment-4252307) on [Commit](https://github.com/zbenjamin/zulip-test/commit/7c994678d2f98797d299abed852d3ff9d0834533) \n \n ~~~ quote \n This line adds /unlucky/ cowbell (because of its line number). We should remove it. \n ~~~ " )
2016-09-20 22:51:11 +02:00
class GithubV2HookTests ( WebhookTestCase ) :
STREAM_NAME = None # type: Optional[text_type]
URL_TEMPLATE = u " /api/v1/external/github "
FIXTURE_DIR_NAME = ' github '
SEND_STREAM = False
BRANCHES = None # type: Optional[text_type]
push_content = """ zbenjamin [pushed](https://github.com/zbenjamin/zulip-test/compare/4f9adc4777d5...b95449196980) to branch master
* [ 48 c329a ] ( https : / / github . com / zbenjamin / zulip - test / commit / 48 c329a0b68a9a379ff195ee3f1c1f4ab0b2a89e ) : Add baz
* [ 06 ebe5f ] ( https : / / github . com / zbenjamin / zulip - test / commit / 06 ebe5f472a32f6f31fd2a665f0c7442b69cce72 ) : Baz needs to be longer
2016-09-26 17:28:14 +02:00
* [ b954491 ] ( https : / / github . com / zbenjamin / zulip - test / commit / b95449196980507f08209bdfdc4f1d611689b7a8 ) : Final edit to baz , I swear """
2016-09-20 22:51:11 +02:00
def test_spam_branch_is_ignored ( self ) :
# type: () -> None
self . SEND_STREAM = True
self . STREAM_NAME = ' commits '
self . BRANCHES = ' dev,staging '
data = self . get_body ( ' push ' )
# We subscribe to the stream in this test, even though
# it won't get written, to avoid failing for the wrong
# reason.
self . subscribe_to_stream ( self . TEST_USER_EMAIL , self . STREAM_NAME )
prior_count = Message . objects . count ( )
result = self . client_post ( self . URL_TEMPLATE , data )
self . assert_json_success ( result )
after_count = Message . objects . count ( )
self . assertEqual ( prior_count , after_count )
def get_body ( self , fixture_name ) :
# type: (text_type) -> Dict[str, text_type]
api_key = self . get_api_key ( self . TEST_USER_EMAIL )
data = ujson . loads ( self . fixture_data ( self . FIXTURE_DIR_NAME , ' v2_ ' + fixture_name ) )
data . update ( { ' email ' : self . TEST_USER_EMAIL ,
' api-key ' : api_key ,
' payload ' : ujson . dumps ( data [ ' payload ' ] ) } )
if self . SEND_STREAM :
data [ ' stream ' ] = self . STREAM_NAME
if self . BRANCHES is not None :
data [ ' branches ' ] = self . BRANCHES
return data
def basic_test ( self , fixture_name , stream_name , expected_subject , expected_content , send_stream = False , branches = None ) :
# type: (text_type, text_type, text_type, text_type, bool, Optional[text_type]) -> None
self . STREAM_NAME = stream_name
self . SEND_STREAM = send_stream
self . BRANCHES = branches
self . send_and_test_stream_message ( fixture_name , expected_subject , expected_content , content_type = None )
def test_user_specified_branches ( self ) :
# type: () -> None
2016-10-05 19:26:09 +02:00
self . basic_test ( ' push ' , ' my_commits ' , ' zulip-test / master ' , self . push_content ,
2016-09-20 22:51:11 +02:00
send_stream = True , branches = " master,staging " )
def test_user_specified_stream ( self ) :
# type: () -> None
""" Around May 2013 the github webhook started to specify the stream.
Before then , the stream was hard coded to " commits " . """
2016-10-05 19:26:09 +02:00
self . basic_test ( ' push ' , ' my_commits ' , ' zulip-test / master ' , self . push_content ,
2016-09-20 22:51:11 +02:00
send_stream = True )
2016-10-07 15:49:35 +02:00
def test_push_multiple_commits ( self ) :
# type: () -> None
commit_info = " * [48c329a](https://github.com/zbenjamin/zulip-test/commit/48c329a0b68a9a379ff195ee3f1c1f4ab0b2a89e): Add baz \n "
expected_subject = " zbenjamin [pushed](https://github.com/zbenjamin/zulip-test/compare/4f9adc4777d5...b95449196980) to branch master \n \n {} [and {} more commit(s)] " . format (
2016-10-12 23:27:24 +02:00
commit_info * COMMITS_LIMIT ,
50 - COMMITS_LIMIT ,
2016-10-07 15:49:35 +02:00
)
self . basic_test ( ' push_commits_more_than_limit ' , ' commits ' , ' zulip-test / master ' , expected_subject )
2016-09-20 22:51:11 +02:00
def test_legacy_hook ( self ) :
# type: () -> None
2016-10-05 19:26:09 +02:00
self . basic_test ( ' push ' , ' commits ' , ' zulip-test / master ' , self . push_content )
2016-09-20 22:51:11 +02:00
def test_issues_opened ( self ) :
# type: () -> None
self . basic_test ( ' issues_opened ' , ' issues ' ,
2016-10-19 22:16:24 +02:00
" zulip-test / Issue #5 The frobnicator doesn ' t work " ,
2016-10-26 21:13:00 +02:00
" zbenjamin opened [Issue #5](https://github.com/zbenjamin/zulip-test/issues/5) \n \n ~~~ quote \n I tried changing the widgets, but I got: \r \n \r \n Permission denied: widgets are immutable \n ~~~ " )
2016-09-20 22:51:11 +02:00
def test_issue_comment ( self ) :
# type: () -> None
self . basic_test ( ' issue_comment ' , ' issues ' ,
2016-10-20 16:42:44 +02:00
" zulip-test / Issue #5 The frobnicator doesn ' t work " ,
2016-10-26 21:13:00 +02:00
" zbenjamin [commented](https://github.com/zbenjamin/zulip-test/issues/5#issuecomment-23374280) on [Issue #5](https://github.com/zbenjamin/zulip-test/issues/5) \n \n ~~~ quote \n Whoops, I did something wrong. \r \n \r \n I ' m sorry. \n ~~~ " )
2016-09-20 22:51:11 +02:00
def test_issues_closed ( self ) :
# type: () -> None
self . basic_test ( ' issues_closed ' , ' issues ' ,
2016-10-19 22:16:24 +02:00
" zulip-test / Issue #5 The frobnicator doesn ' t work " ,
2016-10-26 21:13:00 +02:00
" zbenjamin closed [Issue #5](https://github.com/zbenjamin/zulip-test/issues/5) " )
2016-09-20 22:51:11 +02:00
def test_pull_request_opened ( self ) :
# type: () -> None
self . basic_test ( ' pull_request_opened ' , ' commits ' ,
2016-10-11 18:58:04 +02:00
" zulip-test / PR #7 Counting is hard. " ,
2016-10-26 21:13:00 +02:00
" lfaraone opened [PR #7](https://github.com/zbenjamin/zulip-test/pull/7)(assigned to lfaraone) \n from `patch-2` to `master` \n \n ~~~ quote \n Omitted something I think? \n ~~~ " )
2016-09-20 22:51:11 +02:00
def test_pull_request_closed ( self ) :
# type: () -> None
self . basic_test ( ' pull_request_closed ' , ' commits ' ,
2016-10-11 18:58:04 +02:00
" zulip-test / PR #7 Counting is hard. " ,
2016-10-26 21:13:00 +02:00
" zbenjamin closed [PR #7](https://github.com/zbenjamin/zulip-test/pull/7) " )
2016-09-20 22:51:11 +02:00
def test_pull_request_synchronize ( self ) :
# type: () -> None
self . basic_test ( ' pull_request_synchronize ' , ' commits ' ,
2016-10-11 18:58:04 +02:00
" zulip-test / PR #13 Even more cowbell. " ,
2016-10-26 21:13:00 +02:00
" zbenjamin synchronized [PR #13](https://github.com/zbenjamin/zulip-test/pull/13) " )
2016-09-20 22:51:11 +02:00
def test_pull_request_comment ( self ) :
# type: () -> None
self . basic_test ( ' pull_request_comment ' , ' commits ' ,
2016-10-20 16:42:44 +02:00
" zulip-test / PR #9 Less cowbell. " ,
2016-10-26 21:13:00 +02:00
" zbenjamin [commented](https://github.com/zbenjamin/zulip-test/pull/9#issuecomment-24771110) on [PR #9](https://github.com/zbenjamin/zulip-test/pull/9) \n \n ~~~ quote \n Yeah, who really needs more cowbell than we already have? \n ~~~ " )
2016-09-20 22:51:11 +02:00
def test_pull_request_comment_user_specified_stream ( self ) :
# type: () -> None
self . basic_test ( ' pull_request_comment ' , ' my_commits ' ,
2016-10-20 16:42:44 +02:00
" zulip-test / PR #9 Less cowbell. " ,
2016-10-26 21:13:00 +02:00
" zbenjamin [commented](https://github.com/zbenjamin/zulip-test/pull/9#issuecomment-24771110) on [PR #9](https://github.com/zbenjamin/zulip-test/pull/9) \n \n ~~~ quote \n Yeah, who really needs more cowbell than we already have? \n ~~~ " ,
2016-09-20 22:51:11 +02:00
send_stream = True )
def test_commit_comment ( self ) :
# type: () -> None
self . basic_test ( ' commit_comment ' , ' commits ' ,
2016-10-26 21:29:23 +02:00
" zulip-test " ,
" zbenjamin [commented](https://github.com/zbenjamin/zulip-test/commit/7c994678d2f98797d299abed852d3ff9d0834533#commitcomment-4252302) on [Commit](https://github.com/zbenjamin/zulip-test/commit/7c994678d2f98797d299abed852d3ff9d0834533) \n \n ~~~ quote \n Are we sure this is enough cowbell? \n ~~~ " )
2016-09-20 22:51:11 +02:00
def test_commit_comment_line ( self ) :
# type: () -> None
self . basic_test ( ' commit_comment_line ' , ' commits ' ,
2016-10-26 21:29:23 +02:00
" zulip-test " ,
" zbenjamin [commented](https://github.com/zbenjamin/zulip-test/commit/7c994678d2f98797d299abed852d3ff9d0834533#commitcomment-4252307) on [Commit](https://github.com/zbenjamin/zulip-test/commit/7c994678d2f98797d299abed852d3ff9d0834533) \n \n ~~~ quote \n This line adds /unlucky/ cowbell (because of its line number). We should remove it. \n ~~~ " )