42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
import pytest
|
|
|
|
from common.login_helper import LoginHelper
|
|
|
|
|
|
class _Response:
|
|
status_code = 401
|
|
|
|
def json(self) -> dict[str, object]:
|
|
return {'code': 401, 'msg': '测试响应'}
|
|
|
|
|
|
class _RecordingSession:
|
|
def __init__(self) -> None:
|
|
self.requests: list[dict[str, str]] = []
|
|
|
|
def post(self, url: str, data: dict[str, str], headers: dict[str, str]) -> _Response:
|
|
self.requests.append(data)
|
|
return _Response()
|
|
|
|
|
|
def test_login_uses_admin_password_from_environment_when_password_is_omitted(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setenv('SHIERFENG_TEST_ADMIN_PASSWORD', 'test-only-admin-secret')
|
|
helper = LoginHelper()
|
|
session = _RecordingSession()
|
|
helper.session = session
|
|
|
|
assert helper.login(username='admin', max_retries=1) is None
|
|
assert session.requests == [
|
|
{'username': 'admin', 'password': 'test-only-admin-secret'},
|
|
]
|
|
|
|
|
|
def test_login_fails_fast_when_account_password_environment_is_missing(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.delenv('SHIERFENG_TEST_ADMIN_PASSWORD', raising=False)
|
|
helper = LoginHelper()
|
|
|
|
with pytest.raises(RuntimeError, match='SHIERFENG_TEST_ADMIN_PASSWORD'):
|
|
helper.login(username='admin', max_retries=1)
|