# 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 class DashHome(TestCase): """ Tests create new app view """ def setUp(self): self.password = "password121231" self.username = "dashboard_home_user" self.email = f"{self.username}@example.org" self.user = get_user_model().objects.create( username=self.username, email=self.email, ) self.user.set_password(self.password) self.user.save() 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 payload = { "login": self.username, "password": self.password, } resp = c.post(reverse("accounts.login"), payload) self.assertEqual(resp.status_code, 302) self.assertEqual(resp.headers["location"], reverse("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()