11from django .urls import reverse
22
3- from users .models import Token
43from utilities .testing import APIViewTestCases , create_test_user
4+ from rest_framework import status
55
66from netbox_custom_objects .models import CustomObjectType
77from .base import CustomObjectsTestCase
8+ from core .models import ObjectType
9+ from dcim .models import Device , DeviceRole , DeviceType , Manufacturer , Rack , Site
10+ from users .models import ObjectPermission , Token
11+ from virtualization .models import Cluster , ClusterType
812
913
1014class CustomObjectTest (CustomObjectsTestCase , APIViewTestCases .APIViewTestCase ):
@@ -32,6 +36,72 @@ def setUp(self):
3236
3337 @classmethod
3438 def setUpTestData (cls ):
39+ # Set up some devices to be used in object/multiobject fields
40+ sites = (
41+ Site (name = 'Site 1' , slug = 'site-1' ),
42+ Site (name = 'Site 2' , slug = 'site-2' ),
43+ )
44+ Site .objects .bulk_create (sites )
45+
46+ racks = (
47+ Rack (name = 'Rack 1' , site = sites [0 ]),
48+ Rack (name = 'Rack 2' , site = sites [1 ]),
49+ )
50+ Rack .objects .bulk_create (racks )
51+
52+ manufacturer = Manufacturer .objects .create (name = 'Manufacturer 1' , slug = 'manufacturer-1' )
53+
54+ device_types = (
55+ DeviceType (manufacturer = manufacturer , model = 'Device Type 1' , slug = 'device-type-1' ),
56+ DeviceType (manufacturer = manufacturer , model = 'Device Type 2' , slug = 'device-type-2' , u_height = 2 ),
57+ )
58+ DeviceType .objects .bulk_create (device_types )
59+
60+ roles = (
61+ DeviceRole (name = 'Device Role 1' , slug = 'device-role-1' , color = 'ff0000' ),
62+ DeviceRole (name = 'Device Role 2' , slug = 'device-role-2' , color = '00ff00' ),
63+ )
64+ for role in roles :
65+ role .save ()
66+
67+ cluster_type = ClusterType .objects .create (name = 'Cluster Type 1' , slug = 'cluster-type-1' )
68+
69+ clusters = (
70+ Cluster (name = 'Cluster 1' , type = cluster_type ),
71+ Cluster (name = 'Cluster 2' , type = cluster_type ),
72+ )
73+ Cluster .objects .bulk_create (clusters )
74+
75+ devices = (
76+ Device (
77+ device_type = device_types [0 ],
78+ role = roles [0 ],
79+ name = 'Device 1' ,
80+ site = sites [0 ],
81+ rack = racks [0 ],
82+ cluster = clusters [0 ],
83+ local_context_data = {'A' : 1 }
84+ ),
85+ Device (
86+ device_type = device_types [0 ],
87+ role = roles [0 ],
88+ name = 'Device 2' ,
89+ site = sites [0 ],
90+ rack = racks [0 ],
91+ cluster = clusters [0 ],
92+ local_context_data = {'B' : 2 }
93+ ),
94+ Device (
95+ device_type = device_types [0 ],
96+ role = roles [0 ],
97+ name = 'Device 3' ,
98+ site = sites [0 ],
99+ rack = racks [0 ],
100+ cluster = clusters [0 ],
101+ local_context_data = {'C' : 3 }
102+ ),
103+ )
104+ Device .objects .bulk_create (devices )
35105
36106 # Create test custom object types
37107 cls .custom_object_type1 = CustomObjectType .objects .create (
@@ -48,6 +118,8 @@ def setUpTestData(cls):
48118 slug = "test-objects-2" ,
49119 )
50120
121+ cls .custom_object_type3 = cls .create_complex_custom_object_type (name = "ComplexObject" )
122+
51123 cls .model = cls .custom_object_type1 .get_model ()
52124 cls .create_custom_object_type_field (cls .custom_object_type1 )
53125
@@ -103,6 +175,47 @@ def test_delete_object(self):
103175 # TODO: ObjectChange causes failure
104176 ...
105177
178+ def test_create_with_nested_serializers (self ):
179+ """
180+ POST a single object with a multiobject field's values specified via a list of PKs.
181+ """
182+ model = self .custom_object_type3 .get_model ()
183+
184+ # Set the model for the test class
185+ self .model = model
186+
187+ # Add object-level permission
188+ obj_perm = ObjectPermission (
189+ name = 'Test permission' ,
190+ actions = ['add' ]
191+ )
192+ obj_perm .save ()
193+ obj_perm .users .add (self .user )
194+ obj_perm .object_types .add (ObjectType .objects .get_for_model (self .model ))
195+
196+ devices = Device .objects .all ()
197+
198+ data = {
199+ 'test_field' : 'Test 004' ,
200+ 'devices' : [devices [0 ].id , devices [1 ].id ],
201+ }
202+
203+ initial_count = self ._get_queryset ().count ()
204+
205+ viewname = 'plugins-api:netbox_custom_objects-api:customobject-list'
206+ list_url = reverse (viewname , kwargs = {'custom_object_type' : self .custom_object_type3 .slug })
207+
208+ response = self .client .post (list_url , data , format = 'json' , ** self .header )
209+ self .assertHttpStatus (response , status .HTTP_201_CREATED )
210+ self .assertEqual (self ._get_queryset ().count (), initial_count + 1 )
211+ instance = self ._get_queryset ().get (pk = response .data ['id' ])
212+ self .assertInstanceEqual (
213+ instance ,
214+ self .create_data [0 ],
215+ exclude = self .validation_excluded_fields ,
216+ api = True
217+ )
218+
106219 # TODO: GraphQL
107220 def test_graphql_list_objects (self ):
108221 ...
0 commit comments