1+ from django .core .exceptions import ValidationError
2+
3+ from netbox_acls .choices import ACLTypeChoices
4+ from netbox_acls .models import AccessList , ACLStandardRule
5+
16from .base import BaseTestCase
27
38
@@ -6,4 +11,212 @@ class TestACLStandardRule(BaseTestCase):
611 Test ACLStandardRule model.
712 """
813
9- # TODO: Develop tests for ACLStandardRule model
14+ @classmethod
15+ def setUpTestData (cls ):
16+ """
17+ Extend BaseTestCase's setUpTestData() to create additional data for testing.
18+ """
19+ super ().setUpTestData ()
20+
21+ cls .acl_type = ACLTypeChoices .TYPE_STANDARD
22+ cls .default_action = "deny"
23+
24+ # AccessLists
25+ cls .standard_acl1 = AccessList .objects .create (
26+ name = "STANDARD_ACL" ,
27+ assigned_object = cls .device1 ,
28+ type = cls .acl_type ,
29+ default_action = cls .default_action ,
30+ comments = "STANDARD_ACL" ,
31+ )
32+ cls .standard_acl2 = AccessList .objects .create (
33+ name = "STANDARD_ACL" ,
34+ assigned_object = cls .virtual_machine1 ,
35+ type = cls .acl_type ,
36+ default_action = cls .default_action ,
37+ comments = "STANDARD_ACL" ,
38+ )
39+
40+ def test_acl_standard_rule_creation_success (self ):
41+ """
42+ Test that ACLStandardRule creation passes validation.
43+ """
44+ created_rule = ACLStandardRule (
45+ access_list = self .standard_acl1 ,
46+ index = 10 ,
47+ action = "permit" ,
48+ remark = "" ,
49+ source_prefix = None ,
50+ description = "Created rule with any source prefix" ,
51+ )
52+ created_rule .full_clean ()
53+
54+ self .assertTrue (isinstance (created_rule , ACLStandardRule ), True )
55+ self .assertEqual (created_rule .index , 10 )
56+ self .assertEqual (created_rule .action , "permit" )
57+ self .assertEqual (created_rule .remark , "" )
58+ self .assertEqual (created_rule .source_prefix , None )
59+ self .assertEqual (created_rule .description , "Created rule with any source prefix" )
60+ self .assertEqual (isinstance (created_rule .access_list , AccessList ), True )
61+ self .assertEqual (created_rule .access_list .type , self .acl_type )
62+
63+ def test_acl_standard_rule_source_prefix_creation_success (self ):
64+ """
65+ Test that ACLStandardRule with source prefix creation passes validation.
66+ """
67+ created_rule = ACLStandardRule (
68+ access_list = self .standard_acl1 ,
69+ index = 20 ,
70+ action = "permit" ,
71+ remark = "" ,
72+ source_prefix = self .prefix1 ,
73+ description = "Created rule with source prefix" ,
74+ )
75+ created_rule .full_clean ()
76+
77+ self .assertTrue (isinstance (created_rule , ACLStandardRule ), True )
78+ self .assertEqual (created_rule .index , 20 )
79+ self .assertEqual (created_rule .action , "permit" )
80+ self .assertEqual (created_rule .remark , "" )
81+ self .assertEqual (created_rule .source_prefix , self .prefix1 )
82+ self .assertEqual (created_rule .description , "Created rule with source prefix" )
83+ self .assertEqual (isinstance (created_rule .access_list , AccessList ), True )
84+ self .assertEqual (created_rule .access_list .type , self .acl_type )
85+
86+ def test_acl_standard_rule_remark_creation_success (self ):
87+ """
88+ Test that ACLStandardRule with remark creation passes validation.
89+ """
90+ created_rule = ACLStandardRule (
91+ access_list = self .standard_acl1 ,
92+ index = 30 ,
93+ action = "remark" ,
94+ remark = "Test remark" ,
95+ source_prefix = None ,
96+ description = "Created rule with remark" ,
97+ )
98+ created_rule .full_clean ()
99+
100+ self .assertTrue (isinstance (created_rule , ACLStandardRule ), True )
101+ self .assertEqual (created_rule .index , 30 )
102+ self .assertEqual (created_rule .action , "remark" )
103+ self .assertEqual (created_rule .remark , "Test remark" )
104+ self .assertEqual (created_rule .source_prefix , None )
105+ self .assertEqual (created_rule .description , "Created rule with remark" )
106+ self .assertEqual (isinstance (created_rule .access_list , AccessList ), True )
107+ self .assertEqual (created_rule .access_list .type , self .acl_type )
108+
109+ def test_access_list_extended_to_acl_standard_rule_assignment_fail (self ):
110+ """
111+ Test that Extended Access List cannot be assigned to ACLStandardRule.
112+ """
113+ extended_acl1 = AccessList .objects .create (
114+ name = "EXTENDED_ACL" ,
115+ assigned_object = self .device1 ,
116+ type = ACLTypeChoices .TYPE_EXTENDED ,
117+ default_action = self .default_action ,
118+ comments = "EXTENDED_ACL" ,
119+ )
120+ standard_rule = ACLStandardRule (
121+ access_list = extended_acl1 ,
122+ index = 30 ,
123+ action = "remark" ,
124+ remark = "Test remark" ,
125+ source_prefix = None ,
126+ description = "Created rule with remark" ,
127+ )
128+ with self .assertRaises (ValidationError ):
129+ standard_rule .full_clean ()
130+
131+ def test_duplicate_index_per_acl_fail (self ):
132+ """
133+ Test that the rule index must be unique per AccessList.
134+ """
135+ params = {
136+ "access_list" : self .standard_acl1 ,
137+ "index" : 10 ,
138+ "action" : "permit" ,
139+ }
140+ rule_1 = ACLStandardRule (** params )
141+ rule_1 .full_clean ()
142+ rule_1 .save ()
143+ rule_2 = ACLStandardRule (** params )
144+ with self .assertRaises (ValidationError ):
145+ rule_2 .full_clean ()
146+
147+ def test_acl_standard_rule_action_permit_with_remark_fail (self ):
148+ """
149+ Test that ACLStandardRule with action 'permit' and remark fails validation.
150+ """
151+ invalid_rule = ACLStandardRule (
152+ access_list = self .standard_acl1 ,
153+ index = 10 ,
154+ action = "permit" ,
155+ remark = "Remark" ,
156+ source_prefix = None ,
157+ description = "Invalid rule with action 'permit' and remark" ,
158+ )
159+ with self .assertRaises (ValidationError ):
160+ invalid_rule .full_clean ()
161+
162+ def test_acl_standard_rule_action_remark_with_no_remark_fail (self ):
163+ """
164+ Test that ACLStandardRule with action 'remark' and without remark fails validation.
165+ """
166+ invalid_rule = ACLStandardRule (
167+ access_list = self .standard_acl1 ,
168+ index = 10 ,
169+ action = "remark" ,
170+ remark = "" ,
171+ source_prefix = None ,
172+ description = "Invalid rule with action 'remark' and without remark" ,
173+ )
174+ with self .assertRaises (ValidationError ):
175+ invalid_rule .full_clean ()
176+
177+ def test_acl_standard_rule_action_remark_with_source_prefix_fail (self ):
178+ """
179+ Test that ACLStandardRule with action 'remark' and source prefix fails validation.
180+ """
181+ invalid_rule = ACLStandardRule (
182+ access_list = self .standard_acl1 ,
183+ index = 10 ,
184+ action = "remark" ,
185+ remark = "" ,
186+ source_prefix = self .prefix1 ,
187+ description = "Invalid rule with action 'remark' and source prefix" ,
188+ )
189+ with self .assertRaises (ValidationError ):
190+ invalid_rule .full_clean ()
191+
192+ def test_valid_acl_rule_action_choices (self ):
193+ """
194+ Test ACLStandardRule action choices using VALID choices.
195+ """
196+ valid_acl_rule_action_choices = ["deny" , "permit" , "remark" ]
197+
198+ for action_choice in valid_acl_rule_action_choices :
199+ valid_acl_rule_action = ACLStandardRule (
200+ access_list = self .standard_acl1 ,
201+ index = 10 ,
202+ action = action_choice ,
203+ remark = "Remark" if action_choice == "remark" else None ,
204+ description = f"VALID ACL RULE ACTION CHOICES USED: action={ action_choice } " ,
205+ )
206+ valid_acl_rule_action .full_clean ()
207+
208+ def test_invalid_acl_rule_action_choices (self ):
209+ """
210+ Test ACLStandardRule action choices using INVALID choices.
211+ """
212+ invalid_acl_rule_action_choice = "both"
213+
214+ invalid_acl_rule_action = ACLStandardRule (
215+ access_list = self .standard_acl1 ,
216+ index = 10 ,
217+ action = invalid_acl_rule_action_choice ,
218+ description = f"INVALID ACL RULE ACTION CHOICES USED: action={ invalid_acl_rule_action_choice } " ,
219+ )
220+
221+ with self .assertRaises (ValidationError ):
222+ invalid_acl_rule_action .full_clean ()
0 commit comments