@@ -1727,40 +1727,83 @@ def test_group_templates_apply(self):
17271727
17281728 def test_change_device_group_action_changes_templates (self ):
17291729 path = reverse (f'admin:{ self .app_label } _device_changelist' )
1730- org = self ._get_org (org_name = 'default' )
1730+ org1 = self ._create_org (name = 'org1' , slug = 'org1' )
1731+ org2 = self ._create_org (name = 'org2' , slug = 'org2' )
17311732 t1 = self ._create_template (name = 't1' )
17321733 t2 = self ._create_template (name = 't2' )
1733- dg1 = self ._create_device_group (name = 'test-group-1' , organization = org )
1734+ dg1 = self ._create_device_group (name = 'test-group-1' , organization = org1 )
17341735 dg1 .templates .add (t1 )
1735- dg2 = self ._create_device_group (name = 'test-group-2' , organization = org )
1736+ dg2 = self ._create_device_group (name = 'test-group-2' , organization = org1 )
17361737 dg2 .templates .add (t2 )
1737- device = self ._create_device (organization = org , group = dg1 )
1738- templates = device .config .templates .all ()
1738+ device1 = self ._create_device (organization = org1 , group = dg1 )
1739+ device2 = self ._create_device_config (
1740+ device_opts = {'organization' : org1 , 'mac_address' : '11:22:33:44:55:66' }
1741+ )
1742+ templates = device1 .config .templates .all ()
17391743 self .assertNotIn (t2 , templates )
17401744 self .assertIn (t1 , templates )
17411745 post_data = {
1742- '_selected_action' : [device .pk ],
1746+ '_selected_action' : [device1 .pk ],
17431747 'action' : 'change_group' ,
17441748 'csrfmiddlewaretoken' : 'test' ,
1749+ 'apply' : True ,
17451750 }
1751+
17461752 with self .subTest ('change group' ):
17471753 post_data ['device_group' ] = str (dg2 .pk )
1748- post_data ['apply' ] = True
17491754 response = self .client .post (path , post_data , follow = True )
17501755 self .assertEqual (response .status_code , 200 )
17511756 self .assertContains (
17521757 response , 'Successfully changed group of selected devices.'
17531758 )
1754- templates = device .config .templates .all ()
1759+ templates = device1 .config .templates .all ()
17551760 self .assertIn (t2 , templates )
17561761 self .assertNotIn (t1 , templates )
1762+
17571763 with self .subTest ('unassign group' ):
17581764 post_data ['device_group' ] = ''
17591765 response = self .client .post (path , post_data , follow = True )
17601766 self .assertEqual (response .status_code , 200 )
1761- templates = list (device .config .templates .all ())
1767+ templates = list (device1 .config .templates .all ())
17621768 self .assertEqual (templates , [])
17631769
1770+ with self .subTest ('Change group for multiple devices' ):
1771+ data = post_data .copy ()
1772+ data ['_selected_action' ] = [device1 .pk , device2 .pk ]
1773+ data ['device_group' ] = str (dg2 .pk )
1774+ with patch .object (Device , '_send_device_group_changed_signal' ) as mocked :
1775+ response = self .client .post (path , data , follow = True )
1776+ self .assertEqual (response .status_code , 200 )
1777+ self .assertEqual (len (mocked .call_args_list ), 2 )
1778+
1779+ device2 .organization = org2
1780+ device2 .save ()
1781+
1782+ with self .subTest ('Select devices from different organization' ):
1783+ data = post_data .copy ()
1784+ data ['_selected_action' ] = [device1 .pk , device2 .pk ]
1785+ response = self .client .post (path , data , follow = True )
1786+ self .assertContains (response , 'Select devices from one organization' )
1787+
1788+ data .pop ('apply' )
1789+ data .pop ('device_group' )
1790+ response = self .client .post (path , data , follow = True )
1791+ self .assertContains (response , 'Select devices from one organization' )
1792+
1793+ org_user = self ._create_administrator (organizations = [org1 ])
1794+ self .client .force_login (org_user )
1795+
1796+ with self .subTest ('Select devices from org not managed by user' ):
1797+ data = post_data .copy ()
1798+ data ['_selected_action' ] = [device2 .pk ]
1799+ response = self .client .post (path , data , follow = True )
1800+ self .assertEqual (response .status_code , 403 )
1801+
1802+ data .pop ('apply' )
1803+ data .pop ('device_group' )
1804+ response = self .client .post (path , data , follow = True )
1805+ self .assertEqual (response .status_code , 403 )
1806+
17641807 def test_change_device_group_changes_templates (self ):
17651808 org = self ._get_org (org_name = 'default' )
17661809 t1 = self ._create_template (name = 't1' )
0 commit comments