14
14
from netbox .models .features import ChangeLoggingMixin , CustomFieldsMixin
15
15
from users .models import ObjectPermission
16
16
from .base import ModelTestCase
17
- from .utils import add_custom_field_data , disable_warnings , post_data
17
+ from .utils import add_custom_field_data , disable_warnings , get_random_string , post_data
18
18
19
19
__all__ = (
20
20
'ModelViewTestCase' ,
@@ -169,6 +169,11 @@ def test_create_object_with_permission(self):
169
169
if issubclass (self .model , CustomFieldsMixin ):
170
170
add_custom_field_data (self .form_data , self .model )
171
171
172
+ # If supported, add a changelog message
173
+ if issubclass (self .model , ChangeLoggingMixin ):
174
+ if 'changelog_message' not in self .form_data :
175
+ self .form_data ['changelog_message' ] = get_random_string (10 )
176
+
172
177
# Try POST with model-level permission
173
178
initial_count = self ._get_queryset ().count ()
174
179
request = {
@@ -181,13 +186,14 @@ def test_create_object_with_permission(self):
181
186
self .assertInstanceEqual (instance , self .form_data , exclude = self .validation_excluded_fields )
182
187
183
188
# Verify ObjectChange creation
184
- if issubclass (instance . __class__ , ChangeLoggingMixin ):
189
+ if issubclass (self . model , ChangeLoggingMixin ):
185
190
objectchanges = ObjectChange .objects .filter (
186
191
changed_object_type = ContentType .objects .get_for_model (instance ),
187
192
changed_object_id = instance .pk
188
193
)
189
194
self .assertEqual (len (objectchanges ), 1 )
190
195
self .assertEqual (objectchanges [0 ].action , ObjectChangeActionChoices .ACTION_CREATE )
196
+ self .assertEqual (objectchanges [0 ].message , self .form_data ['changelog_message' ])
191
197
192
198
@override_settings (EXEMPT_VIEW_PERMISSIONS = ['*' ], EXEMPT_EXCLUDE_MODELS = [])
193
199
def test_create_object_with_constrained_permission (self ):
@@ -272,6 +278,11 @@ def test_edit_object_with_permission(self):
272
278
if issubclass (self .model , CustomFieldsMixin ):
273
279
add_custom_field_data (self .form_data , self .model )
274
280
281
+ # If supported, add a changelog message
282
+ if issubclass (self .model , ChangeLoggingMixin ):
283
+ if 'changelog_message' not in self .form_data :
284
+ self .form_data ['changelog_message' ] = get_random_string (10 )
285
+
275
286
# Try POST with model-level permission
276
287
request = {
277
288
'path' : self ._get_url ('edit' , instance ),
@@ -282,13 +293,14 @@ def test_edit_object_with_permission(self):
282
293
self .assertInstanceEqual (instance , self .form_data , exclude = self .validation_excluded_fields )
283
294
284
295
# Verify ObjectChange creation
285
- if issubclass (instance . __class__ , ChangeLoggingMixin ):
296
+ if issubclass (self . model , ChangeLoggingMixin ):
286
297
objectchanges = ObjectChange .objects .filter (
287
298
changed_object_type = ContentType .objects .get_for_model (instance ),
288
299
changed_object_id = instance .pk
289
300
)
290
301
self .assertEqual (len (objectchanges ), 1 )
291
302
self .assertEqual (objectchanges [0 ].action , ObjectChangeActionChoices .ACTION_UPDATE )
303
+ self .assertEqual (objectchanges [0 ].message , self .form_data ['changelog_message' ])
292
304
293
305
@override_settings (EXEMPT_VIEW_PERMISSIONS = ['*' ], EXEMPT_EXCLUDE_MODELS = [])
294
306
def test_edit_object_with_constrained_permission (self ):
@@ -348,6 +360,7 @@ def test_delete_object_without_permission(self):
348
360
@override_settings (EXEMPT_VIEW_PERMISSIONS = ['*' ])
349
361
def test_delete_object_with_permission (self ):
350
362
instance = self ._get_queryset ().first ()
363
+ form_data = {'confirm' : True }
351
364
352
365
# Assign model-level permission
353
366
obj_perm = ObjectPermission (
@@ -361,23 +374,28 @@ def test_delete_object_with_permission(self):
361
374
# Try GET with model-level permission
362
375
self .assertHttpStatus (self .client .get (self ._get_url ('delete' , instance )), 200 )
363
376
377
+ # If supported, add a changelog message
378
+ if issubclass (self .model , ChangeLoggingMixin ):
379
+ form_data ['changelog_message' ] = get_random_string (10 )
380
+
364
381
# Try POST with model-level permission
365
382
request = {
366
383
'path' : self ._get_url ('delete' , instance ),
367
- 'data' : post_data ({ 'confirm' : True } ),
384
+ 'data' : post_data (form_data ),
368
385
}
369
386
self .assertHttpStatus (self .client .post (** request ), 302 )
370
387
with self .assertRaises (ObjectDoesNotExist ):
371
388
self ._get_queryset ().get (pk = instance .pk )
372
389
373
390
# Verify ObjectChange creation
374
- if issubclass (instance . __class__ , ChangeLoggingMixin ):
391
+ if issubclass (self . model , ChangeLoggingMixin ):
375
392
objectchanges = ObjectChange .objects .filter (
376
393
changed_object_type = ContentType .objects .get_for_model (instance ),
377
394
changed_object_id = instance .pk
378
395
)
379
396
self .assertEqual (len (objectchanges ), 1 )
380
397
self .assertEqual (objectchanges [0 ].action , ObjectChangeActionChoices .ACTION_DELETE )
398
+ self .assertEqual (objectchanges [0 ].message , form_data ['changelog_message' ])
381
399
382
400
@override_settings (EXEMPT_VIEW_PERMISSIONS = ['*' ])
383
401
def test_delete_object_with_constrained_permission (self ):
0 commit comments