Skip to content

Commit

Permalink
Improve test coverage for recurring subs
Browse files Browse the repository at this point in the history
  • Loading branch information
brylie committed Jul 4, 2023
1 parent e26a8b3 commit 2ad1cd2
Showing 1 changed file with 60 additions and 3 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)

0 comments on commit 2ad1cd2

Please sign in to comment.