diff --git a/projects/tests/test_create_remove_flavors.py b/projects/tests/test_create_delete_flavors.py similarity index 59% rename from projects/tests/test_create_remove_flavors.py rename to projects/tests/test_create_delete_flavors.py index 07792c01..90c6bcc1 100644 --- a/projects/tests/test_create_remove_flavors.py +++ b/projects/tests/test_create_delete_flavors.py @@ -1,7 +1,7 @@ from django.contrib.auth import get_user_model from django.test import TestCase -from apps.models import Apps, CustomAppInstance +from apps.models import Apps, CustomAppInstance, Subdomain from projects.models import Flavor, Project from projects.views import can_model_instance_be_deleted @@ -14,10 +14,11 @@ class FlavorTestCase(TestCase): def setUp(self): user = User.objects.create_user(test_user["username"], test_user["email"], test_user["password"]) - _ = Project.objects.create_project(name="test-flavor", owner=user, description="") + project = Project.objects.create_project(name="test-flavor", owner=user, description="") User.objects.create_superuser(test_superuser["username"], test_superuser["email"], test_superuser["password"]) + Flavor.objects.create(name="flavor-to-be-deleted", project=project) - def test_forbidden_flavor_creation(self): + def test_flavor_creation_user(self): """ Test regular user not allowed to create flavor """ @@ -25,6 +26,7 @@ def test_forbidden_flavor_creation(self): project = Project.objects.get(name="test-flavor") + n_flavors_before = len(Flavor.objects.all()) response = self.client.post( f"/projects/{project.slug}/createflavor/", { @@ -37,18 +39,28 @@ def test_forbidden_flavor_creation(self): "ephmem_lim": "n", }, ) - self.assertEqual(response.status_code, 403) + n_flavors_after = len(Flavor.objects.all()) - flavors = Flavor.objects.all() - self.assertEqual(len(flavors), 0) + self.assertEqual(n_flavors_before, n_flavors_after) - def test_allowed_flavor_creation(self): + """ + Test regular user not allowed to delete flavor + """ + flavor_to_be_deleted = Flavor.objects.get(name="flavor-to-be-deleted") + n_flavors_before = len(Flavor.objects.all()) + response = self.client.post(f"/projects/{project.slug}/deleteflavor/", {"flavor_pk": flavor_to_be_deleted.pk}) + self.assertEqual(response.status_code, 403) + n_flavors_after = len(Flavor.objects.all()) + self.assertEqual(n_flavors_before, n_flavors_after) + + def test_flavor_creation_deletion_superuser(self): """ Test superuser is allowed to create flavor """ self.client.login(username=test_superuser["email"], password=test_superuser["password"]) project = Project.objects.get(name="test-flavor") + n_flavors_before = len(Flavor.objects.all()) response = self.client.post( f"/projects/{project.slug}/createflavor/", { @@ -61,25 +73,19 @@ def test_allowed_flavor_creation(self): "ephmem_lim": "n", }, ) - self.assertEqual(response.status_code, 302) + n_flavors_after = len(Flavor.objects.all()) - flavors = Flavor.objects.all() - self.assertEqual(len(flavors), 1) + self.assertEqual(n_flavors_before + 1, n_flavors_after) """ - Test it is allowed to delete flavor that is not in use + Test it is not allowed to delete flavor that is in use """ user = User.objects.get(email=test_superuser["email"]) flavor = Flavor.objects.get(name="new-flavor-superuser") - can_flavor_be_deleted = can_model_instance_be_deleted("flavor", flavor.pk) - self.assertTrue(can_flavor_be_deleted) - - """ - Test it is not allowed to delete flavor that is in use - """ app = Apps.objects.create(name="Some App", slug="customapp") + subdomain = Subdomain.objects.create(subdomain="test_internal") self.app_instance = CustomAppInstance.objects.create( access="public", owner=user, @@ -91,7 +97,31 @@ def test_allowed_flavor_creation(self): "environment": {"pk": ""}, }, flavor=flavor, + subdomain=subdomain, ) can_flavor_be_deleted = can_model_instance_be_deleted("flavor", flavor.pk) self.assertFalse(can_flavor_be_deleted) + + n_flavors_before = len(Flavor.objects.all()) + response = self.client.post(f"/projects/{project.slug}/deleteflavor/", {"flavor_pk": flavor.pk}) + self.assertEqual(response.status_code, 302) + n_flavors_after = len(Flavor.objects.all()) + + self.assertEqual(n_flavors_before, n_flavors_after) + + """ + Test it is allowed to delete flavor that is not in use + """ + + flavor_to_be_deleted = Flavor.objects.get(name="flavor-to-be-deleted") + + can_flavor_be_deleted = can_model_instance_be_deleted("flavor", flavor_to_be_deleted.pk) + self.assertTrue(can_flavor_be_deleted) + + n_flavors_before = len(Flavor.objects.all()) + response = self.client.post(f"/projects/{project.slug}/deleteflavor/", {"flavor_pk": flavor_to_be_deleted.pk}) + self.assertEqual(response.status_code, 302) + n_flavors_after = len(Flavor.objects.all()) + + self.assertEqual(n_flavors_before - 1, n_flavors_after)