~ubuntu-branches/ubuntu/utopic/python-django-registration/utopic-proposed

« back to all changes in this revision

Viewing changes to .pc/fix-test-suite.patch/registration/tests/models.py

  • Committer: Package Import Robot
  • Author(s): Raphaël Hertzog
  • Date: 2014-08-06 23:46:28 UTC
  • Revision ID: package-import@ubuntu.com-20140806234628-k1lgla90d5swvm6m
Tags: 1.0+dfsg-2
* Fix and enable the test suite with fix-testsuite.patch. Ensure
  it works with Django 1.6.
* Apply two patches for Django 1.7 compatibility:
  - django-1.7-compat.patch grabbed in an upstream pull request
  - more-django-1.7-fixes.patch authored by myself
  Closes: #755653

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import datetime
 
2
import re
 
3
 
 
4
from django.conf import settings
 
5
from django.contrib.auth.models import User
 
6
from django.contrib.sites.models import Site
 
7
from django.core import mail
 
8
from django.core import management
 
9
from django.test import TestCase
 
10
from django.utils.hashcompat import sha_constructor
 
11
 
 
12
from registration.models import RegistrationProfile
 
13
 
 
14
 
 
15
class RegistrationModelTests(TestCase):
 
16
    """
 
17
    Test the model and manager used in the default backend.
 
18
    
 
19
    """
 
20
    user_info = {'username': 'alice',
 
21
                 'password': 'swordfish',
 
22
                 'email': 'alice@example.com'}
 
23
    
 
24
    def setUp(self):
 
25
        self.old_activation = getattr(settings, 'ACCOUNT_ACTIVATION_DAYS', None)
 
26
        settings.ACCOUNT_ACTIVATION_DAYS = 7
 
27
 
 
28
    def tearDown(self):
 
29
        settings.ACCOUNT_ACTIVATION_DAYS = self.old_activation
 
30
 
 
31
    def test_profile_creation(self):
 
32
        """
 
33
        Creating a registration profile for a user populates the
 
34
        profile with the correct user and a SHA1 hash to use as
 
35
        activation key.
 
36
        
 
37
        """
 
38
        new_user = User.objects.create_user(**self.user_info)
 
39
        profile = RegistrationProfile.objects.create_profile(new_user)
 
40
 
 
41
        self.assertEqual(RegistrationProfile.objects.count(), 1)
 
42
        self.assertEqual(profile.user.id, new_user.id)
 
43
        self.failUnless(re.match('^[a-f0-9]{40}$', profile.activation_key))
 
44
        self.assertEqual(unicode(profile),
 
45
                         "Registration information for alice")
 
46
 
 
47
    def test_activation_email(self):
 
48
        """
 
49
        ``RegistrationProfile.send_activation_email`` sends an
 
50
        email.
 
51
        
 
52
        """
 
53
        new_user = User.objects.create_user(**self.user_info)
 
54
        profile = RegistrationProfile.objects.create_profile(new_user)
 
55
        profile.send_activation_email(Site.objects.get_current())
 
56
        self.assertEqual(len(mail.outbox), 1)
 
57
        self.assertEqual(mail.outbox[0].to, [self.user_info['email']])
 
58
 
 
59
    def test_user_creation(self):
 
60
        """
 
61
        Creating a new user populates the correct data, and sets the
 
62
        user's account inactive.
 
63
        
 
64
        """
 
65
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
66
                                                                    **self.user_info)
 
67
        self.assertEqual(new_user.username, 'alice')
 
68
        self.assertEqual(new_user.email, 'alice@example.com')
 
69
        self.failUnless(new_user.check_password('swordfish'))
 
70
        self.failIf(new_user.is_active)
 
71
 
 
72
    def test_user_creation_email(self):
 
73
        """
 
74
        By default, creating a new user sends an activation email.
 
75
        
 
76
        """
 
77
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
78
                                                                    **self.user_info)
 
79
        self.assertEqual(len(mail.outbox), 1)
 
80
 
 
81
    def test_user_creation_no_email(self):
 
82
        """
 
83
        Passing ``send_email=False`` when creating a new user will not
 
84
        send an activation email.
 
85
        
 
86
        """
 
87
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
88
                                                                    send_email=False,
 
89
                                                                    **self.user_info)
 
90
        self.assertEqual(len(mail.outbox), 0)
 
91
 
 
92
    def test_unexpired_account(self):
 
93
        """
 
94
        ``RegistrationProfile.activation_key_expired()`` is ``False``
 
95
        within the activation window.
 
96
        
 
97
        """
 
98
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
99
                                                                    **self.user_info)
 
100
        profile = RegistrationProfile.objects.get(user=new_user)
 
101
        self.failIf(profile.activation_key_expired())
 
102
 
 
103
    def test_expired_account(self):
 
104
        """
 
105
        ``RegistrationProfile.activation_key_expired()`` is ``True``
 
106
        outside the activation window.
 
107
        
 
108
        """
 
109
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
110
                                                                    **self.user_info)
 
111
        new_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
 
112
        new_user.save()
 
113
        profile = RegistrationProfile.objects.get(user=new_user)
 
114
        self.failUnless(profile.activation_key_expired())
 
115
 
 
116
    def test_valid_activation(self):
 
117
        """
 
118
        Activating a user within the permitted window makes the
 
119
        account active, and resets the activation key.
 
120
        
 
121
        """
 
122
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
123
                                                                    **self.user_info)
 
124
        profile = RegistrationProfile.objects.get(user=new_user)
 
125
        activated = RegistrationProfile.objects.activate_user(profile.activation_key)
 
126
 
 
127
        self.failUnless(isinstance(activated, User))
 
128
        self.assertEqual(activated.id, new_user.id)
 
129
        self.failUnless(activated.is_active)
 
130
 
 
131
        profile = RegistrationProfile.objects.get(user=new_user)
 
132
        self.assertEqual(profile.activation_key, RegistrationProfile.ACTIVATED)
 
133
 
 
134
    def test_expired_activation(self):
 
135
        """
 
136
        Attempting to activate outside the permitted window does not
 
137
        activate the account.
 
138
        
 
139
        """
 
140
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
141
                                                                    **self.user_info)
 
142
        new_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
 
143
        new_user.save()
 
144
 
 
145
        profile = RegistrationProfile.objects.get(user=new_user)
 
146
        activated = RegistrationProfile.objects.activate_user(profile.activation_key)
 
147
 
 
148
        self.failIf(isinstance(activated, User))
 
149
        self.failIf(activated)
 
150
 
 
151
        new_user = User.objects.get(username='alice')
 
152
        self.failIf(new_user.is_active)
 
153
 
 
154
        profile = RegistrationProfile.objects.get(user=new_user)
 
155
        self.assertNotEqual(profile.activation_key, RegistrationProfile.ACTIVATED)
 
156
 
 
157
    def test_activation_invalid_key(self):
 
158
        """
 
159
        Attempting to activate with a key which is not a SHA1 hash
 
160
        fails.
 
161
        
 
162
        """
 
163
        self.failIf(RegistrationProfile.objects.activate_user('foo'))
 
164
 
 
165
    def test_activation_already_activated(self):
 
166
        """
 
167
        Attempting to re-activate an already-activated account fails.
 
168
        
 
169
        """
 
170
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
171
                                                                    **self.user_info)
 
172
        profile = RegistrationProfile.objects.get(user=new_user)
 
173
        RegistrationProfile.objects.activate_user(profile.activation_key)
 
174
 
 
175
        profile = RegistrationProfile.objects.get(user=new_user)
 
176
        self.failIf(RegistrationProfile.objects.activate_user(profile.activation_key))
 
177
 
 
178
    def test_activation_nonexistent_key(self):
 
179
        """
 
180
        Attempting to activate with a non-existent key (i.e., one not
 
181
        associated with any account) fails.
 
182
        
 
183
        """
 
184
        # Due to the way activation keys are constructed during
 
185
        # registration, this will never be a valid key.
 
186
        invalid_key = sha_constructor('foo').hexdigest()
 
187
        self.failIf(RegistrationProfile.objects.activate_user(invalid_key))
 
188
 
 
189
    def test_expired_user_deletion(self):
 
190
        """
 
191
        ``RegistrationProfile.objects.delete_expired_users()`` only
 
192
        deletes inactive users whose activation window has expired.
 
193
        
 
194
        """
 
195
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
196
                                                                    **self.user_info)
 
197
        expired_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
198
                                                                        username='bob',
 
199
                                                                        password='secret',
 
200
                                                                        email='bob@example.com')
 
201
        expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
 
202
        expired_user.save()
 
203
 
 
204
        RegistrationProfile.objects.delete_expired_users()
 
205
        self.assertEqual(RegistrationProfile.objects.count(), 1)
 
206
        self.assertRaises(User.DoesNotExist, User.objects.get, username='bob')
 
207
 
 
208
    def test_management_command(self):
 
209
        """
 
210
        The ``cleanupregistration`` management command properly
 
211
        deletes expired accounts.
 
212
        
 
213
        """
 
214
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
215
                                                                    **self.user_info)
 
216
        expired_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
 
217
                                                                        username='bob',
 
218
                                                                        password='secret',
 
219
                                                                        email='bob@example.com')
 
220
        expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
 
221
        expired_user.save()
 
222
 
 
223
        management.call_command('cleanupregistration')
 
224
        self.assertEqual(RegistrationProfile.objects.count(), 1)
 
225
        self.assertRaises(User.DoesNotExist, User.objects.get, username='bob')