Skip to content

Commit

Permalink
Merge pull request #755 from WesternFriend/improve-payment-test-coverage
Browse files Browse the repository at this point in the history
Improve payment test coverage
  • Loading branch information
brylie authored Jul 4, 2023
2 parents 99f536e + 2ad1cd2 commit 3a62849
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
63 changes: 60 additions & 3 deletions payment/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from braintree import Subscription as BraintreeSubscription
from django.http import HttpRequest, HttpResponse

from django.test import Client, RequestFactory
from django.test import Client, RequestFactory, SimpleTestCase
from django.urls import reverse

from donations.models import Donation
Expand All @@ -14,6 +14,7 @@
from .views import (
MAGAZINE_SUBSCRIPTION_PLAN_ID,
RECURRING_DONATION_PLAN_IDS,
construct_subscription_properties,
render_payment_processing_page,
process_braintree_subscription,
process_braintree_transaction,
Expand Down Expand Up @@ -540,7 +541,10 @@ def test_process_bookstore_order_payment_POST_failure(

class TestProcessSubscriptionPayment(TestCase):
@patch("payment.views.braintree_gateway.customer.create")
def test_process_braintree_subscription_customer_error(self, mock_create_customer):
def test_process_braintree_subscription_customer_error(
self,
mock_create_customer: Mock,
) -> None:
# Arrange
first_name = "Test"
last_name = "User"
Expand Down Expand Up @@ -576,7 +580,7 @@ def test_process_braintree_subscription_customer_error(self, mock_create_custome
}
)
self.assertIsInstance(result, ErrorResult)
self.assertEqual(result.message, "Error message")
self.assertEqual(result.message, "Error message") # type: ignore

@patch("payment.views.get_object_or_404")
@patch("payment.views.process_braintree_subscription")
Expand Down Expand Up @@ -750,3 +754,56 @@ def test_payment_done_view(self) -> None:
def test_payment_canceled_view(self) -> None:
response = self.client.get(reverse("payment:canceled"))
self.assertEqual(response.status_code, 200)


class TestConstructSubscriptionProperties(SimpleTestCase):
def test_construct_subscription_properties_recurring_true(self) -> None:
# Define the input parameters
payment_method_token = "payment_token"
plan_id = "plan_id"
price = 10
recurring = True

# Expected output
expected_output = {
"payment_method_token": payment_method_token,
"plan_id": plan_id,
"price": price,
}

# Call the function with the input parameters
output = construct_subscription_properties(
payment_method_token,
plan_id,
price,
recurring,
)

# Assert that the output is as expected
self.assertEqual(output, expected_output)

def test_construct_subscription_properties_recurring_false(self) -> None:
# Define the input parameters
payment_method_token = "payment_token"
plan_id = "plan_id"
price = 10
recurring = False

# Expected output
expected_output = {
"payment_method_token": payment_method_token,
"plan_id": plan_id,
"price": price,
"number_of_billing_cycles": 1,
}

# Call the function with the input parameters
output = construct_subscription_properties(
payment_method_token,
plan_id,
price,
recurring,
)

# Assert that the output is as expected
self.assertEqual(output, expected_output)
36 changes: 27 additions & 9 deletions payment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ def render_payment_processing_page(
)


def construct_subscription_properties(
payment_method_token: str,
plan_id: str,
price: int,
recurring: bool,
) -> dict[str, str | int]:
"""Construct a dictionary of properties for a Braintree subscription."""

subscription_properties: dict[str, str | int] = {
"payment_method_token": payment_method_token,
"plan_id": plan_id,
"price": price,
}

if not recurring:
# Subscription should only be charged once since it is not recurring
subscription_properties["number_of_billing_cycles"] = 1

return subscription_properties


def process_braintree_subscription(
first_name: str,
last_name: str,
Expand All @@ -67,15 +88,12 @@ def process_braintree_subscription(
if customer_result.is_success:
# TODO: add notification/logging for error in this step

subscription_properties = {
"payment_method_token": customer_result.customer.payment_methods[0].token,
"plan_id": plan_id,
"price": price,
}

if not recurring:
# Subscription should only be charged once since it is not recurring
subscription_properties["number_of_billing_cycles"] = 1
subscription_properties = construct_subscription_properties(
payment_method_token=customer_result.customer.payment_methods[0].token, # type: ignore # noqa: E501
plan_id=plan_id,
price=price,
recurring=recurring,
)

return braintree_gateway.subscription.create(subscription_properties)
else:
Expand Down

0 comments on commit 3a62849

Please sign in to comment.