@@ -68,14 +68,12 @@ def _create_org_traffic_metric(self, org, interface_name):
68
68
69
69
def _create_test_users (self , org ):
70
70
admin = self ._create_admin ()
71
- org2_administrator = self ._create_user ()
72
- OrganizationUser .objects .create (
73
- user = org2_administrator , organization = org , is_admin = True
74
- )
71
+ org_admin = self ._create_user ()
72
+ OrganizationUser .objects .create (user = org_admin , organization = org , is_admin = True )
75
73
groups = Group .objects .filter (name = 'Administrator' )
76
- org2_administrator .groups .set (groups )
74
+ org_admin .groups .set (groups )
77
75
operator = self ._create_operator ()
78
- return admin , org2_administrator , operator
76
+ return admin , org_admin , operator
79
77
80
78
def test_wifi_client_chart (self ):
81
79
def _test_chart_properties (chart ):
@@ -242,7 +240,7 @@ def _test_chart_properties(chart):
242
240
243
241
self .client .force_login (admin )
244
242
with self .subTest ('Test superuser retrieves metric for all organizations' ):
245
- with self .assertNumQueries (2 ):
243
+ with self .assertNumQueries (3 ):
246
244
response = self .client .get (path )
247
245
self .assertEqual (response .status_code , 200 )
248
246
self ._test_response_data (response )
@@ -675,3 +673,49 @@ def test_group_by_time(self):
675
673
with self .subTest ('Test with invalid group time' ):
676
674
response = self .client .get (path , {'time' : '3w' })
677
675
self .assertEqual (response .status_code , 400 )
676
+
677
+ def test_organizations_list (self ):
678
+ path = reverse ('monitoring_general:api_dashboard_timeseries' )
679
+ Organization .objects .all ().delete ()
680
+ org1 = self ._create_org (name = 'org1' , slug = 'org1' )
681
+ admin , org_admin , _ = self ._create_test_users (org1 )
682
+
683
+ self .client .force_login (admin )
684
+ with self .subTest ('Superuser: Only one organization is present' ):
685
+ response = self .client .get (path )
686
+ self .assertEqual (response .status_code , 200 )
687
+ self .assertNotIn ('organizations' , response .data )
688
+
689
+ org2 = self ._create_org (name = 'org2' , slug = 'org2' , id = self .org2_id )
690
+
691
+ with self .subTest ('Superuser: Multiple organizations are present' ):
692
+ response = self .client .get (path )
693
+ self .assertEqual (response .status_code , 200 )
694
+ self .assertIn ('organizations' , response .data )
695
+ self .assertEqual (len (response .data ['organizations' ]), 2 )
696
+ self .assertEqual (
697
+ response .data ['organizations' ],
698
+ [{'id' : org .slug , 'text' : org .name } for org in [org1 , org2 ]],
699
+ )
700
+
701
+ self .client .logout ()
702
+ self .client .force_login (org_admin )
703
+
704
+ with self .subTest ('Non-superuser: Administrator of one organization' ):
705
+ response = self .client .get (path )
706
+ self .assertEqual (response .status_code , 200 )
707
+ self .assertNotIn ('organizations' , response .data )
708
+
709
+ OrganizationUser .objects .create (
710
+ user = org_admin , organization = org2 , is_admin = True
711
+ )
712
+
713
+ with self .subTest ('Non-superuser: Administrator of multiple organizations' ):
714
+ response = self .client .get (path )
715
+ self .assertEqual (response .status_code , 200 )
716
+ self .assertIn ('organizations' , response .data )
717
+ self .assertEqual (len (response .data ['organizations' ]), 2 )
718
+ self .assertEqual (
719
+ response .data ['organizations' ],
720
+ [{'id' : org .slug , 'text' : org .name } for org in [org1 , org2 ]],
721
+ )
0 commit comments