API - searching user is case insensitive

This commit is contained in:
Sam 2022-03-13 10:03:24 +01:00
parent 0b1d99aa7a
commit 08d412bddf
2 changed files with 37 additions and 1 deletions

View File

@ -780,6 +780,42 @@ class TestGetUsers(ApiTestCaseMixin):
'total': 1, 'total': 1,
} }
def test_it_returns_username_matching_query(
self, app: Flask, user_1_admin: User, user_2: User, user_3: User
) -> None:
client, auth_token = self.get_test_client_and_auth_token(
app, user_1_admin.email
)
response = client.get(
'/api/users?q=oto',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['users']) == 1
assert 'toto' in data['data']['users'][0]['username']
def test_it_filtering_on_username_is_case_insensitive(
self, app: Flask, user_1_admin: User, user_2: User, user_3: User
) -> None:
client, auth_token = self.get_test_client_and_auth_token(
app, user_1_admin.email
)
response = client.get(
'/api/users?q=TOTO',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['users']) == 1
assert 'toto' in data['data']['users'][0]['username']
def test_it_returns_empty_users_list_filtering_on_username( def test_it_returns_empty_users_list_filtering_on_username(
self, app: Flask, user_1_admin: User, user_2: User, user_3: User self, app: Flask, user_1_admin: User, user_2: User, user_3: User
) -> None: ) -> None:

View File

@ -194,7 +194,7 @@ def get_users(auth_user: User) -> Dict:
query = params.get('q') query = params.get('q')
users_pagination = ( users_pagination = (
User.query.filter( User.query.filter(
User.username.like('%' + query + '%') if query else True, User.username.ilike('%' + query + '%') if query else True,
) )
.order_by( .order_by(
User.workouts_count.asc() # type: ignore User.workouts_count.asc() # type: ignore