Skip to content

Commit

Permalink
Add test_subscription_end_date_updated_without_paid_through_date
Browse files Browse the repository at this point in the history
  • Loading branch information
brylie committed Jul 9, 2023
1 parent 2903180 commit 4b3df54
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions subscription/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,46 @@ def test_subscription_end_date_updated_with_paid_through_date(
# check that the end_date is updated
expected_end_date = one_year_later + GRACE_PERIOD_DAYS
self.assertEqual(self.subscription.end_date, expected_end_date)

@patch("subscription.views.braintree_gateway.webhook_notification.parse")
def test_subscription_end_date_updated_without_paid_through_date(
self,
mock_parse: Mock,
) -> None:
# Calculate the expected end_date
one_year_with_grace_period = datetime.timedelta(days=365) + GRACE_PERIOD_DAYS
expected_end_date = self.subscription.end_date + one_year_with_grace_period

# Set up the Braintree subscription mock without paid_through_date
mock_braintree_subscription = Mock()
mock_braintree_subscription.id = self.subscription.braintree_subscription_id # type: ignore # noqa: E501
mock_braintree_subscription.paid_through_date = None

mock_webhook_notification = Mock()
mock_webhook_notification.kind = "subscription_charged_successfully"
mock_webhook_notification.subscription = mock_braintree_subscription

# mock parse method to return the mock notification
mock_parse.return_value = mock_webhook_notification

csrf_client = Client(enforce_csrf_checks=True)
response = csrf_client.post(
self.url,
data=json.dumps(self.webhook_notification),
content_type="application/json",
)

# assert that parse was called with the correct arguments
mock_parse.assert_called_once_with(
self.webhook_notification["bt_signature"],
self.webhook_notification["bt_payload"],
)

# check the response status code
self.assertEqual(response.status_code, 200)

# Refresh subscription from db
self.subscription.refresh_from_db()

# check that the end_date is updated
self.assertEqual(self.subscription.end_date, expected_end_date)

0 comments on commit 4b3df54

Please sign in to comment.