Newer
Older
postorius / src / postorius / tests / mailman_api_tests / test_profile.py
@Aurélien Bompard Aurélien Bompard on 11 Aug 2016 4 KB Namespace the user profile view name
# -*- 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 <http://www.gnu.org/licenses/>.

from __future__ import absolute_import, print_function, unicode_literals

from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from mock import patch
from smtplib import SMTPException

from postorius.models import MailmanUser
from postorius.models import AddressConfirmationProfile
from postorius.tests.utils import ViewTestCase


class TestProfile(ViewTestCase):
    """
    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):
        super(TestProfile, self).setUp()
        # 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 test_view_contains_form(self):
        # The view context should contain a form.
        response = self.client.get(reverse('ps_user_profile'))
        self.assertContains(response,
                            'You can add other addresses to your profile')

    def test_post_invalid_form_shows_error_msg(self):
        # Entering an invalid email address should render an error message.
        response = self.client.post(reverse('ps_user_profile'),
                                    {'email': 'invalid_email',
                                     'user_email': self.user.email})
        self.assertContains(response, 'Enter a valid email address.')

    @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('ps_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, 'Please follow the instructions sent '
                                      'via email to confirm the address')

    @patch.object(AddressConfirmationProfile, 'send_confirmation_link')
    def test_post_valid_form_redirects_on_success(
            self, mock_send_confirmation_link):
        # Entering a valid email should render the activation_sent template.
        response = self.client.post(reverse('ps_user_profile'), {
                                    'email': 'new_address@example.org',
                                    'user_email': self.user.email})
        self.assertEqual(mock_send_confirmation_link.call_count, 1)
        self.assertRedirects(response, reverse('ps_user_profile'))

    @patch.object(AddressConfirmationProfile, 'send_confirmation_link',
                  side_effect=SMTPException())
    def test_post_form_with_smtp_exception(self, mock_send_confirmation_link):
        # If a smtp exception occurs display error
        response = self.client.post(reverse('ps_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')

    def test_change_display_name(self):
        # We create a Mailman user, from the django user object.
        self.mm_user = MailmanUser.objects.create_from_django(self.user)
        self.client.post(reverse('ps_user_profile'), {
                         'formname': 'displayname',
                         'display_name': 'testname'})
        # The Mailman user's display name, must have been changed correctly.
        self.assertEqual(self.mm_user.display_name, 'testname')