From ba3a32051d3cb2c96aa04b27d2340b1edcd337d5 Mon Sep 17 00:00:00 2001 From: Jason Novinger Date: Fri, 20 Feb 2026 15:25:27 -0600 Subject: [PATCH] Add tests for ModelAction and register_model_actions --- netbox/utilities/tests/test_permissions.py | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 netbox/utilities/tests/test_permissions.py diff --git a/netbox/utilities/tests/test_permissions.py b/netbox/utilities/tests/test_permissions.py new file mode 100644 index 000000000..fcfe022b0 --- /dev/null +++ b/netbox/utilities/tests/test_permissions.py @@ -0,0 +1,77 @@ +from django.test import TestCase + +from dcim.models import Site +from netbox.registry import registry +from utilities.permissions import ModelAction, register_model_actions + + +class ModelActionTest(TestCase): + + def test_hash(self): + action1 = ModelAction(name='sync') + action2 = ModelAction(name='sync', help_text='Different help') + self.assertEqual(hash(action1), hash(action2)) + + def test_equality_with_model_action(self): + action1 = ModelAction(name='sync') + action2 = ModelAction(name='sync', help_text='Different help') + action3 = ModelAction(name='merge') + self.assertEqual(action1, action2) + self.assertNotEqual(action1, action3) + + def test_equality_with_string(self): + action = ModelAction(name='sync') + self.assertEqual(action, 'sync') + self.assertNotEqual(action, 'merge') + + def test_usable_in_set(self): + action1 = ModelAction(name='sync') + action2 = ModelAction(name='sync', help_text='Different') + action3 = ModelAction(name='merge') + actions = {action1, action2, action3} + self.assertEqual(len(actions), 2) + + +class RegisterModelActionsTest(TestCase): + + def setUp(self): + self.original_actions = dict(registry['model_actions']) + + def tearDown(self): + registry['model_actions'].clear() + registry['model_actions'].update(self.original_actions) + + def test_register_model_action_objects(self): + register_model_actions(Site, [ + ModelAction('test_action', help_text='Test help'), + ]) + actions = registry['model_actions']['dcim.site'] + self.assertEqual(len(actions), 1) + self.assertEqual(actions[0].name, 'test_action') + self.assertEqual(actions[0].help_text, 'Test help') + + def test_register_string_actions(self): + register_model_actions(Site, ['action1', 'action2']) + actions = registry['model_actions']['dcim.site'] + self.assertEqual(len(actions), 2) + self.assertIsInstance(actions[0], ModelAction) + self.assertEqual(actions[0].name, 'action1') + self.assertEqual(actions[1].name, 'action2') + + def test_register_mixed_actions(self): + register_model_actions(Site, [ + ModelAction('with_help', help_text='Has help'), + 'without_help', + ]) + actions = registry['model_actions']['dcim.site'] + self.assertEqual(len(actions), 2) + self.assertEqual(actions[0].help_text, 'Has help') + self.assertEqual(actions[1].help_text, '') + + def test_multiple_registrations_append(self): + register_model_actions(Site, [ModelAction('first')]) + register_model_actions(Site, [ModelAction('second')]) + actions = registry['model_actions']['dcim.site'] + self.assertEqual(len(actions), 2) + self.assertEqual(actions[0].name, 'first') + self.assertEqual(actions[1].name, 'second')