From fd395263506139303bd0560ce09cdf35b4878a23 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 8 Aug 2020 23:02:24 +0200 Subject: [PATCH] Improve billing address testing --- uncloud_pay/models.py | 13 ++++-- uncloud_pay/tests.py | 104 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 104 insertions(+), 13 deletions(-) diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index 2cbfd0f..57a3d07 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -422,6 +422,7 @@ class Bill(models.Model): ending_date = end_of_month(starting_date) + # FIXME above: maybe even use different date / active / open bill bill, created = cls.objects.get_or_create( owner=owner, starting_date=starting_date, @@ -438,8 +439,13 @@ class Bill(models.Model): starting_date=starting_date, ending_date=ending_date) - # Bill all active, recurring orders - #if order. + else: + # Bill all recurring orders -- filter in the next iteration :-) + + br = BillRecord.objects.create(bill=bill, + order=order, + starting_date=starting_date, + ending_date=ending_date) return bill @@ -450,8 +456,7 @@ class Bill(models.Model): # maxtime = time of last order # iterate month based through it - cls.assign_orders_to_bill(owner, year, month) - pass + cls.create_next_bill_for_user(owner) def assign_orders_to_bill(self, owner, year, month): """ diff --git a/uncloud_pay/tests.py b/uncloud_pay/tests.py index b9fb23e..fbf249d 100644 --- a/uncloud_pay/tests.py +++ b/uncloud_pay/tests.py @@ -5,9 +5,77 @@ from datetime import datetime, date, timedelta from .models import * from uncloud_service.models import GenericServiceProduct +class BillingAddressTestCase(TestCase): + def setUp(self): + self.user = get_user_model().objects.create( + username='random_user', + email='jane.random@domain.tld') + + + def test_user_only_inactive_address(self): + """ + Raise an error, when there is no active address + """ + + ba = BillingAddress.objects.create( + owner=self.user, + organization = 'Test org', + street="unknown", + city="unknown", + postal_code="somewhere else", + active=False) + + self.assertRaises(uncloud_pay.models.BillingAddress.DoesNotExist, + BillingAddress.get_address_for, + self.user) + + def test_user_only_active_address(self): + """ + Find the active address + """ + + ba = BillingAddress.objects.create( + owner=self.user, + organization = 'Test org', + street="unknown", + city="unknown", + postal_code="unknown", + active=True) + + + self.assertEqual(BillingAddress.get_address_for(self.user), ba) + + def test_multiple_addresses(self): + """ + Find the active address only, skip inactive + """ + + ba = BillingAddress.objects.create( + owner=self.user, + organization = 'Test org', + street="unknown", + city="unknown", + postal_code="unknown", + active=True) + + ba2 = BillingAddress.objects.create( + owner=self.user, + organization = 'Test org', + street="unknown", + city="unknown", + postal_code="somewhere else", + active=False) + + + self.assertEqual(BillingAddress.get_address_for(self.user), ba) + class ProductActivationTestCase(TestCase): def setUp(self): + self.user_without_address = get_user_model().objects.create( + username='no_home_person', + email='far.away@domain.tld') + self.user = get_user_model().objects.create( username='jdoe', email='john.doe@domain.tld') @@ -16,23 +84,21 @@ class ProductActivationTestCase(TestCase): username='recurrent_product_user', email='jane.doe@domain.tld') - self.user_without_address = get_user_model().objects.create( - username='no_home_person', - email='far.away@domain.tld') - - self.billing_address = BillingAddress.objects.create( + BillingAddress.objects.create( owner=self.user, organization = 'Test org', street="unknown", city="unknown", - postal_code="unknown") + postal_code="unknown", + active=True) - self.billing_address = BillingAddress.objects.create( + BillingAddress.objects.create( owner=self.recurring_user, organization = 'Test org', street="Somewhere", city="Else", - postal_code="unknown") + postal_code="unknown", + active=True) self.order_meta = {} self.order_meta[1] = { @@ -49,7 +115,18 @@ class ProductActivationTestCase(TestCase): recurring_period=RecurringPeriod.ONE_TIME, price=self.order_meta[1]['price'], description=self.order_meta[1]['description'], - billing_address=self.billing_address) + billing_address=BillingAddress.get_address_for(self.user)) + + self.recurring_order = Order.objects.create( + owner=self.recurring_user, + starting_date=timezone.make_aware(datetime.datetime(2020,3,3)), + recurring_period=RecurringPeriod.PER_30D, + price=15, + description="A pretty VM", + billing_address=BillingAddress.get_address_for(self.recurring_user) + ) + + def test_bill_one_time_one_bill_record(self): @@ -70,6 +147,15 @@ class ProductActivationTestCase(TestCase): self.assertEqual(bill.sum, self.order_meta[1]['price']) + def test_bill_creates_record_for_recurring_order(self): + """ + Ensure there is only 1 bill record per order + """ + + bill = Bill.create_next_bill_for_user(self.recurring_user) + + self.assertEqual(self.recurring_order.billrecord_set.count(), 1) + self.assertEqual(bill.billrecord_set.count(), 1) # class NotABillingTC(TestCase): # #class BillingTestCase(TestCase):