Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import subprocess
2import unittest
3from unittest import mock
4from pi3bar.plugins.imap import PasswordDialog, IMAPUnread
7class PasswordDialogTestCase(unittest.TestCase):
8 def test_init(self):
9 on_finish = lambda: None 9 ↛ exitline 9 didn't run the lambda on line 9
10 pd = PasswordDialog('title', on_finish)
11 self.assertEqual('title', pd.title)
12 self.assertEqual(on_finish, pd.on_finish)
14 @mock.patch('pi3bar.plugins.imap.time')
15 def test_run(self, mock_time):
16 m = mock.Mock()
17 m.read = lambda: 'entered_password'
18 m.name = '/tmp/some_tmp_file'
19 with mock.patch('pi3bar.plugins.imap.tempfile') as mock_tempfile:
20 mock_tempfile.NamedTemporaryFile.return_value = m
22 on_finish = lambda output: None
23 pd = PasswordDialog('title', on_finish)
24 with mock.patch('pi3bar.plugins.imap.subprocess.Popen') as mock_popen:
26 def set_poll_output(ms):
27 mock_popen.return_value.poll = lambda: 1
28 mock_time.sleep.side_effect = set_poll_output
30 mock_popen.return_value.poll = lambda: None
31 pd.run()
33 mock_popen.assert_called_once_with(['xterm', '-e', 'whiptail --passwordbox "title" 8 21 2>/tmp/some_tmp_file'], stderr=subprocess.PIPE)
34 mock_time.sleep.assert_called_once_with(0.1)
35 m.close.assert_called_once_with()
37 @mock.patch('pi3bar.plugins.imap.time')
38 def test_run_long_title(self, mock_time):
39 m = mock.Mock()
40 m.read = lambda: 'entered_password'
41 m.name = '/tmp/some_tmp_file'
42 with mock.patch('pi3bar.plugins.imap.tempfile') as mock_tempfile:
43 mock_tempfile.NamedTemporaryFile.return_value = m
45 on_finish = lambda output: None
46 pd = PasswordDialog('this is a really long test title > 20 chars', on_finish)
47 with mock.patch('pi3bar.plugins.imap.subprocess.Popen') as mock_popen:
49 def set_poll_output(ms):
50 mock_popen.return_value.poll = lambda: 1
51 mock_time.sleep.side_effect = set_poll_output
53 mock_popen.return_value.poll = lambda: None
54 pd.run()
56 mock_popen.assert_called_once_with(['xterm', '-e', 'whiptail --passwordbox "this is a really long test title > 20 chars" 8 51 2>/tmp/some_tmp_file'], stderr=subprocess.PIPE)
57 mock_time.sleep.assert_called_once_with(0.1)
58 m.close.assert_called_once_with()
61class IMAPUnreadTestCase(unittest.TestCase):
62 def test_init(self):
63 plugin = IMAPUnread('server', 'email')
64 self.assertEqual('server', plugin.server)
65 self.assertEqual('email', plugin.username)
66 self.assertEqual(993, plugin.port)
67 self.assertEqual('email@server', plugin.instance)
69 @mock.patch.object(IMAPUnread, 'warning')
70 def test_set_no_link(self, mock_warning):
71 plugin = IMAPUnread('server', 'email')
72 plugin.set_no_link()
73 self.assertEqual('NoLink', plugin.full_text)
74 self.assertEqual('Link', plugin.short_text)
75 self.assertEqual('#ff0000', plugin.color)
76 mock_warning.assert_called_once_with('No Link')
78 def test_set_login_failed(self):
79 plugin = IMAPUnread('server', 'email')
80 plugin.set_login_failed()
81 self.assertEqual('Login failed', plugin.full_text)
82 self.assertEqual('login', plugin.short_text)
83 self.assertEqual('#ff0000', plugin.color)
85 def test_set_no_password(self):
86 plugin = IMAPUnread('server', 'email')
87 plugin.set_no_password()
88 self.assertEqual('Password?', plugin.full_text)
89 self.assertEqual('PW?', plugin.short_text)
90 self.assertEqual('#ffff00', plugin.color)
92 def test_set_unread_no_messages(self):
93 plugin = IMAPUnread('server', 'email')
94 plugin.set_unread(0)
95 self.assertEqual('email 0', plugin.full_text)
96 self.assertIsNone(plugin.short_text)
97 self.assertIsNone(plugin.color)
98 self.assertIsNone(plugin.background)
100 def test_set_unread_messages(self):
101 plugin = IMAPUnread('server', 'email')
102 plugin.set_unread(1)
103 self.assertEqual('email 1', plugin.full_text)
104 self.assertIsNone(plugin.short_text)
105 self.assertEqual('#00ff00', plugin.color)
106 self.assertIsNone(plugin.background)
108 @mock.patch.object(IMAPUnread, 'set_no_password')
109 def test_get_without_password_calls_set_no_password(self, mock_set_no_password):
110 plugin = IMAPUnread('server', 'email')
111 plugin.cycle()
112 mock_set_no_password.assert_called_once_with()
114 @mock.patch('pi3bar.plugins.imap.imaplib')
115 @mock.patch.object(IMAPUnread, 'set_no_link')
116 def test_get_socket_error(self, mock_set_no_link, mock_imaplib):
117 from ssl import SSLError
118 mock_imaplib.IMAP4_SSL.side_effect = SSLError
120 plugin = IMAPUnread('server', 'email')
121 plugin.password = 'password'
122 plugin.cycle()
123 mock_set_no_link.assert_called_once_with()
125 @mock.patch('pi3bar.plugins.imap.imaplib')
126 @mock.patch.object(IMAPUnread, 'error')
127 def test_get_exception(self, mock_error, mock_imaplib):
128 mock_imaplib.IMAP4_SSL.side_effect = TypeError
130 plugin = IMAPUnread('server', 'email')
131 plugin.password = 'password'
132 plugin.cycle()
133 mock_error.assert_called_once_with(exc_info=1)
135 @mock.patch('pi3bar.plugins.imap.imaplib')
136 @mock.patch.object(IMAPUnread, 'set_login_failed')
137 @mock.patch.object(IMAPUnread, 'warning')
138 def test_get_login_ssl_error(self, mock_warning, mock_set_login_failed, mock_imaplib):
139 from ssl import SSLError
140 mock_imaplib.IMAP4_SSL.return_value.login.side_effect = SSLError
142 plugin = IMAPUnread('server', 'email')
143 plugin.logger = mock.MagicMock()
144 plugin.password = 'password'
145 plugin.cycle()
146 mock_set_login_failed.assert_called_once_with()
147 mock_warning.assert_called_once_with('Login failed')
149 @mock.patch('pi3bar.plugins.imap.imaplib')
150 @mock.patch.object(IMAPUnread, 'set_unread')
151 @mock.patch.object(IMAPUnread, 'debug')
152 def test_get_5_unread(self, mock_debug, mock_set_unread, mock_imaplib):
153 mock_imaplib.IMAP4_SSL.return_value.search.return_value = ([], ['1 2 3 4 5'])
155 plugin = IMAPUnread('server', 'email')
156 plugin.password = 'password'
157 plugin.cycle()
158 mock_debug.assert_called_once_with('5 unread')
159 mock_set_unread.assert_called_once_with(5)
161 @mock.patch('pi3bar.plugins.imap.imaplib')
162 @mock.patch.object(IMAPUnread, 'set_unread')
163 @mock.patch.object(IMAPUnread, 'debug')
164 def test_get_0_unread(self, mock_debug, mock_set_unread, mock_imaplib):
165 mock_imaplib.IMAP4_SSL.return_value.search.return_value = ([], [''])
167 plugin = IMAPUnread('server', 'email')
168 plugin.password = 'password'
169 plugin.cycle()
170 mock_debug.assert_called_once_with('0 unread')
171 mock_set_unread.assert_called_once_with(0)
173 def test_set_waiting(self):
174 plugin = IMAPUnread('server', 'email')
175 plugin.set_waiting()
176 self.assertEqual('...', plugin.full_text)
177 self.assertEqual('...', plugin.short_text)
178 self.assertEqual('#ffff00', plugin.color)
180 @mock.patch.object(IMAPUnread, 'open_password_dialog')
181 def test_on_click_1_super(self, mock_open_password_dialog):
182 plugin = IMAPUnread('server', 'email')
183 with mock.patch('pi3bar.plugins.base.Plugin.on_click') as mock_super:
184 plugin.on_click(1, 0, 0)
185 mock_super.assert_called_once_with(1, 0, 0)
186 self.assertEqual(0, mock_open_password_dialog.call_count)
188 @mock.patch.object(IMAPUnread, 'open_password_dialog')
189 def test_on_click_2_super(self, mock_open_password_dialog):
190 plugin = IMAPUnread('server', 'email')
191 with mock.patch('pi3bar.plugins.base.Plugin.on_click') as mock_super:
192 plugin.on_click(2, 0, 0)
193 mock_super.assert_called_once_with(2, 0, 0)
194 self.assertEqual(0, mock_open_password_dialog.call_count)
196 @mock.patch.object(IMAPUnread, 'open_password_dialog')
197 def test_on_click_3_call_open_password_dialog(self, mock_open_password_dialog):
198 plugin = IMAPUnread('server', 'email')
199 plugin.on_click(3, 0, 0)
200 mock_open_password_dialog.assert_called_once_with()
202 @mock.patch('pi3bar.plugins.imap.PasswordDialog')
203 @mock.patch.object(IMAPUnread, 'rerun')
204 def test_open_password_dialog(self, mock_retick, mock_password_dialog):
205 plugin = IMAPUnread('server', 'email')
206 plugin.open_password_dialog()
207 self.assertEqual(1, mock_password_dialog.call_count)
208 done = mock_password_dialog.call_args[0][1]
209 done('password')
210 self.assertEqual('password', plugin.password)
211 mock_retick.assert_called_once_with()
213 @mock.patch('pi3bar.plugins.imap.PasswordDialog')
214 @mock.patch.object(IMAPUnread, 'set_login_failed')
215 def test_open_password_dialog_no_password(self, mock_set_login_failed, mock_password_dialog):
216 plugin = IMAPUnread('server', 'email')
217 plugin.open_password_dialog()
218 self.assertEqual(1, mock_password_dialog.call_count)
219 done = mock_password_dialog.call_args[0][1]
220 done('')
221 mock_set_login_failed.assert_called_once_with()