# 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 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 .models import InstanceConfiguration, Instance def register_util(t: TestCase, username: str): t.password = "password121231" t.username = username t.email = f"{t.username}@example.org" t.user = get_user_model().objects.create( username=t.username, email=t.email, ) t.user.set_password(t.password) t.user.save() def create_configurations(t: TestCase): t.instance_config = [ InstanceConfiguration(name="Personal", ram=0.5, cpu=1, storage=25), InstanceConfiguration(name="Enthusiast", ram=2, cpu=2, storage=50), InstanceConfiguration(name="Small Business", ram=8, cpu=4, storage=64), InstanceConfiguration(name="Enterprise", ram=64, cpu=24, storage=1024), ] for instance in t.instance_config: instance.save() print(f"[*][init] Instance {instance.name} is saved") t.assertEqual( InstanceConfiguration.objects.filter(name=instance.name).exists(), True ) def login_util(t: TestCase, c: Client, redirect_to: str): payload = { "login": t.username, "password": t.password, } resp = c.post(reverse("accounts.login"), payload) t.assertEqual(resp.status_code, 302) t.assertEqual(resp.headers["location"], reverse(redirect_to)) class DashHome(TestCase): """ Tests create new app view """ def setUp(self): register_util(t=self, username="dashboard_home_user") def test_dash_is_protected(self): """ Tests if dashboard template renders """ resp = self.client.get(reverse("dash.home")) self.assertEqual(resp.status_code, 302) # default LOGIN redirect URI that is used by @login_required decorator is # /accounts/login. There's a redirection endpoint at /accounts/login/ that # will redirect user to /login. Hence the /accounts prefix redirect_login_uri = ( f"/accounts{reverse('accounts.login')}?next={reverse('dash.home')}" ) self.assertEqual(resp.headers["location"], redirect_login_uri) def test_dash_home_renders(self): """ Tests if login template renders """ c = Client() # username login works login_util(t=self, c=c, redirect_to="accounts.home") # email login works resp = c.get(reverse("dash.home")) self.assertEqual(resp.status_code, 200) self.assertEqual(b"Billing" in resp.content, True) self.assertEqual(b"Support" in resp.content, True) self.assertEqual(b"Logout" in resp.content, True) class InstancesConfig(TestCase): """ Tests InstancesConfig model """ def test_unique_constraint(self): """ Test configuration uniqueness """ config1 = InstanceConfiguration( name="test config 1", ram=0.5, cpu=1, storage=0.5 ) config1.save() config2 = InstanceConfiguration( name="test config 2", ram=0.5, cpu=2, storage=0.5 ) config2.save() with self.assertRaises(IntegrityError): config3 = InstanceConfiguration( name="test config 3", ram=0.5, cpu=1, storage=0.5 ) config3.save() class CreateInstance(TestCase): def setUp(self): register_util(t=self, username="createinstance_user") create_configurations(t=self) def test_create_instance_renders(self): c = Client() login_util(self, c, "accounts.home") urls = [(reverse("dash.instances.new"), "Instance Configuration")] for (url, test) in urls: print(f"[*] Testing URI: {url}") resp = c.get(url) self.assertEqual(resp.status_code, 200) self.assertEqual(b"Billing" in resp.content, True) self.assertEqual(b"Support" in resp.content, True) self.assertEqual(b"Logout" in resp.content, True) self.assertEqual(str.encode(test) in resp.content, True) payload = { "name": "test_create_instance_renders", "configuration": self.instance_config[0].name, } self.assertEqual(Instance.objects.filter(name=payload["name"]).exists(), False) resp = c.post(reverse("dash.instances.new"), payload) self.assertEqual(resp.status_code, 302) self.assertEqual(resp.headers["location"], reverse("dash.home")) self.assertEqual( Instance.objects.filter( name=payload["name"], owned_by=self.user, configuration_id=self.instance_config[0], ).exists(), True, ) resp = c.post(reverse("dash.instances.new"), payload) self.assertEqual(resp.status_code, 400) self.assertEqual(b"Instance name exists" in resp.content, True) payload = { "name": f"2{payload['name']}", "configuration": f"2{payload['name']}", } resp = c.post(reverse("dash.instances.new"), payload) self.assertEqual(resp.status_code, 400) self.assertEqual(b"Configuration doesn" in resp.content, True)