1+ #!/usr/bin/env python3
2+ # -*- coding: utf-8 -*-
3+ """Test script for message logging functionality."""
4+
5+ import unittest
6+ from unittest .mock import Mock , patch , call
7+ from datetime import datetime
8+ import sys
9+ import os
10+
11+ # Add the current directory to the path to import modules
12+ sys .path .insert (0 , os .path .dirname (os .path .abspath (__file__ )))
13+
14+ # Import Bot class by importing the main module
15+ import importlib .util
16+ import sys
17+
18+ # Load the main module
19+ spec = importlib .util .spec_from_file_location ("main" , "__main__.py" )
20+ main_module = importlib .util .module_from_spec (spec )
21+ sys .modules ["main" ] = main_module
22+ spec .loader .exec_module (main_module )
23+
24+ Bot = main_module .Bot
25+ import config
26+
27+
28+ class TestMessageLogging (unittest .TestCase ):
29+ """Test cases for message logging functionality."""
30+
31+ def setUp (self ):
32+ """Set up test fixtures."""
33+ # Mock the BOT_TOKEN to avoid requiring real token for tests
34+ with patch ('__main__.BOT_TOKEN' , 'test_token' ):
35+ self .bot = Bot (token = 'test_token' , group_id = 12345 , debug = False )
36+
37+ # Mock the VK API methods
38+ self .bot .call_method = Mock ()
39+ self .bot .send_msg = Mock ()
40+ self .bot .get_user_name = Mock (return_value = "Test User" )
41+
42+ def test_logging_disabled_when_no_config (self ):
43+ """Test that logging is disabled when configuration is missing."""
44+ # Backup original config
45+ original_logging_chat = config .LOGGING_CHAT_ID
46+ original_main_chats = config .MAIN_CHATS
47+
48+ try :
49+ # Test with no logging chat configured
50+ config .LOGGING_CHAT_ID = None
51+ config .MAIN_CHATS = [2000000001 ]
52+
53+ event = {
54+ "text" : "Test message" ,
55+ "date" : 1640995200 ,
56+ "attachments" : [],
57+ "fwd_messages" : [],
58+ "reply_message" : {}
59+ }
60+
61+ self .bot ._forward_message_to_logging_chat (event , 2000000001 , 12345 )
62+
63+ # Should not call send_msg
64+ self .bot .send_msg .assert_not_called ()
65+
66+ # Test with no main chats configured
67+ config .LOGGING_CHAT_ID = 2000000020
68+ config .MAIN_CHATS = []
69+
70+ self .bot ._forward_message_to_logging_chat (event , 2000000001 , 12345 )
71+
72+ # Should still not call send_msg
73+ self .bot .send_msg .assert_not_called ()
74+
75+ finally :
76+ # Restore original config
77+ config .LOGGING_CHAT_ID = original_logging_chat
78+ config .MAIN_CHATS = original_main_chats
79+
80+ def test_message_not_forwarded_from_non_main_chat (self ):
81+ """Test that messages from non-main chats are not forwarded."""
82+ # Backup original config
83+ original_logging_chat = config .LOGGING_CHAT_ID
84+ original_main_chats = config .MAIN_CHATS
85+
86+ try :
87+ config .LOGGING_CHAT_ID = 2000000020
88+ config .MAIN_CHATS = [2000000001 ] # Only this chat should be forwarded
89+
90+ event = {
91+ "text" : "Test message" ,
92+ "date" : 1640995200 ,
93+ "attachments" : [],
94+ "fwd_messages" : [],
95+ "reply_message" : {}
96+ }
97+
98+ # Send from a chat not in MAIN_CHATS
99+ self .bot ._forward_message_to_logging_chat (event , 2000000002 , 12345 )
100+
101+ # Should not call send_msg
102+ self .bot .send_msg .assert_not_called ()
103+
104+ finally :
105+ # Restore original config
106+ config .LOGGING_CHAT_ID = original_logging_chat
107+ config .MAIN_CHATS = original_main_chats
108+
109+ def test_bot_messages_not_forwarded (self ):
110+ """Test that bot messages (negative from_id) are not forwarded."""
111+ # Backup original config
112+ original_logging_chat = config .LOGGING_CHAT_ID
113+ original_main_chats = config .MAIN_CHATS
114+
115+ try :
116+ config .LOGGING_CHAT_ID = 2000000020
117+ config .MAIN_CHATS = [2000000001 ]
118+
119+ event = {
120+ "text" : "Bot message" ,
121+ "date" : 1640995200 ,
122+ "attachments" : [],
123+ "fwd_messages" : [],
124+ "reply_message" : {}
125+ }
126+
127+ # Send from bot (negative from_id)
128+ self .bot ._forward_message_to_logging_chat (event , 2000000001 , - 12345 )
129+
130+ # Should not call send_msg
131+ self .bot .send_msg .assert_not_called ()
132+
133+ finally :
134+ # Restore original config
135+ config .LOGGING_CHAT_ID = original_logging_chat
136+ config .MAIN_CHATS = original_main_chats
137+
138+ def test_message_forwarding_basic (self ):
139+ """Test basic message forwarding functionality."""
140+ # Backup original config
141+ original_logging_chat = config .LOGGING_CHAT_ID
142+ original_main_chats = config .MAIN_CHATS
143+
144+ try :
145+ config .LOGGING_CHAT_ID = 2000000020
146+ config .MAIN_CHATS = [2000000001 ]
147+
148+ event = {
149+ "text" : "Hello, world!" ,
150+ "date" : 1640995200 , # 2022-01-01 00:00:00
151+ "attachments" : [],
152+ "fwd_messages" : [],
153+ "reply_message" : {}
154+ }
155+
156+ self .bot ._get_chat_title = Mock (return_value = "Test Chat" )
157+
158+ self .bot ._forward_message_to_logging_chat (event , 2000000001 , 12345 )
159+
160+ # Should call send_msg with formatted message
161+ self .bot .send_msg .assert_called_once ()
162+ call_args = self .bot .send_msg .call_args
163+ sent_message = call_args [0 ][0 ] # First positional argument
164+ sent_to_chat = call_args [0 ][1 ] # Second positional argument
165+
166+ # Check that message was sent to logging chat
167+ self .assertEqual (sent_to_chat , 2000000020 )
168+
169+ # Check message format
170+ self .assertIn ("Test Chat" , sent_message )
171+ self .assertIn ("Test User" , sent_message )
172+ self .assertIn ("Hello, world!" , sent_message )
173+ self .assertIn ("2022-01-01" , sent_message )
174+
175+ finally :
176+ # Restore original config
177+ config .LOGGING_CHAT_ID = original_logging_chat
178+ config .MAIN_CHATS = original_main_chats
179+
180+ def test_message_with_attachments (self ):
181+ """Test message forwarding with attachments."""
182+ # Backup original config
183+ original_logging_chat = config .LOGGING_CHAT_ID
184+ original_main_chats = config .MAIN_CHATS
185+
186+ try :
187+ config .LOGGING_CHAT_ID = 2000000020
188+ config .MAIN_CHATS = [2000000001 ]
189+
190+ event = {
191+ "text" : "Check this out!" ,
192+ "date" : 1640995200 ,
193+ "attachments" : [
194+ {"type" : "photo" },
195+ {"type" : "doc" }
196+ ],
197+ "fwd_messages" : [],
198+ "reply_message" : {}
199+ }
200+
201+ self .bot ._get_chat_title = Mock (return_value = "Test Chat" )
202+
203+ self .bot ._forward_message_to_logging_chat (event , 2000000001 , 12345 )
204+
205+ # Should call send_msg
206+ self .bot .send_msg .assert_called_once ()
207+ call_args = self .bot .send_msg .call_args
208+ sent_message = call_args [0 ][0 ]
209+
210+ # Check that attachments are mentioned
211+ self .assertIn ("Attachments:" , sent_message )
212+ self .assertIn ("[photo]" , sent_message )
213+ self .assertIn ("[doc]" , sent_message )
214+
215+ finally :
216+ # Restore original config
217+ config .LOGGING_CHAT_ID = original_logging_chat
218+ config .MAIN_CHATS = original_main_chats
219+
220+
221+ if __name__ == '__main__' :
222+ unittest .main ()
0 commit comments