-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_forward_messages_to_url.py
128 lines (96 loc) · 3.23 KB
/
test_forward_messages_to_url.py
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
from unittest.mock import MagicMock
import pytest
from forward_messages_to_url import handle_new_message, main
import forward_messages_to_url
from pytest_mock import mocker
from unittest.mock import AsyncMock
GROUP_ID = 123
API_ID = 123456
API_HASH = "your_api_hash"
ENDPOINT_URL = "your_endpoint_url"
@pytest.fixture()
def mocked_requests(mocker):
return mocker.patch("forward_messages_to_url.requests.post")
@pytest.fixture
def mock_event():
event = MagicMock()
event.sender = "TestSender"
event.message.text = "Test message"
event.chat_id = GROUP_ID
return event
@pytest.fixture
def mock_response_200():
response = MagicMock()
response.status_code = 200
return response
@pytest.fixture
def mock_response_error():
response = MagicMock()
response.status_code = 500
return response
@pytest.fixture(autouse=True)
def mocked_group_id():
setattr(forward_messages_to_url, "group_id", GROUP_ID)
@pytest.fixture(autouse=True)
def mocked_api_id():
setattr(forward_messages_to_url, "api_id", API_ID)
@pytest.fixture(autouse=True)
def mocked_api_hash():
setattr(forward_messages_to_url, "api_hash", API_HASH)
@pytest.fixture(autouse=True)
def mocked_endpoint_url():
setattr(forward_messages_to_url, "endpoint_url", ENDPOINT_URL)
@pytest.fixture()
def mocked_telegram_client_connect(mocker):
return mocker.patch(
"forward_messages_to_url.TelegramClient.connect", new_callable=AsyncMock
)
@pytest.fixture()
def mocked_telegram_client_start(mocker):
return mocker.patch(
"forward_messages_to_url.TelegramClient.start", new_callable=AsyncMock
)
@pytest.fixture()
def mocked_telegram_client_run_until_disconnected(mocker):
return mocker.patch(
"forward_messages_to_url.TelegramClient.run_until_disconnected",
return_value=None,
new_callable=AsyncMock,
)
@pytest.mark.asyncio
async def test_handle_new_message_success(
mocked_group_id, mocked_requests, mock_event, mock_response_200
):
mocked_requests.return_value = mock_response_200
await handle_new_message(mock_event)
mocked_requests.assert_called_once_with(
"your_endpoint_url", json={"message": "Test message"}
)
@pytest.mark.asyncio
async def test_handle_new_message_error(
mocked_group_id, mocked_requests, mock_event, mock_response_error
):
mocked_requests.return_value = mock_response_error
await handle_new_message(mock_event)
mocked_requests.assert_called_once_with(
"your_endpoint_url", json={"message": "Test message"}
)
@pytest.mark.asyncio
async def test_handle_new_message_error_without_request_response(
mocked_group_id, mocked_requests, mock_event
):
mocked_requests.return_value = "error string!"
await handle_new_message(mock_event)
mocked_requests.assert_called_once_with(
"your_endpoint_url", json={"message": "Test message"}
)
@pytest.mark.asyncio
async def test_telegram_connection(
mocked_telegram_client_connect,
mocked_telegram_client_start,
mocked_telegram_client_run_until_disconnected,
):
await main()
mocked_telegram_client_connect.assert_called_once()
mocked_telegram_client_start.assert_called_once()
mocked_telegram_client_run_until_disconnected.assert_called_once()