webhooks: Rename *topic local variables to *topic_name.

This is preparatory work towards adding a Topic model.
We plan to use the local variable name as 'topic' for
the Topic model objects.

Currently, we use *topic as the local variable name for
topic names.

We rename local variables of the form *topic to *topic_name
so that we don't need to think about type collisions in
individual code paths where we might want to talk about both
Topic objects and strings for the topic name.
This commit is contained in:
Prakhar Pratyush
2024-01-17 20:23:30 +05:30
committed by Tim Abbott
parent 030f899195
commit 3afc8ed7ae
163 changed files with 1725 additions and 1619 deletions

View File

@@ -10,95 +10,95 @@ class StripeHookTests(WebhookTestCase):
WEBHOOK_DIR_NAME = "stripe"
def test_charge_dispute_closed(self) -> None:
expected_topic = "disputes"
expected_topic_name = "disputes"
expected_message = "[Dispute](https://dashboard.stripe.com/disputes/dp_00000000000000) closed. Current status: won."
self.check_webhook(
"charge_dispute_closed",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_charge_dispute_created(self) -> None:
expected_topic = "disputes"
expected_topic_name = "disputes"
expected_message = "[Dispute](https://dashboard.stripe.com/disputes/dp_00000000000000) created. Current status: needs response."
self.check_webhook(
"charge_dispute_created",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_charge_failed(self) -> None:
expected_topic = "charges"
expected_topic_name = "charges"
expected_message = (
"[Charge](https://dashboard.stripe.com/charges/ch_00000000000000) for 1.00 AUD failed"
)
self.check_webhook(
"charge_failed",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
# Credit card charge
def test_charge_succeeded__card(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = "[Charge](https://dashboard.stripe.com/charges/ch_000000000000000000000000) for 1.00 AUD succeeded"
self.check_webhook(
"charge_succeeded__card",
expected_topic,
expected_topic_name,
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 = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = "[Payment](https://dashboard.stripe.com/payments/py_000000000000000000000000) for $1.00 succeeded"
self.check_webhook(
"charge_succeeded__invoice",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_customer_created(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = (
"[Customer](https://dashboard.stripe.com/customers/cus_00000000000000) created"
)
self.check_webhook(
"customer_created",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_customer_created_email(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = "[Customer](https://dashboard.stripe.com/customers/cus_00000000000000) created\nEmail: example@abc.com"
self.check_webhook(
"customer_created_email",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_customer_deleted(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = (
"[Customer](https://dashboard.stripe.com/customers/cus_00000000000000) deleted"
)
self.check_webhook(
"customer_deleted",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_customer_subscription_created(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = """\
[Subscription](https://dashboard.stripe.com/subscriptions/sub_E6STM5w5EX3K28) created
Plan: [flatrate](https://dashboard.stripe.com/plans/plan_E6SQ6RAtmLVtzg)
@@ -106,13 +106,13 @@ Quantity: 800
Billing method: send invoice"""
self.check_webhook(
"customer_subscription_created",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_customer_subscription_created_no_nickname(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = """\
[Subscription](https://dashboard.stripe.com/subscriptions/sub_E6STM5w5EX3K28) created
Plan: https://dashboard.stripe.com/plans/plan_E6SQ6RAtmLVtzg
@@ -120,25 +120,25 @@ Quantity: 800
Billing method: send invoice"""
self.check_webhook(
"customer_subscription_created_no_nickname",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_customer_subscription_deleted(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = (
"[Subscription](https://dashboard.stripe.com/subscriptions/sub_00000000000000) deleted"
)
self.check_webhook(
"customer_subscription_deleted",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_customer_subscription_updated(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "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
@@ -150,97 +150,97 @@ Billing method: send invoice"""
* Trial start is now Dec 06, 2018, 05:53:55 UTC"""
self.check_webhook(
"customer_subscription_updated",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_customer_subscription_trial_will_end(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = "[Subscription](https://dashboard.stripe.com/subscriptions/sub_00000000000000) trial 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.check_webhook(
"customer_subscription_trial_will_end",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_customer_updated__account_balance(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = (
"[Customer](https://dashboard.stripe.com/customers/cus_00000000000000) updated"
"\n* Account balance is now 100"
)
self.check_webhook(
"customer_updated__account_balance",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_customer_discount_created(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = "Discount created ([25.5% off](https://dashboard.stripe.com/coupons/25_00000000000000))."
self.check_webhook(
"customer_discount_created",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_invoice_payment_failed(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = (
"[Invoice](https://dashboard.stripe.com/invoices/in_00000000000000) payment failed"
)
self.check_webhook(
"invoice_payment_failed",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_invoice_created(self) -> None:
expected_topic = "cus_HH97asvHvaYQYp"
expected_topic_name = "cus_HH97asvHvaYQYp"
expected_message = """
[Invoice](https://dashboard.stripe.com/invoices/in_1GpmuuHLwdCOCoR7ghzQDQLW) created (manual)
Total: 0.00 INR
Amount due: 0.00 INR
""".strip()
self.check_webhook("invoice_created", expected_topic, expected_message)
self.check_webhook("invoice_created", expected_topic_name, expected_message)
def test_invoiceitem_created(self) -> None:
expected_topic = "cus_00000000000000"
expected_topic_name = "cus_00000000000000"
expected_message = "[Invoice item](https://dashboard.stripe.com/invoiceitems/ii_00000000000000) created for 10.00 CAD"
self.check_webhook(
"invoiceitem_created",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_invoice_paid(self) -> None:
expected_topic = "cus_FDmrSwQt9Fck5M"
expected_topic_name = "cus_FDmrSwQt9Fck5M"
expected_message = "[Invoice](https://dashboard.stripe.com/invoices/in_1EjLINHuGUuNWDDZjDf2WNqd) is now paid"
self.check_webhook(
"invoice_updated__paid",
expected_topic,
expected_topic_name,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_refund_event(self) -> None:
expected_topic = "refunds"
expected_topic_name = "refunds"
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."
self.check_webhook("refund_event", expected_topic, expected_message)
self.check_webhook("refund_event", expected_topic_name, expected_message)
def test_pseudo_refund_event(self) -> None:
expected_topic = "refunds"
expected_topic_name = "refunds"
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."
self.check_webhook("pseudo_refund_event", expected_topic, expected_message)
self.check_webhook("pseudo_refund_event", expected_topic_name, expected_message)
@patch("zerver.webhooks.stripe.view.check_send_webhook_message")
def test_account_updated_without_previous_attributes_ignore(

View File

@@ -56,11 +56,11 @@ def api_stripe_webhook(
stream: str = "test",
) -> HttpResponse:
try:
topic, body = topic_and_body(payload)
topic_name, body = topic_and_body(payload)
except SuppressedEventError: # nocoverage
return json_success(request)
check_send_webhook_message(
request, user_profile, topic, body, payload["type"].tame(check_string)
request, user_profile, topic_name, body, payload["type"].tame(check_string)
)
return json_success(request)
@@ -78,12 +78,12 @@ def topic_and_body(payload: WildValue) -> Tuple[str, str]:
object_ = payload["data"]["object"] # The full, updated Stripe object
# Set the topic to the customer_id when we can
topic = ""
topic_name = ""
customer_id = object_.get("customer").tame(check_none_or(check_string))
if customer_id is not None:
# Running into the 60 character topic limit.
# topic = '[{}](https://dashboard.stripe.com/customers/{})' % (customer_id, customer_id)
topic = customer_id
topic_name = customer_id
body = None
def update_string(blacklist: Sequence[str] = []) -> str:
@@ -114,7 +114,7 @@ def topic_and_body(payload: WildValue) -> Tuple[str, str]:
if event == "updated":
if "previous_attributes" not in payload["data"]:
raise SuppressedEventError
topic = "account updates"
topic_name = "account updates"
body = update_string()
else:
# Part of Stripe Connect
@@ -127,8 +127,8 @@ def topic_and_body(payload: WildValue) -> Tuple[str, str]:
raise NotImplementedEventTypeError
if category == "charge":
if resource == "charge":
if not topic: # only in legacy fixtures
topic = "charges"
if not topic_name: # only in legacy fixtures
topic_name = "charges"
body = "{resource} for {amount} {verbed}".format(
resource=linkified_id(object_["id"].tame(check_string)),
amount=amount_string(
@@ -139,12 +139,12 @@ def topic_and_body(payload: WildValue) -> Tuple[str, str]:
if object_["failure_code"]: # nocoverage
body += ". Failure code: {}".format(object_["failure_code"].tame(check_string))
if resource == "dispute":
topic = "disputes"
topic_name = "disputes"
body = default_body() + ". Current status: {status}.".format(
status=object_["status"].tame(check_string).replace("_", " ")
)
if resource == "refund":
topic = "refunds"
topic_name = "refunds"
body = "A {resource} for a {charge} of {amount} was updated.".format(
resource=linkified_id(object_["id"].tame(check_string), lower=True),
charge=linkified_id(object_["charge"].tame(check_string), lower=True),
@@ -162,7 +162,7 @@ def topic_and_body(payload: WildValue) -> Tuple[str, str]:
if resource == "customer":
# Running into the 60 character topic limit.
# topic = '[{}](https://dashboard.stripe.com/customers/{})' % (object_['id'], object_['id'])
topic = object_["id"].tame(check_string)
topic_name = object_["id"].tame(check_string)
body = default_body(update_blacklist=["delinquent", "currency", "default_source"])
if event == "created":
if object_["email"]:
@@ -207,7 +207,7 @@ def topic_and_body(payload: WildValue) -> Tuple[str, str]:
object_["billing"].tame(check_string).replace("_", " ")
)
if category == "file": # nocoverage
topic = "files"
topic_name = "files"
body = default_body() + " ({purpose}). \nTitle: {title}".format(
purpose=object_["purpose"].tame(check_string).replace("_", " "),
title=object_["title"].tame(check_string),
@@ -286,7 +286,7 @@ def topic_and_body(payload: WildValue) -> Tuple[str, str]:
if body is None:
raise UnsupportedWebhookEventTypeError(event_type)
return (topic, body)
return (topic_name, body)
def amount_string(amount: int, currency: str) -> str: