2016-09-20 22:51:11 +02:00
# -*- coding: utf-8 -*-
import ujson
2017-11-16 00:43:10 +01:00
2016-11-10 19:30:09 +01:00
from zerver . lib . test_classes import WebhookTestCase
2018-08-01 21:03:30 +02:00
from zerver . lib . send_email import FromAddress
2018-12-07 00:05:57 +01:00
from zerver . models import Recipient , get_user_by_delivery_email , get_realm
2018-08-01 21:03:30 +02:00
from zerver . webhooks . teamcity . view import MISCONFIGURED_PAYLOAD_TYPE_ERROR_MESSAGE
2016-09-20 22:51:11 +02:00
class TeamcityHookTests ( WebhookTestCase ) :
STREAM_NAME = ' teamcity '
URL_TEMPLATE = u " /api/v1/external/teamcity?stream= {stream} &api_key= {api_key} "
2018-11-09 21:23:38 +01:00
TOPIC = u " Project :: Compile "
2016-09-20 22:51:11 +02:00
FIXTURE_DIR_NAME = ' teamcity '
2017-11-04 07:47:46 +01:00
def test_teamcity_success ( self ) - > None :
2018-02-07 03:26:08 +01:00
expected_message = u " Project :: Compile build 5535 - CL 123456 was successful! :thumbs_up: \n Details: [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv), [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952) "
2018-11-09 21:23:38 +01:00
self . send_and_test_stream_message ( ' success ' , self . TOPIC , expected_message )
2016-09-20 22:51:11 +02:00
2018-06-06 16:29:49 +02:00
def test_teamcity_success_branch ( self ) - > None :
expected_message = u " Project :: Compile build 5535 - CL 123456 was successful! :thumbs_up: \n Details: [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv), [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952) "
2018-11-09 20:33:58 +01:00
expected_topic = u " Project :: Compile (MyBranch) "
self . send_and_test_stream_message ( ' success_branch ' , expected_topic , expected_message )
2018-06-06 16:29:49 +02:00
2017-11-04 07:47:46 +01:00
def test_teamcity_broken ( self ) - > None :
2018-02-07 03:26:08 +01:00
expected_message = u " Project :: Compile build 5535 - CL 123456 is broken with status Exit code 1 (new)! :thumbs_down: \n Details: [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv), [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952) "
2018-11-09 21:23:38 +01:00
self . send_and_test_stream_message ( ' broken ' , self . TOPIC , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_teamcity_failure ( self ) - > None :
2018-02-07 03:26:08 +01:00
expected_message = u " Project :: Compile build 5535 - CL 123456 is still broken with status Exit code 1! :thumbs_down: \n Details: [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv), [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952) "
2018-11-09 21:23:38 +01:00
self . send_and_test_stream_message ( ' failure ' , self . TOPIC , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_teamcity_fixed ( self ) - > None :
2018-02-07 03:26:08 +01:00
expected_message = u " Project :: Compile build 5535 - CL 123456 has been fixed! :thumbs_up: \n Details: [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv), [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952) "
2018-11-09 21:23:38 +01:00
self . send_and_test_stream_message ( ' fixed ' , self . TOPIC , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_teamcity_personal ( self ) - > None :
2018-02-07 03:26:08 +01:00
expected_message = u " Your personal build of Project :: Compile build 5535 - CL 123456 is broken with status Exit code 1 (new)! :thumbs_down: \n Details: [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv), [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952) "
2018-04-20 03:57:21 +02:00
payload = ujson . dumps ( ujson . loads ( self . webhook_fixture_data ( self . FIXTURE_DIR_NAME , ' personal ' ) ) )
2016-09-20 22:51:11 +02:00
self . client_post ( self . url , payload , content_type = " application/json " )
msg = self . get_last_message ( )
self . assertEqual ( msg . content , expected_message )
self . assertEqual ( msg . recipient . type , Recipient . PERSONAL )
2018-08-01 21:03:30 +02:00
def test_non_generic_payload_ignore_pm_notification ( self ) - > None :
expected_message = MISCONFIGURED_PAYLOAD_TYPE_ERROR_MESSAGE . format (
2018-12-07 00:05:57 +01:00
bot_name = get_user_by_delivery_email ( ' webhook-bot@zulip.com ' , get_realm ( ' zulip ' ) ) . full_name ,
2018-08-01 21:03:30 +02:00
support_email = FromAddress . SUPPORT
) . strip ( )
payload = self . get_body ( ' slack_non_generic_payload ' )
self . client_post ( self . url , payload , content_type = " application/json " )
msg = self . get_last_message ( )
self . assertEqual ( msg . content , expected_message )
self . assertEqual ( msg . recipient . type , Recipient . PERSONAL )