diff --git a/endpoints/api/search.py b/endpoints/api/search.py index 143edc52f..b11f1b53d 100644 --- a/endpoints/api/search.py +++ b/endpoints/api/search.py @@ -13,6 +13,7 @@ from operator import itemgetter from stringscore import liquidmetal from util.names import parse_robot_username +import anunidecode # Don't listen to pylint's lies. This import is required. import math @resource('/v1/entities/') @@ -27,6 +28,11 @@ class EntitySearch(ApiResource): @nickname('getMatchingEntities') def get(self, prefix, parsed_args): """ Get a list of entities that match the specified prefix. """ + + # Ensure we don't have any unicode characters in the search, as it breaks the search. Nothing + # being searched can have unicode in it anyway, so this is a safe operation. + prefix = prefix.encode('unidecode', 'ignore').replace(' ', '').lower() + teams = [] org_data = [] diff --git a/test/test_api_usage.py b/test/test_api_usage.py index 1b9cea4d1..d56f1cb34 100644 --- a/test/test_api_usage.py +++ b/test/test_api_usage.py @@ -738,6 +738,14 @@ class TestConductSearch(ApiTestCase): class TestGetMatchingEntities(ApiTestCase): + def test_unicode_search(self): + self.login(ADMIN_ACCESS_USER) + + json = self.getJsonResponse(EntitySearch, + params=dict(prefix='北京市', namespace=ORGANIZATION, + includeTeams='true')) + self.assertEquals(0, len(json['results'])) + def test_notinorg(self): self.login(NO_ACCESS_USER)