|
3 | 3 | from django.test import TestCase
|
4 | 4 | from django_recaptcha.client import RecaptchaResponse
|
5 | 5 |
|
6 |
| -from ..forms import PaymentForm |
| 6 | +from ..forms import DonationForm, PaymentForm |
| 7 | +from ..models import DjangoHero, Donation |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class TestPaymentForm(TestCase):
|
@@ -44,3 +45,27 @@ def test_captcha_token_required(self):
|
44 | 45 | )
|
45 | 46 | self.assertFalse(form.is_valid())
|
46 | 47 | self.assertIn("captcha", form.errors)
|
| 48 | + |
| 49 | + @patch("fundraising.forms.stripe.Subscription.retrieve", side_effect=KeyError) |
| 50 | + def test_donation_form_save_atomic(self, *mocks): |
| 51 | + """ |
| 52 | + A stripe error in save() should rollback any change made to the Donation |
| 53 | + """ |
| 54 | + donation = Donation.objects.create( |
| 55 | + interval="monthly", |
| 56 | + subscription_amount=50, |
| 57 | + donor=DjangoHero.objects.create(), |
| 58 | + ) |
| 59 | + form = DonationForm( |
| 60 | + instance=donation, |
| 61 | + data={"subscription_amount": 25, "interval": "yearly"}, |
| 62 | + ) |
| 63 | + |
| 64 | + # Save the form, this will trigger a KeyError but we catch it and move on |
| 65 | + self.assertTrue(form.is_valid()) |
| 66 | + self.assertRaises(KeyError, form.save) |
| 67 | + |
| 68 | + # The donation should not have been updated with new data |
| 69 | + donation.refresh_from_db() |
| 70 | + self.assertEqual(donation.interval, "monthly") |
| 71 | + self.assertEqual(donation.subscription_amount, 50) |
0 commit comments