Revision 8e8b10ae
Added by Serghei Mihai about 7 years ago
tests/test_manager.py | ||
---|---|---|
1 |
import logging |
|
2 |
import mock |
|
1 | 3 |
import os |
2 | 4 |
import pytest |
3 | 5 |
|
4 | 6 |
from django.core.urlresolvers import reverse |
5 | 7 |
from django.contrib.auth.models import User |
8 |
from django.test import override_settings |
|
6 | 9 |
|
7 | 10 |
from corbo.models import Broadcast |
8 | 11 |
|
... | ... | |
209 | 212 |
assert resp.status_int == 302 |
210 | 213 |
assert resp.location == 'http://testserver/manage/category/alerts/' |
211 | 214 |
|
212 |
def test_send_announce(app, admin_user):
|
|
215 |
def test_email_announce(app, admin_user):
|
|
213 | 216 |
app = login(app) |
214 | 217 |
resp = app.get('/manage/') |
215 | 218 |
assert 'New category' in resp.content |
... | ... | |
233 | 236 |
assert 'First announce' in resp.content |
234 | 237 |
resp = resp.click('First announce') |
235 | 238 |
assert 'Send test email' in resp.content |
239 |
assert 'Send test SMS' not in resp.content |
|
236 | 240 |
resp = resp.click('Send test email') |
237 | 241 |
send_form = resp.forms[0] |
238 | 242 |
assert send_form.method == 'post' |
... | ... | |
243 | 247 |
resp = send_form.submit() |
244 | 248 |
assert resp.status_int == 302 |
245 | 249 |
assert resp.location == 'http://testserver/manage/announce/1/' |
250 |
|
|
251 |
@mock.patch('corbo.utils.requests.post') |
|
252 |
def test_sms_announce(mocked_post, app, admin_user, settings): |
|
253 |
app = login(app) |
|
254 |
resp = app.get('/manage/') |
|
255 |
assert 'New category' in resp.content |
|
256 |
category_page = resp.click('New category') |
|
257 |
category_form = category_page.forms[0] |
|
258 |
category_form['name'] = 'Alerts' |
|
259 |
resp = category_form.submit() |
|
260 |
assert resp.status_int == 302 |
|
261 |
assert resp.location.endswith(reverse('manage')) |
|
262 |
resp = resp.follow() |
|
263 |
resp = resp.click('Alerts') |
|
264 |
|
|
265 |
# create new announce |
|
266 |
assert 'New announce' in resp.content |
|
267 |
announce_page = resp.click('New announce') |
|
268 |
announce_form = announce_page.forms[0] |
|
269 |
announce_form['title'] = 'First announce' |
|
270 |
announce_form['text'] = 'announce content' |
|
271 |
resp = announce_form.submit() |
|
272 |
assert resp.status_int == 302 |
|
273 |
assert resp.location.endswith(reverse('view_category', kwargs={'slug': 'alerts'})) |
|
274 |
resp = resp.follow() |
|
275 |
|
|
276 |
# view announce |
|
277 |
assert 'First announce' in resp.content |
|
278 |
settings.SMS_GATEWAY_URL = 'http:/passerelle.com' |
|
279 |
resp = resp.click('First announce') |
|
280 |
assert 'Send test SMS' in resp.content |
|
281 |
|
|
282 |
# open send sms form |
|
283 |
resp = resp.click('Send test SMS') |
|
284 |
send_form = resp.forms[0] |
|
285 |
assert 'mobile' in send_form.fields |
|
286 |
assert send_form.fields['mobile'][0].value == '' |
|
287 |
# submit with no mobile |
|
288 |
resp = send_form.submit() |
|
289 |
assert resp.status_int == 200 |
|
290 |
|
|
291 |
form = resp.forms[0] |
|
292 |
form['mobile'] = '0607080900' |
|
293 |
# simulate response from passerelle |
|
294 |
mocked_response = mock.Mock() |
|
295 |
mocked_response.json.return_value = {'err': 0, 'data': True} |
|
296 |
mocked_post.return_value = mocked_response |
|
297 |
resp = form.submit() |
|
298 |
assert resp.location.endswith(reverse('view_announce', kwargs={'pk': 1})) |
|
299 |
resp = resp.follow() |
|
300 |
# make sure the form informs about the success |
|
301 |
assert 'SMS successfully sent' in resp.content |
|
302 |
|
|
303 |
resp = resp.click('Send test SMS') |
|
304 |
form = resp.forms[0] |
|
305 |
form['mobile'] = '0607080900' |
|
306 |
# simulate error from passerelle |
|
307 |
mocked_response.json.return_value = {'err': 1, 'data': None, 'err_desc': 'Destination error'} |
|
308 |
resp = form.submit() |
|
309 |
resp = resp.follow() |
|
310 |
assert 'Error occured while sending SMS' in resp.content |
|
311 |
|
|
312 |
def test_sms_announce_with_invalid_gateway_url(app, admin_user, settings, caplog): |
|
313 |
app = login(app) |
|
314 |
resp = app.get('/manage/') |
|
315 |
assert 'New category' in resp.content |
|
316 |
category_page = resp.click('New category') |
|
317 |
category_form = category_page.forms[0] |
|
318 |
category_form['name'] = 'Alerts' |
|
319 |
resp = category_form.submit() |
|
320 |
resp = resp.follow() |
|
321 |
resp = resp.click('Alerts') |
|
322 |
assert 'New announce' in resp.content |
|
323 |
announce_page = resp.click('New announce') |
|
324 |
announce_form = announce_page.forms[0] |
|
325 |
announce_form['title'] = 'First announce' |
|
326 |
announce_form['text'] = 'announce content' |
|
327 |
resp = announce_form.submit() |
|
328 |
assert resp.status_int == 302 |
|
329 |
assert resp.location == 'http://testserver/manage/category/alerts/' |
|
330 |
resp = resp.follow() |
|
331 |
assert 'First announce' in resp.content |
|
332 |
settings.SMS_GATEWAY_URL='invalid_url' |
|
333 |
resp = resp.click('First announce') |
|
334 |
assert 'Send test SMS' in resp.content |
|
335 |
resp = resp.click('Send test SMS') |
|
336 |
form = resp.forms[0] |
|
337 |
form['mobile'] = '0607080900' |
|
338 |
resp = form.submit() |
|
339 |
records = caplog.records |
|
340 |
assert len(records) == 1 |
|
341 |
for record in records: |
|
342 |
assert record.name == 'corbo.utils' |
|
343 |
assert record.levelno == logging.WARNING |
|
344 |
assert 'Invalid URL' in record.getMessage() |
Also available in: Unified diff
manager: add test sms send (#20174)