2020-05-26 07:16:25 +02:00
from unittest . mock import MagicMock , patch
2018-09-25 19:28:05 +02:00
2016-11-10 19:30:09 +01:00
from zerver . lib . test_classes import WebhookTestCase
2016-09-20 22:51:11 +02:00
2020-01-14 22:06:24 +01:00
2016-09-20 22:51:11 +02:00
class FreshdeskHookTests ( WebhookTestCase ) :
STREAM_NAME = ' freshdesk '
2020-04-09 21:51:58 +02:00
URL_TEMPLATE = " /api/v1/external/freshdesk?stream= {stream} "
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_ticket_creation ( self ) - > None :
2016-09-20 22:51:11 +02:00
"""
Messages are generated on ticket creation through Freshdesk ' s
" Dispatch ' r " service .
"""
2020-04-09 21:51:58 +02:00
expected_topic = " #11: Test ticket subject ☃ "
2019-05-07 03:44:33 +02:00
expected_message = """
Requester ☃ Bob < requester - bob @example.com > created [ ticket #11](http://test1234zzz.freshdesk.com/helpdesk/tickets/11):
2016-09-20 22:51:11 +02:00
2019-05-07 03:44:33 +02:00
` ` ` quote
2016-09-20 22:51:11 +02:00
Test ticket description ☃ .
2019-05-07 03:44:33 +02:00
` ` `
* * * Type * * : Incident
* * * Priority * * : High
* * * Status * * : Pending
""" .strip()
2016-09-20 22:51:11 +02:00
2020-03-10 11:48:26 +01:00
self . api_stream_message ( self . test_user , ' ticket_created ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_status_change ( self ) - > None :
2016-09-20 22:51:11 +02:00
"""
Messages are generated when a ticket ' s status changes through
Freshdesk ' s " Observer " service.
"""
2020-04-09 21:51:58 +02:00
expected_topic = " #11: Test ticket subject ☃ "
2019-05-07 03:44:33 +02:00
expected_message = """
Requester Bob < requester - bob @example.com > updated [ ticket #11](http://test1234zzz.freshdesk.com/helpdesk/tickets/11):
* * * Status * * : Resolved - > Waiting on Customer
""" .strip()
2016-09-20 22:51:11 +02:00
2020-03-10 11:48:26 +01:00
self . api_stream_message ( self . test_user , ' status_changed ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_priority_change ( self ) - > None :
2016-09-20 22:51:11 +02:00
"""
Messages are generated when a ticket ' s priority changes through
Freshdesk ' s " Observer " service.
"""
2020-04-09 21:51:58 +02:00
expected_topic = " #11: Test ticket subject "
2019-05-07 03:44:33 +02:00
expected_message = """
Requester Bob < requester - bob @example.com > updated [ ticket #11](http://test1234zzz.freshdesk.com/helpdesk/tickets/11):
2016-09-20 22:51:11 +02:00
2019-05-07 03:44:33 +02:00
* * * Priority * * : High - > Low
""" .strip()
2020-03-10 11:48:26 +01:00
self . api_stream_message ( self . test_user , ' priority_changed ' , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-09-20 22:51:11 +02:00
2018-09-25 19:28:05 +02:00
@patch ( ' zerver.lib.webhooks.common.check_send_webhook_message ' )
def test_unknown_event_payload_ignore (
self , check_send_webhook_message_mock : MagicMock ) - > None :
"""
Ignore unknown event payloads .
"""
self . url = self . build_webhook_url ( )
payload = self . get_body ( ' unknown_payload ' )
kwargs = {
2020-03-10 11:48:26 +01:00
' HTTP_AUTHORIZATION ' : self . encode_email ( self . test_user . email ) ,
2018-09-25 19:28:05 +02:00
' content_type ' : ' application/x-www-form-urlencoded ' ,
}
result = self . client_post ( self . url , payload , * * kwargs )
self . assertFalse ( check_send_webhook_message_mock . called )
self . assert_json_success ( result )
2018-05-10 19:34:01 +02:00
def note_change ( self , fixture : str , note_type : str ) - > None :
2016-09-20 22:51:11 +02:00
"""
Messages are generated when a note gets added to a ticket through
Freshdesk ' s " Observer " service.
"""
2020-04-09 21:51:58 +02:00
expected_topic = " #11: Test ticket subject "
2019-05-07 03:44:33 +02:00
expected_message = """
Requester Bob < requester - bob @example.com > added a { } note to \
[ ticket #11](http://test1234zzz.freshdesk.com/helpdesk/tickets/11).
""" .strip().format(note_type)
2020-03-10 11:48:26 +01:00
self . api_stream_message ( self . test_user , fixture , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_private_note_change ( self ) - > None :
2016-09-20 22:51:11 +02:00
self . note_change ( " private_note " , " private " )
2017-11-04 07:47:46 +01:00
def test_public_note_change ( self ) - > None :
2016-09-20 22:51:11 +02:00
self . note_change ( " public_note " , " public " )
2017-11-04 07:47:46 +01:00
def test_inline_image ( self ) - > None :
2016-09-20 22:51:11 +02:00
"""
Freshdesk sends us descriptions as HTML , so we have to make the
descriptions Zulip markdown - friendly while still doing our best to
preserve links and images .
"""
2020-04-09 21:51:58 +02:00
expected_topic = " #12: Not enough ☃ guinea pigs "
2019-05-07 03:44:33 +02:00
expected_message = """
2019-07-24 02:49:16 +02:00
Requester \u2603 Bob < requester - bob @example.com > created [ ticket #12](http://test1234zzz.freshdesk.com/helpdesk/tickets/12):\n\n``` quote\nThere are too many cat pictures on the internet \u2603. We need more guinea pigs.\nExhibit 1:\n\n \n\n[guinea_pig.png](http://cdn.freshdesk.com/data/helpdesk/attachments/production/12744808/original/guinea_pig.png)\n```\n\n* **Type**: Problem\n* **Priority**: Urgent\n* **Status**: Open
2019-05-07 03:44:33 +02:00
""" .strip()
2020-03-10 11:48:26 +01:00
self . api_stream_message ( self . test_user , " inline_images " , expected_topic , expected_message ,
2017-12-14 19:02:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-09-20 22:51:11 +02:00
2018-05-10 19:34:01 +02:00
def get_body ( self , fixture_name : str ) - > str :
2018-04-20 03:57:21 +02:00
return self . webhook_fixture_data ( " freshdesk " , fixture_name , file_type = " json " )