~ps-jenkins/friends/latestsnapshot-0.2.0daily13.05.07.1-0ubuntu1

« back to all changes in this revision

Viewing changes to friends/tests/test_dispatcher.py

  • Committer: Robert Bruce Park
  • Date: 2013-05-01 17:05:12 UTC
  • mfrom: (160.11.31 trunk-next)
  • Revision ID: robert.park@canonical.com-20130501170512-d3xrvv4vmv24k3ir
Land trunk-next into lp:friends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
DBusGMainLoop(set_as_default=True)
35
35
 
36
36
 
 
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."""
39
41
 
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 = {}
48
50
 
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]
58
60
 
59
61
        self.assertIsNone(self.dispatcher.Refresh())
60
62
 
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')
63
65
 
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')
69
70
 
70
71
    def test_clear_indicators(self):
75
76
    def test_do(self):
76
77
        account = mock.Mock()
77
78
        account.id = '345'
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
80
81
 
81
82
        self.dispatcher.Do('like', '345', '23346356767354626')
82
 
        self.dispatcher.account_manager.get.assert_called_once_with(
83
 
            '345')
 
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)
86
86
 
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')
92
91
 
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
97
96
 
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)
101
100
 
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')
107
105
 
108
106
    def test_send_message(self):
111
109
        account3 = mock.Mock()
112
110
        account2.send_enabled = False
113
111
 
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 = [
116
114
            account1,
117
115
            account2,
118
116
            account3,
119
117
            ]
120
118
 
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(
128
126
 
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
133
131
 
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)
139
137
 
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')
145
142
 
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
150
147
 
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)
154
151
 
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')
161
157
 
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
166
162
 
167
163
        success = mock.Mock()
168
164
        failure = mock.Mock()
172
168
                               'A thousand words',
173
169
                               success=success,
174
170
                               failure=failure)
175
 
        self.dispatcher.account_manager.get.assert_called_once_with('2')
 
171
        self.dispatcher.accounts.get.assert_called_once_with(2)
176
172
        account.protocol.assert_called_once_with(
177
173
            'upload',
178
174
            'file://path/to/image.png',
182
178
            )
183
179
 
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')
189
184
 
190
185
    def test_get_features(self):
214
209
            self.dispatcher.URLShorten('http://tinyurl.com/foo'))
215
210
 
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),
226
220
            'short url')
227
 
        lookup_mock.is_shortened.assert_called_once_with(long_url)
228
221
        self.dispatcher.settings.get_boolean.assert_called_once_with(
229
222
            'shorten-urls')
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(
232
225
            long_url)
233
226
 
234
227
    @mock.patch('friends.service.dispatcher.GLib')
243
236
 
244
237
    @mock.patch('friends.service.dispatcher.GLib')
245
238
    def test_manage_timers_set(self, glib):
 
239
        glib.timeout_add_seconds.reset_mock()
246
240
        manager = ManageTimers()
247
241
        manager.timers = set()
248
242
        manager.clear_all_timers = mock.Mock()