Skip to content

Commit

Permalink
include more flavor deletion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akochari committed Nov 26, 2024
1 parent 3120ce4 commit 0618247
Showing 1 changed file with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -14,17 +14,19 @@
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
"""
self.client.login(username=test_user["email"], password=test_user["password"])

project = Project.objects.get(name="test-flavor")

n_flavors_before = len(Flavor.objects.all())
response = self.client.post(
f"/projects/{project.slug}/createflavor/",
{
Expand All @@ -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/",
{
Expand All @@ -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,
Expand All @@ -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)

0 comments on commit 0618247

Please sign in to comment.