|
Line 0
Link Here
|
|
|
1 |
import datetime |
| 2 |
import hashlib |
| 3 |
import re |
| 4 |
|
| 5 |
from django.utils import six |
| 6 |
from django.apps import apps |
| 7 |
from django.conf import settings |
| 8 |
from django.core import mail |
| 9 |
from django.core import management |
| 10 |
from django.test import TestCase |
| 11 |
|
| 12 |
from registration.models import RegistrationProfile |
| 13 |
from registration.users import UserModel |
| 14 |
|
| 15 |
Site = apps.get_model('sites', 'Site') |
| 16 |
|
| 17 |
|
| 18 |
class RegistrationModelTests(TestCase): |
| 19 |
""" |
| 20 |
Test the model and manager used in the default backend. |
| 21 |
|
| 22 |
""" |
| 23 |
user_info = {'username': 'alice', |
| 24 |
'password': 'swordfish', |
| 25 |
'email': 'alice@example.com'} |
| 26 |
|
| 27 |
def setUp(self): |
| 28 |
self.old_activation = getattr(settings, |
| 29 |
'ACCOUNT_ACTIVATION_DAYS', None) |
| 30 |
self.old_reg_email = getattr(settings, |
| 31 |
'REGISTRATION_DEFAULT_FROM_EMAIL', None) |
| 32 |
self.old_email_html = getattr(settings, |
| 33 |
'REGISTRATION_EMAIL_HTML', None) |
| 34 |
self.old_django_email = getattr(settings, |
| 35 |
'DEFAULT_FROM_EMAIL', None) |
| 36 |
|
| 37 |
settings.ACCOUNT_ACTIVATION_DAYS = 7 |
| 38 |
settings.REGISTRATION_DEFAULT_FROM_EMAIL = 'registration@email.com' |
| 39 |
settings.REGISTRATION_EMAIL_HTML = True |
| 40 |
settings.DEFAULT_FROM_EMAIL = 'django@email.com' |
| 41 |
|
| 42 |
def tearDown(self): |
| 43 |
settings.ACCOUNT_ACTIVATION_DAYS = self.old_activation |
| 44 |
settings.REGISTRATION_DEFAULT_FROM_EMAIL = self.old_reg_email |
| 45 |
settings.REGISTRATION_EMAIL_HTML = self.old_email_html |
| 46 |
settings.DEFAULT_FROM_EMAIL = self.old_django_email |
| 47 |
|
| 48 |
def test_profile_creation(self): |
| 49 |
""" |
| 50 |
Creating a registration profile for a user populates the |
| 51 |
profile with the correct user and a SHA1 hash to use as |
| 52 |
activation key. |
| 53 |
|
| 54 |
""" |
| 55 |
new_user = UserModel().objects.create_user(**self.user_info) |
| 56 |
profile = RegistrationProfile.objects.create_profile(new_user) |
| 57 |
|
| 58 |
self.assertEqual(RegistrationProfile.objects.count(), 1) |
| 59 |
self.assertEqual(profile.user.id, new_user.id) |
| 60 |
self.failUnless(re.match('^[a-f0-9]{40}$', profile.activation_key)) |
| 61 |
self.assertEqual(six.text_type(profile), |
| 62 |
"Registration information for alice") |
| 63 |
|
| 64 |
def test_activation_email(self): |
| 65 |
""" |
| 66 |
``RegistrationProfile.send_activation_email`` sends an |
| 67 |
email. |
| 68 |
|
| 69 |
""" |
| 70 |
new_user = UserModel().objects.create_user(**self.user_info) |
| 71 |
profile = RegistrationProfile.objects.create_profile(new_user) |
| 72 |
profile.send_activation_email(Site.objects.get_current()) |
| 73 |
self.assertEqual(len(mail.outbox), 1) |
| 74 |
self.assertEqual(mail.outbox[0].to, [self.user_info['email']]) |
| 75 |
|
| 76 |
def test_activation_email_uses_registration_default_from_email(self): |
| 77 |
""" |
| 78 |
``RegistrationProfile.send_activation_email`` sends an |
| 79 |
email. |
| 80 |
|
| 81 |
""" |
| 82 |
new_user = UserModel().objects.create_user(**self.user_info) |
| 83 |
profile = RegistrationProfile.objects.create_profile(new_user) |
| 84 |
profile.send_activation_email(Site.objects.get_current()) |
| 85 |
self.assertEqual(mail.outbox[0].from_email, 'registration@email.com') |
| 86 |
|
| 87 |
def test_activation_email_falls_back_to_django_default_from_email(self): |
| 88 |
""" |
| 89 |
``RegistrationProfile.send_activation_email`` sends an |
| 90 |
email. |
| 91 |
|
| 92 |
""" |
| 93 |
settings.REGISTRATION_DEFAULT_FROM_EMAIL = None |
| 94 |
new_user = UserModel().objects.create_user(**self.user_info) |
| 95 |
profile = RegistrationProfile.objects.create_profile(new_user) |
| 96 |
profile.send_activation_email(Site.objects.get_current()) |
| 97 |
self.assertEqual(mail.outbox[0].from_email, 'django@email.com') |
| 98 |
|
| 99 |
def test_activation_email_is_html_by_default(self): |
| 100 |
""" |
| 101 |
``RegistrationProfile.send_activation_email`` sends an html |
| 102 |
email by default. |
| 103 |
|
| 104 |
""" |
| 105 |
new_user = UserModel().objects.create_user(**self.user_info) |
| 106 |
profile = RegistrationProfile.objects.create_profile(new_user) |
| 107 |
profile.send_activation_email(Site.objects.get_current()) |
| 108 |
|
| 109 |
self.assertEqual(len(mail.outbox[0].alternatives), 1) |
| 110 |
|
| 111 |
def test_activation_email_is_plain_text_if_html_disabled(self): |
| 112 |
""" |
| 113 |
``RegistrationProfile.send_activation_email`` sends a plain |
| 114 |
text email if settings.REGISTRATION_EMAIL_HTML is False. |
| 115 |
|
| 116 |
""" |
| 117 |
settings.REGISTRATION_EMAIL_HTML = False |
| 118 |
new_user = UserModel().objects.create_user(**self.user_info) |
| 119 |
profile = RegistrationProfile.objects.create_profile(new_user) |
| 120 |
profile.send_activation_email(Site.objects.get_current()) |
| 121 |
|
| 122 |
self.assertEqual(len(mail.outbox[0].alternatives), 0) |
| 123 |
|
| 124 |
def test_user_creation(self): |
| 125 |
""" |
| 126 |
Creating a new user populates the correct data, and sets the |
| 127 |
user's account inactive. |
| 128 |
|
| 129 |
""" |
| 130 |
new_user = RegistrationProfile.objects.create_inactive_user( |
| 131 |
site=Site.objects.get_current(), **self.user_info) |
| 132 |
self.assertEqual(new_user.username, 'alice') |
| 133 |
self.assertEqual(new_user.email, 'alice@example.com') |
| 134 |
self.failUnless(new_user.check_password('swordfish')) |
| 135 |
self.failIf(new_user.is_active) |
| 136 |
|
| 137 |
def test_user_creation_email(self): |
| 138 |
""" |
| 139 |
By default, creating a new user sends an activation email. |
| 140 |
|
| 141 |
""" |
| 142 |
RegistrationProfile.objects.create_inactive_user( |
| 143 |
site=Site.objects.get_current(), **self.user_info) |
| 144 |
self.assertEqual(len(mail.outbox), 1) |
| 145 |
|
| 146 |
def test_user_creation_no_email(self): |
| 147 |
""" |
| 148 |
Passing ``send_email=False`` when creating a new user will not |
| 149 |
send an activation email. |
| 150 |
|
| 151 |
""" |
| 152 |
RegistrationProfile.objects.create_inactive_user( |
| 153 |
site=Site.objects.get_current(), |
| 154 |
send_email=False, **self.user_info) |
| 155 |
self.assertEqual(len(mail.outbox), 0) |
| 156 |
|
| 157 |
def test_unexpired_account(self): |
| 158 |
""" |
| 159 |
``RegistrationProfile.activation_key_expired()`` is ``False`` |
| 160 |
within the activation window. |
| 161 |
|
| 162 |
""" |
| 163 |
new_user = RegistrationProfile.objects.create_inactive_user( |
| 164 |
site=Site.objects.get_current(), **self.user_info) |
| 165 |
profile = RegistrationProfile.objects.get(user=new_user) |
| 166 |
self.failIf(profile.activation_key_expired()) |
| 167 |
|
| 168 |
def test_expired_account(self): |
| 169 |
""" |
| 170 |
``RegistrationProfile.activation_key_expired()`` is ``True`` |
| 171 |
outside the activation window. |
| 172 |
|
| 173 |
""" |
| 174 |
new_user = RegistrationProfile.objects.create_inactive_user( |
| 175 |
site=Site.objects.get_current(), **self.user_info) |
| 176 |
new_user.date_joined -= datetime.timedelta( |
| 177 |
days=settings.ACCOUNT_ACTIVATION_DAYS + 1) |
| 178 |
new_user.save() |
| 179 |
profile = RegistrationProfile.objects.get(user=new_user) |
| 180 |
self.failUnless(profile.activation_key_expired()) |
| 181 |
|
| 182 |
def test_valid_activation(self): |
| 183 |
""" |
| 184 |
Activating a user within the permitted window makes the |
| 185 |
account active, and resets the activation key. |
| 186 |
|
| 187 |
""" |
| 188 |
new_user = RegistrationProfile.objects.create_inactive_user( |
| 189 |
site=Site.objects.get_current(), **self.user_info) |
| 190 |
profile = RegistrationProfile.objects.get(user=new_user) |
| 191 |
activated = (RegistrationProfile.objects |
| 192 |
.activate_user(profile.activation_key)) |
| 193 |
|
| 194 |
self.failUnless(isinstance(activated, UserModel())) |
| 195 |
self.assertEqual(activated.id, new_user.id) |
| 196 |
self.failUnless(activated.is_active) |
| 197 |
|
| 198 |
profile = RegistrationProfile.objects.get(user=new_user) |
| 199 |
self.assertTrue(profile.activated) |
| 200 |
|
| 201 |
def test_expired_activation(self): |
| 202 |
""" |
| 203 |
Attempting to activate outside the permitted window does not |
| 204 |
activate the account. |
| 205 |
|
| 206 |
""" |
| 207 |
new_user = RegistrationProfile.objects.create_inactive_user( |
| 208 |
site=Site.objects.get_current(), **self.user_info) |
| 209 |
new_user.date_joined -= datetime.timedelta( |
| 210 |
days=settings.ACCOUNT_ACTIVATION_DAYS + 1) |
| 211 |
new_user.save() |
| 212 |
|
| 213 |
profile = RegistrationProfile.objects.get(user=new_user) |
| 214 |
activated = (RegistrationProfile.objects |
| 215 |
.activate_user(profile.activation_key)) |
| 216 |
|
| 217 |
self.failIf(isinstance(activated, UserModel())) |
| 218 |
self.failIf(activated) |
| 219 |
|
| 220 |
new_user = UserModel().objects.get(username='alice') |
| 221 |
self.failIf(new_user.is_active) |
| 222 |
|
| 223 |
profile = RegistrationProfile.objects.get(user=new_user) |
| 224 |
self.assertFalse(profile.activated) |
| 225 |
|
| 226 |
def test_activation_invalid_key(self): |
| 227 |
""" |
| 228 |
Attempting to activate with a key which is not a SHA1 hash |
| 229 |
fails. |
| 230 |
|
| 231 |
""" |
| 232 |
self.failIf(RegistrationProfile.objects.activate_user('foo')) |
| 233 |
|
| 234 |
def test_activation_already_activated(self): |
| 235 |
""" |
| 236 |
Attempting to re-activate an already-activated account fails. |
| 237 |
|
| 238 |
""" |
| 239 |
new_user = RegistrationProfile.objects.create_inactive_user( |
| 240 |
site=Site.objects.get_current(), **self.user_info) |
| 241 |
profile = RegistrationProfile.objects.get(user=new_user) |
| 242 |
RegistrationProfile.objects.activate_user(profile.activation_key) |
| 243 |
|
| 244 |
profile = RegistrationProfile.objects.get(user=new_user) |
| 245 |
self.assertEqual(RegistrationProfile.objects.activate_user(profile.activation_key), new_user) |
| 246 |
|
| 247 |
def test_activation_deactivated(self): |
| 248 |
""" |
| 249 |
Attempting to re-activate a deactivated account fails. |
| 250 |
""" |
| 251 |
new_user = RegistrationProfile.objects.create_inactive_user( |
| 252 |
site=Site.objects.get_current(), **self.user_info) |
| 253 |
profile = RegistrationProfile.objects.get(user=new_user) |
| 254 |
RegistrationProfile.objects.activate_user(profile.activation_key) |
| 255 |
|
| 256 |
# Deactivate the new user. |
| 257 |
new_user.is_active = False |
| 258 |
new_user.save() |
| 259 |
|
| 260 |
# Try to activate again and ensure False is returned. |
| 261 |
failed = RegistrationProfile.objects.activate_user(profile.activation_key) |
| 262 |
self.assertFalse(failed) |
| 263 |
|
| 264 |
def test_activation_nonexistent_key(self): |
| 265 |
""" |
| 266 |
Attempting to activate with a non-existent key (i.e., one not |
| 267 |
associated with any account) fails. |
| 268 |
|
| 269 |
""" |
| 270 |
# Due to the way activation keys are constructed during |
| 271 |
# registration, this will never be a valid key. |
| 272 |
invalid_key = hashlib.sha1(six.b('foo')).hexdigest() |
| 273 |
self.failIf(RegistrationProfile.objects.activate_user(invalid_key)) |
| 274 |
|
| 275 |
def test_expired_user_deletion(self): |
| 276 |
""" |
| 277 |
``RegistrationProfile.objects.delete_expired_users()`` only |
| 278 |
deletes inactive users whose activation window has expired. |
| 279 |
|
| 280 |
""" |
| 281 |
RegistrationProfile.objects.create_inactive_user( |
| 282 |
site=Site.objects.get_current(), **self.user_info) |
| 283 |
expired_user = (RegistrationProfile.objects |
| 284 |
.create_inactive_user( |
| 285 |
site=Site.objects.get_current(), |
| 286 |
username='bob', |
| 287 |
password='secret', |
| 288 |
email='bob@example.com')) |
| 289 |
expired_user.date_joined -= datetime.timedelta( |
| 290 |
days=settings.ACCOUNT_ACTIVATION_DAYS + 1) |
| 291 |
expired_user.save() |
| 292 |
|
| 293 |
RegistrationProfile.objects.delete_expired_users() |
| 294 |
self.assertEqual(RegistrationProfile.objects.count(), 1) |
| 295 |
self.assertRaises(UserModel().DoesNotExist, |
| 296 |
UserModel().objects.get, username='bob') |
| 297 |
|
| 298 |
def test_management_command(self): |
| 299 |
""" |
| 300 |
The ``cleanupregistration`` management command properly |
| 301 |
deletes expired accounts. |
| 302 |
|
| 303 |
""" |
| 304 |
RegistrationProfile.objects.create_inactive_user( |
| 305 |
site=Site.objects.get_current(), **self.user_info) |
| 306 |
expired_user = (RegistrationProfile.objects |
| 307 |
.create_inactive_user(site=Site.objects.get_current(), |
| 308 |
username='bob', |
| 309 |
password='secret', |
| 310 |
email='bob@example.com')) |
| 311 |
expired_user.date_joined -= datetime.timedelta( |
| 312 |
days=settings.ACCOUNT_ACTIVATION_DAYS + 1) |
| 313 |
expired_user.save() |
| 314 |
|
| 315 |
management.call_command('cleanupregistration') |
| 316 |
self.assertEqual(RegistrationProfile.objects.count(), 1) |
| 317 |
self.assertRaises(UserModel().DoesNotExist, |
| 318 |
UserModel().objects.get, username='bob') |
| 319 |
|
| 320 |
def test_resend_activation_email(self): |
| 321 |
""" |
| 322 |
Test resending activation email to an existing user |
| 323 |
""" |
| 324 |
user = RegistrationProfile.objects.create_inactive_user( |
| 325 |
site=Site.objects.get_current(), send_email=False, **self.user_info) |
| 326 |
self.assertEqual(len(mail.outbox), 0) |
| 327 |
|
| 328 |
profile = RegistrationProfile.objects.get(user=user) |
| 329 |
orig_activation_key = profile.activation_key |
| 330 |
|
| 331 |
self.assertTrue(RegistrationProfile.objects.resend_activation_mail( |
| 332 |
email=self.user_info['email'], |
| 333 |
site=Site.objects.get_current(), |
| 334 |
)) |
| 335 |
|
| 336 |
profile = RegistrationProfile.objects.get(pk=profile.pk) |
| 337 |
new_activation_key = profile.activation_key |
| 338 |
|
| 339 |
self.assertNotEqual(orig_activation_key, new_activation_key) |
| 340 |
self.assertEqual(len(mail.outbox), 1) |
| 341 |
|
| 342 |
def test_resend_activation_email_nonexistent_user(self): |
| 343 |
""" |
| 344 |
Test resending activation email to a nonexisting user |
| 345 |
""" |
| 346 |
self.assertFalse(RegistrationProfile.objects.resend_activation_mail( |
| 347 |
email=self.user_info['email'], |
| 348 |
site=Site.objects.get_current(), |
| 349 |
)) |
| 350 |
self.assertEqual(len(mail.outbox), 0) |
| 351 |
|
| 352 |
def test_resend_activation_email_activated_user(self): |
| 353 |
""" |
| 354 |
Test the scenario where user tries to resend activation code |
| 355 |
to the already activated user's email |
| 356 |
""" |
| 357 |
user = RegistrationProfile.objects.create_inactive_user( |
| 358 |
site=Site.objects.get_current(), send_email=False, **self.user_info) |
| 359 |
|
| 360 |
profile = RegistrationProfile.objects.get(user=user) |
| 361 |
activated = (RegistrationProfile.objects |
| 362 |
.activate_user(profile.activation_key)) |
| 363 |
self.assertTrue(activated.is_active) |
| 364 |
|
| 365 |
self.assertFalse(RegistrationProfile.objects.resend_activation_mail( |
| 366 |
email=self.user_info['email'], |
| 367 |
site=Site.objects.get_current(), |
| 368 |
)) |
| 369 |
self.assertEqual(len(mail.outbox), 0) |
| 370 |
|
| 371 |
def test_resend_activation_email_expired_user(self): |
| 372 |
""" |
| 373 |
Test the scenario where user tries to resend activation code |
| 374 |
to the expired user's email |
| 375 |
""" |
| 376 |
new_user = RegistrationProfile.objects.create_inactive_user( |
| 377 |
site=Site.objects.get_current(), send_email=False, **self.user_info) |
| 378 |
new_user.date_joined -= datetime.timedelta( |
| 379 |
days=settings.ACCOUNT_ACTIVATION_DAYS + 1) |
| 380 |
new_user.save() |
| 381 |
|
| 382 |
profile = RegistrationProfile.objects.get(user=new_user) |
| 383 |
self.assertTrue(profile.activation_key_expired()) |
| 384 |
|
| 385 |
self.assertFalse(RegistrationProfile.objects.resend_activation_mail( |
| 386 |
email=self.user_info['email'], |
| 387 |
site=Site.objects.get_current(), |
| 388 |
)) |
| 389 |
self.assertEqual(len(mail.outbox), 0) |