diff --git a/src/postorius/forms.py b/src/postorius/forms.py index 20c897d..a4a9760 100644 --- a/src/postorius/forms.py +++ b/src/postorius/forms.py @@ -902,3 +902,7 @@ class ListDeleteForm(forms.Form): list_name = forms.EmailField(widget=forms.HiddenInput()) + + +class AddressActivationForm(forms.Form): + email = forms.EmailField() diff --git a/src/postorius/templates/postorius/user_address_activation.html b/src/postorius/templates/postorius/user_address_activation.html new file mode 100644 index 0000000..53e8c0d --- /dev/null +++ b/src/postorius/templates/postorius/user_address_activation.html @@ -0,0 +1,13 @@ +{% extends postorius_base_template %} +{% load url from future %} +{% load i18n %} +{% block main %} +{% include 'postorius/menu/user_nav.html' %} + +

{% trans 'Add another email address to your account' %}

+
+ {% csrf_token %} + {{ form.as_p }} + +
+{% endblock main %} diff --git a/src/postorius/templates/postorius/user_address_activation_sent.html b/src/postorius/templates/postorius/user_address_activation_sent.html new file mode 100644 index 0000000..58c6d09 --- /dev/null +++ b/src/postorius/templates/postorius/user_address_activation_sent.html @@ -0,0 +1,10 @@ +{% extends postorius_base_template %} +{% load url from future %} +{% load i18n %} +{% block main %} +{% include 'postorius/menu/user_nav.html' %} + +

{% trans 'Email address activation sent' %}

+

A confirmation link has been sent to the email address you submitted. Please check your email account an click on the confirmation link to add this address for your account.

+{% endblock main %} + diff --git a/src/postorius/tests/test_address_activation.py b/src/postorius/tests/test_address_activation.py new file mode 100644 index 0000000..d8c9167 --- /dev/null +++ b/src/postorius/tests/test_address_activation.py @@ -0,0 +1,67 @@ +from __future__ import (absolute_import, division, print_function, + unicode_literals) + + +from django.conf import settings +from django.core.urlresolvers import reverse +from django.test.client import Client, RequestFactory +from django.test.utils import override_settings +from django.utils import unittest +from mock import patch + +from postorius.forms import AddressActivationForm +from postorius.views.user import AddressActivationView + + +class TestAddressActivationForm(unittest.TestCase): + + def test_valid_email_is_valid(self): + form = AddressActivationForm({'email': 'les@example.org'}) + self.assertTrue(form.is_valid()) + + def test_invalid_email_is_not_valid(self): + form = AddressActivationForm({'email': 'les@example'}) + self.assertFalse(form.is_valid()) + + +class TestAddressActivationView(unittest.TestCase): + + def test_view_is_connected(self): + # The view should be connected in the url configuration. + response = Client().get(reverse('address_activation')) + self.assertEqual(response.status_code, 200) + + def test_view_contains_form(self): + # The view context should contain a form. + response = Client().get(reverse('address_activation')) + self.assertTrue('form' in response.context) + + def test_view_renders_correct_template(self): + # The view should render the user_address_activation template. + response = Client().get(reverse('address_activation')) + self.assertTrue('postorius/user_address_activation.html' + in [t.name for t in response.templates]) + + def test_post_invalid_form_shows_error_msg(self): + # Entering an invalid email address should render an error message. + response = Client().post(reverse('address_activation'), + {'email': 'invalid_email'}) + self.assertTrue('Enter a valid email address.' in + response.content) + + @patch.object(AddressActivationView, '_handle_address') + def test_post_valid_form_renders_success_template(self, handle_address_mock): + # Entering a valid email should render the activation_sent template. + response = Client().post(reverse('address_activation'), + {'email': 'new_address@example.org'}) + self.assertTrue('postorius/user_address_activation_sent.html' + in [t.name for t in response.templates]) + + @patch.object(AddressActivationView, '_handle_address') + def test_post_valid_form_calls_handle_address_method(self, handle_address_mock): + # Entering a valid email should render the activation_sent template. + response = Client().post(reverse('address_activation'), + {'email': 'new_address@example.org'}) + self.assertEqual(handle_address_mock.call_count, 1) + args, kwargs = handle_address_mock.call_args + self.assertTrue('new_address@example.org' in args) diff --git a/src/postorius/urls.py b/src/postorius/urls.py index df63a87..0f2bdb0 100644 --- a/src/postorius/urls.py +++ b/src/postorius/urls.py @@ -108,4 +108,7 @@ url(r'^more_info/(?P[^/]+)/(?P[^/]+)$', 'more_info_tab', name='more_info_tab'), url(r'^lists/(?P[^/]+)/', include(per_list_urlpatterns)), + url(r'^users/address_activation/$', + AddressActivationView.as_view(), + name='address_activation'), ) diff --git a/src/postorius/views/user.py b/src/postorius/views/user.py index f9430f4..7f97421 100644 --- a/src/postorius/views/user.py +++ b/src/postorius/views/user.py @@ -39,6 +39,7 @@ from django.template import Context, loader, RequestContext from django.utils.decorators import method_decorator from django.utils.translation import gettext as _ +from django.views.generic import TemplateView from urllib2 import HTTPError from postorius import utils @@ -231,6 +232,28 @@ context_instance=RequestContext(request)) +class AddressActivationView(TemplateView): + + def _handle_address(self, address): + pass + + def get(self, request): + form = AddressActivationForm() + return render_to_response('postorius/user_address_activation.html', + {'form': form}, + context_instance=RequestContext(request)) + + def post(self, request): + form = AddressActivationForm(request.POST) + if form.is_valid(): + self._handle_address(request.POST['email']) + return render_to_response('postorius/user_address_activation_sent.html', + context_instance=RequestContext(request)) + return render_to_response('postorius/user_address_activation.html', + {'form': form}, + context_instance=RequestContext(request)) + + @user_passes_test(lambda u: u.is_superuser) def user_index(request, page=1, template='postorius/users/index.html'): """Show a table of all users.