diff --git a/src/postorius/tests/mailman_api_tests/test_profile.py b/src/postorius/tests/mailman_api_tests/test_profile.py new file mode 100644 index 0000000..c54d232 --- /dev/null +++ b/src/postorius/tests/mailman_api_tests/test_profile.py @@ -0,0 +1,96 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2012-2015 by the Free Software Foundation, Inc. +# +# This file is part of Postorius. +# +# Postorius is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# Postorius is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# Postorius. If not, see . + +import logging + +from django.core.urlresolvers import reverse +from django.contrib.auth.models import User +from django.test import Client, TestCase +try: + from urllib2 import HTTPError +except ImportError: + from urllib.error import HTTPError + +from postorius.utils import get_client +from postorius.tests import MM_VCR +from postorius.models import AddressConfirmationProfile +from mock import patch, call +from smtplib import SMTPException + + +logger = logging.getLogger(__name__) +vcr_log = logging.getLogger('vcr') +vcr_log.setLevel(logging.WARNING) + + +class TestAddressActivationView(TestCase): + """ + Tests to make sure the view is properly connected, renders the form + correctly and starts the actual address activation process if a valid + form is submitted. + """ + + def setUp(self): + # We create a new user and log that user in. + # We don't use Client().login because it triggers the browserid dance. + self.user = User.objects.create_user( + username='les', email='les@example.org', password='secret') + self.client.post(reverse('user_login'), + {'username': 'les', 'password': 'secret'}) + + def tearDown(self): + # Log out and delete user. + self.client.logout() + self.user.delete() + + @MM_VCR.use_cassette('test_user_profile.yaml') + def test_view_contains_form(self): + # The view context should contain a form. + response = self.client.get(reverse('user_profile')) + self.assertContains(response, 'You can add other addresses to your profile') + + @MM_VCR.use_cassette('test_user_profile.yaml') + def test_post_invalid_form_shows_error_msg(self): + # Entering an invalid email address should render an error message. + response = self.client.post(reverse('user_profile'), { + 'email': 'invalid_email', + 'user_email': self.user.email}) + self.assertContains(response, 'Enter a valid email address.') + + @MM_VCR.use_cassette('test_user_profile.yaml') + @patch.object(AddressConfirmationProfile, 'send_confirmation_link') + def test_post_valid_form_shows_success_message( + self, mock_send_confirmation_link): + # Entering a valid email should render the activation_sent template. + response = self.client.post(reverse('user_profile'), { + 'email': 'new_address@example.org', + 'user_email': self.user.email}) + self.assertEqual(mock_send_confirmation_link.call_count, 1) + self.assertContains(response, + 'Please follow the instructions sent via email to confirm the address') + + @patch.object(AddressConfirmationProfile, 'send_confirmation_link', + side_effect=SMTPException()) + @MM_VCR.use_cassette('test_user_profile.yaml') + def test_post_form_with_smtp_exception(self, mock_send_confirmation_link): + # If a smtp exception occurs display error + response = self.client.post(reverse('user_profile'), { + 'email': 'new_address@example.org', + 'user_email': self.user.email}, follow=True) + self.assertEqual(mock_send_confirmation_link.call_count, 1) + self.assertContains(response, 'Currently emails can not be added, please try again later') + diff --git a/src/postorius/tests/test_address_activation.py b/src/postorius/tests/test_address_activation.py index 2efedf7..3a96f32 100644 --- a/src/postorius/tests/test_address_activation.py +++ b/src/postorius/tests/test_address_activation.py @@ -15,7 +15,7 @@ from postorius.forms import AddressActivationForm from postorius.models import AddressConfirmationProfile from postorius import views -from postorius.views.user import AddressActivationView, address_activation_link +from postorius.views.user import address_activation_link class TestAddressActivationForm(TestCase): @@ -48,61 +48,6 @@ self.assertFalse(form.is_valid()) -class TestAddressActivationView(TestCase): - """ - Tests to make sure the view is properly connected, renders the form - correctly and starts the actual address activation process if a valid - form is submitted. - """ - - def setUp(self): - # We create a new user and log that user in. - # We don't use Client().login because it triggers the browserid dance. - self.user = User.objects.create_user( - username='les', email='les@example.org', password='secret') - self.client.post(reverse('user_login'), - {'username': 'les', 'password': 'secret'}) - - def tearDown(self): - # Log out and delete user. - self.client.logout() - self.user.delete() - - def test_view_is_connected(self): - # The view should be connected in the url configuration. - response = self.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 = self.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 = self.client.get(reverse('address_activation')) - self.assertIn('postorius/user/address_activation.html', - [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 = self.client.post(reverse('address_activation'), { - 'email': 'invalid_email', - 'user_email': self.user.email}) - self.assertTrue('Enter a valid email address.' in response.content) - - @patch.object(AddressConfirmationProfile, 'send_confirmation_link') - def test_post_valid_form_renders_success_template( - self, mock_send_confirmation_link): - # Entering a valid email should render the activation_sent template. - response = self.client.post(reverse('address_activation'), { - 'email': 'new_address@example.org', - 'user_email': self.user.email}) - self.assertEqual(mock_send_confirmation_link.call_count, 1) - self.assertIn('postorius/user/address_activation_sent.html', - [t.name for t in response.templates]) - - class TestAddressConfirmationProfile(TestCase): """ Test the confirmation of an email address activation (validating token,