2020-01-14 22:06:24 +01:00
from zerver . lib . send_email import FromAddress
2018-06-04 22:34:26 +02:00
from zerver . lib . test_classes import WebhookTestCase
2018-12-18 22:14:44 +01:00
from zerver . models import Recipient
from zerver . webhooks . zabbix . view import MISCONFIGURED_PAYLOAD_ERROR_MESSAGE
2020-01-14 22:06:24 +01:00
2018-06-04 22:34:26 +02:00
class ZabbixHookTests ( WebhookTestCase ) :
STREAM_NAME = ' zabbix '
2020-04-09 21:51:58 +02:00
URL_TEMPLATE = " /api/v1/external/zabbix?api_key= {api_key} &stream= {stream} "
2018-06-04 22:34:26 +02:00
FIXTURE_DIR_NAME = ' zabbix '
def test_zabbix_alert_message ( self ) - > None :
"""
Tests if zabbix alert is handled correctly
"""
2020-04-09 21:51:58 +02:00
expected_topic = " www.example.com "
expected_message = " PROBLEM (Average) alert on [www.example.com](https://zabbix.example.com/tr_events.php?triggerid=14032&eventid=10528): \n * Zabbix agent on www.example.com is unreachable for 5 minutes \n * Agent ping is Up (1) "
2018-11-09 20:33:58 +01:00
self . send_and_test_stream_message ( ' zabbix_alert ' , expected_topic , expected_message )
2018-06-04 22:34:26 +02:00
2018-12-18 22:14:44 +01:00
def test_zabbix_invalid_payload_with_missing_data ( self ) - > None :
"""
Tests if invalid Zabbix payloads are handled correctly
"""
self . url = self . build_webhook_url ( )
payload = self . get_body ( ' zabbix_invalid_payload_with_missing_data ' )
result = self . client_post ( self . url , payload , content_type = ' application/json ' )
self . assert_json_error ( result , " Invalid payload " )
expected_message = MISCONFIGURED_PAYLOAD_ERROR_MESSAGE . format (
bot_name = self . test_user . full_name ,
support_email = FromAddress . SUPPORT
) . strip ( )
msg = self . get_last_message ( )
self . assertEqual ( msg . content , expected_message )
self . assertEqual ( msg . recipient . type , Recipient . PERSONAL )
2018-06-04 22:34:26 +02:00
def get_body ( self , fixture_name : str ) - > str :
return self . webhook_fixture_data ( " zabbix " , fixture_name , file_type = " json " )