@@ -1727,40 +1727,83 @@ def test_group_templates_apply(self):
1727
1727
1728
1728
def test_change_device_group_action_changes_templates (self ):
1729
1729
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' )
1731
1732
t1 = self ._create_template (name = 't1' )
1732
1733
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 )
1734
1735
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 )
1736
1737
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 ()
1739
1743
self .assertNotIn (t2 , templates )
1740
1744
self .assertIn (t1 , templates )
1741
1745
post_data = {
1742
- '_selected_action' : [device .pk ],
1746
+ '_selected_action' : [device1 .pk ],
1743
1747
'action' : 'change_group' ,
1744
1748
'csrfmiddlewaretoken' : 'test' ,
1749
+ 'apply' : True ,
1745
1750
}
1751
+
1746
1752
with self .subTest ('change group' ):
1747
1753
post_data ['device_group' ] = str (dg2 .pk )
1748
- post_data ['apply' ] = True
1749
1754
response = self .client .post (path , post_data , follow = True )
1750
1755
self .assertEqual (response .status_code , 200 )
1751
1756
self .assertContains (
1752
1757
response , 'Successfully changed group of selected devices.'
1753
1758
)
1754
- templates = device .config .templates .all ()
1759
+ templates = device1 .config .templates .all ()
1755
1760
self .assertIn (t2 , templates )
1756
1761
self .assertNotIn (t1 , templates )
1762
+
1757
1763
with self .subTest ('unassign group' ):
1758
1764
post_data ['device_group' ] = ''
1759
1765
response = self .client .post (path , post_data , follow = True )
1760
1766
self .assertEqual (response .status_code , 200 )
1761
- templates = list (device .config .templates .all ())
1767
+ templates = list (device1 .config .templates .all ())
1762
1768
self .assertEqual (templates , [])
1763
1769
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
+
1764
1807
def test_change_device_group_changes_templates (self ):
1765
1808
org = self ._get_org (org_name = 'default' )
1766
1809
t1 = self ._create_template (name = 't1' )
0 commit comments