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
12
from registration.models import RegistrationProfile
15
class RegistrationModelTests(TestCase):
17
Test the model and manager used in the default backend.
20
user_info = {'username': 'alice',
21
'password': 'swordfish',
22
'email': 'alice@example.com'}
25
self.old_activation = getattr(settings, 'ACCOUNT_ACTIVATION_DAYS', None)
26
settings.ACCOUNT_ACTIVATION_DAYS = 7
29
settings.ACCOUNT_ACTIVATION_DAYS = self.old_activation
31
def test_profile_creation(self):
33
Creating a registration profile for a user populates the
34
profile with the correct user and a SHA1 hash to use as
38
new_user = User.objects.create_user(**self.user_info)
39
profile = RegistrationProfile.objects.create_profile(new_user)
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")
47
def test_activation_email(self):
49
``RegistrationProfile.send_activation_email`` sends an
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']])
59
def test_user_creation(self):
61
Creating a new user populates the correct data, and sets the
62
user's account inactive.
65
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
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)
72
def test_user_creation_email(self):
74
By default, creating a new user sends an activation email.
77
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
79
self.assertEqual(len(mail.outbox), 1)
81
def test_user_creation_no_email(self):
83
Passing ``send_email=False`` when creating a new user will not
84
send an activation email.
87
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
90
self.assertEqual(len(mail.outbox), 0)
92
def test_unexpired_account(self):
94
``RegistrationProfile.activation_key_expired()`` is ``False``
95
within the activation window.
98
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
100
profile = RegistrationProfile.objects.get(user=new_user)
101
self.failIf(profile.activation_key_expired())
103
def test_expired_account(self):
105
``RegistrationProfile.activation_key_expired()`` is ``True``
106
outside the activation window.
109
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
111
new_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
113
profile = RegistrationProfile.objects.get(user=new_user)
114
self.failUnless(profile.activation_key_expired())
116
def test_valid_activation(self):
118
Activating a user within the permitted window makes the
119
account active, and resets the activation key.
122
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
124
profile = RegistrationProfile.objects.get(user=new_user)
125
activated = RegistrationProfile.objects.activate_user(profile.activation_key)
127
self.failUnless(isinstance(activated, User))
128
self.assertEqual(activated.id, new_user.id)
129
self.failUnless(activated.is_active)
131
profile = RegistrationProfile.objects.get(user=new_user)
132
self.assertEqual(profile.activation_key, RegistrationProfile.ACTIVATED)
134
def test_expired_activation(self):
136
Attempting to activate outside the permitted window does not
137
activate the account.
140
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
142
new_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
145
profile = RegistrationProfile.objects.get(user=new_user)
146
activated = RegistrationProfile.objects.activate_user(profile.activation_key)
148
self.failIf(isinstance(activated, User))
149
self.failIf(activated)
151
new_user = User.objects.get(username='alice')
152
self.failIf(new_user.is_active)
154
profile = RegistrationProfile.objects.get(user=new_user)
155
self.assertNotEqual(profile.activation_key, RegistrationProfile.ACTIVATED)
157
def test_activation_invalid_key(self):
159
Attempting to activate with a key which is not a SHA1 hash
163
self.failIf(RegistrationProfile.objects.activate_user('foo'))
165
def test_activation_already_activated(self):
167
Attempting to re-activate an already-activated account fails.
170
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
172
profile = RegistrationProfile.objects.get(user=new_user)
173
RegistrationProfile.objects.activate_user(profile.activation_key)
175
profile = RegistrationProfile.objects.get(user=new_user)
176
self.failIf(RegistrationProfile.objects.activate_user(profile.activation_key))
178
def test_activation_nonexistent_key(self):
180
Attempting to activate with a non-existent key (i.e., one not
181
associated with any account) fails.
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))
189
def test_expired_user_deletion(self):
191
``RegistrationProfile.objects.delete_expired_users()`` only
192
deletes inactive users whose activation window has expired.
195
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
197
expired_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
200
email='bob@example.com')
201
expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
204
RegistrationProfile.objects.delete_expired_users()
205
self.assertEqual(RegistrationProfile.objects.count(), 1)
206
self.assertRaises(User.DoesNotExist, User.objects.get, username='bob')
208
def test_management_command(self):
210
The ``cleanupregistration`` management command properly
211
deletes expired accounts.
214
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
216
expired_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
219
email='bob@example.com')
220
expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
223
management.call_command('cleanupregistration')
224
self.assertEqual(RegistrationProfile.objects.count(), 1)
225
self.assertRaises(User.DoesNotExist, User.objects.get, username='bob')