Skip to content

Commit

Permalink
add new test for bulk perm update on service and portfolio
Browse files Browse the repository at this point in the history
  • Loading branch information
Sispheor committed Dec 13, 2024
1 parent cd1bf97 commit 5aab666
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
15 changes: 13 additions & 2 deletions service_catalog/models/portfolio.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import logging

from django.db.models import ForeignKey, SET_NULL, CharField, ImageField
from django.urls import reverse_lazy

from Squest.utils.squest_model import SquestModel
from profiles.models import Permission

logger = logging.getLogger(__name__)

class Portfolio(SquestModel):
name = CharField(max_length=100)
Expand Down Expand Up @@ -36,5 +40,12 @@ def delete(self, using=None, keep_parents=False):
super(Portfolio, self).delete(using, keep_parents)

def bulk_set_permission_on_operation(self, target_permission):
for service in self.service_list.all():
service.bulk_set_permission_on_operation(target_permission)
from service_catalog.models import Operation
logger.debug(f"Bulk edit permission on portfolio {self.name} to permission {target_permission}")
# check if all permission are already set to the target perm
all_permission_current_portfolio = Permission.objects.filter(operation__service__parent_portfolio=self).distinct()
# if the target perm is already the one used we do nothing
if all_permission_current_portfolio.count() == 1:
if all_permission_current_portfolio.first() == target_permission:
return
Operation.objects.filter(service__parent_portfolio=self).update(permission=target_permission)
6 changes: 6 additions & 0 deletions tests/test_service_catalog/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import tempfile

from django.contrib.contenttypes.models import ContentType
from django.test.testcases import TransactionTestCase
from django.contrib.auth.models import User
from django.test import TestCase
Expand Down Expand Up @@ -369,6 +371,10 @@ def setUp(self):
process_timeout_second=30
)

operation_content_type = ContentType.objects.get_for_model(Operation)
self.admin_operation, _ = Permission.objects.get_or_create(
content_type=operation_content_type, codename="is_admin_operation")

# ---------------------------------------------------
# Small job template
# ---------------------------------------------------
Expand Down
26 changes: 26 additions & 0 deletions tests/test_service_catalog/test_models/test_portfolio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from service_catalog.forms import FormUtils
from test_service_catalog.base_test_request import BaseTestRequest


class TestPortfolio(BaseTestRequest):

def setUp(self):
super(TestPortfolio, self).setUp()

def test_bulk_set_permission_on_operation(self):
self.service_test.parent_portfolio = self.portfolio_test_1
self.service_test.save()
self.service_test_2.parent_portfolio = self.portfolio_test_1
self.service_test_2.save()

for current_perm in self.service_test.operations.values_list("permission", flat=True):
self.assertEqual(current_perm, FormUtils.get_default_permission_for_operation())
for current_perm in self.service_test_2.operations.values_list("permission", flat=True):
self.assertEqual(current_perm, FormUtils.get_default_permission_for_operation())

# apply new perm
self.portfolio_test_1.bulk_set_permission_on_operation(self.admin_operation)
for current_perm in self.service_test.operations.values_list("permission", flat=True):
self.assertEqual(current_perm, self.admin_operation.id)
for current_perm in self.service_test_2.operations.values_list("permission", flat=True):
self.assertEqual(current_perm, self.admin_operation.id)
17 changes: 17 additions & 0 deletions tests/test_service_catalog/test_models/test_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from service_catalog.forms import FormUtils
from test_service_catalog.base_test_request import BaseTestRequest


class TestService(BaseTestRequest):

def setUp(self):
super(TestService, self).setUp()

def test_bulk_set_permission_on_operation(self):
for current_perm in self.service_test.operations.values_list("permission", flat=True):
self.assertEqual(current_perm, FormUtils.get_default_permission_for_operation())

# apply new perm
self.service_test.bulk_set_permission_on_operation(self.admin_operation)
for current_perm in self.service_test.operations.values_list("permission", flat=True):
self.assertEqual(current_perm, self.admin_operation.id)

0 comments on commit 5aab666

Please sign in to comment.