Revision 8e8b10ae
Added by Serghei Mihai about 7 years ago
corbo/forms.py | ||
---|---|---|
81 | 81 |
|
82 | 82 |
class SendTestEmailForm(forms.Form): |
83 | 83 |
email = forms.EmailField() |
84 |
|
|
85 |
|
|
86 |
class SendTestSMSForm(forms.Form): |
|
87 |
mobile = forms.CharField(label=_('Mobile number')) |
corbo/manage_urls.py | ||
---|---|---|
2 | 2 |
|
3 | 3 |
from .views import add_announce, edit_announce, delete_announce, \ |
4 | 4 |
add_category, edit_category, view_category, delete_category, manage, \ |
5 |
subscriptions_import, view_announce, email_announce, menu_json |
|
5 |
subscriptions_import, view_announce, email_announce, sms_announce, \ |
|
6 |
menu_json |
|
6 | 7 |
|
7 | 8 |
urlpatterns = patterns('', |
8 | 9 |
url(r'^$', manage, name='manage'), |
... | ... | |
14 | 15 |
name='delete_announce'), |
15 | 16 |
url(r'^announce/email/(?P<pk>\d+)/$', email_announce, |
16 | 17 |
name='email_announce'), |
18 |
url(r'^announce/sms/(?P<pk>\d+)/$', sms_announce, |
|
19 |
name='sms_announce'), |
|
17 | 20 |
url(r'^category/(?P<slug>[\w-]+)/$', view_category, |
18 | 21 |
name='view_category'), |
19 | 22 |
url(r'^announce/(?P<pk>\d+)/$', view_announce, |
corbo/templates/corbo/announce_view.html | ||
---|---|---|
12 | 12 |
<a href="{% url 'delete_announce' pk=object.pk %}" rel="popup">{% trans 'Delete' %}</a> |
13 | 13 |
<a href="{% url 'edit_announce' pk=object.pk %}">{% trans 'Edit' %}</a> |
14 | 14 |
<a href="{% url 'email_announce' pk=object.pk %}" rel="popup">{% trans 'Send test email' %}</a> |
15 |
{% if sms_enabled %} |
|
16 |
<a href="{% url 'sms_announce' pk=object.pk %}" rel="popup">{% trans 'Send test SMS' %}</a> |
|
17 |
{% endif %} |
|
15 | 18 |
{% endblock %} |
16 | 19 |
{% block content %} |
17 | 20 |
<div class="announce_block"> |
corbo/templates/corbo/email_test_announce_form.html | ||
---|---|---|
1 |
{% extends "corbo/manage.html" %} |
|
2 |
{% load i18n static %} |
|
3 |
|
|
4 |
{% block breadcrumb %} |
|
5 |
{{ block.super }} |
|
6 |
<a href="{% url "view_announce" pk=object.pk %}">{{ object.title }}</a> |
|
7 |
{% endblock %} |
|
8 |
|
|
9 |
{% block appbar %} |
|
10 |
<h2>{% trans "Send test email" %}</h2> |
|
11 |
{% endblock %} |
|
12 |
|
|
13 |
|
|
14 |
{% block content %} |
|
15 |
<form method="post"> |
|
16 |
{% csrf_token %} |
|
17 |
{{ form.as_p }} |
|
18 |
<div class="buttons"> |
|
19 |
<button class="submit-button">{% trans "Send" %}</button> |
|
20 |
<a href="{% url "view_announce" pk=object.pk %}" class="cancel">{% trans "Cancel" %}</a> |
|
21 |
</div> |
|
22 |
</form> |
|
23 |
{% endblock %} |
corbo/templates/corbo/sms_test_announce_form.html | ||
---|---|---|
1 |
{% extends "corbo/manage.html" %} |
|
2 |
{% load i18n static %} |
|
3 |
|
|
4 |
{% block breadcrumb %} |
|
5 |
{{ block.super }} |
|
6 |
<a href="{% url "view_announce" pk=object.pk %}">{{ object.title }}</a> |
|
7 |
{% endblock %} |
|
8 |
|
|
9 |
{% block appbar %} |
|
10 |
<h2>{% trans "Send test SMS" %}</h2> |
|
11 |
{% endblock %} |
|
12 |
|
|
13 |
{% block content %} |
|
14 |
<form method="post"> |
|
15 |
{% csrf_token %} |
|
16 |
{{ form.as_p }} |
|
17 |
<div class="buttons"> |
|
18 |
<button class="submit-button">{% trans "Send" %}</button> |
|
19 |
<a href="{% url "view_announce" pk=object.pk %}" class="cancel">{% trans "Cancel" %}</a> |
|
20 |
</div> |
|
21 |
</form> |
|
22 |
{% endblock %} |
corbo/templates/corbo/test_email_send_form.html | ||
---|---|---|
1 |
{% extends "corbo/manage.html" %} |
|
2 |
{% load i18n static %} |
|
3 |
|
|
4 |
{% block breadcrumb %} |
|
5 |
{{ block.super }} |
|
6 |
<a href="{% url "view_announce" pk=object.pk %}">{{ object.title }}</a> |
|
7 |
{% endblock %} |
|
8 |
|
|
9 |
{% block appbar %} |
|
10 |
<h2>{% trans "Send test email" %}</h2> |
|
11 |
{% endblock %} |
|
12 |
|
|
13 |
|
|
14 |
{% block content %} |
|
15 |
<form method="post"> |
|
16 |
{% csrf_token %} |
|
17 |
{{ form.as_p }} |
|
18 |
<div class="buttons"> |
|
19 |
<button class="submit-button">{% trans "Send" %}</button> |
|
20 |
<a href="{% url "view_announce" pk=object.pk %}" class="cancel">{% trans "Cancel" %}</a> |
|
21 |
</div> |
|
22 |
</form> |
|
23 |
{% endblock %} |
corbo/views.py | ||
---|---|---|
20 | 20 |
|
21 | 21 |
import models |
22 | 22 |
from .forms import AnnounceForm, CategoryForm, SubscriptionsImportForm, \ |
23 |
SendTestEmailForm |
|
23 |
SendTestEmailForm, SendTestSMSForm
|
|
24 | 24 |
from . import utils |
25 | 25 |
|
26 | 26 |
try: |
... | ... | |
264 | 264 |
context = super(AnnounceView, self).get_context_data(**kwargs) |
265 | 265 |
context['category'] = self.object.category |
266 | 266 |
context['broadcasts'] = self.object.broadcast_set.filter(deliver_time__isnull=False) |
267 |
context['sms_enabled'] = settings.SMS_GATEWAY_URL |
|
267 | 268 |
return context |
268 | 269 |
|
269 | 270 |
view_announce = AnnounceView.as_view() |
270 | 271 |
|
271 | 272 |
|
272 |
class EmailAnnounceView(FormView): |
|
273 |
form_class = SendTestEmailForm |
|
274 |
template_name = 'corbo/test_email_send_form.html' |
|
275 |
|
|
273 |
class SendAnnounceView(FormView): |
|
276 | 274 |
def get_initial(self): |
277 |
return {'email': self.request.user.email} |
|
275 |
return {'email': self.request.user.email, |
|
276 |
'mobile': self.request.session.get('mellon_session', {}).get('mobile', '')} |
|
278 | 277 |
|
279 | 278 |
def get_success_url(self, *args, **kwargs): |
280 | 279 |
return reverse('view_announce', kwargs={'pk': self.kwargs['pk']}) |
281 | 280 |
|
281 |
def get_context_data(self, **kwargs): |
|
282 |
context = super(SendAnnounceView, self).get_context_data(**kwargs) |
|
283 |
context['object'] = models.Announce.objects.get(pk=self.kwargs['pk']) |
|
284 |
return context |
|
285 |
|
|
286 |
|
|
287 |
class EmailAnnounceView(SendAnnounceView): |
|
288 |
form_class = SendTestEmailForm |
|
289 |
template_name = 'corbo/email_test_announce_form.html' |
|
290 |
|
|
282 | 291 |
def form_valid(self, form): |
283 | 292 |
email = form.cleaned_data['email'] |
284 | 293 |
announce = models.Announce.objects.get(pk=self.kwargs['pk']) |
... | ... | |
286 | 295 |
messages.info(self.request, _('Email successfully sent')) |
287 | 296 |
return super(EmailAnnounceView, self).form_valid(form) |
288 | 297 |
|
289 |
def get_context_data(self, **kwargs): |
|
290 |
context = super(EmailAnnounceView, self).get_context_data(**kwargs) |
|
291 |
context['object'] = models.Announce.objects.get(pk=self.kwargs['pk']) |
|
292 |
return context |
|
293 |
|
|
294 | 298 |
email_announce = EmailAnnounceView.as_view() |
295 | 299 |
|
300 |
|
|
301 |
class SMSAnnounceView(SendAnnounceView): |
|
302 |
form_class = SendTestSMSForm |
|
303 |
template_name = 'corbo/sms_test_announce_form.html' |
|
304 |
|
|
305 |
def form_valid(self, form): |
|
306 |
mobile = form.cleaned_data['mobile'] |
|
307 |
announce = models.Announce.objects.get(pk=self.kwargs['pk']) |
|
308 |
sms_sent = utils.send_sms(announce.text, [mobile]) |
|
309 |
if sms_sent == 1: |
|
310 |
messages.info(self.request, _('SMS successfully sent')) |
|
311 |
else: |
|
312 |
messages.error(self.request, _('Error occured while sending SMS')) |
|
313 |
return super(SMSAnnounceView, self).form_valid(form) |
|
314 |
|
|
315 |
sms_announce = SMSAnnounceView.as_view() |
|
316 |
|
|
317 |
|
|
296 | 318 |
def menu_json(request): |
297 | 319 |
label = _('Announces') |
298 | 320 |
json_str = json.dumps([{'label': force_text(label), |
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)