|
|
@@ -3,10 +3,12 @@ import hashlib
|
|
|
import io
|
|
|
import logging
|
|
|
import uuid
|
|
|
-from unittest.mock import PropertyMock, patch
|
|
|
+from contextlib import contextmanager
|
|
|
+from unittest.mock import MagicMock, PropertyMock, patch
|
|
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
from django.contrib.messages import get_messages
|
|
|
+from django.core.files.uploadedfile import SimpleUploadedFile
|
|
|
from django.test import tag
|
|
|
from django.urls import reverse
|
|
|
from django.utils.html import escape
|
|
|
@@ -14,7 +16,7 @@ from django.utils.timezone import now
|
|
|
|
|
|
from core.choices import JobStatusChoices, ManagedFileRootPathChoices
|
|
|
from core.events import *
|
|
|
-from core.models import DataFile, DataSource, Job, ObjectType
|
|
|
+from core.models import DataFile, DataSource, Job, ManagedFile, ObjectType
|
|
|
from dcim.models import DeviceType, Manufacturer, Site
|
|
|
from extras.choices import *
|
|
|
from extras.models import *
|
|
|
@@ -1529,9 +1531,42 @@ class ScriptListViewTestCase(TestCase):
|
|
|
self.assertTemplateUsed(response, 'extras/inc/script_list_content.html')
|
|
|
|
|
|
|
|
|
-class ScriptModuleCreateViewTestCase(TestCase):
|
|
|
+class ScriptModuleViewTestCase(TestCase):
|
|
|
user_permissions = ['core.add_managedfile', 'extras.add_scriptmodule']
|
|
|
|
|
|
+ @classmethod
|
|
|
+ def setUpTestData(cls):
|
|
|
+ with patch.object(ScriptModule, 'sync_classes'):
|
|
|
+ cls.module = ScriptModule.objects.create(
|
|
|
+ file_root=ManagedFileRootPathChoices.SCRIPTS,
|
|
|
+ file_path='test_script.py',
|
|
|
+ )
|
|
|
+
|
|
|
+ @contextmanager
|
|
|
+ def _patched_script_storage(self):
|
|
|
+ """Mock script storage and skip script class synchronization."""
|
|
|
+ storage = MagicMock()
|
|
|
+ with (
|
|
|
+ patch('extras.forms.scripts.storages') as form_storages,
|
|
|
+ patch('core.models.files.storages') as file_storages,
|
|
|
+ patch.object(ScriptModule, 'sync_classes'),
|
|
|
+ ):
|
|
|
+ form_storages.create_storage.return_value = storage
|
|
|
+ file_storages.create_storage.return_value = storage
|
|
|
+ yield storage
|
|
|
+
|
|
|
+ def _grant_constrained_permission(self, model, action, file_path):
|
|
|
+ """Grant the action on the model, constrained to a single file path."""
|
|
|
+ obj_perm = ObjectPermission(
|
|
|
+ name=f'{action} {model._meta.model_name}',
|
|
|
+ constraints={'file_path': file_path},
|
|
|
+ actions=[action],
|
|
|
+ )
|
|
|
+ obj_perm.save()
|
|
|
+ obj_perm.users.add(self.user)
|
|
|
+ obj_perm.object_types.add(ObjectType.objects.get_for_model(model, for_concrete_model=False))
|
|
|
+ return obj_perm
|
|
|
+
|
|
|
@tag('regression')
|
|
|
def test_default_return_url(self):
|
|
|
"""
|
|
|
@@ -1542,6 +1577,122 @@ class ScriptModuleCreateViewTestCase(TestCase):
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
self.assertEqual(response.context['return_url'], reverse('extras:script_list'))
|
|
|
|
|
|
+ @tag('regression') # Ref: #23828
|
|
|
+ def test_add_script_module_with_partial_permissions(self):
|
|
|
+ """
|
|
|
+ Require both add permissions to create a script module.
|
|
|
+ """
|
|
|
+ script_content = b"from extras.scripts import Script\nclass TestScript(Script):\n pass\n"
|
|
|
+ for permission in ('extras.add_scriptmodule', 'core.add_managedfile'):
|
|
|
+ with self.cleanupSubTest(permission=permission):
|
|
|
+ self.remove_permissions(permission)
|
|
|
+ with (
|
|
|
+ self._patched_script_storage() as storage,
|
|
|
+ patch('extras.forms.scripts.validate_script_content') as validate_script_content,
|
|
|
+ ):
|
|
|
+ response = self.client.post(reverse('extras:scriptmodule_add'), {
|
|
|
+ 'upload_file': SimpleUploadedFile('zz_partial.py', script_content, content_type='text/plain'),
|
|
|
+ })
|
|
|
+ self.assertHttpStatus(response, 403)
|
|
|
+ validate_script_content.assert_not_called()
|
|
|
+ storage.save.assert_not_called()
|
|
|
+
|
|
|
+ @tag('regression') # Ref: #23828
|
|
|
+ def test_edit_script_module_with_partial_permissions(self):
|
|
|
+ """
|
|
|
+ Require both change permissions to edit a script module.
|
|
|
+ """
|
|
|
+ url = reverse('extras:scriptmodule_edit', kwargs={'pk': self.module.pk})
|
|
|
+ script_content = b"from extras.scripts import Script\nclass TestScript(Script):\n pass\n"
|
|
|
+ for permission in ('extras.change_scriptmodule', 'core.change_managedfile'):
|
|
|
+ with self.cleanupSubTest(permission=permission):
|
|
|
+ self.add_permissions(permission)
|
|
|
+ with (
|
|
|
+ self._patched_script_storage() as storage,
|
|
|
+ patch('extras.forms.scripts.validate_script_content') as validate_script_content,
|
|
|
+ ):
|
|
|
+ response = self.client.post(url, {
|
|
|
+ 'upload_file': SimpleUploadedFile('test_script.py', script_content, content_type='text/plain'),
|
|
|
+ })
|
|
|
+ self.assertHttpStatus(response, 403)
|
|
|
+ validate_script_content.assert_not_called()
|
|
|
+ storage.save.assert_not_called()
|
|
|
+
|
|
|
+ self.add_permissions('extras.change_scriptmodule', 'core.change_managedfile')
|
|
|
+ self.assertHttpStatus(self.client.get(url), 200)
|
|
|
+
|
|
|
+ @tag('regression') # Ref: #23828
|
|
|
+ def test_edit_script_module_with_constrained_permission(self):
|
|
|
+ """
|
|
|
+ Enforce both models' change permission constraints.
|
|
|
+ """
|
|
|
+ url = reverse('extras:scriptmodule_edit', kwargs={'pk': self.module.pk})
|
|
|
+ script_content = b"from extras.scripts import Script\nclass TestScript(Script):\n pass\n"
|
|
|
+ for model, other_permission in (
|
|
|
+ (ScriptModule, 'core.change_managedfile'),
|
|
|
+ (ManagedFile, 'extras.change_scriptmodule'),
|
|
|
+ ):
|
|
|
+ with self.cleanupSubTest(model=model._meta.model_name):
|
|
|
+ self.add_permissions(other_permission)
|
|
|
+ obj_perm = self._grant_constrained_permission(model, 'change', 'other.py')
|
|
|
+ self.assertHttpStatus(self.client.get(url), 404)
|
|
|
+ with (
|
|
|
+ self._patched_script_storage() as storage,
|
|
|
+ patch('extras.forms.scripts.validate_script_content') as validate_script_content,
|
|
|
+ ):
|
|
|
+ response = self.client.post(url, {
|
|
|
+ 'upload_file': SimpleUploadedFile('test_script.py', script_content, content_type='text/plain'),
|
|
|
+ })
|
|
|
+ self.assertHttpStatus(response, 404)
|
|
|
+ validate_script_content.assert_not_called()
|
|
|
+ storage.save.assert_not_called()
|
|
|
+
|
|
|
+ obj_perm.constraints = {'file_path': self.module.file_path}
|
|
|
+ obj_perm.save()
|
|
|
+ self.assertHttpStatus(self.client.get(url), 200)
|
|
|
+
|
|
|
+ @tag('regression') # Ref: #23828
|
|
|
+ def test_delete_script_module_with_partial_permissions(self):
|
|
|
+ """
|
|
|
+ Require both delete permissions to delete a script module.
|
|
|
+ """
|
|
|
+ url = reverse('extras:scriptmodule_delete', kwargs={'pk': self.module.pk})
|
|
|
+ for permission in ('extras.delete_scriptmodule', 'core.delete_managedfile'):
|
|
|
+ with self.cleanupSubTest(permission=permission):
|
|
|
+ self.add_permissions(permission)
|
|
|
+ with self._patched_script_storage() as storage:
|
|
|
+ response = self.client.post(url, {'confirm': True})
|
|
|
+ self.assertHttpStatus(response, 403)
|
|
|
+ storage.delete.assert_not_called()
|
|
|
+ self.assertTrue(ScriptModule.objects.filter(pk=self.module.pk).exists())
|
|
|
+
|
|
|
+ self.add_permissions('extras.delete_scriptmodule', 'core.delete_managedfile')
|
|
|
+ self.assertHttpStatus(self.client.get(url), 200)
|
|
|
+
|
|
|
+ @tag('regression') # Ref: #23828
|
|
|
+ def test_delete_script_module_with_constrained_permission(self):
|
|
|
+ """
|
|
|
+ Enforce both models' delete permission constraints.
|
|
|
+ """
|
|
|
+ url = reverse('extras:scriptmodule_delete', kwargs={'pk': self.module.pk})
|
|
|
+ for model, other_permission in (
|
|
|
+ (ScriptModule, 'core.delete_managedfile'),
|
|
|
+ (ManagedFile, 'extras.delete_scriptmodule'),
|
|
|
+ ):
|
|
|
+ with self.cleanupSubTest(model=model._meta.model_name):
|
|
|
+ self.add_permissions(other_permission)
|
|
|
+ obj_perm = self._grant_constrained_permission(model, 'delete', 'other.py')
|
|
|
+ self.assertHttpStatus(self.client.get(url), 404)
|
|
|
+ with self._patched_script_storage() as storage:
|
|
|
+ response = self.client.post(url, {'confirm': True})
|
|
|
+ self.assertHttpStatus(response, 404)
|
|
|
+ storage.delete.assert_not_called()
|
|
|
+ self.assertTrue(ScriptModule.objects.filter(pk=self.module.pk).exists())
|
|
|
+
|
|
|
+ obj_perm.constraints = {'file_path': self.module.file_path}
|
|
|
+ obj_perm.save()
|
|
|
+ self.assertHttpStatus(self.client.get(url), 200)
|
|
|
+
|
|
|
|
|
|
class ScriptDefaultBackfillTestCase(TestCase):
|
|
|
"""
|