From 5ad181097035f085615bf0e1a28a44ed22645ac7 Mon Sep 17 00:00:00 2001 From: ryanmerolle Date: Fri, 3 Feb 2023 14:38:28 +0000 Subject: [PATCH] minimize duplicate code in model tests --- netbox_acls/tests/test_models.py | 65 ++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/netbox_acls/tests/test_models.py b/netbox_acls/tests/test_models.py index 8c50c78..5e3af74 100644 --- a/netbox_acls/tests/test_models.py +++ b/netbox_acls/tests/test_models.py @@ -71,6 +71,7 @@ def setUpTestData(cls): type=clustertype, ) virtual_machine = VirtualMachine.objects.create(name="VirtualMachine 1") + prefix = Prefix.objects.create(prefix="10.0.0.0/8") class TestAccessList(BaseTestCase): @@ -78,19 +79,35 @@ class TestAccessList(BaseTestCase): Test AccessList model. """ + common_acl_params = { + "assigned_object_id": 1, + "type": ACLTypeChoices.TYPE_EXTENDED, + "default_action": ACLActionChoices.ACTION_PERMIT, + } + + def test_wrong_assigned_object_type_fail(self): + """ + Test that AccessList cannot be assigned to an object type other than Device, VirtualChassis, VirtualMachine, or Cluster. + """ + acl_bad_gfk = AccessList( + name="TestACL_Wrong_GFK", + assigned_object_type=ContentType.objects.get_for_model(Prefix), + **self.common_acl_params, + ) + with self.assertRaises(ValidationError): + acl_bad_gfk.full_clean() + def test_alphanumeric_plus_success(self): """ Test that AccessList names with alphanumeric characters, '_', or '-' pass validation. """ acl_good_name = AccessList( - name="Testacl-good_name-1", + name="Testacl-Good_Name-1", assigned_object_type=ContentType.objects.get_for_model(Device), - assigned_object_id=1, - type=ACLTypeChoices.TYPE_EXTENDED, - default_action=ACLActionChoices.ACTION_PERMIT, + **self.common_acl_params, ) acl_good_name.full_clean() - # TODO: test_alphanumeric_plus_success - VirtualChassis & Cluster + # TODO: test_alphanumeric_plus_success - VirtualChassis, VirtualMachine & Cluster def test_duplicate_name_success(self): """ @@ -103,23 +120,23 @@ def test_duplicate_name_success(self): "default_action": ACLActionChoices.ACTION_PERMIT, } AccessList.objects.create( - **params, + name="GOOD-DUPLICATE-ACL", assigned_object_type=ContentType.objects.get_for_model(Device), - assigned_object_id=1, + **self.common_acl_params, ) vm_acl = AccessList( - **params, + name="GOOD-DUPLICATE-ACL", assigned_object_type=ContentType.objects.get_for_model(VirtualMachine), - assigned_object_id=1, + **self.common_acl_params, ) vm_acl.full_clean() - # TODO: test_duplicate_name_success - VirtualChassis & Cluster - #vc_acl = AccessList( - # **params, + # TODO: test_duplicate_name_success - VirtualChassis, VirtualMachine & Cluster + # vc_acl = AccessList( + # "name": "GOOD-DUPLICATE-ACL", # assigned_object_type=ContentType.objects.get_for_model(VirtualChassis), - # assigned_object_id=1, - #) - #vc_acl.full_clean() + # **self.common_acl_params, + # ) + # vc_acl.full_clean() def test_alphanumeric_plus_fail(self): """ @@ -131,10 +148,8 @@ def test_alphanumeric_plus_fail(self): bad_acl_name = AccessList( name=f"Testacl-bad_name_{i}_{char}", assigned_object_type=ContentType.objects.get_for_model(Device), - assigned_object_id=1, - type=ACLTypeChoices.TYPE_EXTENDED, - default_action=ACLActionChoices.ACTION_PERMIT, comments=f'ACL with "{char}" in name', + **self.common_acl_params, ) with self.assertRaises(ValidationError): bad_acl_name.full_clean() @@ -146,9 +161,8 @@ def test_duplicate_name_fail(self): params = { "name": "FAIL-DUPLICATE-ACL", "assigned_object_type": ContentType.objects.get_for_model(Device), + **self.common_acl_params, "assigned_object_id": 1, - "type": ACLTypeChoices.TYPE_STANDARD, - "default_action": ACLActionChoices.ACTION_PERMIT, } acl_1 = AccessList.objects.create(**params) acl_1.save() @@ -172,18 +186,18 @@ def setUpTestData(cls): """ super().setUpTestData() - #interfaces = Interface.objects.bulk_create( + # interfaces = Interface.objects.bulk_create( # ( # Interface(name="Interface 1", device=device, type="1000baset"), # Interface(name="Interface 2", device=device, type="1000baset"), # ) - #) - #vminterfaces = VMInterface.objects.bulk_create( + # ) + # vminterfaces = VMInterface.objects.bulk_create( # ( # VMInterface(name="Interface 1", virtual_machine=virtual_machine), # VMInterface(name="Interface 2", virtual_machine=virtual_machine), # ) - #) + # ) # prefixes = Prefix.objects.bulk_create( # ( # Prefix(prefix=IPNetwork("10.0.0.0/24")), @@ -224,10 +238,12 @@ def test_acl_already_assinged_fail(self): # TODO: Investigate a Base model for ACLStandardRule and ACLExtendedRule + class TestACLStandardRule(BaseTestCase): """ Test ACLStandardRule model. """ + # TODO: Develop tests for ACLStandardRule model @@ -235,4 +251,5 @@ class ACLExtendedRule(BaseTestCase): """ Test ACLExtendedRule model. """ + # TODO: Develop tests for ACLExtendedRule model