1
1
from itertools import cycle
2
2
3
- from dcim .models import Device
3
+ from dcim .models import Device , VirtualChassis
4
4
from django .contrib .contenttypes .models import ContentType
5
5
from django .core .exceptions import ValidationError
6
6
from ipam .models import Prefix
7
+ from virtualization .models import VirtualMachine
7
8
8
9
from netbox_acls .models import AccessList
9
10
@@ -20,6 +21,84 @@ class TestAccessList(BaseTestCase):
20
21
"default_action" : "permit" ,
21
22
}
22
23
24
+ def test_accesslist_standard_creation (self ):
25
+ """
26
+ Test that AccessList Standard creation passes validation.
27
+ """
28
+ acl_name = "Test-ACL-Standard-Type"
29
+
30
+ created_acl = AccessList (
31
+ name = acl_name ,
32
+ assigned_object = self .device1 ,
33
+ type = "standard" ,
34
+ default_action = "deny" ,
35
+ )
36
+
37
+ self .assertTrue (isinstance (created_acl , AccessList ), True )
38
+ self .assertEqual (created_acl .name , acl_name )
39
+ self .assertEqual (created_acl .type , "standard" )
40
+ self .assertEqual (created_acl .default_action , "deny" )
41
+ self .assertEqual (isinstance (created_acl .assigned_object , Device ), True )
42
+ self .assertEqual (created_acl .assigned_object , self .device1 )
43
+
44
+ def test_accesslist_extended_creation (self ):
45
+ """
46
+ Test that AccessList Extended creation passes validation.
47
+ """
48
+ acl_name = "Test-ACL-Extended-Type"
49
+
50
+ created_acl = AccessList (
51
+ name = acl_name ,
52
+ assigned_object = self .device2 ,
53
+ type = "extended" ,
54
+ default_action = "permit" ,
55
+ )
56
+
57
+ self .assertTrue (isinstance (created_acl , AccessList ))
58
+ self .assertEqual (created_acl .name , acl_name )
59
+ self .assertEqual (created_acl .type , "extended" )
60
+ self .assertEqual (created_acl .default_action , "permit" )
61
+ self .assertEqual (isinstance (created_acl .assigned_object , Device ), True )
62
+ self .assertEqual (created_acl .assigned_object , self .device2 )
63
+
64
+ def test_accesslist_creation_with_virtual_chassis (self ):
65
+ """
66
+ Test that AccessList creation with an assigned virtual chassis passes validation.
67
+ """
68
+ acl_name = "Test-ACL-with-Virtual-Machine"
69
+
70
+ created_acl = AccessList (
71
+ name = acl_name ,
72
+ assigned_object = self .virtual_chassis1 ,
73
+ ** self .common_acl_params ,
74
+ )
75
+
76
+ self .assertTrue (isinstance (created_acl , AccessList ))
77
+ self .assertEqual (created_acl .name , acl_name )
78
+ self .assertEqual (created_acl .type , "extended" )
79
+ self .assertEqual (created_acl .default_action , "permit" )
80
+ self .assertEqual (isinstance (created_acl .assigned_object , VirtualChassis ), True )
81
+ self .assertEqual (created_acl .assigned_object , self .virtual_chassis1 )
82
+
83
+ def test_accesslist_creation_with_virtual_machine (self ):
84
+ """
85
+ Test that AccessList creation with an assigned virtual machine passes validation.
86
+ """
87
+ acl_name = "Test-ACL-with-Virtual-Machine"
88
+
89
+ created_acl = AccessList (
90
+ name = acl_name ,
91
+ assigned_object = self .virtual_machine1 ,
92
+ ** self .common_acl_params ,
93
+ )
94
+
95
+ self .assertTrue (isinstance (created_acl , AccessList ))
96
+ self .assertEqual (created_acl .name , acl_name )
97
+ self .assertEqual (created_acl .type , "extended" )
98
+ self .assertEqual (created_acl .default_action , "permit" )
99
+ self .assertEqual (isinstance (created_acl .assigned_object , VirtualMachine ), True )
100
+ self .assertEqual (created_acl .assigned_object , self .virtual_machine1 )
101
+
23
102
def test_wrong_assigned_object_type_fail (self ):
24
103
"""
25
104
Test that AccessList cannot be assigned to an object type other than Device, VirtualChassis, VirtualMachine,
@@ -57,13 +136,22 @@ def test_duplicate_name_success(self):
57
136
** self .common_acl_params ,
58
137
)
59
138
device_acl .full_clean ()
60
- # TODO: test_duplicate_name_success - VirtualChassis, VirtualMachine & Cluster
61
- # vc_acl = AccessList(
62
- # "name": "GOOD-DUPLICATE-ACL",
63
- # assigned_object_type=ContentType.objects.get_for_model(VirtualChassis),
64
- # **self.common_acl_params,
65
- # )
66
- # vc_acl.full_clean()
139
+
140
+ # Virtual Chassis
141
+ vc_acl = AccessList (
142
+ name = "GOOD-DUPLICATE-ACL" ,
143
+ assigned_object = self .virtual_chassis1 ,
144
+ ** self .common_acl_params ,
145
+ )
146
+ vc_acl .full_clean ()
147
+
148
+ # Virtual Machine
149
+ vm_acl = AccessList (
150
+ name = "GOOD-DUPLICATE-ACL" ,
151
+ assigned_object = self .virtual_machine1 ,
152
+ ** self .common_acl_params ,
153
+ )
154
+ vm_acl .full_clean ()
67
155
68
156
def test_alphanumeric_plus_fail (self ):
69
157
"""
@@ -96,7 +184,38 @@ def test_duplicate_name_per_device_fail(self):
96
184
acl_2 = AccessList (** params )
97
185
with self .assertRaises (ValidationError ):
98
186
acl_2 .full_clean ()
99
- # TODO: test_duplicate_name_fail - VirtualChassis & Cluster
187
+
188
+ def test_duplicate_name_per_virtual_chassis_fail (self ):
189
+ """
190
+ Test that AccessList names must be unique per virtual chassis.
191
+ """
192
+ params = {
193
+ "name" : "FAIL-DUPLICATE-ACL" ,
194
+ "assigned_object_type" : ContentType .objects .get_for_model (VirtualChassis ),
195
+ "assigned_object_id" : self .virtual_chassis1 .id ,
196
+ ** self .common_acl_params ,
197
+ }
198
+ acl_1 = AccessList .objects .create (** params )
199
+ acl_1 .save ()
200
+ acl_2 = AccessList (** params )
201
+ with self .assertRaises (ValidationError ):
202
+ acl_2 .full_clean ()
203
+
204
+ def test_duplicate_name_per_virtual_machine_fail (self ):
205
+ """
206
+ Test that AccessList names must be unique per virtual machine.
207
+ """
208
+ params = {
209
+ "name" : "FAIL-DUPLICATE-ACL" ,
210
+ "assigned_object_type" : ContentType .objects .get_for_model (VirtualMachine ),
211
+ "assigned_object_id" : self .virtual_machine1 .id ,
212
+ ** self .common_acl_params ,
213
+ }
214
+ acl_1 = AccessList .objects .create (** params )
215
+ acl_1 .save ()
216
+ acl_2 = AccessList (** params )
217
+ with self .assertRaises (ValidationError ):
218
+ acl_2 .full_clean ()
100
219
101
220
def test_valid_acl_choices (self ):
102
221
"""
0 commit comments