-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_keylogger.py
More file actions
37 lines (31 loc) · 1.38 KB
/
Copy pathtest_keylogger.py
File metadata and controls
37 lines (31 loc) · 1.38 KB
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
import unittest
from unittest.mock import patch, MagicMock
class TestKeylogger(unittest.TestCase):
def test_bug_is_fixed(self):
"""
This test checks that the bug has been fixed.
The bug was that the listener thread was created as a daemon thread.
"""
with open("keylogger.py", "r") as f:
content = f.read()
self.assertNotIn("listener_thread.daemon = True", content, "The bug (daemon thread) should be fixed.")
def test_send_key_to_telegram(self):
"""
This test checks the send_key_to_telegram function.
We mock pynput and requests to avoid the ImportError.
"""
# Mock the entire pynput module
pynput_mock = MagicMock()
pynput_mock.keyboard = MagicMock()
with patch.dict('sys.modules', {'pynput': pynput_mock, 'pynput.keyboard': pynput_mock.keyboard, 'requests': MagicMock()}):
import importlib
import keylogger
importlib.reload(keylogger)
keylogger.requests.post = MagicMock()
keylogger.send_key_to_telegram("a")
keylogger.requests.post.assert_called_once()
args, kwargs = keylogger.requests.post.call_args
self.assertTrue(args[0].startswith("https://api.telegram.org/bot"))
self.assertEqual(kwargs['data']['text'], "a")
if __name__ == '__main__':
unittest.main()