2022-05-18 11:15:19 +02:00
from unittest import mock
import orjson
2023-10-12 19:43:45 +02:00
from typing_extensions import override
2022-05-18 11:15:19 +02:00
2022-11-17 09:30:48 +01:00
from zerver . lib . exceptions import UnsupportedWebhookEventTypeError
2016-11-10 19:30:09 +01:00
from zerver . lib . test_classes import WebhookTestCase
2022-05-18 11:15:19 +02:00
from zerver . webhooks . pivotal . view import api_pivotal_webhook_v5
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 PivotalV3HookTests ( WebhookTestCase ) :
2024-05-04 22:02:50 +02:00
CHANNEL_NAME = " pivotal "
2020-04-09 21:51:58 +02:00
URL_TEMPLATE = " /api/v1/external/pivotal?stream= {stream} &api_key= {api_key} "
2021-07-16 11:40:46 +02:00
WEBHOOK_DIR_NAME = " pivotal "
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_accepted ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " My new Feature story "
2016-09-20 22:51:11 +02:00
expected_message = ' Leo Franchi accepted " My new Feature story " \
2019-05-09 21:14:42 +02:00
[ ( view ) ] ( https : / / www . pivotaltracker . com / s / projects / 807213 / stories / 48276573 ) . '
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" accepted " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2022-05-18 11:15:19 +02:00
def test_bad_subject ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Story changed "
2022-05-18 11:15:19 +02:00
expected_message = " Leo Franchi accepted My new Feature story \
[ ( view ) ] ( https : / / www . pivotaltracker . com / s / projects / 807213 / stories / 48276573 ) . "
self . check_webhook (
2024-01-17 15:53:30 +01:00
" bad_accepted " , expected_topic_name , expected_message , content_type = " application/xml "
2022-05-18 11:15:19 +02:00
)
2017-11-04 07:47:46 +01:00
def test_commented ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Comment added "
2016-09-20 22:51:11 +02:00
expected_message = ' Leo Franchi added comment: " FIX THIS NOW " \
2019-05-09 21:14:42 +02:00
[ ( view ) ] ( https : / / www . pivotaltracker . com / s / projects / 807213 / stories / 48276573 ) . '
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" commented " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_created ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " My new Feature story "
2016-09-20 22:51:11 +02:00
expected_message = ' Leo Franchi added " My new Feature story " \
( unscheduled feature ) : \n \n ~ ~ ~ quote \nThis is my long description \n ~ ~ ~ \n \n \
2019-05-09 21:14:42 +02:00
[ ( view ) ] ( https : / / www . pivotaltracker . com / s / projects / 807213 / stories / 48276573 ) . '
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" created " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_delivered ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Another new story "
2016-09-20 22:51:11 +02:00
expected_message = ' Leo Franchi delivered " Another new story " \
2019-05-09 21:14:42 +02:00
[ ( view ) ] ( https : / / www . pivotaltracker . com / s / projects / 807213 / stories / 48278289 ) . '
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" delivered " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_finished ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Another new story "
2016-09-20 22:51:11 +02:00
expected_message = ' Leo Franchi finished " Another new story " \
2019-05-09 21:14:42 +02:00
[ ( view ) ] ( https : / / www . pivotaltracker . com / s / projects / 807213 / stories / 48278289 ) . '
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" finished " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_moved ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " My new Feature story "
2016-09-20 22:51:11 +02:00
expected_message = ' Leo Franchi edited " My new Feature story " \
2019-05-09 21:14:42 +02:00
[ ( view ) ] ( https : / / www . pivotaltracker . com / s / projects / 807213 / stories / 48276573 ) . '
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" moved " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_rejected ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Another new story "
2016-09-20 22:51:11 +02:00
expected_message = ' Leo Franchi rejected " Another new story " with comments: \
2019-05-09 21:14:42 +02:00
" Not good enough, sorry " [ ( view ) ] ( https : / / www . pivotaltracker . com / s / projects / 807213 / stories / 48278289 ) . '
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" rejected " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_started ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Another new story "
2016-09-20 22:51:11 +02:00
expected_message = ' Leo Franchi started " Another new story " \
2019-05-09 21:14:42 +02:00
[ ( view ) ] ( https : / / www . pivotaltracker . com / s / projects / 807213 / stories / 48278289 ) . '
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" started " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_created_estimate ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Another new story "
2016-09-20 22:51:11 +02:00
expected_message = ' Leo Franchi added " Another new story " \
( unscheduled feature worth 2 story points ) : \n \n ~ ~ ~ quote \nSome loong description \n ~ ~ ~ \n \n \
2019-05-09 21:14:42 +02:00
[ ( view ) ] ( https : / / www . pivotaltracker . com / s / projects / 807213 / stories / 48278289 ) . '
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" created_estimate " ,
expected_topic_name ,
expected_message ,
content_type = " application/xml " ,
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_type_changed ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " My new Feature story "
2016-09-20 22:51:11 +02:00
expected_message = ' Leo Franchi edited " My new Feature story " \
2019-05-09 21:14:42 +02:00
[ ( view ) ] ( https : / / www . pivotaltracker . com / s / projects / 807213 / stories / 48276573 ) . '
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" type_changed " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2023-10-12 19:43:45 +02:00
@override
2018-05-10 19:34:01 +02:00
def get_body ( self , fixture_name : str ) - > str :
2021-02-12 08:20:45 +01:00
return self . webhook_fixture_data ( " pivotal " , fixture_name , file_type = " xml " )
2016-09-20 22:51:11 +02:00
2021-02-12 08:19:30 +01:00
2016-09-20 22:51:11 +02:00
class PivotalV5HookTests ( WebhookTestCase ) :
2024-05-04 22:02:50 +02:00
CHANNEL_NAME = " pivotal "
2020-04-09 21:51:58 +02:00
URL_TEMPLATE = " /api/v1/external/pivotal?stream= {stream} &api_key= {api_key} "
2021-07-16 11:40:46 +02:00
WEBHOOK_DIR_NAME = " pivotal "
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_accepted ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " #63486316: Story of the Year "
2016-09-20 22:51:11 +02:00
expected_message = """ Leo Franchi updated [Hard Code](https://www.pivotaltracker.com/s/projects/807213): [Story of the Year](http://www.pivotaltracker.com/story/show/63486316):
2017-02-12 04:22:13 +01:00
* state changed from * * unstarted * * to * * accepted * * """
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" accepted " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_commented ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " #63486316: Story of the Year "
2016-09-20 22:51:11 +02:00
expected_message = """ Leo Franchi added a comment to [Hard Code](https://www.pivotaltracker.com/s/projects/807213): [Story of the Year](http://www.pivotaltracker.com/story/show/63486316):
~ ~ ~ quote
A comment on the story
~ ~ ~ """
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" commented " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_created ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " #63495662: Story that I created "
2016-09-20 22:51:11 +02:00
expected_message = """ Leo Franchi created bug: [Hard Code](https://www.pivotaltracker.com/s/projects/807213): [Story that I created](http://www.pivotaltracker.com/story/show/63495662)
* State is * * unscheduled * *
* Description is
> What a description """
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" created " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_delivered ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " #63486316: Story of the Year "
2016-09-20 22:51:11 +02:00
expected_message = """ Leo Franchi updated [Hard Code](https://www.pivotaltracker.com/s/projects/807213): [Story of the Year](http://www.pivotaltracker.com/story/show/63486316):
2017-02-12 04:22:13 +01:00
* state changed from * * accepted * * to * * delivered * * """
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" delivered " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_finished ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " #63486316: Story of the Year "
2016-09-20 22:51:11 +02:00
expected_message = """ Leo Franchi updated [Hard Code](https://www.pivotaltracker.com/s/projects/807213): [Story of the Year](http://www.pivotaltracker.com/story/show/63486316):
2017-02-12 04:22:13 +01:00
* state changed from * * delivered * * to * * accepted * * """
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" finished " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_moved ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " #63496066: Pivotal Test "
2019-05-09 21:14:42 +02:00
expected_message = """ Leo Franchi moved [Hard Code](https://www.pivotaltracker.com/s/projects/807213): [Pivotal Test](http://www.pivotaltracker.com/story/show/63496066) from **unstarted** to **unscheduled**. """
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" moved " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_rejected ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " #63486316: Story of the Year "
2016-09-20 22:51:11 +02:00
expected_message = """ Leo Franchi updated [Hard Code](https://www.pivotaltracker.com/s/projects/807213): [Story of the Year](http://www.pivotaltracker.com/story/show/63486316):
* Comment added :
~ ~ ~ quote
Try again next time
~ ~ ~
2017-02-12 04:22:13 +01:00
* state changed from * * delivered * * to * * rejected * * """
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" rejected " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_started ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " #63495972: Fresh Story "
2016-09-20 22:51:11 +02:00
expected_message = """ Leo Franchi updated [Hard Code](https://www.pivotaltracker.com/s/projects/807213): [Fresh Story](http://www.pivotaltracker.com/story/show/63495972):
2017-02-12 04:22:13 +01:00
* state changed from * * unstarted * * to * * started * * """
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" started " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_created_estimate ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " #63496066: Pivotal Test "
2016-09-20 22:51:11 +02:00
expected_message = """ Leo Franchi updated [Hard Code](https://www.pivotaltracker.com/s/projects/807213): [Pivotal Test](http://www.pivotaltracker.com/story/show/63496066):
2017-02-12 04:22:13 +01:00
* estimate is now * * 3 points * * """
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" created_estimate " ,
expected_topic_name ,
expected_message ,
content_type = " application/xml " ,
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_type_changed ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " #63496066: Pivotal Test "
2016-09-20 22:51:11 +02:00
expected_message = """ Leo Franchi updated [Hard Code](https://www.pivotaltracker.com/s/projects/807213): [Pivotal Test](http://www.pivotaltracker.com/story/show/63496066):
* estimate changed from 3 to * * 0 points * *
2017-02-12 04:22:13 +01:00
* type changed from * * feature * * to * * bug * * """
2020-08-23 15:49:24 +02:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" type_changed " , expected_topic_name , expected_message , content_type = " application/xml "
2020-08-23 15:49:24 +02:00
)
2016-09-20 22:51:11 +02:00
2022-05-18 11:15:19 +02:00
def test_bad_payload ( self ) - > None :
bad = ( " foo " , None , " bar " )
2024-07-14 20:30:42 +02:00
with (
self . assertRaisesRegex ( AssertionError , " Unable to handle Pivotal payload " ) ,
mock . patch ( " zerver.webhooks.pivotal.view.api_pivotal_webhook_v3 " , return_value = bad ) ,
) :
self . check_webhook ( " accepted " , expect_topic = " foo " )
2022-05-18 11:15:19 +02:00
def test_bad_request ( self ) - > None :
request = mock . MagicMock ( )
hamlet = self . example_user ( " hamlet " )
bad = orjson . loads ( self . get_body ( " bad_request " ) )
with mock . patch ( " zerver.webhooks.pivotal.view.orjson.loads " , return_value = bad ) :
result = api_pivotal_webhook_v5 ( request , hamlet )
self . assertEqual ( result [ 0 ] , " #0: " )
bad = orjson . loads ( self . get_body ( " bad_kind " ) )
2024-07-14 20:30:42 +02:00
with (
self . assertRaisesRegex ( UnsupportedWebhookEventTypeError , " ' unknown_kind ' .* supported " ) ,
mock . patch ( " zerver.webhooks.pivotal.view.orjson.loads " , return_value = bad ) ,
) :
api_pivotal_webhook_v5 ( request , hamlet )
2022-05-18 11:15:19 +02:00
2023-10-12 19:43:45 +02:00
@override
2018-05-10 19:34:01 +02:00
def get_body ( self , fixture_name : str ) - > str :
2021-02-12 08:20:45 +01:00
return self . webhook_fixture_data ( " pivotal " , f " v5_ { fixture_name } " , file_type = " json " )