def test_unknown_user_pillow(self): FormProcessorTestUtils.delete_all_xforms() user_id = 'test-unknown-user' metadata = TestFormMetadata(domain=TEST_DOMAIN, user_id='test-unknown-user') form = get_form_ready_to_save(metadata) FormProcessorInterface(domain=TEST_DOMAIN).save_processed_models([form]) # send to kafka topic = topics.FORM_SQL if settings.TESTS_SHOULD_USE_SQL_BACKEND else topics.FORM since = self._get_kafka_seq() producer.send_change(topic, _form_to_change_meta(form)) # send to elasticsearch pillow = get_unknown_users_pillow() pillow.process_changes(since=since, forever=False) self.elasticsearch.indices.refresh(self.index_info.index) # the default query doesn't include unknown users so should have no results self.assertEqual(0, UserES().run().total) # clear the default filters which hide unknown users user_es = UserES().remove_default_filters() results = user_es.run() self.assertEqual(1, results.total) user_doc = results.hits[0] self.assertEqual(TEST_DOMAIN, user_doc['domain']) self.assertEqual(user_id, user_doc['_id']) self.assertEqual('UnknownUser', user_doc['doc_type'])
def test_unknown_user_pillow(self): FormProcessorTestUtils.delete_all_xforms() user_id = 'test-unknown-user' metadata = TestFormMetadata(domain=TEST_DOMAIN, user_id='test-unknown-user') form = get_form_ready_to_save(metadata) FormProcessorInterface(domain=TEST_DOMAIN).save_processed_models( [form]) # send to kafka topic = topics.FORM_SQL if settings.TESTS_SHOULD_USE_SQL_BACKEND else topics.FORM since = self._get_kafka_seq() producer.send_change(topic, _form_to_change_meta(form)) # send to elasticsearch pillow = get_xform_pillow() pillow.process_changes(since=since, forever=False) self.elasticsearch.indices.refresh(self.index_info.index) # the default query doesn't include unknown users so should have no results self.assertEqual(0, UserES().run().total) # clear the default filters which hide unknown users user_es = UserES().remove_default_filters() results = user_es.run() self.assertEqual(1, results.total) user_doc = results.hits[0] self.assertEqual(TEST_DOMAIN, user_doc['domain']) self.assertEqual(user_id, user_doc['_id']) self.assertEqual('UnknownUser', user_doc['doc_type'])
def get_users_by_location(self, user_supported_locations): if not self.national_user: user_query = UserES().mobile_users().domain( self.domain).location(user_supported_locations).fields( ['username', 'assigned_location_ids', 'last_login']) else: user_query = UserES().mobile_users().domain(self.domain).fields( ['username', 'assigned_location_ids', 'last_login']) return [u for u in user_query.run().hits]
def get_users_at_location(self): user_query = UserES().domain( self.domain_object.name ).mobile_users().location( self.location.location_id ).fields(['_id', 'username', 'first_name', 'last_name']) return [ dict(id=u['_id'], text=user_display_string( u['username'], u.get('first_name', ''), u.get('last_name', '') )) for u in user_query.run().hits]
def supply_points_users(self, supply_points): query = UserES().mobile_users().domain(self.config['domain']).term("location_id", [sp for sp in supply_points]) with_reporters = set() with_in_charge = set() for hit in query.run().hits: with_reporters.add(hit['location_id']) if hit['user_data'].get('role') == 'In Charge': with_in_charge.add(hit['location_id']) return with_reporters, with_in_charge
def supply_points_users(self): query = UserES().mobile_users().domain(self.config['domain']).term( "location_id", list(self.config['reporting_supply_points'])) with_reporters = set() with_in_charge = set() for hit in query.run().hits: with_reporters.add(hit['location_id']) if 'In Charge' in hit['user_data'].get('role', []): with_in_charge.add(hit['location_id']) return with_reporters, with_in_charge
def supply_points_users(self): query = UserES().mobile_users().domain(self.config['domain']).term( "location_id", list(self.config['reporting_supply_points']) ) with_reporters = set() with_in_charge = set() for hit in query.run().hits: with_reporters.add(hit['location_id']) if 'In Charge' in hit['user_data'].get('role', []): with_in_charge.add(hit['location_id']) return with_reporters, with_in_charge
def supply_points_users(self): query = UserES().mobile_users().domain(self.config['domain']).term( "location_id", self.supply_points_locations_ids) with_reporters = set() for hit in query.run().hits: with_reporters.add(hit['location_id']) with_in_charge = set( FacilityInCharge.objects.filter( location__location_id__in=self.supply_points_locations_ids). values_list('location__location_id', flat=True).distinct()) return with_reporters, with_in_charge
def supply_points_users(self): query = UserES().mobile_users().domain(self.config['domain']).term( "location_id", self.supply_points_locations_ids ) with_reporters = set() for hit in query.run().hits: with_reporters.add(hit['location_id']) with_in_charge = set(FacilityInCharge.objects.filter( location__location_id__in=self.supply_points_locations_ids ).values_list('location__location_id', flat=True).distinct()) return with_reporters, with_in_charge
def test_unknown_user_reindexer(self): FormProcessorTestUtils.delete_all_xforms() user_id = 'test-unknown-user' metadata = TestFormMetadata(domain=self.domain, user_id='test-unknown-user') form = get_form_ready_to_save(metadata) FormProcessorInterface(domain=self.domain).save_processed_models([form]) ensure_index_deleted(USER_INDEX) call_command('ptop_fast_reindex_unknownusers', noinput=True, bulk=True) # the default query doesn't include unknown users so should have no results self.assertEqual(0, UserES().run().total) user_es = UserES() # hack: clear the default filters which hide unknown users # todo: find a better way to do this. user_es._default_filters = ESQuery.default_filters results = user_es.run() self.assertEqual(1, results.total) user_doc = results.hits[0] self.assertEqual(self.domain, user_doc['domain']) self.assertEqual(user_id, user_doc['_id']) self.assertEqual('UnknownUser', user_doc['doc_type']) form.delete() delete_es_index(USER_INDEX)