diff --git a/api/apps/user_app.py b/api/apps/user_app.py index e08e434d49..702e1bd855 100644 --- a/api/apps/user_app.py +++ b/api/apps/user_app.py @@ -222,7 +222,7 @@ async def oauth_callback(channel): if not users: try: try: - avatar = download_img(user_info.avatar_url) + avatar = await download_img(user_info.avatar_url) except Exception as e: logging.exception(e) avatar = "" @@ -317,7 +317,7 @@ async def github_callback(): # User isn't try to register try: try: - avatar = download_img(user_info["avatar_url"]) + avatar = await download_img(user_info["avatar_url"]) except Exception as e: logging.exception(e) avatar = "" @@ -421,7 +421,7 @@ async def feishu_callback(): # User isn't try to register try: try: - avatar = download_img(user_info["avatar_url"]) + avatar = await download_img(user_info["avatar_url"]) except Exception as e: logging.exception(e) avatar = "" diff --git a/common/misc_utils.py b/common/misc_utils.py index 19b608ca7f..1826be77f3 100644 --- a/common/misc_utils.py +++ b/common/misc_utils.py @@ -27,16 +27,16 @@ import uuid from concurrent.futures import ThreadPoolExecutor -import requests def get_uuid(): return uuid.uuid1().hex -def download_img(url): +async def download_img(url): if not url: return "" - response = requests.get(url) + from common.http_client import async_request + response = await async_request("GET", url) return "data:" + \ response.headers.get('Content-Type', 'image/jpg') + ";" + \ "base64," + base64.b64encode(response.content).decode("utf-8") diff --git a/pyproject.toml b/pyproject.toml index 73006ac28f..02efa55a33 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -218,6 +218,7 @@ markers = [ "p3: low priority test cases", "smoke: smoke test cases", "auth: authentication UI tests", + "asyncio: mark test as async", ] # Test collection and runtime configuration diff --git a/test/unit_test/common/test_misc_utils.py b/test/unit_test/common/test_misc_utils.py index b407c49b7d..82c8f97657 100644 --- a/test/unit_test/common/test_misc_utils.py +++ b/test/unit_test/common/test_misc_utils.py @@ -15,6 +15,7 @@ # import uuid import hashlib +import pytest from common.misc_utils import get_uuid, download_img, hash_str2int, convert_bytes @@ -91,14 +92,16 @@ class TestGetUuid: class TestDownloadImg: """Test cases for download_img function""" - def test_empty_url_returns_empty_string(self): + @pytest.mark.asyncio + async def test_empty_url_returns_empty_string(self): """Test that empty URL returns empty string""" - result = download_img("") + result = await download_img("") assert result == "" - def test_none_url_returns_empty_string(self): + @pytest.mark.asyncio + async def test_none_url_returns_empty_string(self): """Test that None URL returns empty string""" - result = download_img(None) + result = await download_img(None) assert result == ""