ruff.toml has declared `quote-style = "single"` and line-length 120 since the lint job landed, and 118 of 128 files follow it. The panel backend and the netbox configuration were written in black/prettier style instead, so a `ruff format --check` would have failed on them from the start. Bring them onto the style the repository already declares, which is what makes the check adoptable at all. Formatting only: apart from quote style the diff is multi-line expressions joined where they fit inside 120 columns. Both suites still pass afterwards (25 pytest, and ruff check is clean). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
102 lines
2.9 KiB
Python
102 lines
2.9 KiB
Python
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
from app.auth_service import TelegramAuthService
|
|
from app.models import PhoneStart, StringSessionStart
|
|
|
|
|
|
class FakeClient:
|
|
instances = []
|
|
requires_password = False
|
|
|
|
def __init__(self, *_args, **kwargs):
|
|
self.kwargs = kwargs
|
|
self.is_connected = False
|
|
self.disconnected = False
|
|
self.__class__.instances.append(self)
|
|
|
|
async def connect(self):
|
|
self.is_connected = True
|
|
|
|
async def disconnect(self):
|
|
self.is_connected = False
|
|
self.disconnected = True
|
|
|
|
async def send_code(self, _phone):
|
|
return SimpleNamespace(phone_code_hash='hash')
|
|
|
|
async def sign_in(self, *_args):
|
|
if self.requires_password:
|
|
from pyrogram.errors import SessionPasswordNeeded
|
|
|
|
raise SessionPasswordNeeded()
|
|
|
|
async def check_password(self, _password):
|
|
return None
|
|
|
|
async def get_me(self):
|
|
return SimpleNamespace(id=1)
|
|
|
|
async def export_session_string(self):
|
|
return 'exported-session'
|
|
|
|
|
|
def phone_payload() -> PhoneStart:
|
|
return PhoneStart(
|
|
instance_id='test',
|
|
display_name='Test',
|
|
api_id=123,
|
|
api_hash='0123456789abcdef',
|
|
phone='+421900000000',
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_phone_code_success_closes_and_forgets_client(monkeypatch) -> None:
|
|
monkeypatch.setattr('app.auth_service.Client', FakeClient)
|
|
auth = TelegramAuthService()
|
|
|
|
flow_id = await auth.start_phone(phone_payload())
|
|
result = await auth.submit_code(flow_id, '12345')
|
|
|
|
assert result.session_string == 'exported-session'
|
|
assert flow_id not in auth.flows
|
|
assert FakeClient.instances[-1].disconnected is True
|
|
assert FakeClient.instances[-1].kwargs['in_memory'] is True
|
|
assert FakeClient.instances[-1].kwargs['no_updates'] is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_string_session_is_validated_and_closed(monkeypatch) -> None:
|
|
monkeypatch.setattr('app.auth_service.Client', FakeClient)
|
|
auth = TelegramAuthService()
|
|
payload = StringSessionStart(
|
|
instance_id='test',
|
|
display_name='Test',
|
|
api_id=123,
|
|
api_hash='0123456789abcdef',
|
|
session_string='x' * 64,
|
|
)
|
|
|
|
result = await auth.validate_string_session(payload)
|
|
|
|
assert result.session_string == 'exported-session'
|
|
assert FakeClient.instances[-1].disconnected is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_start_phone_rejects_overflow(monkeypatch) -> None:
|
|
from app.errors import PanelError
|
|
|
|
monkeypatch.setattr('app.auth_service.Client', FakeClient)
|
|
auth = TelegramAuthService(ttl_seconds=3600, max_flows=2)
|
|
|
|
await auth.start_phone(phone_payload())
|
|
await auth.start_phone(phone_payload())
|
|
|
|
with pytest.raises(PanelError) as error:
|
|
await auth.start_phone(phone_payload())
|
|
|
|
assert error.value.status_code == 429
|
|
assert 'Too many pending' in error.value.detail
|