|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from astrbot.cli.commands import cmd_init |
| 6 | +from astrbot.core.utils.auth_password import verify_dashboard_password |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.asyncio |
| 10 | +async def test_init_without_initial_password_env_does_not_create_config( |
| 11 | + monkeypatch, |
| 12 | + tmp_path, |
| 13 | +): |
| 14 | + async def fake_check_dashboard(_data_path): |
| 15 | + return None |
| 16 | + |
| 17 | + monkeypatch.delenv(cmd_init.DASHBOARD_INITIAL_PASSWORD_ENV, raising=False) |
| 18 | + monkeypatch.setattr(cmd_init, "check_dashboard", fake_check_dashboard) |
| 19 | + (tmp_path / ".astrbot").touch() |
| 20 | + |
| 21 | + await cmd_init.initialize_astrbot(tmp_path) |
| 22 | + |
| 23 | + assert not (tmp_path / "data" / "cmd_config.json").exists() |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.asyncio |
| 27 | +async def test_init_uses_initial_password_env_to_create_config( |
| 28 | + monkeypatch, |
| 29 | + tmp_path, |
| 30 | +): |
| 31 | + async def fake_check_dashboard(_data_path): |
| 32 | + return None |
| 33 | + |
| 34 | + initial_password = "AstrBotInitialPassword123" |
| 35 | + monkeypatch.setenv(cmd_init.DASHBOARD_INITIAL_PASSWORD_ENV, initial_password) |
| 36 | + monkeypatch.setattr(cmd_init, "check_dashboard", fake_check_dashboard) |
| 37 | + (tmp_path / ".astrbot").touch() |
| 38 | + |
| 39 | + await cmd_init.initialize_astrbot(tmp_path) |
| 40 | + |
| 41 | + config_path = tmp_path / "data" / "cmd_config.json" |
| 42 | + config = json.loads(config_path.read_text(encoding="utf-8-sig")) |
| 43 | + dashboard_config = config["dashboard"] |
| 44 | + |
| 45 | + assert verify_dashboard_password( |
| 46 | + dashboard_config["pbkdf2_password"], |
| 47 | + initial_password, |
| 48 | + ) |
| 49 | + assert verify_dashboard_password( |
| 50 | + dashboard_config["password"], |
| 51 | + initial_password, |
| 52 | + ) |
| 53 | + assert dashboard_config["password_change_required"] is True |
| 54 | + assert dashboard_config["password_storage_upgraded"] is True |
0 commit comments