From 6883de0f1c7e6ca959e480e5628066977eb29f03 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sat, 21 Sep 2024 12:19:49 -0400 Subject: [PATCH] feat: partially test http server api --- astrbot/bootstrap.py | 9 ++++-- tests/test_http_server.py | 51 +++++++++++++++++++++++++++++++++ util/updator/astrbot_updator.py | 3 ++ util/updator/zip_updator.py | 2 +- 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 tests/test_http_server.py diff --git a/astrbot/bootstrap.py b/astrbot/bootstrap.py index 36e8a18a8..5a8a4c837 100644 --- a/astrbot/bootstrap.py +++ b/astrbot/bootstrap.py @@ -65,6 +65,11 @@ class AstrBotBootstrap(): self.context.plugin_updator = self.plugin_manager.updator self.context.message_handler = self.message_handler self.context.command_manager = self.command_manager + + + # load dashboard + self.dashboard.run_http_server() + dashboard_task = asyncio.create_task(self.dashboard.ws_server(), name="dashboard") if self.test_mode: return @@ -77,9 +82,7 @@ class AstrBotBootstrap(): platform_tasks = self.load_platform() # load metrics uploader metrics_upload_task = asyncio.create_task(self.metrics_uploader.upload_metrics(), name="metrics-uploader") - # load dashboard - self.dashboard.run_http_server() - dashboard_task = asyncio.create_task(self.dashboard.ws_server(), name="dashboard") + tasks = [metrics_upload_task, dashboard_task, *platform_tasks, *self.context.ext_tasks] tasks = [self.handle_task(task) for task in tasks] await asyncio.gather(*tasks) diff --git a/tests/test_http_server.py b/tests/test_http_server.py new file mode 100644 index 000000000..b26ca1f31 --- /dev/null +++ b/tests/test_http_server.py @@ -0,0 +1,51 @@ + +import aiohttp +import pytest + +BASE_URL = "http://0.0.0.0:6185/api" + +async def get_url(url): + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + return await response.json() + +async def post_url(url, data): + async with aiohttp.ClientSession() as session: + async with session.post(url, json=data) as response: + return await response.json() + +class TestHTTPServer: + @pytest.mark.asyncio + async def test_config(self): + configs = await get_url(f"{BASE_URL}/configs") + assert 'data' in configs and 'metadata' in configs['data'] \ + and 'config' in configs['data'] + config = configs['data']['config'] + # test post config + await post_url(f"{BASE_URL}/astrbot-configs", config) + # text post config with invalid data + assert 'rate_limit' in config['platform_settings'] + config['platform_settings']['rate_limit'] = "invalid" + ret = await post_url(f"{BASE_URL}/astrbot-configs", config) + assert 'status' in ret and ret['status'] == 'error' + + @pytest.mark.asyncio + async def test_update(self): + await get_url(f"{BASE_URL}/check_update") + + @pytest.mark.asyncio + async def test_plugins(self): + pname = "astrbot_plugin_bilibili" + url = f"https://github.com/Soulter/{pname}" + + await get_url(f"{BASE_URL}/extensions") + + # test install plugin + await post_url(f"{BASE_URL}/extensions/install", { + "url": url + }) + + # test uninstall plugin + await post_url(f"{BASE_URL}/extensions/uninstall", { + "name": pname + }) \ No newline at end of file diff --git a/util/updator/astrbot_updator.py b/util/updator/astrbot_updator.py index a0245f760..21ccb92ad 100644 --- a/util/updator/astrbot_updator.py +++ b/util/updator/astrbot_updator.py @@ -31,6 +31,9 @@ class AstrBotUpdator(RepoZipUpdator): pass def _reboot(self, delay: int = None, context = None): + if os.environ.get('TEST_MODE', 'off') == 'on': + logger.info("测试模式下不会重启。") + return # if delay: time.sleep(delay) py = sys.executable context.running = False diff --git a/util/updator/zip_updator.py b/util/updator/zip_updator.py index b0e2889d7..e6745bd3b 100644 --- a/util/updator/zip_updator.py +++ b/util/updator/zip_updator.py @@ -109,7 +109,7 @@ class RepoZipUpdator(): releases = await self.fetch_release_info(url=release_url) if not releases: # download from the default branch directly. - logger.warn(f"未在仓库 {author}/{repo} 中找到任何发布版本,将从默认分支下载。") + logger.warning(f"未在仓库 {author}/{repo} 中找到任何发布版本,将从默认分支下载。") release_url = f"https://github.com/{author}/{repo}/archive/refs/heads/master.zip" else: release_url = releases[0]['zipball_url']