diff --git a/billing/tests.py b/billing/tests.py index 90d83fd..d5a2b74 100644 --- a/billing/tests.py +++ b/billing/tests.py @@ -27,8 +27,10 @@ from payments import get_payment_model, RedirectNeeded, PaymentStatus from accounts.tests import register_util, login_util from dash.tests import create_configurations, create_instance_util +from dash.models import Instance from .models import Payment +from .utils import payment_fullfilled class BillingTest(TestCase): @@ -49,6 +51,8 @@ class BillingTest(TestCase): t=self, c=c, instance_name=instance_name, config=self.instance_config[0] ) + instance = Instance.objects.get(name=instance_name) + payment_uri = reverse("billing.invoice.generate", args=(instance_name,)) # generate invoice @@ -78,11 +82,17 @@ class BillingTest(TestCase): resp = c.get(reverse("billing.invoice.pending")) self.assertEqual(str.encode(invoice_uri) in resp.content, True) + self.assertEqual(payment_fullfilled(instance=instance), False) + # simulate payment. There's probably a better way to do this payment = get_payment_model().objects.get(paid_by=self.user) payment.status = PaymentStatus.CONFIRMED payment.save() + self.assertEqual(payment_fullfilled(instance=instance), True) + + # + # check if paid invoice is listed in paid invoice list view resp = c.get(reverse("billing.invoice.paid")) self.assertEqual(str.encode(invoice_uri) in resp.content, True) diff --git a/billing/utils.py b/billing/utils.py new file mode 100644 index 0000000..9693de7 --- /dev/null +++ b/billing/utils.py @@ -0,0 +1,33 @@ +# Copyright © 2022 Aravinth Manivannan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +from datetime import datetime, timedelta, timezone +from payments import get_payment_model, RedirectNeeded, PaymentStatus +from django.contrib.auth.models import User +from django.shortcuts import get_object_or_404 + +from dash.models import Instance + + +def payment_fullfilled(instance: Instance) -> bool: + Payment = get_payment_model() + now = datetime.now(tz=timezone.utc) + delta = now - timedelta(seconds=(60 * 60 * 24 * 30)) # one month + + payment = None + for p in Payment.objects.filter(date__gt=(delta), instance_name=instance.name): + if p.status == PaymentStatus.CONFIRMED: + return True + + return False