diff --git a/billing/tests.py b/billing/tests.py index 36f23e8..54cf554 100644 --- a/billing/tests.py +++ b/billing/tests.py @@ -35,107 +35,107 @@ from .models import Payment from .utils import payment_fullfilled -# class BillingTest(TestCase): -# """ -# Tests billing system -# """ -# -# def setUp(self): -# self.username = "billing_user" -# register_util(t=self, username=self.username) -# create_configurations(t=self) -# -# @override_settings(HOSTEA=infra_custom_config(test_name="test_payments")) -# def test_payments(self): -# c = Client() -# login_util(self, c, "accounts.home") -# instance_name = "test_payments" -# create_instance_util( -# t=self, c=c, instance_name=instance_name, config=self.instance_config[0] -# ) -# -# instance = Instance.objects.get(name=instance_name) -# -# self.assertEqual(payment_fullfilled(instance=instance), True) -# -# payment = get_payment_model().objects.get(paid_by=self.user) -# invoice_uri = reverse("billing.invoice.details", args=(payment.public_ref,)) -# -# # 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) -# -# # check if the paid invoice is displayed in the pending invoice view, should not be displayed -# resp = c.get(reverse("billing.invoice.pending")) -# self.assertEqual(str.encode(invoice_uri) in resp.content, False) -# -# # check if the invoice details view is rendering paid invoice version -# resp = c.get(invoice_uri) -# self.assertEqual(str.encode(instance_name) in resp.content, True) -# self.assertEqual( -# str.encode(str(self.instance_config[0].rent)) in resp.content, True -# ) -# self.assertEqual(str.encode("Paid on") in resp.content, True) -# -# # try to generate an invoice for the second time on the same VM -# # shouldn't be possible since payment is already made for the duration -# payment_uri = reverse("billing.invoice.generate", args=(instance.name,)) -# resp = c.get(payment_uri) -# self.assertEqual(resp.status_code, 400) -# -# ## payment success page; no real functionality but user is redirected here -# # by stripe if payment is successful -# resp = c.get(reverse("billing.invoice.success", args=(payment.public_ref,))) -# self.assertEqual( -# resp.headers["Location"], -# reverse("infra.create", args=(payment.instance_name,)), -# ) -# -# # create_instance_util creates an instance and pays for it. An email is -# # sent when the invoice is generated and one after payment is made -# # -# # So we are first checking for the last email that was sent(receipt) -# # and then the Gitea instance credentials notification followed by the -# # invoice generation email. -# receipt_mail = mail.outbox.pop() -# self.assertEqual( -# all( -# [ -# receipt_mail.to[0] == self.email, -# "This is a receipt for your latest Hostea payment" -# in receipt_mail.body, -# ] -# ), -# True, -# ) -# -# instance_notificaiton = mail.outbox.pop() -# self.assertEqual( -# all( -# [ -# instance_notificaiton.to[0] == self.email, -# "Congratulations on your new Gitea instance!" -# in instance_notificaiton.body, -# ] -# ), -# True, -# ) -# -# invoice_generated_mail = mail.outbox.pop() -# self.assertEqual( -# all( -# [ -# invoice_generated_mail.to[0] == self.email, -# "An invoice is generated" in invoice_generated_mail.body, -# ] -# ), -# True, -# ) -# -# ## payment failure page; no real functionality but user is redirected here -# # by stripe if payment is successful -# resp = c.get(reverse("billing.invoice.fail", args=(payment.public_ref,))) -# self.assertEqual(b"failed" in resp.content, True) +class BillingTest(TestCase): + """ + Tests billing system + """ + + def setUp(self): + self.username = "billing_user" + register_util(t=self, username=self.username) + create_configurations(t=self) + + @override_settings(HOSTEA=infra_custom_config(test_name="test_payments")) + def test_payments(self): + c = Client() + login_util(self, c, "accounts.home") + instance_name = "test_payments" + create_instance_util( + t=self, c=c, instance_name=instance_name, config=self.instance_config[0] + ) + + instance = Instance.objects.get(name=instance_name) + + self.assertEqual(payment_fullfilled(instance=instance), True) + + payment = get_payment_model().objects.get(paid_by=self.user) + invoice_uri = reverse("billing.invoice.details", args=(payment.public_ref,)) + + # 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) + + # check if the paid invoice is displayed in the pending invoice view, should not be displayed + resp = c.get(reverse("billing.invoice.pending")) + self.assertEqual(str.encode(invoice_uri) in resp.content, False) + + # check if the invoice details view is rendering paid invoice version + resp = c.get(invoice_uri) + self.assertEqual(str.encode(instance_name) in resp.content, True) + self.assertEqual( + str.encode(str(self.instance_config[0].rent)) in resp.content, True + ) + self.assertEqual(str.encode("Paid on") in resp.content, True) + + # try to generate an invoice for the second time on the same VM + # shouldn't be possible since payment is already made for the duration + payment_uri = reverse("billing.invoice.generate", args=(instance.name,)) + resp = c.get(payment_uri) + self.assertEqual(resp.status_code, 400) + + ## payment success page; no real functionality but user is redirected here + # by stripe if payment is successful + resp = c.get(reverse("billing.invoice.success", args=(payment.public_ref,))) + self.assertEqual( + resp.headers["Location"], + reverse("infra.create", args=(payment.instance_name,)), + ) + + # create_instance_util creates an instance and pays for it. An email is + # sent when the invoice is generated and one after payment is made + # + # So we are first checking for the last email that was sent(receipt) + # and then the Gitea instance credentials notification followed by the + # invoice generation email. + receipt_mail = mail.outbox.pop() + self.assertEqual( + all( + [ + receipt_mail.to[0] == self.email, + "This is a receipt for your latest Hostea payment" + in receipt_mail.body, + ] + ), + True, + ) + + instance_notificaiton = mail.outbox.pop() + self.assertEqual( + all( + [ + instance_notificaiton.to[0] == self.email, + "Congratulations on your new Gitea instance!" + in instance_notificaiton.body, + ] + ), + True, + ) + + invoice_generated_mail = mail.outbox.pop() + self.assertEqual( + all( + [ + invoice_generated_mail.to[0] == self.email, + "An invoice is generated" in invoice_generated_mail.body, + ] + ), + True, + ) + + ## payment failure page; no real functionality but user is redirected here + # by stripe if payment is successful + resp = c.get(reverse("billing.invoice.fail", args=(payment.public_ref,))) + self.assertEqual(b"failed" in resp.content, True) class GenerateInvoiceCommand(TestCase): diff --git a/billing/utils.py b/billing/utils.py index 8598c45..afbb4d4 100644 --- a/billing/utils.py +++ b/billing/utils.py @@ -34,11 +34,13 @@ def __get_delta(): delta = now - timedelta(seconds=(60 * 60 * 24 * 30)) # one month return delta + def get_invoice_link(payment: Payment): invoice_link = reverse("billing.invoice.details", args=(payment.public_ref,)) parsed = urlparse(settings.PAYMENT_HOST) return urlunparse((parsed.scheme, parsed.netloc, invoice_link, "", "", "")) + def payment_fullfilled(instance: Instance) -> bool: delta = __get_delta() @@ -108,7 +110,6 @@ def generate_invoice(instance: Instance) -> Payment: email = instance.owned_by.email sender = settings.DEFAULT_FROM_EMAIL - send_mail( subject="[Hostea] An invoice is generated for your Hostea VM", message=body, diff --git a/billing/views.py b/billing/views.py index a282eb6..403e4dd 100644 --- a/billing/views.py +++ b/billing/views.py @@ -33,7 +33,7 @@ from .utils import ( generate_invoice as generate_invoice_util, GenerateInvoiceErrorCode, GenerateInvoiceException, - get_invoice_link + get_invoice_link, )