Skip to content

Commit c062b63

Browse files
committed
minimize duplicate code in model tests
1 parent d70cab5 commit c062b63

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

netbox_acls/tests/test_models.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,43 @@ def setUpTestData(cls):
7171
type=clustertype,
7272
)
7373
virtual_machine = VirtualMachine.objects.create(name="VirtualMachine 1")
74+
prefix = Prefix.objects.create(prefix="10.0.0.0/8")
7475

7576

7677
class TestAccessList(BaseTestCase):
7778
"""
7879
Test AccessList model.
7980
"""
8081

82+
common_acl_params = {
83+
"assigned_object_id": 1,
84+
"type": ACLTypeChoices.TYPE_EXTENDED,
85+
"default_action": ACLActionChoices.ACTION_PERMIT,
86+
}
87+
88+
def test_wrong_assigned_object_type_fail(self):
89+
"""
90+
Test that AccessList cannot be assigned to an object type other than Device, VirtualChassis, VirtualMachine, or Cluster.
91+
"""
92+
acl_bad_gfk = AccessList(
93+
name="TestACL_Wrong_GFK",
94+
assigned_object_type=ContentType.objects.get_for_model(Prefix),
95+
**self.common_acl_params,
96+
)
97+
with self.assertRaises(ValidationError):
98+
acl_bad_gfk.full_clean()
99+
81100
def test_alphanumeric_plus_success(self):
82101
"""
83102
Test that AccessList names with alphanumeric characters, '_', or '-' pass validation.
84103
"""
85104
acl_good_name = AccessList(
86-
name="Testacl-good_name-1",
105+
name="Testacl-Good_Name-1",
87106
assigned_object_type=ContentType.objects.get_for_model(Device),
88-
assigned_object_id=1,
89-
type=ACLTypeChoices.TYPE_EXTENDED,
90-
default_action=ACLActionChoices.ACTION_PERMIT,
107+
**self.common_acl_params,
91108
)
92109
acl_good_name.full_clean()
93-
# TODO: test_alphanumeric_plus_success - VirtualChassis & Cluster
110+
# TODO: test_alphanumeric_plus_success - VirtualChassis, VirtualMachine & Cluster
94111

95112
def test_duplicate_name_success(self):
96113
"""
@@ -103,23 +120,23 @@ def test_duplicate_name_success(self):
103120
"default_action": ACLActionChoices.ACTION_PERMIT,
104121
}
105122
AccessList.objects.create(
106-
**params,
123+
name="GOOD-DUPLICATE-ACL",
107124
assigned_object_type=ContentType.objects.get_for_model(Device),
108-
assigned_object_id=1,
125+
**self.common_acl_params,
109126
)
110127
vm_acl = AccessList(
111-
**params,
128+
name="GOOD-DUPLICATE-ACL",
112129
assigned_object_type=ContentType.objects.get_for_model(VirtualMachine),
113-
assigned_object_id=1,
130+
**self.common_acl_params,
114131
)
115132
vm_acl.full_clean()
116-
# TODO: test_duplicate_name_success - VirtualChassis & Cluster
117-
#vc_acl = AccessList(
118-
# **params,
133+
# TODO: test_duplicate_name_success - VirtualChassis, VirtualMachine & Cluster
134+
# vc_acl = AccessList(
135+
# "name": "GOOD-DUPLICATE-ACL",
119136
# assigned_object_type=ContentType.objects.get_for_model(VirtualChassis),
120-
# assigned_object_id=1,
121-
#)
122-
#vc_acl.full_clean()
137+
# **self.common_acl_params,
138+
# )
139+
# vc_acl.full_clean()
123140

124141
def test_alphanumeric_plus_fail(self):
125142
"""
@@ -131,10 +148,8 @@ def test_alphanumeric_plus_fail(self):
131148
bad_acl_name = AccessList(
132149
name=f"Testacl-bad_name_{i}_{char}",
133150
assigned_object_type=ContentType.objects.get_for_model(Device),
134-
assigned_object_id=1,
135-
type=ACLTypeChoices.TYPE_EXTENDED,
136-
default_action=ACLActionChoices.ACTION_PERMIT,
137151
comments=f'ACL with "{char}" in name',
152+
**self.common_acl_params,
138153
)
139154
with self.assertRaises(ValidationError):
140155
bad_acl_name.full_clean()
@@ -146,9 +161,8 @@ def test_duplicate_name_fail(self):
146161
params = {
147162
"name": "FAIL-DUPLICATE-ACL",
148163
"assigned_object_type": ContentType.objects.get_for_model(Device),
164+
**self.common_acl_params,
149165
"assigned_object_id": 1,
150-
"type": ACLTypeChoices.TYPE_STANDARD,
151-
"default_action": ACLActionChoices.ACTION_PERMIT,
152166
}
153167
acl_1 = AccessList.objects.create(**params)
154168
acl_1.save()
@@ -172,18 +186,18 @@ def setUpTestData(cls):
172186
"""
173187
super().setUpTestData()
174188

175-
#interfaces = Interface.objects.bulk_create(
189+
# interfaces = Interface.objects.bulk_create(
176190
# (
177191
# Interface(name="Interface 1", device=device, type="1000baset"),
178192
# Interface(name="Interface 2", device=device, type="1000baset"),
179193
# )
180-
#)
181-
#vminterfaces = VMInterface.objects.bulk_create(
194+
# )
195+
# vminterfaces = VMInterface.objects.bulk_create(
182196
# (
183197
# VMInterface(name="Interface 1", virtual_machine=virtual_machine),
184198
# VMInterface(name="Interface 2", virtual_machine=virtual_machine),
185199
# )
186-
#)
200+
# )
187201
# prefixes = Prefix.objects.bulk_create(
188202
# (
189203
# Prefix(prefix=IPNetwork("10.0.0.0/24")),
@@ -224,15 +238,18 @@ def test_acl_already_assinged_fail(self):
224238

225239
# TODO: Investigate a Base model for ACLStandardRule and ACLExtendedRule
226240

241+
227242
class TestACLStandardRule(BaseTestCase):
228243
"""
229244
Test ACLStandardRule model.
230245
"""
246+
231247
# TODO: Develop tests for ACLStandardRule model
232248

233249

234250
class ACLExtendedRule(BaseTestCase):
235251
"""
236252
Test ACLExtendedRule model.
237253
"""
254+
238255
# TODO: Develop tests for ACLExtendedRule model

0 commit comments

Comments
 (0)