2016-12-03 18:51:33 +01:00
# -*- coding: utf-8 -*-
2017-02-26 09:10:14 +01:00
import mock
2017-11-16 00:43:10 +01:00
from zerver . lib . test_classes import WebhookTestCase
2016-12-03 18:51:33 +01:00
class StripeHookTests ( WebhookTestCase ) :
STREAM_NAME = ' test '
2018-03-16 22:53:50 +01:00
URL_TEMPLATE = " /api/v1/external/stripe?&api_key= {api_key} &stream= {stream} "
2016-12-03 18:51:33 +01:00
FIXTURE_DIR_NAME = ' stripe '
2017-11-04 07:47:46 +01:00
def test_charge_dispute_closed ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Charge ch_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " A charge dispute for **10.01aud** has been closed as **won**. \n The charge in dispute was **[ch_00000000000000](https://dashboard.stripe.com/payments/ch_00000000000000)**. "
# use fixture named stripe_charge_dispute_closed
self . send_and_test_stream_message ( ' charge_dispute_closed ' , expected_subject , expected_message ,
content_type = " application/x-www-form-urlencoded " )
2017-11-04 07:47:46 +01:00
def test_charge_dispute_created ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Charge ch_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " A charge dispute for **1000jpy** has been created. \n The charge in dispute is **[ch_00000000000000](https://dashboard.stripe.com/payments/ch_00000000000000)**. "
# use fixture named stripe_charge_dispute_created
self . send_and_test_stream_message ( ' charge_dispute_created ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_charge_failed ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Charge ch_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " A charge with id **[ch_00000000000000](https://dashboard.stripe.com/payments/ch_00000000000000)** for **1.00aud** has failed. "
# use fixture named stripe_charge_failed
self . send_and_test_stream_message ( ' charge_failed ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_charge_succeeded ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Charge ch_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " A charge with id **[ch_00000000000000](https://dashboard.stripe.com/payments/ch_00000000000000)** for **1.00aud** has succeeded. "
# use fixture named stripe_charge_succeeded
self . send_and_test_stream_message ( ' charge_succeeded ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_customer_created_email ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Customer cus_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " A new customer with id **[cus_00000000000000](https://dashboard.stripe.com/customers/cus_00000000000000)** and email **example@abc.com** has been created. "
# use fixture named stripe_customer_created_email
self . send_and_test_stream_message ( ' customer_created_email ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_customer_created ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Customer cus_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " A new customer with id **[cus_00000000000000](https://dashboard.stripe.com/customers/cus_00000000000000)** has been created. "
# use fixture named stripe_customer_created
self . send_and_test_stream_message ( ' customer_created ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_customer_deleted ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Customer cus_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " A customer with id **[cus_00000000000000](https://dashboard.stripe.com/customers/cus_00000000000000)** has been deleted. "
# use fixture named stripe_customer_deleted
self . send_and_test_stream_message ( ' customer_deleted ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_customer_subscription_created ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Customer sub_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " A new customer subscription for **20.00aud** every **month** has been created. \n The subscription has id **[sub_00000000000000](https://dashboard.stripe.com/subscriptions/sub_00000000000000)**. "
# use fixture named stripe_customer_subscription_created
self . send_and_test_stream_message ( ' customer_subscription_created ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_customer_subscription_deleted ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Customer sub_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " The customer subscription with id **[sub_00000000000000](https://dashboard.stripe.com/subscriptions/sub_00000000000000)** was deleted. "
# use fixture named stripe_customer_subscription_deleted
self . send_and_test_stream_message ( ' customer_subscription_deleted ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_customer_subscription_trial_will_end ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Customer sub_00000000000000 "
2017-02-26 09:10:14 +01:00
expected_message = u " The customer subscription trial with id **[sub_00000000000000](https://dashboard.stripe.com/subscriptions/sub_00000000000000)** will end in 3 days. "
# 3 days before the end of the trial, plus a little bit to make sure the rounding is working
with mock . patch ( ' time.time ' , return_value = 1480892861 - 3 * 3600 * 24 + 100 ) :
# use fixture named stripe_customer_subscription_trial_will_end
self . send_and_test_stream_message ( ' customer_subscription_trial_will_end ' ,
expected_subject , expected_message ,
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_invoice_payment_failed ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Invoice in_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " An invoice payment on invoice with id **[in_00000000000000](https://dashboard.stripe.com/invoices/in_00000000000000)** and with **0.00aud** due has failed. "
# use fixture named stripe_invoice_payment_failed
self . send_and_test_stream_message ( ' invoice_payment_failed ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_order_payment_failed ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Order or_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " An order payment on order with id **[or_00000000000000](https://dashboard.stripe.com/orders/or_00000000000000)** for **15.00aud** has failed. "
# use fixture named stripe_order_payment_failed
self . send_and_test_stream_message ( ' order_payment_failed ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_order_payment_succeeded ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Order or_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " An order payment on order with id **[or_00000000000000](https://dashboard.stripe.com/orders/or_00000000000000)** for **15.00aud** has succeeded. "
# use fixture named stripe_order_payment_succeeded
self . send_and_test_stream_message ( ' order_payment_succeeded ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_order_updated ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Order or_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " The order with id **[or_00000000000000](https://dashboard.stripe.com/orders/or_00000000000000)** for **15.00aud** has been updated. "
# use fixture named stripe_order_updated
self . send_and_test_stream_message ( ' order_updated ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_transfer_failed ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Transfer tr_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " The transfer with description **Transfer to test@example.com** and id **[tr_00000000000000](https://dashboard.stripe.com/transfers/tr_00000000000000)** for amount **11.00aud** has failed. "
# use fixture named stripe_transfer_failed
self . send_and_test_stream_message ( ' transfer_failed ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_transfer_paid ( self ) - > None :
2016-12-23 18:29:52 +01:00
expected_subject = u " Transfer tr_00000000000000 "
2016-12-03 18:51:33 +01:00
expected_message = u " The transfer with description **Transfer to test@example.com** and id **[tr_00000000000000](https://dashboard.stripe.com/transfers/tr_00000000000000)** for amount **11.00aud** has been paid. "
# use fixture named stripe_transfer_paid
self . send_and_test_stream_message ( ' transfer_paid ' , expected_subject , expected_message ,
2016-12-11 14:30:45 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01: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 ( " stripe " , fixture_name , file_type = " json " )