This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_vumi_app.py
280 lines (231 loc) · 11.7 KB
/
test_vumi_app.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""Tests for go.apps.sequential_send.vumi_app"""
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.internet.task import Clock, LoopingCall
from vumi.message import TransportUserMessage
from vumi.tests.helpers import VumiTestCase
from vumi.tests.utils import LogCatcher
from go.apps.sequential_send.vumi_app import SequentialSendApplication
from go.apps.sequential_send import vumi_app as sequential_send_module
from go.apps.tests.helpers import AppWorkerHelper
class TestSequentialSendApplication(VumiTestCase):
transport_type = u'sms'
@inlineCallbacks
def setUp(self):
self.app_helper = self.add_helper(
AppWorkerHelper(SequentialSendApplication))
self.clock = Clock()
self.patch(sequential_send_module, 'LoopingCall', self.looping_call)
self.app = yield self.app_helper.get_app_worker({})
def looping_call(self, *args, **kwargs):
looping_call = LoopingCall(*args, **kwargs)
looping_call.clock = self.clock
return looping_call
@inlineCallbacks
def reply_to(self, msg, content, continue_session=True, **kw):
session_event = (None if continue_session
else TransportUserMessage.SESSION_CLOSE)
reply = TransportUserMessage(
to_addr=msg['from_addr'],
from_addr=msg['to_addr'],
group=msg['group'],
in_reply_to=msg['message_id'],
content=content,
session_event=session_event,
transport_name=msg['transport_name'],
transport_type=msg['transport_type'],
transport_metadata=msg['transport_metadata'],
helper_metadata=msg['helper_metadata'],
**kw)
yield self.dispatch(reply)
@inlineCallbacks
def wait_for_messages(self, nr_of_messages, total_length):
msgs = yield self.wait_for_dispatched_messages(total_length)
returnValue(msgs[-1 * nr_of_messages:])
@inlineCallbacks
def _stub_out_async(self, *convs):
"""Stub out async components.
NOTE: Riak stuff takes a while and messes up fake clock timing, so we
stub it out. It gets tested in other test methods. Also, we replace the
redis manager for the same reason.
"""
# Avoid hitting Riak for the conversation and Redis for poll times.
expected = [[conv.user_account.key, conv.key] for conv in convs]
poll_times = [(yield self.app._get_last_poll_time())]
scheduled_conversations = yield self.app._get_scheduled_conversations()
def get_conversations(conv_pointers):
self.assertEqual(sorted(conv_pointers), sorted(expected))
return list(convs)
self.app.get_conversations = get_conversations
self.app._get_last_poll_time = lambda: poll_times[-1]
self.app._set_last_poll_time = lambda t: poll_times.append(str(t))
self.app._get_scheduled_conversations = lambda: scheduled_conversations
self.message_convs = []
# Fake the message send by adding the convs to a list.
def send_scheduled_messages(sched_conv):
self.message_convs.append(sched_conv)
self.app.send_scheduled_messages = send_scheduled_messages
def check_message_convs_and_advance(self, convs, seconds):
self.assertEqual(convs, self.message_convs)
return self.clock.advance(seconds)
@inlineCallbacks
def test_schedule_daily_conv(self):
conv = yield self.app_helper.create_conversation(
config={'schedule': {'recurring': 'daily', 'time': '00:01:40'}})
yield self.app_helper.start_conversation(conv)
conv = yield self.app_helper.get_conversation(conv.key)
yield self._stub_out_async(conv)
yield self.check_message_convs_and_advance([], 70)
yield self.check_message_convs_and_advance([], 70)
yield self.check_message_convs_and_advance([conv], 70)
yield self.check_message_convs_and_advance([conv], 3600 * 24 - 140)
yield self.check_message_convs_and_advance([conv], 70)
yield self.check_message_convs_and_advance([conv, conv], 70)
self.assertEqual(self.message_convs, [conv, conv])
@inlineCallbacks
def test_schedule_daily_with_stopped_conv(self):
conv = yield self.app_helper.create_conversation(
config={'schedule': {'recurring': 'daily', 'time': '00:01:40'}})
yield self.app_helper.start_conversation(conv)
conv = yield self.app_helper.get_conversation(conv.key)
yield self.app_helper.stop_conversation(conv)
conv = yield self.app_helper.get_conversation(conv.key)
yield self._stub_out_async()
yield self.check_message_convs_and_advance([], 70)
yield self.check_message_convs_and_advance([], 70)
# had it been scheduled it should show up after from here on onwards
yield self.check_message_convs_and_advance([], 70)
yield self.check_message_convs_and_advance([], 3600 * 24 - 140)
yield self.check_message_convs_and_advance([], 70)
yield self.check_message_convs_and_advance([], 70)
self.assertEqual(self.message_convs, [])
@inlineCallbacks
def test_schedule_day_of_month_conv(self):
conv = yield self.app_helper.create_conversation(config={
'schedule': {
'recurring': 'day_of_month',
'time': '12:00:00', 'days':
'1, 5',
},
})
yield self.app_helper.start_conversation(conv)
conv = yield self.app_helper.get_conversation(conv.key)
yield self._stub_out_async(conv)
yield self.check_message_convs_and_advance([], 3600 * 11)
yield self.check_message_convs_and_advance([], 3600 * 13)
yield self.check_message_convs_and_advance([conv], 3600 * 24)
yield self.check_message_convs_and_advance([conv], 3600 * 48)
yield self.check_message_convs_and_advance([conv], 3600 * 13)
yield self.check_message_convs_and_advance([conv, conv], 3600 * 11)
yield self.check_message_convs_and_advance(
[conv, conv], 3600 * 24 * 20)
self.assertEqual(self.message_convs, [conv, conv])
@inlineCallbacks
def test_schedule_convs(self):
"""Test multiple conversation scheduling.
NOTE: Riak stuff takes a while and messes up fake clock timing, so we
stub it out. It gets tested in other test methods.
"""
conv1 = yield self.app_helper.create_conversation(
config={'schedule': {'recurring': 'daily', 'time': '00:01:40'}})
yield self.app_helper.start_conversation(conv1)
conv1 = yield self.app_helper.get_conversation(conv1.key)
conv2 = yield self.app_helper.create_conversation(
config={'schedule': {'recurring': 'daily', 'time': '00:02:30'}})
yield self.app_helper.start_conversation(conv2)
conv2 = yield self.app_helper.get_conversation(conv2.key)
yield self._stub_out_async(conv1, conv2)
yield self.check_message_convs_and_advance([], 70)
yield self.check_message_convs_and_advance([], 70)
yield self.check_message_convs_and_advance([conv1], 70)
yield self.check_message_convs_and_advance(
[conv1, conv2], 3600 * 24 - 140)
yield self.check_message_convs_and_advance([conv1, conv2], 70)
yield self.check_message_convs_and_advance([conv1, conv2, conv1], 70)
self.assertEqual(self.message_convs, [conv1, conv2, conv1, conv2])
@inlineCallbacks
def test_get_conversations(self):
"""Test get_conversation, because we stub it out elsewhere.
"""
conv1 = yield self.app_helper.create_conversation(
config={'schedule': {'recurring': 'daily', 'time': '00:01:40'}})
yield self.app_helper.start_conversation(conv1)
conv2 = yield self.app_helper.create_conversation(
config={'schedule': {'recurring': 'daily', 'time': '00:02:30'}})
yield self.app_helper.start_conversation(conv2)
yield self.app_helper.create_conversation(
config={'schedule': {'recurring': 'daily', 'time': '00:02:30'}})
convs = yield self.app.get_conversations([
[conv1.user_account.key, conv1.key],
[conv2.user_account.key, conv2.key]])
self.assertEqual(sorted([c.key for c in convs]),
sorted([conv1.key, conv2.key]))
@inlineCallbacks
def test_get_conversations_missing_conv(self):
"""
Test get_conversation when it's expecting a conversation that doesn't
exist.
"""
conv = yield self.app_helper.create_conversation(
config={'schedule': {'recurring': 'daily', 'time': '00:01:40'}})
yield self.app_helper.start_conversation(conv)
with LogCatcher(message='Conversation .* not found.') as lc:
convs = yield self.app.get_conversations(
[[conv.user_account.key, conv.key], ['badaccount', 'badkey']])
self.assertEqual(
lc.messages(),
['Conversation badkey for account badaccount not found.'])
self.assertEqual([c.key for c in convs], [conv.key])
@inlineCallbacks
def test_sends(self):
"""Test send_scheduled_messages, because we stub it out elsewhere.
"""
group = yield self.app_helper.create_group(u'group')
contact1 = yield self.app_helper.create_contact(
u'27831234567', name=u'First', surname=u'Contact', groups=[group])
contact2 = yield self.app_helper.create_contact(
u'27831234568', name=u'Second', surname=u'Contact', groups=[group])
conv = yield self.app_helper.create_conversation(config={
'schedule': {'recurring': 'daily', 'time': '00:01:40'},
'messages': ['foo', 'bar'],
}, groups=[group])
yield self.app_helper.start_conversation(conv)
conv = yield self.app_helper.get_conversation(conv.key)
# Send to two contacts.
yield self.app.send_scheduled_messages(conv)
[msg1, msg2] = sorted(
self.app_helper.get_dispatched_outbound(),
key=lambda m: m['to_addr'])
self.assertEqual(msg1['content'], 'foo')
self.assertEqual(msg1['to_addr'], contact1.msisdn)
self.assertEqual(msg1['helper_metadata']['go'], {
'user_account': conv.user_account.key,
'conversation_type': 'sequential_send',
'conversation_key': conv.key,
})
self.assertEqual(msg2['content'], 'foo')
self.assertEqual(msg2['to_addr'], contact2.msisdn)
self.assertEqual(msg2['helper_metadata']['go'], {
'user_account': conv.user_account.key,
'conversation_type': 'sequential_send',
'conversation_key': conv.key,
})
# Send to previous two contacts and a new third contact.
contact3 = yield self.app_helper.create_contact(
u'27831234569', name=u'Third', surname=u'Contact', groups=[group])
yield self.app.send_scheduled_messages(conv)
[msg1, msg2, msg3] = sorted(
self.app_helper.get_dispatched_outbound()[2:],
key=lambda m: m['to_addr'])
self.assertEqual(msg1['content'], 'bar')
self.assertEqual(msg1['to_addr'], contact1.msisdn)
self.assertEqual(msg2['content'], 'bar')
self.assertEqual(msg2['to_addr'], contact2.msisdn)
self.assertEqual(msg3['content'], 'foo')
self.assertEqual(msg3['to_addr'], contact3.msisdn)
# Previous two contacts are done, so we should only send to the third.
yield self.app.send_scheduled_messages(conv)
[msg] = sorted(
self.app_helper.get_dispatched_outbound()[5:],
key=lambda m: m['to_addr'])
self.assertEqual(msg['content'], 'bar')
self.assertEqual(msg['to_addr'], contact3.msisdn)