2022-06-21 19:07:03 +00:00
|
|
|
# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
import time
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
from django.contrib.auth import get_user_model
|
2022-07-07 15:21:33 +00:00
|
|
|
from django.core import mail
|
2022-06-21 19:07:03 +00:00
|
|
|
from django.core.management import call_command
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.test import TestCase, Client, override_settings
|
|
|
|
from django.utils.http import urlencode
|
|
|
|
from django.contrib.auth import authenticate
|
2022-07-07 20:16:17 +00:00
|
|
|
from django.core.management import call_command
|
2022-06-21 19:07:03 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from payments import get_payment_model, RedirectNeeded, PaymentStatus
|
|
|
|
|
|
|
|
from accounts.tests import register_util, login_util
|
2022-07-01 14:24:15 +00:00
|
|
|
from dash.tests import create_configurations, create_instance_util, infra_custom_config
|
2022-06-25 10:57:43 +00:00
|
|
|
from dash.models import Instance
|
2022-06-21 19:07:03 +00:00
|
|
|
|
|
|
|
from .models import Payment
|
2022-06-25 10:57:43 +00:00
|
|
|
from .utils import payment_fullfilled
|
2022-06-21 19:07:03 +00:00
|
|
|
|
|
|
|
|
2022-07-08 14:01:06 +00:00
|
|
|
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")
|
2022-09-11 20:55:56 +00:00
|
|
|
instance_name = "tpayments"
|
2022-07-08 14:01:06 +00:00
|
|
|
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,
|
2022-07-08 17:08:32 +00:00
|
|
|
"Congratulations on your new Hostea instance!"
|
2022-07-08 14:01:06 +00:00
|
|
|
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)
|
2022-07-07 20:16:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GenerateInvoiceCommand(TestCase):
|
2022-06-21 19:07:03 +00:00
|
|
|
"""
|
2022-07-07 20:16:17 +00:00
|
|
|
Test command: manage.py generate_invoice
|
2022-06-21 19:07:03 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
2022-07-07 20:16:17 +00:00
|
|
|
self.username = "test_generate_invoice_cmd_user"
|
2022-06-21 19:07:03 +00:00
|
|
|
register_util(t=self, username=self.username)
|
|
|
|
create_configurations(t=self)
|
|
|
|
|
2022-07-07 20:16:17 +00:00
|
|
|
@override_settings(
|
|
|
|
HOSTEA=infra_custom_config(test_name="test_generate_invoice_cmd")
|
|
|
|
)
|
|
|
|
def test_cmd(self):
|
2022-06-21 19:07:03 +00:00
|
|
|
c = Client()
|
|
|
|
login_util(self, c, "accounts.home")
|
2022-09-11 20:55:56 +00:00
|
|
|
instance_name = "tgeninvmd"
|
2022-06-21 19:07:03 +00:00
|
|
|
create_instance_util(
|
|
|
|
t=self, c=c, instance_name=instance_name, config=self.instance_config[0]
|
|
|
|
)
|
|
|
|
|
2022-07-07 20:16:17 +00:00
|
|
|
stdout = StringIO()
|
|
|
|
stderr = StringIO()
|
2022-06-25 10:57:43 +00:00
|
|
|
|
2022-07-07 20:16:17 +00:00
|
|
|
instance = Instance.objects.get(name=instance_name)
|
2022-06-25 10:57:43 +00:00
|
|
|
self.assertEqual(payment_fullfilled(instance=instance), True)
|
2022-07-07 20:16:17 +00:00
|
|
|
prev_len = len(mail.outbox)
|
2022-06-25 10:57:43 +00:00
|
|
|
|
2022-07-07 20:16:17 +00:00
|
|
|
# username exists
|
|
|
|
call_command(
|
|
|
|
"generate_invoice",
|
|
|
|
stdout=stdout,
|
|
|
|
stderr=stderr,
|
2022-06-28 18:52:49 +00:00
|
|
|
)
|
2022-07-07 20:16:17 +00:00
|
|
|
out = stdout.getvalue()
|
|
|
|
print(out)
|
|
|
|
self.assertEqual(instance_name in out, True)
|
2022-06-21 19:07:03 +00:00
|
|
|
|
2022-07-07 20:16:17 +00:00
|
|
|
self.assertEqual(prev_len, len(mail.outbox))
|
2022-07-07 15:21:33 +00:00
|
|
|
|
2022-07-07 20:16:17 +00:00
|
|
|
# delete payment and re-generate with command
|
|
|
|
get_payment_model().objects.get(instance_name=instance_name).delete()
|
2022-07-07 15:21:33 +00:00
|
|
|
|
2022-07-07 20:16:17 +00:00
|
|
|
stdout = StringIO()
|
|
|
|
stderr = StringIO()
|
2022-07-07 15:21:33 +00:00
|
|
|
|
2022-07-07 20:16:17 +00:00
|
|
|
call_command(
|
|
|
|
"generate_invoice",
|
|
|
|
stdout=stdout,
|
|
|
|
stderr=stderr,
|
|
|
|
)
|
|
|
|
out = stdout.getvalue()
|
|
|
|
print("out")
|
|
|
|
print(out)
|
|
|
|
self.assertEqual(instance_name in out, True)
|
|
|
|
self.assertEqual(f"Payment not fulfilled for instance: {instance}" in out, True)
|
|
|
|
self.assertEqual(prev_len + 1, len(mail.outbox))
|