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
8
7
9
8
from netbox_acls .models import AccessList
10
9
@@ -20,16 +19,16 @@ class TestAccessList(BaseTestCase):
20
19
"type" : "extended" ,
21
20
"default_action" : "permit" ,
22
21
}
23
- # device = Device.objects.first()
24
22
25
23
def test_wrong_assigned_object_type_fail (self ):
26
24
"""
27
- Test that AccessList cannot be assigned to an object type other than Device, VirtualChassis, VirtualMachine, or Cluster.
25
+ Test that AccessList cannot be assigned to an object type other than Device, VirtualChassis, VirtualMachine,
26
+ or Cluster.
28
27
"""
29
28
acl_bad_gfk = AccessList (
30
29
name = "TestACL_Wrong_GFK" ,
31
30
assigned_object_type = ContentType .objects .get_for_model (Prefix ),
32
- assigned_object_id = Prefix . objects . first () ,
31
+ assigned_object_id = self . prefix1 . id ,
33
32
** self .common_acl_params ,
34
33
)
35
34
with self .assertRaises (ValidationError ):
@@ -40,31 +39,24 @@ def test_alphanumeric_plus_success(self):
40
39
Test that AccessList names with alphanumeric characters, '_', or '-' pass validation.
41
40
"""
42
41
acl_good_name = AccessList (
43
- name = "Testacl -Good_Name-1" ,
42
+ name = "Test-ACL -Good_Name-1" ,
44
43
assigned_object_type = ContentType .objects .get_for_model (Device ),
45
- assigned_object_id = 1 , # TODO - replace with Device.objects.first()
44
+ assigned_object_id = self . device1 . id ,
46
45
** self .common_acl_params ,
47
46
)
48
47
acl_good_name .full_clean ()
49
- # TODO: test_alphanumeric_plus_success - VirtualChassis, VirtualMachine & Cluster
50
48
51
49
def test_duplicate_name_success (self ):
52
50
"""
53
51
Test that AccessList names can be non-unique if associated with different devices.
54
52
"""
55
- AccessList .objects .create (
53
+ # Device
54
+ device_acl = AccessList (
56
55
name = "GOOD-DUPLICATE-ACL" ,
57
- assigned_object_type = ContentType .objects .get_for_model (Device ),
58
- assigned_object_id = 1 , # TODO - replace with Device.objects.first()
59
- ** self .common_acl_params ,
60
- )
61
- vm_acl = AccessList (
62
- name = "GOOD-DUPLICATE-ACL" ,
63
- assigned_object_type = ContentType .objects .get_for_model (VirtualMachine ),
64
- assigned_object_id = 1 , # TODO - replace with VirtualMachine.objects.first().id,
56
+ assigned_object = self .device1 ,
65
57
** self .common_acl_params ,
66
58
)
67
- vm_acl .full_clean ()
59
+ device_acl .full_clean ()
68
60
# TODO: test_duplicate_name_success - VirtualChassis, VirtualMachine & Cluster
69
61
# vc_acl = AccessList(
70
62
# "name": "GOOD-DUPLICATE-ACL",
@@ -81,23 +73,23 @@ def test_alphanumeric_plus_fail(self):
81
73
82
74
for i , char in enumerate (non_alphanumeric_plus_chars , start = 1 ):
83
75
bad_acl_name = AccessList (
84
- name = f"Testacl -bad_name_{ i } _{ char } " ,
85
- assigned_object_type = ContentType . objects . get_for_model ( Device ) ,
76
+ name = f"Test-ACL -bad_name_{ i } _{ char } " ,
77
+ assigned_object = self . device1 ,
86
78
comments = f'ACL with "{ char } " in name' ,
87
79
** self .common_acl_params ,
88
80
)
89
81
with self .assertRaises (ValidationError ):
90
82
bad_acl_name .full_clean ()
91
83
92
- def test_duplicate_name_fail (self ):
84
+ def test_duplicate_name_per_device_fail (self ):
93
85
"""
94
86
Test that AccessList names must be unique per device.
95
87
"""
96
88
params = {
97
89
"name" : "FAIL-DUPLICATE-ACL" ,
98
90
"assigned_object_type" : ContentType .objects .get_for_model (Device ),
91
+ "assigned_object_id" : self .device1 .id ,
99
92
** self .common_acl_params ,
100
- "assigned_object_id" : 1 , # TODO - replace with Device.objects.first()
101
93
}
102
94
acl_1 = AccessList .objects .create (** params )
103
95
acl_1 .save ()
@@ -122,11 +114,10 @@ def test_valid_acl_choices(self):
122
114
for default_action , acl_type in valid_acl_choices :
123
115
valid_acl_choice = AccessList (
124
116
name = f"TestACL_Valid_Choice_{ default_action } _{ acl_type } " ,
125
- comments = f"VALID ACL CHOICES USED: { default_action = } { acl_type = } " ,
117
+ assigned_object = self . device1 ,
126
118
type = acl_type ,
127
119
default_action = default_action ,
128
- assigned_object_type = ContentType .objects .get_for_model (Device ),
129
- assigned_object_id = 1 , # TODO - replace with Device.objects.first()
120
+ comments = f"VALID ACL CHOICES USED: { default_action = } { acl_type = } " ,
130
121
)
131
122
valid_acl_choice .full_clean ()
132
123
@@ -138,11 +129,10 @@ def test_invalid_acl_choices(self):
138
129
invalid_acl_default_action_choice = "log"
139
130
invalid_acl_default_action = AccessList (
140
131
name = f"TestACL_Valid_Choice_{ invalid_acl_default_action_choice } _{ valid_acl_types [0 ]} " ,
141
- comments = f"INVALID ACL DEFAULT CHOICE USED: default_action=' { invalid_acl_default_action_choice } '" ,
132
+ assigned_object = self . device1 ,
142
133
type = valid_acl_types [0 ],
143
134
default_action = invalid_acl_default_action_choice ,
144
- assigned_object_type = ContentType .objects .get_for_model (Device ),
145
- assigned_object_id = 1 , # TODO - replace with Device.objects.first()
135
+ comments = f"INVALID ACL DEFAULT CHOICE USED: default_action='{ invalid_acl_default_action_choice } '" ,
146
136
)
147
137
with self .assertRaises (ValidationError ):
148
138
invalid_acl_default_action .full_clean ()
@@ -151,11 +141,10 @@ def test_invalid_acl_choices(self):
151
141
invalid_acl_type = "super-dupper-extended"
152
142
invalid_acl_type = AccessList (
153
143
name = f"TestACL_Valid_Choice_{ valid_acl_default_action_choices [0 ]} _{ invalid_acl_type } " ,
154
- comments = f"INVALID ACL DEFAULT CHOICE USED: type=' { invalid_acl_type } '" ,
144
+ assigned_object = self . device1 ,
155
145
type = invalid_acl_type ,
156
146
default_action = valid_acl_default_action_choices [0 ],
157
- assigned_object_type = ContentType .objects .get_for_model (Device ),
158
- assigned_object_id = 1 , # TODO - replace with Device.objects.first()
147
+ comments = f"INVALID ACL DEFAULT CHOICE USED: type='{ invalid_acl_type } '" ,
159
148
)
160
149
with self .assertRaises (ValidationError ):
161
150
invalid_acl_type .full_clean ()
0 commit comments