From ab412cb877266ef7259712e13c550b321eeefac3 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 25 Aug 2020 21:31:12 +0200 Subject: [PATCH] Test that creating products w/o correct billing address fails --- .../migrations/0014_auto_20200825_1915.py | 33 +++++++++++++++ uncloud_pay/models.py | 27 ++++++++++++ uncloud_pay/tests.py | 42 +++++++++++++------ 3 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 uncloud_pay/migrations/0014_auto_20200825_1915.py diff --git a/uncloud_pay/migrations/0014_auto_20200825_1915.py b/uncloud_pay/migrations/0014_auto_20200825_1915.py new file mode 100644 index 0000000..97c4b7a --- /dev/null +++ b/uncloud_pay/migrations/0014_auto_20200825_1915.py @@ -0,0 +1,33 @@ +# Generated by Django 3.1 on 2020-08-25 19:15 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud_pay', '0013_auto_20200809_1237'), + ] + + operations = [ + migrations.AddField( + model_name='sampleonetimeproduct', + name='ot_price', + field=models.IntegerField(default=5), + ), + migrations.AddField( + model_name='samplerecurringproduct', + name='rc_price', + field=models.IntegerField(default=10), + ), + migrations.AddField( + model_name='samplerecurringproductonetimefee', + name='ot_price', + field=models.IntegerField(default=5), + ), + migrations.AddField( + model_name='samplerecurringproductonetimefee', + name='rc_price', + field=models.IntegerField(default=10), + ), + ] diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index f4d4c52..1f9b61a 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -789,8 +789,25 @@ class Product(UncloudModel): # FIXME: use the right type of exception here! raise Exception("Did not implement the discounter for this case") + + def save(self, *args, **kwargs): + try: + ba = BillingAddress.get_address_for(self.owner) + except BillingAddress.DoesNotExist: + raise ValidationError("User does not have a billing address") + + if not ba.active: + raise ValidationError("User does not have an active billing address") + + super().save(*args, **kwargs) + + # Sample products included into uncloud class SampleOneTimeProduct(Product): + """ + Products are usually more complex, but this product shows how easy + it can be to create your own one time product. + """ default_recurring_period = RecurringPeriod.ONE_TIME @@ -801,6 +818,11 @@ class SampleOneTimeProduct(Product): return self.ot_price class SampleRecurringProduct(Product): + """ + Products are usually more complex, but this product shows how easy + it can be to create your own recurring fee product. + """ + default_recurring_period = RecurringPeriod.PER_30D rc_price = models.IntegerField(default=10) @@ -810,6 +832,11 @@ class SampleRecurringProduct(Product): return self.rc_price class SampleRecurringProductOneTimeFee(Product): + """ + Products are usually more complex, but this product shows how easy + it can be to create your own one time + recurring fee product. + """ + default_recurring_period = RecurringPeriod.PER_30D ot_price = models.IntegerField(default=5) diff --git a/uncloud_pay/tests.py b/uncloud_pay/tests.py index 69af3cc..6a59531 100644 --- a/uncloud_pay/tests.py +++ b/uncloud_pay/tests.py @@ -16,7 +16,7 @@ class ProductTestCase(TestCase): username='random_user', email='jane.random@domain.tld') - ba = BillingAddress.objects.create( + self.ba = BillingAddress.objects.create( owner=self.user, organization = 'Test org', street="unknown", @@ -34,6 +34,29 @@ class ProductTestCase(TestCase): self.assertEqual(p.one_time_price, 5) self.assertEqual(p.recurring_price, 0) + def test_create_product_without_active_billing_address(self): + """ + Fail to create a product without an active billing address + """ + + self.ba.active = False + self.ba.save() + + with self.assertRaises(ValidationError): + p = SampleOneTimeProduct.objects.create(owner=self.user) + + def test_create_product_without_billing_address(self): + """ + Fail to create a product without a billing address + """ + + user2 = get_user_model().objects.create( + username='random_user2', + email='jane.randomly@domain.tld') + + with self.assertRaises(ValidationError): + p = SampleOneTimeProduct.objects.create(owner=user2) + def test_create_order_creates_correct_order_count(self): """ @@ -329,6 +352,7 @@ class BillTestCase(TestCase): class ModifyProductTestCase(TestCase): + def setUp(self): self.user = get_user_model().objects.create( username='random_user', @@ -340,23 +364,15 @@ class ModifyProductTestCase(TestCase): street="unknown", city="unknown", postal_code="somewhere else", - active=False) + active=True) - def test_user_no_address(self): - """ - Raise an error, when there is no address + def test_modify_recurring_product(self): """ - - self.assertRaises(uncloud_pay.models.BillingAddress.DoesNotExist, - BillingAddress.get_address_for, - self.user) - - def test_user_only_inactive_address(self): - """ - Raise an error, when there is no active address """ + product = SampleRecurringProduct.objects.create(owner=self.user) + # class NotABillingTC(TestCase):