def test_location_restricted_cases(self): domain_obj = bootstrap_domain(self.domain) self.addCleanup(domain_obj.delete) location_type_names = ['state', 'county', 'city'] location_structure = [('Massachusetts', [('Middlesex', [ ('Cambridge', []), ('Somerville', []), ]), ('Suffolk', [ ('Boston', []), ])])] locations = setup_locations_and_types(self.domain, location_type_names, [], location_structure)[1] middlesex_user = CommCareUser.create(self.domain, 'guy-from-middlesex', '***', None, None) middlesex_user.add_to_assigned_locations(locations['Middlesex']) restrict_user_by_location(self.domain, middlesex_user) fake_request = MagicMock() fake_request.domain = self.domain fake_request.couch_user = middlesex_user self._send_case_to_es(owner_id=locations['Boston'].get_id) middlesex_case = self._send_case_to_es( owner_id=locations['Middlesex'].get_id) cambridge_case = self._send_case_to_es( owner_id=locations['Cambridge'].get_id) returned_case_ids = query_location_restricted_cases( CaseES().domain(self.domain), fake_request).get_ids() self.assertItemsEqual(returned_case_ids, [middlesex_case.case_id, cambridge_case.case_id])
def setUp(self): delete_all_users() delete_all_locations() delete_all_repeaters() self.old_domain_obj = create_domain(self.old_domain) self.mobile_worker = CommCareUser.create( self.old_domain, '*****@*****.**', '123') self.web_user = WebUser.create(self.old_domain, '*****@*****.**', '***', is_active=True) self.location_types, self.locations = setup_locations_and_types( self.old_domain, location_types=["state", "county", "city"], stock_tracking_types=[], locations=[ ('Massachusetts', [('Middlesex', [ ('Cambridge', []), ('Somerville', []), ]), ('Suffolk', [ ('Boston', []), ])]), ('California', [ ('Los Angeles', []), ]), ])
def setUp(self): super(BaseTest, self).setUp() self.location_types, self.locations = setup_locations_and_types( self.domain, self.location_type_names, [], self.location_structure, )
def setUpClass(cls): super(LocationAggregationTest, cls).setUpClass() # save locations that are setup in module so we can add them back later cls.all_locations = list( SQLLocation.objects.filter(domain=cls.domain_name).all()) cls.all_location_types = list( LocationType.objects.filter(domain=cls.domain_name).all()) SQLLocation.objects.filter(domain=cls.domain_name).delete() LocationType.objects.filter(domain=cls.domain_name).delete() setup_locations_and_types( cls.domain_name, ['state', 'district', 'block', 'supervisor', 'awc'], [], [('State1', [ ('District1', [ ('Block1', [ ('Supervisor1', [ ('Awc1', []), ('Awc2', []), ]), ('Supervisor2', [ ('Awc3', []), ]), ]), ('Block2', []), ]), ])]) block1 = SQLLocation.objects.filter(domain=cls.domain_name, name='Block1').first() block1.metadata = {"map_location_name": "Not Block1"} block1.save() sup2 = SQLLocation.objects.filter(domain=cls.domain_name, name='Supervisor2').first() sup2.metadata = {"is_test_location": "test"} sup2.save() awc1 = SQLLocation.objects.filter(domain=cls.domain_name, name='Awc1').first() awc1.location_id = 'a1' awc1.save() cls.helper = LocationAggregationDistributedHelper()
def test_location(self): from corehq.apps.locations.models import LocationType, SQLLocation from corehq.apps.locations.tests.util import setup_locations_and_types expected_object_counts = Counter({LocationType: 3, SQLLocation: 11}) location_type_names = ['province', 'district', 'city'] location_structure = [ ('Western Cape', [ ('Cape Winelands', [ ('Stellenbosch', []), ('Paarl', []), ]), ('Cape Town', [ ('Cape Town City', []), ]) ]), ('Gauteng', [ ('Ekurhuleni ', [ ('Alberton', []), ('Benoni', []), ('Springs', []), ]), ]), ] location_types, locations = setup_locations_and_types( self.domain_name, location_type_names, [], location_structure, ) self._dump_and_load(expected_object_counts) names = ['Cape Winelands', 'Paarl', 'Cape Town'] location_ids = [locations[name].location_id for name in names] result = SQLLocation.objects.get_locations_and_children(location_ids) self.assertItemsEqual( [loc.name for loc in result], ['Cape Winelands', 'Stellenbosch', 'Paarl', 'Cape Town', 'Cape Town City'] ) result = SQLLocation.objects.get_locations_and_children([locations['Gauteng'].location_id]) self.assertItemsEqual( [loc.name for loc in result], ['Gauteng', 'Ekurhuleni ', 'Alberton', 'Benoni', 'Springs'] )
def test_owner_filter(self): setup_locations_and_types( self.domain, ['state', 'health-department', 'team', 'sub-team'], [], [('State1', [ ('HealthDepartment1', [ ('Team1', [ ('SubTeam1', []), ('SubTeam2', []), ]), ('Team2', []), ]), ])]) team1 = SQLLocation.objects.filter(domain=self.domain, name='Team1').first() health_department = SQLLocation.objects.filter( domain=self.domain, name='HealthDepartment1').first() self.addCleanup(delete_all_locations) user = CommCareUser.create(self.domain, 'm2', 'abc', None, None, location=team1) send_to_elasticsearch('users', user.to_json()) self.es.indices.refresh(USER_INDEX_INFO.index) self.addCleanup(delete_all_users) cases = [ new_case( domain=self.domain, foo="peach", bar="plumb", server_modified_on=datetime.utcnow() + timedelta(hours=-1), owner_id='123', ), new_case( domain=self.domain, foo="orange", bar="melon", server_modified_on=datetime.utcnow(), owner_id=user. user_id, # this user is part of the team1 location. ), new_case( domain=self.domain, foo="grape", bar="pineapple", server_modified_on=datetime.utcnow(), ), ] for case in cases: send_to_elasticsearch("cases", case.to_json()) self.addCleanup(self._cleanup_case(case.case_id)) self.es.indices.refresh(CASE_INDEX_INFO.index) self.export_instance.filters.show_project_data = False self.export_instance.filters.locations = [ health_department.location_id ] self.export_instance.filters.users = ['123'] self.export_instance.save() checkpoint = _generate_incremental_export(self.incremental_export) data = checkpoint.get_blob().read().decode("utf-8-sig") expected = "Foo column,Bar column\r\npeach,plumb\r\norange,melon\r\n" self.assertEqual(data, expected)