File size: 7,688 Bytes
c0e9d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""W4-A business-scope tests.

Credential-free unit tests proving that the business-connection request wrapping
is scoped to a dedicated subclass used only for assistant/bot clients, and that
user-account clients are never affected by the business context variable.
"""

from __future__ import annotations

import asyncio
import sys
import types
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import patch

import pytest
from telethon import TelegramClient
from telethon.functions import InvokeWithBusinessConnectionRequest
from telethon.tl import TLRequest


@contextmanager
def _isolated_client_modules():
    """Import pyUltroid client modules without running the production bootstrap."""
    saved = {name: module for name, module in sys.modules.items()}
    for name in list(sys.modules):
        if name == "pyUltroid" or name.startswith("pyUltroid."):
            sys.modules.pop(name, None)
    try:
        root = str(Path(__file__).resolve().parents[1])
        if root not in sys.path:
            sys.path.insert(0, root)
        yield
    finally:
        for name in list(sys.modules):
            if name == "pyUltroid" or name.startswith("pyUltroid."):
                sys.modules.pop(name, None)
        sys.modules.update(saved)


@pytest.fixture
def client_mods(monkeypatch):
    with _isolated_client_modules():
        original_argv = sys.argv[:]
        sys.argv = ["test_business_scope"]
        try:
            for key, value in {
                "API_ID": "12345",
                "API_HASH": "deadbeefcafe",
                "SESSION": "AQABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAAB",
                "LOG_CHANNEL": "1",
                "BOT_TOKEN": "123456:dummy-token",
            }.items():
                monkeypatch.setenv(key, value)

            from pyUltroid.startup.BaseClient import BusinessBotsClient, UltroidClient
            from pyUltroid.startup.business import (
                _BUSINESS_WRAPPABLE_REQUESTS,
                BusinessMessageEvent,
                business_connection_id,
            )

            yield types.SimpleNamespace(
                BusinessBotsClient=BusinessBotsClient,
                BusinessMessageEvent=BusinessMessageEvent,
                UltroidClient=UltroidClient,
                business_connection_id=business_connection_id,
                wrappable=_BUSINESS_WRAPPABLE_REQUESTS,
            )
        finally:
            sys.argv[:] = original_argv


def test_business_message_flags_fall_back_to_raw_peer_type(client_mods, monkeypatch):
    from telethon.tl.types import PeerUser

    monkeypatch.setattr("pyUltroid.ultroid_bot", types.SimpleNamespace(uid=1))
    message = types.SimpleNamespace(
        id=42,
        peer_id=PeerUser(user_id=22),
        from_id=PeerUser(user_id=22),
        message="business message",
        reply_to=types.SimpleNamespace(reply_to_msg_id=5),
        media=None,
        entities=None,
        post=False,
    )
    update = types.SimpleNamespace(message=message, connection_id="conn-test")

    event = client_mods.BusinessMessageEvent(update, object())

    assert event.is_private is True
    assert event.is_group is False
    assert event.is_channel is False
    assert event.reply_to_msg_id == 5
    assert event.is_reply is True


class DummyRequest(TLRequest):
    CONSTRUCTOR_ID = 0x1234ABCD
    SUBCLASS_OF_ID = 0x5678EF01

    def __init__(self):
        pass

    def to_dict(self):
        return {"_": "DummyRequest"}


class OtherRequest(TLRequest):
    CONSTRUCTOR_ID = 0x11111111
    SUBCLASS_OF_ID = 0x22222222

    def __init__(self):
        pass

    def to_dict(self):
        return {"_": "OtherRequest"}


@pytest.fixture
def capture_telegram_call(monkeypatch):
    """Patch TelegramClient.__call__ to record the query it receives."""
    captured = []

    async def fake_call(self, query, *args, **kwargs):
        captured.append(query)
        return "mocked"

    monkeypatch.setattr(TelegramClient, "__call__", fake_call)
    return captured


@pytest.fixture
def wrappable_with_dummy(monkeypatch, client_mods):
    """Make the production allow-list include the test DummyRequest class."""
    monkeypatch.setattr(
        "pyUltroid.startup.business._BUSINESS_WRAPPABLE_REQUESTS",
        client_mods.wrappable | {"DummyRequest"},
    )


class TestBusinessBotsClient:
    def test_wraps_request_when_business_context_active(
        self, client_mods, capture_telegram_call, wrappable_with_dummy
    ):
        client = object.__new__(client_mods.BusinessBotsClient)
        req = DummyRequest()
        token = client_mods.business_connection_id.set("conn-123")
        try:
            asyncio.run(client(req))
        finally:
            client_mods.business_connection_id.reset(token)

        assert len(capture_telegram_call) == 1
        wrapped = capture_telegram_call[0]
        assert isinstance(wrapped, InvokeWithBusinessConnectionRequest)
        assert wrapped.connection_id == "conn-123"
        assert wrapped.query is req

    def test_does_not_wrap_when_no_business_context(
        self, client_mods, capture_telegram_call, wrappable_with_dummy
    ):
        client = object.__new__(client_mods.BusinessBotsClient)
        req = DummyRequest()
        asyncio.run(client(req))

        assert len(capture_telegram_call) == 1
        assert capture_telegram_call[0] is req

    def test_non_wrappable_request_left_untouched(
        self, client_mods, capture_telegram_call, wrappable_with_dummy
    ):
        client = object.__new__(client_mods.BusinessBotsClient)
        req = OtherRequest()
        token = client_mods.business_connection_id.set("conn-123")
        try:
            asyncio.run(client(req))
        finally:
            client_mods.business_connection_id.reset(token)

        assert len(capture_telegram_call) == 1
        assert capture_telegram_call[0] is req


class TestUserClientUntouched:
    def test_ultroid_client_does_not_wrap_in_business_context(
        self, client_mods, capture_telegram_call, wrappable_with_dummy
    ):
        """A plain user UltroidClient must ignore the business context variable."""
        client = object.__new__(client_mods.UltroidClient)
        req = DummyRequest()
        token = client_mods.business_connection_id.set("conn-123")
        try:
            asyncio.run(client(req))
        finally:
            client_mods.business_connection_id.reset(token)

        assert len(capture_telegram_call) == 1
        assert capture_telegram_call[0] is req
        assert not isinstance(
            capture_telegram_call[0], InvokeWithBusinessConnectionRequest
        )

    def test_ultroid_client_has_no_business_call_override(self, client_mods):
        assert client_mods.UltroidClient.__call__ is TelegramClient.__call__


class TestClientFactory:
    def test_bot_token_uses_business_bots_client(self, client_mods, monkeypatch):
        # Bypass UltroidClient.__init__ to avoid any network setup.
        monkeypatch.setattr(client_mods.UltroidClient, "__init__", lambda *a, **k: None)
        client = client_mods.UltroidClient("asst", bot_token="123456:abc")
        assert isinstance(client, client_mods.BusinessBotsClient)
        assert isinstance(client, client_mods.UltroidClient)
        assert isinstance(client, TelegramClient)

    def test_no_bot_token_uses_plain_ultroid_client(self, client_mods, monkeypatch):
        monkeypatch.setattr(client_mods.UltroidClient, "__init__", lambda *a, **k: None)
        client = client_mods.UltroidClient("session")
        assert type(client) is client_mods.UltroidClient
        assert not isinstance(client, client_mods.BusinessBotsClient)