test_activate_account.py 7.99 KB
Newer Older
1 2
"""Tests for account activation"""
import unittest
3
from uuid import uuid4
4 5

from django.conf import settings
6
from django.core.urlresolvers import reverse
7 8
from django.test import TestCase, override_settings
from mock import patch
9

10
from edxmako.shortcuts import render_to_string
11
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
12
from student.models import Registration
13
from student.tests.factories import UserFactory
14 15 16 17 18 19 20 21 22 23


@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
class TestActivateAccount(TestCase):
    """Tests for account creation"""

    def setUp(self):
        super(TestActivateAccount, self).setUp()
        self.username = "jack"
        self.email = "jack@fake.edx.org"
24 25 26 27
        self.password = "test-password"
        self.user = UserFactory.create(
            username=self.username, email=self.email, password=self.password, is_active=False,
        )
28 29 30 31 32 33

        # Set Up Registration
        self.registration = Registration()
        self.registration.register(self.user)
        self.registration.save()

34 35
        self.platform_name = configuration_helpers.get_value('PLATFORM_NAME', settings.PLATFORM_NAME)
        self.activation_email_support_link = configuration_helpers.get_value(
36 37
            'ACTIVATION_EMAIL_SUPPORT_LINK', settings.ACTIVATION_EMAIL_SUPPORT_LINK
        ) or settings.SUPPORT_SITE_LINK
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    def login(self):
        """
        Login with test user.

        Since, only active users can login, so we must activate the user before login.
        This method does the following tasks in order,
            1. Stores user's active/in-active status in a variable.
            2. Makes sure user account is active.
            3. Authenticated user with the client.
            4. Reverts user's original active/in-active status.
        """
        is_active = self.user.is_active

        # Make sure user is active before login
        self.user.is_active = True
        self.user.save()
        self.client.login(username=self.username, password=self.password)

        # Revert user activation status
        self.user.is_active = is_active
        self.user.save()

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    def assert_no_tracking(self, mock_segment_identify):
        """ Assert that activate sets the flag but does not call segment. """
        # Ensure that the user starts inactive
        self.assertFalse(self.user.is_active)

        # Until you explicitly activate it
        self.registration.activate()
        self.assertTrue(self.user.is_active)
        self.assertFalse(mock_segment_identify.called)

    @override_settings(
        LMS_SEGMENT_KEY="testkey",
        MAILCHIMP_NEW_USER_LIST_ID="listid"
    )
    @patch('student.models.analytics.identify')
    def test_activation_with_keys(self, mock_segment_identify):
        expected_segment_payload = {
            'email': self.email,
            'username': self.username,
            'activated': 1,
        }
        expected_segment_mailchimp_list = {
            "MailChimp": {
                "listId": settings.MAILCHIMP_NEW_USER_LIST_ID
            }
        }

        # Ensure that the user starts inactive
        self.assertFalse(self.user.is_active)

        # Until you explicitly activate it
        self.registration.activate()
        self.assertTrue(self.user.is_active)
        mock_segment_identify.assert_called_with(
            self.user.id,
            expected_segment_payload,
            expected_segment_mailchimp_list
        )

    @override_settings(LMS_SEGMENT_KEY="testkey")
    @patch('student.models.analytics.identify')
    def test_activation_without_mailchimp_key(self, mock_segment_identify):
        self.assert_no_tracking(mock_segment_identify)

    @override_settings(MAILCHIMP_NEW_USER_LIST_ID="listid")
    @patch('student.models.analytics.identify')
    def test_activation_without_segment_key(self, mock_segment_identify):
        self.assert_no_tracking(mock_segment_identify)

    @patch('student.models.analytics.identify')
    def test_activation_without_keys(self, mock_segment_identify):
        self.assert_no_tracking(mock_segment_identify)
113 114 115 116 117 118 119 120 121 122 123 124 125

    @override_settings(FEATURES=dict(settings.FEATURES, DISPLAY_ACCOUNT_ACTIVATION_MESSAGE_ON_SIDEBAR=True))
    def test_account_activation_message(self):
        """
        Verify that account correct activation message is displayed.

        If logged in user has not activated his/her account, make sure that an
        account activation message is displayed on dashboard sidebar.
        """
        # Log in with test user.
        self.login()
        expected_message = render_to_string(
            'registration/account_activation_sidebar_notice.html',
126 127 128 129 130
            {
                'email': self.user.email,
                'platform_name': self.platform_name,
                'activation_email_support_link': self.activation_email_support_link
            }
131 132 133 134 135 136 137 138 139 140 141
        )

        response = self.client.get(reverse('dashboard'))
        self.assertContains(response, expected_message, html=True)

        # Now make sure account activation message goes away when user activated the account
        self.user.is_active = True
        self.user.save()
        self.login()
        expected_message = render_to_string(
            'registration/account_activation_sidebar_notice.html',
142 143 144 145 146
            {
                'email': self.user.email,
                'platform_name': self.platform_name,
                'activation_email_support_link': self.activation_email_support_link
            }
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        )
        response = self.client.get(reverse('dashboard'))
        self.assertNotContains(response, expected_message, html=True)

    @override_settings(FEATURES=dict(settings.FEATURES, DISPLAY_ACCOUNT_ACTIVATION_MESSAGE_ON_SIDEBAR=False))
    def test_account_activation_message_disabled(self):
        """
        Verify that old account activation message is displayed when
        DISPLAY_ACCOUNT_ACTIVATION_MESSAGE_ON_SIDEBAR is disabled.
        """
        # Log in with test user.
        self.login()
        expected_message = render_to_string(
            'registration/activate_account_notice.html',
            {'email': self.user.email}
        )

        response = self.client.get(reverse('dashboard'))
        self.assertContains(response, expected_message, html=True)

        # Now make sure account activation message goes away when user activated the account
        self.user.is_active = True
        self.user.save()
        self.login()
        expected_message = render_to_string(
            'registration/activate_account_notice.html',
            {'email': self.user.email}
        )
        response = self.client.get(reverse('dashboard'))
        self.assertNotContains(response, expected_message, html=True)
177 178 179 180 181 182 183 184 185 186 187 188 189

    def test_account_activation_notification_on_logistration(self):
        """
        Verify that logistration page displays success/error/info messages
        about account activation.
        """
        login_page_url = "{login_url}?next={redirect_url}".format(
            login_url=reverse('signin_user'),
            redirect_url=reverse('dashboard'),
        )
        # Access activation link, message should say that account has been activated.
        response = self.client.get(reverse('activate', args=[self.registration.activation_key]), follow=True)
        self.assertRedirects(response, login_page_url)
190
        self.assertContains(response, 'Success! You have activated your account.')
191 192 193 194 195 196 197 198 199 200 201

        # Access activation link again, message should say that account is already active.
        response = self.client.get(reverse('activate', args=[self.registration.activation_key]), follow=True)
        self.assertRedirects(response, login_page_url)
        self.assertContains(response, 'This account has already been activated.')

        # Open account activation page with an invalid activation link,
        # there should be an error message displayed.
        response = self.client.get(reverse('activate', args=[uuid4().hex]), follow=True)
        self.assertRedirects(response, login_page_url)
        self.assertContains(response, 'Your account could not be activated')