2020-08-21 13:59:11 +02:00
from typing import Dict
2020-08-20 15:44:48 +02:00
from unittest . mock import patch
2017-11-16 00:43:10 +01:00
2020-08-20 18:01:40 +02:00
import orjson
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 TrelloHookTests ( WebhookTestCase ) :
2021-02-12 08:20:45 +01:00
STREAM_NAME = " trello "
2020-04-09 21:51:58 +02:00
URL_TEMPLATE = " /api/v1/external/trello?stream= {stream} &api_key= {api_key} "
2021-02-12 08:20:45 +01:00
FIXTURE_DIR_NAME = " trello "
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_confirmation_request ( self ) - > None :
2017-08-26 01:24:50 +02:00
response = self . client_head ( self . build_webhook_url ( ) )
2016-11-15 17:20:22 +01:00
self . assertEqual ( response . status_code , 200 , response )
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_card_was_moved_to_another_list ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " TomaszKolek moved [This is a card.](https://trello.com/c/r33ylX2Z) from Basics to Intermediate. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " changing_cards_list " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_card_was_renamed ( self ) - > None :
2021-02-12 08:20:45 +01:00
expected_message = ' TomaszKolek renamed the card from " Old name " to [New name](https://trello.com/c/r33ylX2Z). '
2020-08-23 15:49:24 +02:00
self . check_webhook ( " renaming_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_label_was_added_to_card ( self ) - > None :
2021-02-12 08:20:45 +01:00
expected_message = ' TomaszKolek added a green label with " text value " to [Card name](https://trello.com/c/r33ylX2Z). '
2020-08-23 15:49:24 +02:00
self . check_webhook ( " adding_label_to_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_label_was_removing_from_card ( self ) - > None :
2021-02-12 08:20:45 +01:00
expected_message = ' TomaszKolek removed a green label with " text value " from [New Card](https://trello.com/c/r33ylX2Z). '
2020-08-23 15:49:24 +02:00
self . check_webhook ( " removing_label_from_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_member_was_added_to_card ( self ) - > None :
2021-02-12 08:19:30 +01:00
expected_message = (
" TomaszKolek added TomaszKolek to [Card name](https://trello.com/c/9BduUcVQ). "
)
2020-08-23 15:49:24 +02:00
self . check_webhook ( " adding_member_to_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_member_was_removed_from_card ( self ) - > None :
2021-02-12 08:19:30 +01:00
expected_message = (
" TomaszKolek removed Trello from [Card name](https://trello.com/c/9BduUcVQ). "
)
2020-08-23 15:49:24 +02:00
self . check_webhook ( " removing_member_from_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_due_date_was_set ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " TomaszKolek set due date for [Card name](https://trello.com/c/9BduUcVQ) to 2016-05-11 10:00:00 UTC. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " setting_due_date_to_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_due_date_was_changed ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " TomaszKolek changed due date for [Card name](https://trello.com/c/9BduUcVQ) from 2016-05-11 10:00:00 UTC to 2016-05-24 10:00:00 UTC. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " changing_due_date_on_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_due_date_was_removed ( self ) - > None :
2021-02-12 08:19:30 +01:00
expected_message = (
" TomaszKolek removed the due date from [Card name](https://trello.com/c/9BduUcVQ). "
)
2020-08-23 15:49:24 +02:00
self . check_webhook ( " removing_due_date_from_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_card_was_archived ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " TomaszKolek archived [Card name](https://trello.com/c/9BduUcVQ). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " archiving_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_card_was_reopened ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " TomaszKolek reopened [Card name](https://trello.com/c/9BduUcVQ). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " reopening_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_card_was_created ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " TomaszKolek created [New card](https://trello.com/c/5qrgGdD5). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " creating_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_attachment_was_added_to_card ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " TomaszKolek added [attachment_name](http://url.com) to [New card](https://trello.com/c/xPKXoSTQ). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " adding_attachment_to_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_checklist_was_added_to_card ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " TomaszKolek added the Checklist checklist to [New card](https://trello.com/c/xPKXoSTQ). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " adding_checklist_to_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2019-02-22 00:00:06 +01:00
def test_trello_webhook_when_check_item_is_checked ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " Eeshan Garg checked **Tomatoes** in **Checklist** ([Something something](https://trello.com/c/R2thJK3P)). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " check_item_on_card_checklist " , " Zulip " , expected_message )
2019-02-22 00:00:06 +01:00
def test_trello_webhook_when_check_item_is_unchecked ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " Eeshan Garg unchecked **Tomatoes** in **Checklist** ([Something something](https://trello.com/c/R2thJK3P)). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " uncheck_item_on_card_checklist " , " Zulip " , expected_message )
2019-02-22 00:00:06 +01:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_member_was_removed_from_board ( self ) - > None :
2021-02-12 08:19:30 +01:00
expected_message = (
" TomaszKolek removed Trello from [Welcome Board](https://trello.com/b/iqXXzYEj). "
)
2020-08-23 15:49:24 +02:00
self . check_webhook ( " removing_member_from_board " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_member_was_added_to_board ( self ) - > None :
2021-02-12 08:19:30 +01:00
expected_message = (
" TomaszKolek added Trello to [Welcome Board](https://trello.com/b/iqXXzYEj). "
)
2020-08-23 15:49:24 +02:00
self . check_webhook ( " adding_member_to_board " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_list_was_added_to_board ( self ) - > None :
2021-02-12 08:19:30 +01:00
expected_message = (
" TomaszKolek added New list list to [Welcome Board](https://trello.com/b/iqXXzYEj). "
)
2020-08-23 15:49:24 +02:00
self . check_webhook ( " adding_new_list_to_board " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_comment_was_added_to_card ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " TomaszKolek commented on [New card](https://trello.com/c/xPKXoSTQ): \n ~~~ quote \n New comment \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " adding_comment_to_card " , " Welcome Board " , expected_message )
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_board_was_renamed ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " TomaszKolek renamed the board from Welcome Board to [New name](https://trello.com/b/iqXXzYEj). "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " renaming_board " , " New name " , expected_message )
2017-07-27 06:33:57 +02:00
2020-08-20 15:44:48 +02:00
def verify_post_is_ignored ( self , payload : str ) - > None :
with patch ( " zerver.webhooks.trello.view.check_send_webhook_message " ) as m :
result = self . client_post ( self . url , payload , content_type = " application/json " )
self . assertFalse ( m . called )
2017-07-27 06:33:57 +02:00
self . assert_json_success ( result )
2017-09-15 03:11:48 +02:00
2020-08-20 15:44:48 +02:00
def test_trello_webhook_when_card_is_moved_within_single_list_ignore ( self ) - > None :
2021-02-12 08:20:45 +01:00
payload = self . get_body ( " moving_card_within_single_list " )
2020-08-20 15:44:48 +02:00
self . verify_post_is_ignored ( payload )
def test_trello_webhook_when_board_background_is_changed_ignore ( self ) - > None :
2021-02-12 08:20:45 +01:00
payload = self . get_body ( " change_board_background_image " )
2020-08-20 15:44:48 +02:00
self . verify_post_is_ignored ( payload )
2019-02-21 23:16:09 +01:00
2020-08-20 18:06:30 +02:00
def test_ignored_card_actions ( self ) - > None :
2020-08-20 18:01:40 +02:00
"""
Certain card - related actions are now ignored solely based on the
action type , and we don ' t need to do any other parsing to ignore
them as invalid .
"""
2020-08-20 18:06:30 +02:00
actions = [
2020-09-03 16:00:59 +02:00
" copyCard " ,
2020-08-20 18:06:30 +02:00
" createCheckItem " ,
" updateCheckItem " ,
" updateList " ,
]
for action in actions :
data = dict (
model = " whatever " ,
action = dict (
type = action ,
) ,
)
payload = orjson . dumps ( data ) . decode ( )
2020-08-21 13:59:11 +02:00
self . verify_post_is_ignored ( payload )
def test_ignoring_card_updates ( self ) - > None :
fields = [
2020-08-21 14:12:49 +02:00
" cover " ,
2020-08-22 14:33:01 +02:00
" dueComplete " ,
2020-08-21 14:12:49 +02:00
" idAttachmentCover " ,
2020-08-21 13:59:11 +02:00
" pos " ,
]
for field in fields :
card : Dict [ str , object ] = { }
old = { }
old [ field ] = " should-be-ignored "
data = dict (
model = " whatever " ,
action = dict (
type = " updateCard " ,
2021-02-12 08:19:30 +01:00
data = dict ( card = card , old = old ) ,
2020-08-21 13:59:11 +02:00
) ,
)
payload = orjson . dumps ( data ) . decode ( )
2020-08-20 18:06:30 +02:00
self . verify_post_is_ignored ( payload )
2017-07-04 19:24:59 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_description_was_added_to_card ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " Marco Matarazzo set description for [New Card](https://trello.com/c/P2r0z66z) to: \n ~~~ quote \n New Description \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " adding_description_to_card " , " Welcome Board " , expected_message )
2017-07-04 19:24:59 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_description_was_removed_from_card ( self ) - > None :
2021-02-12 08:19:30 +01:00
expected_message = (
" Marco Matarazzo removed description from [New Card](https://trello.com/c/P2r0z66z). "
)
2020-08-23 15:49:24 +02:00
self . check_webhook ( " removing_description_from_card " , " Welcome Board " , expected_message )
2017-07-04 19:24:59 +02:00
2017-11-04 07:47:46 +01:00
def test_trello_webhook_when_description_was_changed_on_card ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_message = " Marco Matarazzo changed description for [New Card](https://trello.com/c/P2r0z66z) from \n ~~~ quote \n New Description \n ~~~ \n to \n ~~~ quote \n Changed Description \n ~~~ "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " changing_description_on_card " , " Welcome Board " , expected_message )