diff --git a/billing/tests.py b/billing/tests.py index 16a6c37..ac6cca2 100644 --- a/billing/tests.py +++ b/billing/tests.py @@ -26,7 +26,7 @@ from django.conf import settings 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.tests import create_configurations, create_instance_util, infra_custom_config from dash.models import Instance from .models import Payment @@ -43,55 +43,21 @@ class BillingTest(TestCase): 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_create_instance_renders" + 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) - payment_uri = reverse("billing.invoice.generate", args=(instance_name,)) - - # generate invoice - resp = c.get(payment_uri) - self.assertEqual(resp.status_code, 302) - invoice_uri = resp.headers["Location"] - self.assertEqual("invoice/payment/" in invoice_uri, True) - - # try to generate duplicate invoice, but should get redirected to previous invoice - resp = c.get(payment_uri) - self.assertEqual(resp.status_code, 302) - self.assertEqual(invoice_uri == resp.headers["Location"], True) - - # check if invoice details page is displaying the invoice - # if payment is yet to be made: - # template will show payment button - # else: - # template will show payment date - 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, False) - - # check if the unpaid invoice is displayed in the pending invoice view - 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) - # + 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")) @@ -111,6 +77,7 @@ class BillingTest(TestCase): # 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) diff --git a/dash/tests.py b/dash/tests.py index 8d02936..899adea 100644 --- a/dash/tests.py +++ b/dash/tests.py @@ -12,12 +12,20 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import subprocess +import shutil +import os +from time import sleep +from pathlib import Path + + from django.contrib.auth import get_user_model from django.utils.http import urlencode from django.urls import reverse from django.test import TestCase, Client, override_settings from django.conf import settings from django.db.utils import IntegrityError +from payments import get_payment_model, RedirectNeeded, PaymentStatus from accounts.tests import login_util, register_util @@ -44,6 +52,28 @@ def create_configurations(t: TestCase): ) +def infra_custom_config(test_name: str): + def create_fleet_repo(test_name: str): + subprocess.run( + ["./integration/ci.sh", "new_fleet_repo", test_name], + stdout=subprocess.DEVNULL, + stderr=subprocess.STDOUT, + ) + sleep(10) + + create_fleet_repo(test_name=test_name) + + c = settings.HOSTEA + path = Path(f"/tmp/hostea/dashboard/{test_name}/repo") + if path.exists(): + shutil.rmtree(path) + c["INFRA"]["HOSTEA_REPO"]["PATH"] = str(path) + remote_base = os.environ.get("HOSTEA_INFRA_HOSTEA_REPO_REMOTE") + c["INFRA"]["HOSTEA_REPO"]["REMOTE"] = f"{remote_base}{test_name}.git" + print(c["INFRA"]["HOSTEA_REPO"]["REMOTE"]) + return c + + def create_instance_util( t: TestCase, c: Client, instance_name: str, config: InstanceConfiguration ): @@ -55,6 +85,22 @@ def create_instance_util( resp.headers["location"], reverse("billing.invoice.generate", args=(instance_name,)), ) + # generate invoice + payment_uri = reverse("billing.invoice.generate", args=(instance_name,)) + resp = c.get(payment_uri) + t.assertEqual(resp.status_code, 302) + invoice_uri = resp.headers["Location"] + t.assertEqual("invoice/payment/" in invoice_uri, True) + + # simulate payment. There's probably a better way to do this + payment = get_payment_model().objects.get( + paid_by=t.user, instance_name=instance_name + ) + payment.status = PaymentStatus.CONFIRMED + payment.save() + + resp = c.get(reverse("infra.create", args=(instance_name,))) + t.assertEqual(resp.status_code, 200) class DashHome(TestCase): @@ -151,6 +197,9 @@ class CreateInstance(TestCase): register_util(t=self, username="createinstance_user") create_configurations(t=self) + @override_settings( + HOSTEA=infra_custom_config(test_name="test_create_instance_renders") + ) def test_create_instance_renders(self): c = Client() login_util(self, c, "accounts.home") @@ -236,12 +285,16 @@ class CreateInstance(TestCase): resp = c.post(delete_uri) self.assertEqual(resp.status_code, 302) - self.assertEqual(resp.headers["location"], reverse("dash.home")) + self.assertEqual( + resp.headers["location"], reverse("infra.rm", args=(instance.name,)) + ) + resp = c.get(resp.headers["location"]) + self.assertEqual(resp.status_code, 302) + self.assertEqual(resp.headers["location"], reverse("dash.instances.list")) self.assertEqual( Instance.objects.filter( - name=instance.name, + name=instance_name, owned_by=self.user, - configuration_id=self.instance_config[0], ).exists(), False, ) diff --git a/infrastructure/tests.py b/infrastructure/tests.py index cb59015..71d576d 100644 --- a/infrastructure/tests.py +++ b/infrastructure/tests.py @@ -14,7 +14,6 @@ # along with this program. If not, see . import shutil import time -import os from io import StringIO from urllib.parse import urlunparse from pathlib import Path @@ -28,22 +27,13 @@ from git import Repo from dash.models import Instance, InstanceConfiguration from accounts.tests import register_util, login_util -from dash.tests import create_configurations, create_instance_util +from dash.tests import create_configurations, create_instance_util, infra_custom_config from infrastructure.management.commands.vm import translate_sizes from .utils import Infra, Worker from .models import InstanceCreated, Job, JobType -def custom_config(test_name: str): - c = settings.HOSTEA - path = Path(f"/tmp/hostea/dashboard/{test_name}/repo") - if path.exists(): - shutil.rmtree(path) - c["INFRA"]["HOSTEA_REPO"]["PATH"] = str(path) - return c - - class InfraUtilTest(TestCase): """ Tests billing system @@ -54,7 +44,7 @@ class InfraUtilTest(TestCase): register_util(t=self, username=self.username) create_configurations(t=self) - @override_settings(HOSTEA=custom_config(test_name="test_path_util")) + @override_settings(HOSTEA=infra_custom_config(test_name="test_path_util")) def test_path_utils(self): infra = Infra() subdomain = "foo" @@ -85,7 +75,7 @@ class InfraUtilTest(TestCase): infra._hostscript_path(subdomain=subdomain), ) - @override_settings(HOSTEA=custom_config(test_name="test_add_vm")) + @override_settings(HOSTEA=infra_custom_config(test_name="test_add_vm")) def test_add_vm(self): infra = Infra() c = Client() @@ -105,9 +95,12 @@ class InfraUtilTest(TestCase): after_add = infra.repo.head.commit.hexsha self.assertEqual(before_add is not after_add, True) - c = custom_config(test_name="test_add_vm--get-head") - path = c["INFRA"]["HOSTEA_REPO"]["PATH"] + # c = infra_custom_config(test_name="test_add_vm--get-head") + path = Path("/tmp/hostea/dashboard/check-test_add_vm") + if path.exists(): + shutil.rmtree(path) repo = Repo.clone_from(conf["REMOTE"], path, env=infra.env) + repo.git.pull(env=infra.env) self.assertEqual(repo.head.commit.hexsha == after_add, True) before_rm = infra.repo.head.commit.hexsha @@ -115,10 +108,10 @@ class InfraUtilTest(TestCase): after_rm = infra.repo.head.commit.hexsha self.assertEqual(before_add is not after_add, True) - repo.git.pull() + repo.git.pull(env=infra.env) self.assertEqual(repo.head.commit.hexsha == after_rm, True) - @override_settings(HOSTEA=custom_config(test_name="test_cmd")) + @override_settings(HOSTEA=infra_custom_config(test_name="test_cmd")) def test_cmd(self): subdomain = "cmd_vm" infra = Infra() @@ -204,7 +197,7 @@ class InfraUtilTest(TestCase): w = Worker(job=job) w.start() - time.sleep(5) + time.sleep(15) self.assertEqual(w.is_alive(), False) w.join() self.assertEqual(