2020-08-07 01:09:47 +02:00
import orjson
2017-11-16 00:43:10 +01:00
2018-08-01 21:03:30 +02:00
from zerver . lib . send_email import FromAddress
2020-01-14 22:06:24 +01:00
from zerver . lib . test_classes import WebhookTestCase
from zerver . models import Recipient , get_realm , get_user_by_delivery_email
2020-06-11 00:54:34 +02:00
from zerver . webhooks . teamcity . view import MISCONFIGURED_PAYLOAD_TYPE_ERROR_MESSAGE
2020-01-14 22:06:24 +01:00
2016-09-20 22:51:11 +02:00
2021-12-17 05:13:07 +01:00
class TeamCityHookTests ( WebhookTestCase ) :
2021-02-12 08:20:45 +01:00
STREAM_NAME = " teamcity "
2020-04-09 21:51:58 +02:00
URL_TEMPLATE = " /api/v1/external/teamcity?stream= {stream} &api_key= {api_key} "
TOPIC = " Project :: Compile "
2021-06-26 09:18:33 +02:00
WEBHOOK_DIR_NAME = " teamcity "
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_teamcity_success ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " Project :: Compile build 5535 - CL 123456 was successful! :thumbs_up: See [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv) and [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " 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 :
2020-04-09 21:51:58 +02:00
expected_message = " Project :: Compile build 5535 - CL 123456 was successful! :thumbs_up: See [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv) and [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952). "
expected_topic = " Project :: Compile (MyBranch) "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " 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 :
2020-04-09 21:51:58 +02:00
expected_message = " Project :: Compile build 5535 - CL 123456 is broken with status Exit code 1 (new)! :thumbs_down: See [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv) and [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " 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 :
2020-04-09 21:51:58 +02:00
expected_message = " Project :: Compile build 5535 - CL 123456 is still broken with status Exit code 1! :thumbs_down: See [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv) and [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " 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 :
2020-04-09 21:51:58 +02:00
expected_message = " Project :: Compile build 5535 - CL 123456 has been fixed! :thumbs_up: See [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv) and [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " 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 :
2020-04-09 21:51:58 +02:00
expected_message = " Your personal build for Project :: Compile build 5535 - CL 123456 is broken with status Exit code 1 (new)! :thumbs_down: See [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv) and [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952). "
2021-02-12 08:19:30 +01:00
payload = orjson . dumps (
2021-06-26 09:18:33 +02:00
orjson . loads ( self . webhook_fixture_data ( self . WEBHOOK_DIR_NAME , " personal " ) )
2021-02-12 08:19:30 +01:00
)
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 (
2021-02-12 08:19:30 +01:00
bot_name = get_user_by_delivery_email (
2021-02-12 08:20:45 +01:00
" webhook-bot@zulip.com " , get_realm ( " zulip " )
2021-02-12 08:19:30 +01:00
) . full_name ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
support_email = FromAddress . SUPPORT ,
2018-08-01 21:03:30 +02:00
) . strip ( )
2021-02-12 08:20:45 +01:00
payload = self . get_body ( " slack_non_generic_payload " )
2018-08-01 21:03:30 +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 )