2020-05-26 07:16:25 +02:00
from unittest import mock
from unittest . mock import MagicMock , patch
2017-02-26 09:10:14 +01:00
2017-11-16 00:43:10 +01:00
from zerver . lib . test_classes import WebhookTestCase
2020-01-14 22:06:24 +01:00
2016-12-03 18:51:33 +01:00
class StripeHookTests ( WebhookTestCase ) :
2021-02-12 08:20:45 +01:00
STREAM_NAME = " test "
2018-03-16 22:53:50 +01:00
URL_TEMPLATE = " /api/v1/external/stripe?&api_key= {api_key} &stream= {stream} "
2021-02-12 08:20:45 +01:00
FIXTURE_DIR_NAME = " stripe "
2016-12-03 18:51:33 +01:00
2017-11-04 07:47:46 +01:00
def test_charge_dispute_closed ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " disputes "
expected_message = " [Dispute](https://dashboard.stripe.com/disputes/dp_00000000000000) closed. Current status: won. "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" charge_dispute_closed " ,
expected_topic ,
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_charge_dispute_created ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " disputes "
expected_message = " [Dispute](https://dashboard.stripe.com/disputes/dp_00000000000000) created. Current status: needs response. "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" charge_dispute_created " ,
expected_topic ,
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_charge_failed ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " charges "
2021-02-12 08:19:30 +01:00
expected_message = (
" [Charge](https://dashboard.stripe.com/charges/ch_00000000000000) for 1.00 AUD failed "
)
2020-08-23 15:49:24 +02:00
self . check_webhook (
" charge_failed " ,
expected_topic ,
expected_message ,
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 :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
expected_message = " [Charge](https://dashboard.stripe.com/charges/ch_000000000000000000000000) for 1.00 AUD succeeded "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" charge_succeeded__card " ,
expected_topic ,
expected_message ,
content_type = " application/x-www-form-urlencoded " ,
)
2018-12-02 09:21:09 +01:00
# ACH payment (really a 'payment', rather than a 'charge')
def test_charge_succeeded__invoice ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
expected_message = " [Payment](https://dashboard.stripe.com/payments/py_000000000000000000000000) for $1.00 succeeded "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" charge_succeeded__invoice " ,
expected_topic ,
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_customer_created ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
2021-02-12 08:19:30 +01:00
expected_message = (
" [Customer](https://dashboard.stripe.com/customers/cus_00000000000000) created "
)
2020-08-23 15:49:24 +02:00
self . check_webhook (
" customer_created " ,
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_created_email ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
expected_message = " [Customer](https://dashboard.stripe.com/customers/cus_00000000000000) created \n Email: example@abc.com "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" 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 :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
2021-02-12 08:19:30 +01:00
expected_message = (
" [Customer](https://dashboard.stripe.com/customers/cus_00000000000000) deleted "
)
2020-08-23 15:49:24 +02:00
self . check_webhook (
" customer_deleted " ,
expected_topic ,
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_customer_subscription_created ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
expected_message = """ \
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 """
2020-08-23 15:49:24 +02:00
self . check_webhook (
" customer_subscription_created " ,
expected_topic ,
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_customer_subscription_deleted ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
2021-02-12 08:19:30 +01:00
expected_message = (
" [Subscription](https://dashboard.stripe.com/subscriptions/sub_00000000000000) deleted "
)
2020-08-23 15:49:24 +02:00
self . check_webhook (
" customer_subscription_deleted " ,
expected_topic ,
expected_message ,
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 :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
2018-12-06 18:40:43 +01:00
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 """
2020-08-23 15:49:24 +02:00
self . check_webhook (
" customer_subscription_updated " ,
expected_topic ,
expected_message ,
content_type = " application/x-www-form-urlencoded " ,
)
2018-12-06 18:40:43 +01:00
2017-11-04 07:47:46 +01:00
def test_customer_subscription_trial_will_end ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
expected_message = " [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
2021-02-12 08:20:45 +01:00
with mock . patch ( " time.time " , return_value = 1480892861 - 3 * 3600 * 24 + 100 ) :
2017-02-26 09:10:14 +01:00
# use fixture named stripe_customer_subscription_trial_will_end
2020-08-23 15:49:24 +02:00
self . check_webhook (
" customer_subscription_trial_will_end " ,
expected_topic ,
expected_message ,
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 "
2021-02-12 08:19:30 +01:00
expected_message = (
" [Customer](https://dashboard.stripe.com/customers/cus_00000000000000) updated "
+ " \n * Account balance is now 100 "
)
2020-08-23 15:49:24 +02:00
self . check_webhook (
" customer_updated__account_balance " ,
expected_topic ,
expected_message ,
content_type = " application/x-www-form-urlencoded " ,
)
2018-12-03 07:44:04 +01:00
2018-11-28 22:11:08 +01:00
def test_customer_discount_created ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
expected_message = " Discount created ([25.5 % o ff](https://dashboard.stripe.com/coupons/25_00000000000000)). "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" customer_discount_created " ,
expected_topic ,
expected_message ,
content_type = " application/x-www-form-urlencoded " ,
)
2018-11-28 22:11:08 +01:00
2019-02-04 21:18:47 +01:00
def test_invoice_payment_failed ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
2021-02-12 08:19:30 +01:00
expected_message = (
" [Invoice](https://dashboard.stripe.com/invoices/in_00000000000000) payment failed "
)
2020-08-23 15:49:24 +02:00
self . check_webhook (
" invoice_payment_failed " ,
expected_topic ,
expected_message ,
content_type = " application/x-www-form-urlencoded " ,
)
2019-02-05 23:08:43 +01:00
2020-06-03 05:29:16 +02:00
def test_invoice_created ( self ) - > None :
expected_topic = " cus_HH97asvHvaYQYp "
expected_message = """
[ Invoice ] ( https : / / dashboard . stripe . com / invoices / in_1GpmuuHLwdCOCoR7ghzQDQLW ) created ( manual )
Total : 0.00 INR
Amount due : 0.00 INR
""" .strip()
2020-08-23 15:49:24 +02:00
self . check_webhook ( " invoice_created " , expected_topic , expected_message )
2020-06-03 05:29:16 +02:00
2019-02-05 23:08:43 +01:00
def test_invoiceitem_created ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_00000000000000 "
expected_message = " [Invoice item](https://dashboard.stripe.com/invoiceitems/ii_00000000000000) created for 10.00 CAD "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" invoiceitem_created " ,
2019-02-05 23:08:43 +01:00
expected_topic ,
expected_message ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
content_type = " application/x-www-form-urlencoded " ,
2019-02-05 23:08:43 +01:00
)
2019-04-04 22:54:07 +02:00
2019-06-09 13:43:58 +02:00
def test_invoice_paid ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " cus_FDmrSwQt9Fck5M "
expected_message = " [Invoice](https://dashboard.stripe.com/invoices/in_1EjLINHuGUuNWDDZjDf2WNqd) is now paid "
2020-08-23 15:49:24 +02:00
self . check_webhook (
" invoice_updated__paid " ,
2019-06-09 13:43:58 +02:00
expected_topic ,
expected_message ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
content_type = " application/x-www-form-urlencoded " ,
2019-06-09 13:43:58 +02:00
)
2020-05-14 09:42:22 +02:00
def test_refund_event ( self ) - > None :
expected_topic = " refunds "
2020-11-25 12:17:08 +01:00
expected_message = " A [refund](https://dashboard.stripe.com/refunds/re_1Gib6ZHLwdCOCoR7VrzCnXlj) for a [charge](https://dashboard.stripe.com/charges/ch_1Gib61HLwdCOCoR71rnkccye) of 300000.00 INR was updated. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " refund_event " , expected_topic , expected_message )
2020-05-14 09:42:22 +02:00
2020-05-14 12:02:03 +02:00
def test_pseudo_refund_event ( self ) - > None :
expected_topic = " refunds "
2020-11-25 12:17:08 +01:00
expected_message = " A [refund](https://dashboard.stripe.com/refunds/pyr_abcde12345ABCDF) for a [payment](https://dashboard.stripe.com/payments/py_abcde12345ABCDG) of 12.34 EUR was updated. "
2020-08-23 15:49:24 +02:00
self . check_webhook ( " pseudo_refund_event " , expected_topic , expected_message )
2020-05-14 12:02:03 +02:00
2021-02-12 08:20:45 +01:00
@patch ( " zerver.webhooks.stripe.view.check_send_webhook_message " )
2019-04-04 22:54:07 +02:00
def test_account_updated_without_previous_attributes_ignore (
2021-02-12 08:19:30 +01:00
self , check_send_webhook_message_mock : MagicMock
) - > None :
2019-04-04 22:54:07 +02:00
self . url = self . build_webhook_url ( )
2021-02-12 08:20:45 +01:00
payload = self . get_body ( " account_updated_without_previous_attributes " )
2019-04-04 22:54:07 +02:00
result = self . client_post ( self . url , payload , content_type = " application/json " )
self . assertFalse ( check_send_webhook_message_mock . called )
self . assert_json_success ( result )