manager: add test sms send (#20174)
This commit is contained in:
parent
d0bd7a5a38
commit
8e8b10aeb5
|
@ -81,3 +81,7 @@ class SubscriptionsImportForm(forms.Form):
|
|||
|
||||
class SendTestEmailForm(forms.Form):
|
||||
email = forms.EmailField()
|
||||
|
||||
|
||||
class SendTestSMSForm(forms.Form):
|
||||
mobile = forms.CharField(label=_('Mobile number'))
|
||||
|
|
|
@ -2,7 +2,8 @@ from django.conf.urls import patterns, include, url
|
|||
|
||||
from .views import add_announce, edit_announce, delete_announce, \
|
||||
add_category, edit_category, view_category, delete_category, manage, \
|
||||
subscriptions_import, view_announce, email_announce, menu_json
|
||||
subscriptions_import, view_announce, email_announce, sms_announce, \
|
||||
menu_json
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^$', manage, name='manage'),
|
||||
|
@ -14,6 +15,8 @@ urlpatterns = patterns('',
|
|||
name='delete_announce'),
|
||||
url(r'^announce/email/(?P<pk>\d+)/$', email_announce,
|
||||
name='email_announce'),
|
||||
url(r'^announce/sms/(?P<pk>\d+)/$', sms_announce,
|
||||
name='sms_announce'),
|
||||
url(r'^category/(?P<slug>[\w-]+)/$', view_category,
|
||||
name='view_category'),
|
||||
url(r'^announce/(?P<pk>\d+)/$', view_announce,
|
||||
|
|
|
@ -12,6 +12,9 @@
|
|||
<a href="{% url 'delete_announce' pk=object.pk %}" rel="popup">{% trans 'Delete' %}</a>
|
||||
<a href="{% url 'edit_announce' pk=object.pk %}">{% trans 'Edit' %}</a>
|
||||
<a href="{% url 'email_announce' pk=object.pk %}" rel="popup">{% trans 'Send test email' %}</a>
|
||||
{% if sms_enabled %}
|
||||
<a href="{% url 'sms_announce' pk=object.pk %}" rel="popup">{% trans 'Send test SMS' %}</a>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
{% block content %}
|
||||
<div class="announce_block">
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
{% extends "corbo/manage.html" %}
|
||||
{% load i18n static %}
|
||||
|
||||
{% block breadcrumb %}
|
||||
{{ block.super }}
|
||||
<a href="{% url "view_announce" pk=object.pk %}">{{ object.title }}</a>
|
||||
{% endblock %}
|
||||
|
||||
{% block appbar %}
|
||||
<h2>{% trans "Send test SMS" %}</h2>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<div class="buttons">
|
||||
<button class="submit-button">{% trans "Send" %}</button>
|
||||
<a href="{% url "view_announce" pk=object.pk %}" class="cancel">{% trans "Cancel" %}</a>
|
||||
</div>
|
||||
</form>
|
||||
{% endblock %}
|
|
@ -20,7 +20,7 @@ from django.utils.translation import ngettext
|
|||
|
||||
import models
|
||||
from .forms import AnnounceForm, CategoryForm, SubscriptionsImportForm, \
|
||||
SendTestEmailForm
|
||||
SendTestEmailForm, SendTestSMSForm
|
||||
from . import utils
|
||||
|
||||
try:
|
||||
|
@ -264,21 +264,30 @@ class AnnounceView(DetailView):
|
|||
context = super(AnnounceView, self).get_context_data(**kwargs)
|
||||
context['category'] = self.object.category
|
||||
context['broadcasts'] = self.object.broadcast_set.filter(deliver_time__isnull=False)
|
||||
context['sms_enabled'] = settings.SMS_GATEWAY_URL
|
||||
return context
|
||||
|
||||
view_announce = AnnounceView.as_view()
|
||||
|
||||
|
||||
class EmailAnnounceView(FormView):
|
||||
form_class = SendTestEmailForm
|
||||
template_name = 'corbo/test_email_send_form.html'
|
||||
|
||||
class SendAnnounceView(FormView):
|
||||
def get_initial(self):
|
||||
return {'email': self.request.user.email}
|
||||
return {'email': self.request.user.email,
|
||||
'mobile': self.request.session.get('mellon_session', {}).get('mobile', '')}
|
||||
|
||||
def get_success_url(self, *args, **kwargs):
|
||||
return reverse('view_announce', kwargs={'pk': self.kwargs['pk']})
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(SendAnnounceView, self).get_context_data(**kwargs)
|
||||
context['object'] = models.Announce.objects.get(pk=self.kwargs['pk'])
|
||||
return context
|
||||
|
||||
|
||||
class EmailAnnounceView(SendAnnounceView):
|
||||
form_class = SendTestEmailForm
|
||||
template_name = 'corbo/email_test_announce_form.html'
|
||||
|
||||
def form_valid(self, form):
|
||||
email = form.cleaned_data['email']
|
||||
announce = models.Announce.objects.get(pk=self.kwargs['pk'])
|
||||
|
@ -286,13 +295,26 @@ class EmailAnnounceView(FormView):
|
|||
messages.info(self.request, _('Email successfully sent'))
|
||||
return super(EmailAnnounceView, self).form_valid(form)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(EmailAnnounceView, self).get_context_data(**kwargs)
|
||||
context['object'] = models.Announce.objects.get(pk=self.kwargs['pk'])
|
||||
return context
|
||||
|
||||
email_announce = EmailAnnounceView.as_view()
|
||||
|
||||
|
||||
class SMSAnnounceView(SendAnnounceView):
|
||||
form_class = SendTestSMSForm
|
||||
template_name = 'corbo/sms_test_announce_form.html'
|
||||
|
||||
def form_valid(self, form):
|
||||
mobile = form.cleaned_data['mobile']
|
||||
announce = models.Announce.objects.get(pk=self.kwargs['pk'])
|
||||
sms_sent = utils.send_sms(announce.text, [mobile])
|
||||
if sms_sent == 1:
|
||||
messages.info(self.request, _('SMS successfully sent'))
|
||||
else:
|
||||
messages.error(self.request, _('Error occured while sending SMS'))
|
||||
return super(SMSAnnounceView, self).form_valid(form)
|
||||
|
||||
sms_announce = SMSAnnounceView.as_view()
|
||||
|
||||
|
||||
def menu_json(request):
|
||||
label = _('Announces')
|
||||
json_str = json.dumps([{'label': force_text(label),
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
import logging
|
||||
import mock
|
||||
import os
|
||||
import pytest
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import override_settings
|
||||
|
||||
from corbo.models import Broadcast
|
||||
|
||||
|
@ -209,7 +212,7 @@ def test_delete_announce(app, admin_user):
|
|||
assert resp.status_int == 302
|
||||
assert resp.location == 'http://testserver/manage/category/alerts/'
|
||||
|
||||
def test_send_announce(app, admin_user):
|
||||
def test_email_announce(app, admin_user):
|
||||
app = login(app)
|
||||
resp = app.get('/manage/')
|
||||
assert 'New category' in resp.content
|
||||
|
@ -233,6 +236,7 @@ def test_send_announce(app, admin_user):
|
|||
assert 'First announce' in resp.content
|
||||
resp = resp.click('First announce')
|
||||
assert 'Send test email' in resp.content
|
||||
assert 'Send test SMS' not in resp.content
|
||||
resp = resp.click('Send test email')
|
||||
send_form = resp.forms[0]
|
||||
assert send_form.method == 'post'
|
||||
|
@ -243,3 +247,98 @@ def test_send_announce(app, admin_user):
|
|||
resp = send_form.submit()
|
||||
assert resp.status_int == 302
|
||||
assert resp.location == 'http://testserver/manage/announce/1/'
|
||||
|
||||
@mock.patch('corbo.utils.requests.post')
|
||||
def test_sms_announce(mocked_post, app, admin_user, settings):
|
||||
app = login(app)
|
||||
resp = app.get('/manage/')
|
||||
assert 'New category' in resp.content
|
||||
category_page = resp.click('New category')
|
||||
category_form = category_page.forms[0]
|
||||
category_form['name'] = 'Alerts'
|
||||
resp = category_form.submit()
|
||||
assert resp.status_int == 302
|
||||
assert resp.location.endswith(reverse('manage'))
|
||||
resp = resp.follow()
|
||||
resp = resp.click('Alerts')
|
||||
|
||||
# create new announce
|
||||
assert 'New announce' in resp.content
|
||||
announce_page = resp.click('New announce')
|
||||
announce_form = announce_page.forms[0]
|
||||
announce_form['title'] = 'First announce'
|
||||
announce_form['text'] = 'announce content'
|
||||
resp = announce_form.submit()
|
||||
assert resp.status_int == 302
|
||||
assert resp.location.endswith(reverse('view_category', kwargs={'slug': 'alerts'}))
|
||||
resp = resp.follow()
|
||||
|
||||
# view announce
|
||||
assert 'First announce' in resp.content
|
||||
settings.SMS_GATEWAY_URL = 'http:/passerelle.com'
|
||||
resp = resp.click('First announce')
|
||||
assert 'Send test SMS' in resp.content
|
||||
|
||||
# open send sms form
|
||||
resp = resp.click('Send test SMS')
|
||||
send_form = resp.forms[0]
|
||||
assert 'mobile' in send_form.fields
|
||||
assert send_form.fields['mobile'][0].value == ''
|
||||
# submit with no mobile
|
||||
resp = send_form.submit()
|
||||
assert resp.status_int == 200
|
||||
|
||||
form = resp.forms[0]
|
||||
form['mobile'] = '0607080900'
|
||||
# simulate response from passerelle
|
||||
mocked_response = mock.Mock()
|
||||
mocked_response.json.return_value = {'err': 0, 'data': True}
|
||||
mocked_post.return_value = mocked_response
|
||||
resp = form.submit()
|
||||
assert resp.location.endswith(reverse('view_announce', kwargs={'pk': 1}))
|
||||
resp = resp.follow()
|
||||
# make sure the form informs about the success
|
||||
assert 'SMS successfully sent' in resp.content
|
||||
|
||||
resp = resp.click('Send test SMS')
|
||||
form = resp.forms[0]
|
||||
form['mobile'] = '0607080900'
|
||||
# simulate error from passerelle
|
||||
mocked_response.json.return_value = {'err': 1, 'data': None, 'err_desc': 'Destination error'}
|
||||
resp = form.submit()
|
||||
resp = resp.follow()
|
||||
assert 'Error occured while sending SMS' in resp.content
|
||||
|
||||
def test_sms_announce_with_invalid_gateway_url(app, admin_user, settings, caplog):
|
||||
app = login(app)
|
||||
resp = app.get('/manage/')
|
||||
assert 'New category' in resp.content
|
||||
category_page = resp.click('New category')
|
||||
category_form = category_page.forms[0]
|
||||
category_form['name'] = 'Alerts'
|
||||
resp = category_form.submit()
|
||||
resp = resp.follow()
|
||||
resp = resp.click('Alerts')
|
||||
assert 'New announce' in resp.content
|
||||
announce_page = resp.click('New announce')
|
||||
announce_form = announce_page.forms[0]
|
||||
announce_form['title'] = 'First announce'
|
||||
announce_form['text'] = 'announce content'
|
||||
resp = announce_form.submit()
|
||||
assert resp.status_int == 302
|
||||
assert resp.location == 'http://testserver/manage/category/alerts/'
|
||||
resp = resp.follow()
|
||||
assert 'First announce' in resp.content
|
||||
settings.SMS_GATEWAY_URL='invalid_url'
|
||||
resp = resp.click('First announce')
|
||||
assert 'Send test SMS' in resp.content
|
||||
resp = resp.click('Send test SMS')
|
||||
form = resp.forms[0]
|
||||
form['mobile'] = '0607080900'
|
||||
resp = form.submit()
|
||||
records = caplog.records
|
||||
assert len(records) == 1
|
||||
for record in records:
|
||||
assert record.name == 'corbo.utils'
|
||||
assert record.levelno == logging.WARNING
|
||||
assert 'Invalid URL' in record.getMessage()
|
||||
|
|
Reference in New Issue