34
34
DBusGMainLoop(set_as_default=True)
37
@mock.patch('friends.service.dispatcher.GLib.timeout_add_seconds',
38
mock.Mock(return_value=42))
37
39
class TestDispatcher(unittest.TestCase):
38
40
"""Test the dispatcher's ability to dispatch."""
40
42
@mock.patch('dbus.service.BusName')
41
@mock.patch('friends.service.dispatcher.AccountManager')
42
@mock.patch('friends.service.dispatcher.Dispatcher.Refresh')
43
@mock.patch('friends.service.dispatcher.find_accounts')
43
44
@mock.patch('dbus.service.Object.__init__')
44
45
def setUp(self, *mocks):
45
46
self.log_mock = LogMock('friends.service.dispatcher',
46
47
'friends.utils.account')
47
48
self.dispatcher = Dispatcher(mock.Mock(), mock.Mock())
49
self.dispatcher.accounts = {}
49
51
def tearDown(self):
50
52
self.log_mock.stop()
53
55
def test_refresh(self, threading_mock):
54
56
account = mock.Mock()
55
57
threading_mock.activeCount.return_value = 1
56
self.dispatcher.account_manager = mock.Mock()
57
self.dispatcher.account_manager.get_all.return_value = [account]
58
self.dispatcher.accounts = mock.Mock()
59
self.dispatcher.accounts.values.return_value = [account]
59
61
self.assertIsNone(self.dispatcher.Refresh())
61
self.dispatcher.account_manager.get_all.assert_called_once_with()
63
self.dispatcher.accounts.values.assert_called_once_with()
62
64
account.protocol.assert_called_once_with('receive')
64
66
self.assertEqual(self.log_mock.empty(),
65
'Clearing 1 shutdown timer(s)...\n'
67
'Clearing timer id: 42\n'
66
68
'Refresh requested\n'
67
'Clearing 0 shutdown timer(s)...\n'
68
69
'Starting new shutdown timer...\n')
70
71
def test_clear_indicators(self):
76
77
account = mock.Mock()
78
self.dispatcher.account_manager = mock.Mock()
79
self.dispatcher.account_manager.get.return_value = account
79
self.dispatcher.accounts = mock.Mock()
80
self.dispatcher.accounts.get.return_value = account
81
82
self.dispatcher.Do('like', '345', '23346356767354626')
82
self.dispatcher.account_manager.get.assert_called_once_with(
83
self.dispatcher.accounts.get.assert_called_once_with(345)
84
84
account.protocol.assert_called_once_with(
85
85
'like', '23346356767354626', success=STUB, failure=STUB)
87
87
self.assertEqual(self.log_mock.empty(),
88
'Clearing 1 shutdown timer(s)...\n'
88
'Clearing timer id: 42\n'
89
89
'345: like 23346356767354626\n'
90
'Clearing 0 shutdown timer(s)...\n'
91
90
'Starting new shutdown timer...\n')
93
92
def test_failing_do(self):
94
93
account = mock.Mock()
95
self.dispatcher.account_manager = mock.Mock()
96
self.dispatcher.account_manager.get.return_value = None
94
self.dispatcher.accounts = mock.Mock()
95
self.dispatcher.accounts.get.return_value = None
98
97
self.dispatcher.Do('unlike', '6', '23346356767354626')
99
self.dispatcher.account_manager.get.assert_called_once_with('6')
98
self.dispatcher.accounts.get.assert_called_once_with(6)
100
99
self.assertEqual(account.protocol.call_count, 0)
102
101
self.assertEqual(self.log_mock.empty(),
103
'Clearing 1 shutdown timer(s)...\n'
102
'Clearing timer id: 42\n'
104
103
'Could not find account: 6\n'
105
'Clearing 0 shutdown timer(s)...\n'
106
104
'Starting new shutdown timer...\n')
108
106
def test_send_message(self):
111
109
account3 = mock.Mock()
112
110
account2.send_enabled = False
114
self.dispatcher.account_manager = mock.Mock()
115
self.dispatcher.account_manager.get_all.return_value = [
112
self.dispatcher.accounts = mock.Mock()
113
self.dispatcher.accounts.values.return_value = [
121
119
self.dispatcher.SendMessage('Howdy friends!')
122
self.dispatcher.account_manager.get_all.assert_called_once_with()
120
self.dispatcher.accounts.values.assert_called_once_with()
123
121
account1.protocol.assert_called_once_with(
124
122
'send', 'Howdy friends!', success=STUB, failure=STUB)
125
123
account3.protocol.assert_called_once_with(
129
127
def test_send_reply(self):
130
128
account = mock.Mock()
131
self.dispatcher.account_manager = mock.Mock()
132
self.dispatcher.account_manager.get.return_value = account
129
self.dispatcher.accounts = mock.Mock()
130
self.dispatcher.accounts.get.return_value = account
134
132
self.dispatcher.SendReply('2', 'objid', '[Hilarious Response]')
135
self.dispatcher.account_manager.get.assert_called_once_with('2')
133
self.dispatcher.accounts.get.assert_called_once_with(2)
136
134
account.protocol.assert_called_once_with(
137
135
'send_thread', 'objid', '[Hilarious Response]',
138
136
success=STUB, failure=STUB)
140
138
self.assertEqual(self.log_mock.empty(),
141
'Clearing 1 shutdown timer(s)...\n'
139
'Clearing timer id: 42\n'
142
140
'Replying to 2, objid\n'
143
'Clearing 0 shutdown timer(s)...\n'
144
141
'Starting new shutdown timer...\n')
146
143
def test_send_reply_failed(self):
147
144
account = mock.Mock()
148
self.dispatcher.account_manager = mock.Mock()
149
self.dispatcher.account_manager.get.return_value = None
145
self.dispatcher.accounts = mock.Mock()
146
self.dispatcher.accounts.get.return_value = None
151
148
self.dispatcher.SendReply('2', 'objid', '[Hilarious Response]')
152
self.dispatcher.account_manager.get.assert_called_once_with('2')
149
self.dispatcher.accounts.get.assert_called_once_with(2)
153
150
self.assertEqual(account.protocol.call_count, 0)
155
152
self.assertEqual(self.log_mock.empty(),
156
'Clearing 1 shutdown timer(s)...\n'
153
'Clearing timer id: 42\n'
157
154
'Replying to 2, objid\n'
158
155
'Could not find account: 2\n'
159
'Clearing 0 shutdown timer(s)...\n'
160
156
'Starting new shutdown timer...\n')
162
158
def test_upload_async(self):
163
159
account = mock.Mock()
164
self.dispatcher.account_manager = mock.Mock()
165
self.dispatcher.account_manager.get.return_value = account
160
self.dispatcher.accounts = mock.Mock()
161
self.dispatcher.accounts.get.return_value = account
167
163
success = mock.Mock()
168
164
failure = mock.Mock()
184
180
self.assertEqual(self.log_mock.empty(),
185
'Clearing 1 shutdown timer(s)...\n'
181
'Clearing timer id: 42\n'
186
182
'Uploading file://path/to/image.png to 2\n'
187
'Clearing 0 shutdown timer(s)...\n'
188
183
'Starting new shutdown timer...\n')
190
185
def test_get_features(self):
214
209
self.dispatcher.URLShorten('http://tinyurl.com/foo'))
216
211
@mock.patch('friends.service.dispatcher.logging')
217
@mock.patch('friends.service.dispatcher.lookup')
218
def test_urlshorten(self, lookup_mock, logging_mock):
219
lookup_mock.is_shortened.return_value = False
220
lookup_mock.lookup.return_value = mock.Mock()
221
lookup_mock.lookup.return_value.shorten.return_value = 'short url'
212
@mock.patch('friends.service.dispatcher.Short')
213
def test_urlshorten(self, short_mock, logging_mock):
214
short_mock().sub.return_value = 'short url'
215
short_mock.reset_mock()
222
216
self.dispatcher.settings.get_string.return_value = 'is.gd'
223
217
long_url = 'http://example.com/really/really/long'
224
218
self.assertEqual(
225
219
self.dispatcher.URLShorten(long_url),
227
lookup_mock.is_shortened.assert_called_once_with(long_url)
228
221
self.dispatcher.settings.get_boolean.assert_called_once_with(
230
lookup_mock.lookup.assert_called_once_with('is.gd')
231
lookup_mock.lookup.return_value.shorten.assert_called_once_with(
223
short_mock.assert_called_once_with('is.gd')
224
short_mock.return_value.sub.assert_called_once_with(
234
227
@mock.patch('friends.service.dispatcher.GLib')