3838DeviceGroup = load_model ('config' , 'DeviceGroup' )
3939Template = load_model ('config' , 'Template' )
4040Vpn = load_model ('config' , 'Vpn' )
41+ OrganizationConfigSettings = load_model ('config' , 'OrganizationConfigSettings' )
4142Ca = load_model ('django_x509' , 'Ca' )
4243Cert = load_model ('django_x509' , 'Cert' )
4344User = get_user_model ()
@@ -1428,9 +1429,24 @@ def test_clone_template(self):
14281429 self .assertEqual (LogEntry .objects .all ().count (), 3 )
14291430
14301431 def test_get_default_values (self ):
1431- t1 = self ._create_template (name = 't1' , default_values = {'name1' : 'test1' })
1432+ org = self ._get_org ()
1433+ OrganizationConfigSettings .objects .create (
1434+ organization = org ,
1435+ context = {
1436+ 'name1' : 'organization variable' ,
1437+ 'name3' : 'should not appear' ,
1438+ },
1439+ )
1440+ t1 = self ._create_template (
1441+ name = 't1' , default_values = {'name1' : 'test1' }, organization = org
1442+ )
14321443 group = self ._create_device_group (
1433- context = {'name1' : 'device group' , 'name2' : 'should not appear' }
1444+ organization = org ,
1445+ context = {
1446+ 'name1' : 'device group' ,
1447+ 'name2' : 'should not appear' ,
1448+ 'name4' : 'should not appear' ,
1449+ },
14341450 )
14351451 path = reverse ('admin:get_default_values' )
14361452
@@ -1459,12 +1475,43 @@ def test_get_default_values(self):
14591475 response_data = response .json ()
14601476 self .assertEqual (response_data , expected )
14611477 self .assertNotIn ('name2' , response_data )
1478+ self .assertNotIn ('name4' , response_data )
1479+
1480+ with self .subTest ('get default values conflicting with organization' ):
1481+ with self .assertNumQueries (4 ):
1482+ response = self .client .get (
1483+ path , {'pks' : f'{ t1 .pk } ' , 'organization' : str (org .pk )}
1484+ )
1485+ self .assertEqual (response .status_code , 200 )
1486+ expected = {'default_values' : {'name1' : 'organization variable' }}
1487+ response_data = response .json ()
1488+ self .assertEqual (response_data , expected )
1489+ self .assertNotIn ('name3' , response_data )
1490+
1491+ with self .subTest ('get default values conflicting with organization and group' ):
1492+ with self .assertNumQueries (5 ):
1493+ response = self .client .get (
1494+ path ,
1495+ {
1496+ 'pks' : f'{ t1 .pk } ' ,
1497+ 'group' : str (group .pk ),
1498+ 'organization' : str (org .pk ),
1499+ },
1500+ )
1501+ self .assertEqual (response .status_code , 200 )
1502+ expected = {'default_values' : {'name1' : 'device group' }}
1503+ response_data = response .json ()
1504+ self .assertEqual (response_data , expected )
1505+ self .assertNotIn ('name2' , response_data )
1506+ self .assertNotIn ('name3' , response_data )
1507+ self .assertNotIn ('name4' , response_data )
14621508
14631509 def test_get_default_values_invalid_pks (self ):
14641510 path = reverse ('admin:get_default_values' )
14651511 expected = {
14661512 'template' : {'error' : 'invalid template pks were received' },
1467- 'group' : {'error' : 'invalid group pk were received' },
1513+ 'group' : {'error' : 'invalid group pk was received' },
1514+ 'organization' : {'error' : 'invalid organization pk was received' },
14681515 }
14691516
14701517 with self .subTest ('test with invalid template pk' ):
@@ -1482,6 +1529,11 @@ def test_get_default_values_invalid_pks(self):
14821529 self .assertEqual (r .status_code , 400 )
14831530 self .assertEqual (r .json (), expected ['group' ])
14841531
1532+ with self .subTest ('test with invalid organization pk' ):
1533+ r = self .client .get (path , {'organization' : 'invalid' , 'pks' : str (uuid4 ())})
1534+ self .assertEqual (r .status_code , 400 )
1535+ self .assertEqual (r .json (), expected ['organization' ])
1536+
14851537 def _test_system_context_field_helper (self , path ):
14861538 r = self .client .get (path )
14871539 self .assertEqual (r .status_code , 200 )
@@ -1524,7 +1576,6 @@ def test_no_system_context(self, *args):
15241576 self ._create_template ()
15251577 path = reverse (f'admin:{ self .app_label } _template_add' )
15261578 r = self .client .get (path )
1527- print (app_settings .CONTEXT )
15281579 self .assertContains (
15291580 r , 'There are no system defined variables available right now'
15301581 )
0 commit comments