@@ -67,7 +67,7 @@ def test_export_users(self):
6767 "location" ,
6868 "notes" ,
6969 "language" ,
70- "organizations" ,
70+ "organizations (organization_id, is_admin) " ,
7171 ]
7272 self .assertEqual (csv_data [0 ], expected_headers )
7373 # Ensuring ordering
@@ -265,6 +265,16 @@ def test_subfields_dict_field(self):
265265 csv_data = list (csv_reader )
266266 # 2 users + 1 header
267267 self .assertEqual (len (csv_data ), 3 )
268+ expected_headers = [
269+ "id" ,
270+ "auth_token (key)" ,
271+ "auth_token (key, created)" ,
272+ "birth_date (year)" ,
273+ "openwisp_users_organizationuser (organization_id)" ,
274+ "openwisp_users_organizationuser (organization_id, is_admin)" ,
275+ "openwisp_users_organizationuser.organization_id" ,
276+ ]
277+ self .assertEqual (csv_data [0 ], expected_headers )
268278 # user1: token present, birth_date None, one org membership
269279 self .assertEqual (csv_data [1 ][0 ], str (user1 .id ))
270280 # subfields, single obj
@@ -318,3 +328,37 @@ def test_plain_relation_field_returns_empty_string(self):
318328 self ._create_org_user (organization = org , user = user , is_admin = True )
319329 result = Command ()._get_field_value (user , "openwisp_users_organizationuser" )
320330 self .assertEqual (result , "" )
331+
332+ @capture_stdout ()
333+ def test_custom_header (self ):
334+ def _get_is_active (user ):
335+ return "yes" if user .is_active else "no"
336+
337+ config = {
338+ "fields" : [
339+ "id" ,
340+ {
341+ "name" : "active_status" ,
342+ "header" : "Active?" ,
343+ "callable" : _get_is_active ,
344+ },
345+ {
346+ "name" : "custom_orgs" ,
347+ "header_fields" : ["org_id" , "is_admin" ],
348+ "callable" : _get_is_active ,
349+ },
350+ ],
351+ "select_related" : [],
352+ "prefetch_related" : [],
353+ }
354+ self ._create_user (is_active = True )
355+ with patch .object (app_settings , "EXPORT_USERS_COMMAND_CONFIG" , config ):
356+ call_command ("export_users" , filename = self .temp_file .name )
357+ with open (self .temp_file .name , "r" , encoding = "utf-8" ) as f :
358+ csv_reader = csv .reader (f )
359+ csv_data = list (csv_reader )
360+ self .assertEqual (
361+ csv_data [0 ], ["id" , "Active?" , "custom_orgs (org_id, is_admin)" ]
362+ )
363+ self .assertEqual (csv_data [1 ][1 ], "yes" )
364+ self .assertEqual (csv_data [1 ][2 ], "yes" )
0 commit comments