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 :
2018-11-21 01:39:37 +01:00
expected_topic = u " disputes "
expected_message = u " [Dispute](https://dashboard.stripe.com/disputes/dp_00000000000000) closed. Current status: won. "
2018-11-09 20:33:58 +01:00
self . send_and_test_stream_message ( ' charge_dispute_closed ' , expected_topic , expected_message ,
2016-12-03 18:51:33 +01:00
content_type = " application/x-www-form-urlencoded " )
2017-11-04 07:47:46 +01:00
def test_charge_dispute_created ( self ) - > None :
2018-11-21 01:39:37 +01:00
expected_topic = u " disputes "
expected_message = u " [Dispute](https://dashboard.stripe.com/disputes/dp_00000000000000) created. Current status: needs response. "
2018-11-09 20:33:58 +01:00
self . send_and_test_stream_message ( ' charge_dispute_created ' , expected_topic , 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 :
2018-11-21 01:39:37 +01:00
expected_topic = u " charges "
expected_message = u " [Charge](https://dashboard.stripe.com/charges/ch_00000000000000) for 1.00 AUD failed "
2018-11-09 20:33:58 +01:00
self . send_and_test_stream_message ( ' charge_failed ' , expected_topic , 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-12-02 09:21:09 +01:00
# Credit card charge
def test_charge_succeeded__card ( self ) - > None :
expected_topic = u " cus_00000000000000 "
expected_message = u " [Charge](https://dashboard.stripe.com/charges/ch_000000000000000000000000) for 1.00 AUD succeeded "
self . send_and_test_stream_message ( ' charge_succeeded__card ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
# ACH payment (really a 'payment', rather than a 'charge')
def test_charge_succeeded__invoice ( self ) - > None :
expected_topic = u " cus_00000000000000 "
expected_message = u " [Payment](https://dashboard.stripe.com/payments/py_000000000000000000000000) for $1.00 succeeded "
self . send_and_test_stream_message ( ' charge_succeeded__invoice ' , expected_topic , 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 :
2018-11-21 01:39:37 +01:00
expected_topic = u " cus_00000000000000 "
expected_message = u " [Customer](https://dashboard.stripe.com/customers/cus_00000000000000) created "
2018-11-09 20:33:58 +01:00
self . send_and_test_stream_message ( ' customer_created ' , expected_topic , 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-11-21 01:39:37 +01:00
def test_customer_created_email ( self ) - > None :
expected_topic = u " cus_00000000000000 "
expected_message = u " [Customer](https://dashboard.stripe.com/customers/cus_00000000000000) created \n Email: example@abc.com "
self . send_and_test_stream_message ( ' customer_created_email ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2018-11-21 01:39:37 +01:00
def test_customer_deleted ( self ) - > None :
expected_topic = u " cus_00000000000000 "
expected_message = u " [Customer](https://dashboard.stripe.com/customers/cus_00000000000000) deleted "
2018-11-09 20:33:58 +01:00
self . send_and_test_stream_message ( ' customer_deleted ' , expected_topic , 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 :
2018-11-21 01:39:37 +01:00
expected_topic = u " cus_00000000000000 "
expected_message = u """ \
2018-12-06 20:10:27 +01:00
[ Subscription ] ( https : / / dashboard . stripe . com / subscriptions / sub_E6STM5w5EX3K28 ) created
Plan : [ flatrate ] ( https : / / dashboard . stripe . com / plans / plan_E6SQ6RAtmLVtzg )
Quantity : 800
Billing method : send invoice """
2018-11-09 20:33:58 +01:00
self . send_and_test_stream_message ( ' customer_subscription_created ' , expected_topic , 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 :
2018-11-21 01:39:37 +01:00
expected_topic = u " cus_00000000000000 "
expected_message = u " [Subscription](https://dashboard.stripe.com/subscriptions/sub_00000000000000) deleted "
2018-11-09 20:33:58 +01:00
self . send_and_test_stream_message ( ' customer_subscription_deleted ' , expected_topic , 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-12-06 18:40:43 +01:00
def test_customer_subscription_updated ( self ) - > None :
expected_topic = u " cus_00000000000000 "
expected_message = """ \
[ Subscription ] ( https : / / dashboard . stripe . com / subscriptions / sub_E6STM5w5EX3K28 ) updated
* Billing cycle anchor is now Nov 01 , 2019 , 12 : 00 : 00 UTC
* Current period end is now Nov 01 , 2019 , 12 : 00 : 00 UTC
* Current period start is now Dec 06 , 2018 , 05 : 53 : 55 UTC
* Start is now Dec 06 , 2018 , 05 : 53 : 55 UTC
* Status is now trialing
* Trial end is now Nov 01 , 2019 , 12 : 00 : 00 UTC
* Trial start is now Dec 06 , 2018 , 05 : 53 : 55 UTC """
self . send_and_test_stream_message ( ' customer_subscription_updated ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
2017-11-04 07:47:46 +01:00
def test_customer_subscription_trial_will_end ( self ) - > None :
2018-11-21 01:39:37 +01:00
expected_topic = u " cus_00000000000000 "
expected_message = u " [Subscription](https://dashboard.stripe.com/subscriptions/sub_00000000000000) trial will end in 3 days "
2017-02-26 09:10:14 +01:00
# 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 ' ,
2018-11-09 20:33:58 +01:00
expected_topic , expected_message ,
2017-02-26 09:10:14 +01:00
content_type = " application/x-www-form-urlencoded " )
2016-12-03 18:51:33 +01:00
2018-12-06 09:06:23 +01:00
def test_customer_updated__account_balance ( self ) - > None :
2018-12-03 07:44:04 +01:00
expected_topic = " cus_00000000000000 "
expected_message = " [Customer](https://dashboard.stripe.com/customers/cus_00000000000000) updated " + \
2018-12-06 09:06:23 +01:00
" \n * Account balance is now 100 "
self . send_and_test_stream_message ( ' customer_updated__account_balance ' , expected_topic , expected_message ,
2018-12-03 07:44:04 +01:00
content_type = " application/x-www-form-urlencoded " )
2018-11-28 22:11:08 +01:00
def test_customer_discount_created ( self ) - > None :
expected_topic = u " cus_00000000000000 "
expected_message = u " Discount created ([25.5 % o ff](https://dashboard.stripe.com/coupons/25_00000000000000)). "
self . send_and_test_stream_message ( ' customer_discount_created ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
2019-01-10 21:01:03 +01:00
def test_payout_canceled ( self ) - > None :
expected_topic = u " Payout po_00000000000000 "
expected_message = u " **[Payout](https://dashboard.stripe.com/payout/po_00000000000000)** for amount **$11.00** has been canceled. "
self . send_and_test_stream_message ( ' payout_canceled ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_payout_created ( self ) - > None :
expected_topic = u " Payout po_00000000000000 "
expected_message = u " **[Payout](https://dashboard.stripe.com/payout/po_00000000000000)** for amount **$11.00** has been created. "
self . send_and_test_stream_message ( ' payout_created ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_payout_failed ( self ) - > None :
expected_topic = u " Payout po_00000000000000 "
expected_message = u " **[Payout](https://dashboard.stripe.com/payout/po_00000000000000)** for amount **$11.00** has failed. "
self . send_and_test_stream_message ( ' payout_failed ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_payout_paid ( self ) - > None :
expected_topic = u " Payout po_00000000000000 "
expected_message = u " **[Payout](https://dashboard.stripe.com/payout/po_00000000000000)** for amount **$11.00** has been paid. "
self . send_and_test_stream_message ( ' payout_paid ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_payout_updated ( self ) - > None :
expected_topic = u " Payout po_00000000000000 "
expected_message = u " **[Payout](https://dashboard.stripe.com/payout/po_00000000000000)** for amount **$11.00** has been updated. "
self . send_and_test_stream_message ( ' payout_updated ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
2019-01-26 11:44:46 +01:00
def test_invoice_created ( self ) - > None :
expected_topic = u " invoice in_00000000000000 "
expected_message = u " An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been created. "
self . send_and_test_stream_message ( ' invoice_created ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_invoice_deleted ( self ) - > None :
expected_topic = u " invoice in_00000000000000 "
expected_message = u " An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been deleted. "
self . send_and_test_stream_message ( ' invoice_deleted ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_invoice_finalized ( self ) - > None :
expected_topic = u " invoice in_00000000000000 "
expected_message = u " An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been finalized. "
self . send_and_test_stream_message ( ' invoice_finalized ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_invoice_marked_uncollectible ( self ) - > None :
expected_topic = u " invoice in_00000000000000 "
expected_message = u " An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been marked as uncollectible. "
self . send_and_test_stream_message ( ' invoice_marked_uncollectible ' , expected_topic , expected_message , content_type = " application/x-www-form-urlencoded " )
def test_invoice_payment_failed ( self ) - > None :
expected_topic = u " invoice in_00000000000000 "
expected_message = u " An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has failed. "
self . send_and_test_stream_message ( ' invoice_payment_failed ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_invoice_payment_succeeded ( self ) - > None :
expected_topic = u " invoice in_00000000000000 "
expected_message = u " An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has succeeded. "
self . send_and_test_stream_message ( ' invoice_payment_succeeded ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_invoice_sent ( self ) - > None :
expected_topic = u " invoice in_00000000000000 "
expected_message = u " An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been sent. "
self . send_and_test_stream_message ( ' invoice_sent ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_invoice_upcoming ( self ) - > None :
expected_topic = u " invoice None "
expected_message = u " An upcoming **[invoice](https://dashboard.stripe.com/invoices/None)** for the payment of amount **$10.00** has been created. "
self . send_and_test_stream_message ( ' invoice_upcoming ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_invoice_updated ( self ) - > None :
expected_topic = u " invoice in_00000000000000 "
expected_message = u " An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been updated. "
self . send_and_test_stream_message ( ' invoice_updated ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )
def test_invoice_voided ( self ) - > None :
expected_topic = u " invoice in_00000000000000 "
expected_message = u " An **[invoice](https://dashboard.stripe.com/invoices/in_00000000000000)** for the payment of amount **$10.00** has been voided. "
self . send_and_test_stream_message ( ' invoice_voided ' , expected_topic , expected_message ,
content_type = " application/x-www-form-urlencoded " )