def process_fcm_token_result(self, token_list, importedResultJSON): ret_map = {"ios": [], "android": []} if "results" in importedResultJSON: importedResult = importedResultJSON["results"] for i, result in enumerate(importedResult): if result["status"] == "OK" and "registration_token" in result: ret_map["ios"].append(result["registration_token"]) logging.debug( "Found firebase mapping from %s -> %s at index %d" % (result["apns_token"], result["registration_token"], i)) edb.get_push_token_mapping_db().insert({ "native_token": result["apns_token"], "platform": "ios", "mapped_token": result["registration_token"] }) else: logging.debug( "Must already be android token, leave it unchanged") # TODO: Determine whether to store a mapping here or not depending on what the result # for an android token is ret_map["android"].append(token_list[i]) return ret_map
def testFcmMapping(self): import emission.net.ext_service.push.notify_interface_impl.firebase as pnif import emission.core.get_database as edb self.test_token_list_ios = ["device_token_ios_%s" % i for i in range(10)] self.test_token_list_android = ["device_token_android_%s" % i for i in range(10)] self.test_token_map = {"ios": self.test_token_list_ios, "android": self.test_token_list_android} logging.debug("test token map = %s" % self.test_token_map) try: fcm_instance = pnif.get_interface({"server_auth_token": "firebase_api_key"}) (mapped_token_map, unmapped_token_list) = fcm_instance.map_existing_fcm_tokens(self.test_token_map) # At this point, there is nothing in the database, so no iOS tokens will be mapped self.assertEqual(len(mapped_token_map["ios"]), 0) # android tokens should not be mapped, so they will be returned as-is self.assertEqual(len(mapped_token_map["android"]), 10) # and all iOS tokens will be returned as needing a mapping self.assertEqual(len(unmapped_token_list), 10) # Now, pretend to get back some mappings to_succeed_ios_tokens = self.test_token_list_ios[::2] to_fail_ios_tokens = self.test_token_list_ios[1::2] fake_result = generate_fake_result(to_succeed_ios_tokens, to_fail_ios_tokens) newly_mapped_token_list = fcm_instance.process_fcm_token_result(fake_result) logging.debug("to_succeed_ios_tokens = %s, to_fail_ios_tokens = %s, newly_mapped_token_list = %s" % (to_succeed_ios_tokens, to_fail_ios_tokens, newly_mapped_token_list)) self.assertEqual(len(newly_mapped_token_list), len(to_succeed_ios_tokens)) # Now try to map again - this time, all the successful ios entries should be mapped, and # the android ones not (mapped_token_map, unmapped_list) = fcm_instance.map_existing_fcm_tokens(self.test_token_map) self.assertEqual(len(mapped_token_map["ios"]), len(to_succeed_ios_tokens)) self.assertEqual(len(mapped_token_map["android"]), 10) self.assertEqual(len(unmapped_list), len(to_fail_ios_tokens)) to_succeed_ios_tokens_call_2 = to_fail_ios_tokens[::2] to_fail_ios_tokens_call_2 = to_fail_ios_tokens[1::2] fake_result = generate_fake_result(to_succeed_ios_tokens_call_2, to_fail_ios_tokens_call_2) # Fake the mapped result again newly_mapped_token_list = fcm_instance.process_fcm_token_result(fake_result) logging.debug("to_succeed_ios_tokens_call_2 = %s, to_fail_ios_tokens_call_2 = %s, newly_mapped_token_list = %s" % (to_succeed_ios_tokens_call_2, to_fail_ios_tokens_call_2, newly_mapped_token_list)) self.assertEqual(len(newly_mapped_token_list), len(to_succeed_ios_tokens_call_2)) # Now try to map again - again, all the ios entries that succeeded # in the past two calls should be mapped, and the failed ones not (mapped_token_map, unmapped_list) = fcm_instance.map_existing_fcm_tokens(self.test_token_map) self.assertEqual(len(mapped_token_map["ios"]), len(to_succeed_ios_tokens) + len(to_succeed_ios_tokens_call_2)) self.assertEqual(len(mapped_token_map["android"]), 10) self.assertEqual(len(unmapped_list), len(to_fail_ios_tokens_call_2)) finally: # Delete everything from the database edb.get_push_token_mapping_db().delete_many({})
def testFcmNoMapping(self): import emission.net.ext_service.push.notify_interface_impl.firebase as pnif import emission.core.get_database as edb self.test_token_list_ios = [ "device_token_ios_%s" % i for i in range(10) ] self.test_token_list_android = [ "device_token_android_%s" % i for i in range(10) ] self.test_token_map = { "ios": self.test_token_list_ios, "android": self.test_token_list_android } logging.debug("test token map = %s" % self.test_token_map) fcm_instance = pnif.get_interface({ "server_auth_token": "firebase_api_key", "ios_token_format": "fcm" }) (mapped_token_map, unmapped_token_list) = fcm_instance.map_existing_fcm_tokens( self.test_token_map) # These are assumed to be FCM tokens directly, so no mapping required self.assertEqual(len(mapped_token_map["ios"]), 10) # android tokens should not be mapped, so they will be returned as-is self.assertEqual(len(mapped_token_map["android"]), 10) # no tokens will be returned as needing a mapping self.assertEqual(len(unmapped_token_list), 0) # and there will be no entries in the token mapping database self.assertEqual(edb.get_push_token_mapping_db().count_documents({}), 0)
def process_fcm_token_result(self, importedResultJSON): ret_list = [] if importedResultJSON is not None and "results" in importedResultJSON: importedResult = importedResultJSON["results"] for i, result in enumerate(importedResult): if result["status"] == "OK" and "registration_token" in result: ret_list.append(result["registration_token"]) logging.debug("Found firebase mapping from %s -> %s at index %d"% (result["apns_token"], result["registration_token"], i)); edb.get_push_token_mapping_db().insert({"native_token": result["apns_token"], "platform": "ios", "mapped_token": result["registration_token"]}) else: logging.warning("Got error %s while mapping iOS token at index %d" % (result, i)); return ret_list
def map_existing_fcm_tokens(self, token_map): if self.is_fcm_format: logging.info( "iOS tokens are already in the FCM format, no mapping required" ) return ({ "ios": token_map["ios"], "android": token_map["android"] }, []) # android tokens never need to be mapped, so let's just not even check them mapped_token_map = {"ios": [], "android": token_map["android"]} unmapped_token_list = [] for token in token_map["ios"]: existing_mapping = edb.get_push_token_mapping_db().find_one( {"native_token": token}) if existing_mapping is not None: assert (existing_mapping["native_token"] == token) mapped_token = existing_mapping["mapped_token"] mapped_platform = existing_mapping["platform"] # we are only iterating over ios mappings anyway logging.debug("%s: mapped %s -> %s" % (mapped_platform, token, mapped_token)) assert (mapped_platform == "ios") mapped_token_map[mapped_platform].append(mapped_token) else: logging.debug( "No mapping found for token %s, need to query from database" % token) unmapped_token_list.append(token) return (mapped_token_map, unmapped_token_list)
def process_fcm_token_result(self, importedResultJSON): ret_list = [] if importedResultJSON is not None and "results" in importedResultJSON: importedResult = importedResultJSON["results"] for i, result in enumerate(importedResult): if result["status"] == "OK" and "registration_token" in result: ret_list.append(result["registration_token"]) logging.debug( "Found firebase mapping from %s -> %s at index %d" % (result["apns_token"], result["registration_token"], i)) edb.get_push_token_mapping_db().insert({ "native_token": result["apns_token"], "platform": "ios", "mapped_token": result["registration_token"] }) else: logging.warning( "Got error %s while mapping iOS token at index %d" % (result, i)) return ret_list
def map_existing_fcm_tokens(self, token_list): mapped_token_list = [] unmapped_token_list = [] for token in token_list: existing_mapping = edb.get_push_token_mapping_db().find_one( {"native_token": token}) if existing_mapping is not None: assert (existing_mapping["native_token"] == token) mapped_token = existing_mapping["mapped_token"] logging.debug("mapped %s -> %s" % (token, mapped_token)) mapped_token_list.append(mapped_token) else: logging.debug( "No mapping found for token %s, need to query from database" % token) unmapped_token_list.append(token) return (mapped_token_list, unmapped_token_list)
def map_existing_fcm_tokens(self, token_map): # android tokens never need to be mapped, so let's just not even check them mapped_token_map = {"ios": [], "android": token_map["android"]} unmapped_token_list = [] for token in token_map["ios"]: existing_mapping = edb.get_push_token_mapping_db().find_one({"native_token": token}) if existing_mapping is not None: assert(existing_mapping["native_token"] == token) mapped_token = existing_mapping["mapped_token"] mapped_platform = existing_mapping["platform"] # we are only iterating over ios mappings anyway logging.debug("%s: mapped %s -> %s" % (mapped_platform, token, mapped_token)) assert(mapped_platform == "ios") mapped_token_map[mapped_platform].append(mapped_token) else: logging.debug("No mapping found for token %s, need to query from database" % token) unmapped_token_list.append(token) return (mapped_token_map, unmapped_token_list)
def testFcmMapping(self): import emission.net.ext_service.push.notify_interface_impl.firebase as pnif import emission.core.get_database as edb self.test_token_list = ["device_token_%s" % i for i in range(10)] logging.debug("test token list = %s" % self.test_token_list) try: fcm_instance = pnif.get_interface( {"server_auth_token": "firebase_api_key"}) (mapped_list, unmapped_list) = fcm_instance.map_existing_fcm_tokens( self.test_token_list) # At this point, there is nothing in the database, so nothing will be mapped self.assertEqual(len(mapped_list), 0) self.assertEqual(len(unmapped_list), 10) # Now, pretend to get back some mappings pretend_ios_tokens = self.test_token_list[:5:2] pretend_android_tokens = self.test_token_list[ 6::2] + self.test_token_list[1::2] fake_result = generate_fake_result(pretend_ios_tokens, pretend_android_tokens) newly_mapped_token_list = fcm_instance.process_fcm_token_result( self.test_token_list, fake_result) logging.debug( "pretend_ios_tokens = %s, pretend_android_tokens = %s, newly_mapped_token_list = %s" % (pretend_ios_tokens, pretend_android_tokens, newly_mapped_token_list)) self.assertEqual(len(newly_mapped_token_list), len(self.test_token_list)) # Now try to map again - this time, all the ios entries should be mapped, and # the android ones not (mapped_list, unmapped_list) = fcm_instance.map_existing_fcm_tokens( self.test_token_list) self.assertEqual(len(mapped_list), len(pretend_ios_tokens)) self.assertEqual(len(unmapped_list), len(pretend_android_tokens)) pretend_ios_tokens = self.test_token_list[::2] pretend_android_tokens = self.test_token_list[1::2] fake_result = generate_fake_result(pretend_ios_tokens, pretend_android_tokens) # Fake the mapped result again newly_mapped_token_list = fcm_instance.process_fcm_token_result( self.test_token_list, fake_result) logging.debug( "pretend_ios_tokens = %s, pretend_android_tokens = %s, newly_mapped_token_list = %s" % (pretend_ios_tokens, pretend_android_tokens, newly_mapped_token_list)) self.assertEqual(len(newly_mapped_token_list), len(self.test_token_list)) # Now try to map again - again, all the ios entries should be mapped, and # the android ones not (mapped_list, unmapped_list) = fcm_instance.map_existing_fcm_tokens( self.test_token_list) finally: # Delete everything from the database edb.get_push_token_mapping_db().remove()