def test_activation(self): """ Test that user activation actually activates the user and properly resets the activation key, and fails for an already-active or expired user, or an invalid key. """ # Activating a valid user returns the user. self.failUnlessEqual( RegistrationProfile.activate_user( self.sample_user.activation_key).pk, self.sample_user.pk) # The activated user must now be active. self.failUnless(User.objects.get(pk=self.sample_user.pk).is_active) # The activation key must now be reset to the "already activated" constant. self.failUnlessEqual( RegistrationProfile.objects.get( user=self.sample_user).activation_key, RegistrationProfile.ACTIVATED) # Activating an expired user returns False. self.failIf( RegistrationProfile.activate_user( self.expired_user.activation_key)) # Activating from a key that isn't a SHA1 hash returns False. self.failIf(RegistrationProfile.activate_user('foo')) # Activating from a key that doesn't exist returns False. self.failIf( RegistrationProfile.activate_user(sha.new('foo').hexdigest()))
def test_activation(self): """ Test that user activation actually activates the user and properly resets the activation key, and fails for an already-active or expired user, or an invalid key. """ # Activating a valid user returns the user. self.failUnlessEqual(RegistrationProfile.activate_user(self.sample_user.activation_key).pk, self.sample_user.pk) # The activated user must now be active. self.failUnless(User.objects.get(pk=self.sample_user.pk).is_active) # The activation key must now be reset to the "already activated" constant. self.failUnlessEqual( RegistrationProfile.objects.get(user=self.sample_user).activation_key, RegistrationProfile.ACTIVATED ) # Activating an expired user returns False. self.failIf(RegistrationProfile.activate_user(self.expired_user.activation_key)) # Activating from a key that isn't a SHA1 hash returns False. self.failIf(RegistrationProfile.activate_user("foo")) # Activating from a key that doesn't exist returns False. self.failIf(RegistrationProfile.activate_user(sha.new("foo").hexdigest()))
def setUp(self): self.sample_user = RegistrationProfile.create_inactive_user( username='******', password='******', email='*****@*****.**') self.expired_user = RegistrationProfile.create_inactive_user( username='******', password='******', email='*****@*****.**') self.expired_user.date_joined -= datetime.timedelta( days=settings.ACCOUNT_ACTIVATION_DAYS + 1) self.expired_user.save()
def test_expired_user_deletion(self): """ Test that ``RegistrationProfile.objects.delete_expired_users()`` deletes only inactive users whose activation window has expired. """ RegistrationProfile.delete_expired_users() self.assertEqual(RegistrationProfile.objects.count(), 1)
def setUp(self): self.sample_user = RegistrationProfile.create_inactive_user( username="******", password="******", email="*****@*****.**" ) self.expired_user = RegistrationProfile.create_inactive_user( username="******", password="******", email="*****@*****.**" ) self.expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1) self.expired_user.save()
def test_expired_user_deletion(self): """ Test that ``RegistrationProfile.objects.delete_expired_users()`` deletes only inactive users whose activation window has expired. """ RegistrationProfile.delete_expired_users() self.assertEqual(RegistrationProfile.objects.count(), 1)
def save(self): """ Create the new ``User`` and ``RegistrationProfile``, and #returns the ``User``. This is essentially a light wrapper around ``RegistrationProfile.objects.create_inactive_user()``, feeding it the form data and a profile callback (see the documentation on ``create_inactive_user()`` for details) if supplied. """ new_user = RegistrationProfile.create_inactive_user( username=self.cleaned_data['username'], password=self.cleaned_data['password1'], email=self.cleaned_data['email'], #profile_callback=profile_callback, send_email = False) new_user.is_active = True new_user.activation_key = u"ALREADY_ACTIVATED" new_user.save() #RegistrationProfile.activate_user(new_user.activation_key) #new_user = RegistrationProfile.create_inactive_user( # username=self.cleaned_data['username'], # password=self.cleaned_data['password1'], # email=self.cleaned_data['email']) return new_user
def test_account_expiration_condition(self): """ Test that ``RegistrationProfile.activation_key_expired()`` returns ``True`` for expired users and for active users, and ``False`` otherwise. """ # Unexpired user returns False. self.failIf(self.sample_user.activation_key_expired()) # Expired user returns True. self.failUnless(self.expired_user.activation_key_expired()) # Activated user returns True. RegistrationProfile.activate_user(self.sample_user.activation_key) self.failUnless(self.sample_user.activation_key_expired())
def test_account_expiration_condition(self): """ Test that ``RegistrationProfile.activation_key_expired()`` returns ``True`` for expired users and for active users, and ``False`` otherwise. """ # Unexpired user returns False. self.failIf(self.sample_user.activation_key_expired()) # Expired user returns True. self.failUnless(self.expired_user.activation_key_expired()) # Activated user returns True. RegistrationProfile.activate_user(self.sample_user.activation_key) self.failUnless(self.sample_user.activation_key_expired())
def activate(request, activation_key, template_name='registration/activate.html', extra_context=None): """ Activate a ``User``'s account from an activation key, if their key is valid and hasn't expired. By default, use the template ``registration/activate.html``; to change this, pass the name of a template as the keyword argument ``template_name``. **Required arguments** ``activation_key`` The activation key to validate and use for activating the ``User``. **Optional arguments** ``extra_context`` A dictionary of variables to add to the template context. Any callable object in this dictionary will be called to produce the end result which appears in the context. ``template_name`` A custom template to use. **Context:** ``account`` The ``User`` object corresponding to the account, if the activation was successful. ``False`` if the activation was not successful. ``expiration_days`` The number of days for which activation keys stay valid after registration. Any extra variables supplied in the ``extra_context`` argument (see above). **Template:** registration/activate.html or ``template_name`` keyword argument. """ activation_key = activation_key.lower() # Normalize before trying anything with it. account = RegistrationProfile.activate_user(activation_key) if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name, { 'account': account, 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS }, context_instance=context)
def activate(request, activation_key, template_name='registration/activate.html', extra_context=None): """ Activate a ``User``'s account from an activation key, if their key is valid and hasn't expired. By default, use the template ``registration/activate.html``; to change this, pass the name of a template as the keyword argument ``template_name``. **Required arguments** ``activation_key`` The activation key to validate and use for activating the ``User``. **Optional arguments** ``extra_context`` A dictionary of variables to add to the template context. Any callable object in this dictionary will be called to produce the end result which appears in the context. ``template_name`` A custom template to use. **Context:** ``account`` The ``User`` object corresponding to the account, if the activation was successful. ``False`` if the activation was not successful. ``expiration_days`` The number of days for which activation keys stay valid after registration. Any extra variables supplied in the ``extra_context`` argument (see above). **Template:** registration/activate.html or ``template_name`` keyword argument. """ activation_key = activation_key.lower() # Normalize before trying anything with it. account = RegistrationProfile.activate_user(activation_key) if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name, { 'account': account, 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS }, context_instance=context)
def clean_email(self): """ Validate that the supplied email address is unique for the site. """ if RegistrationProfile.objects( email__iexact=self.cleaned_data['email']): raise forms.ValidationError(_(u'This email address is already in use. Please supply a different email address.')) return self.cleaned_data['email']
def clean_email(self): """ Validate that the supplied email address is unique for the site. """ if RegistrationProfile.objects( email__iexact=self.cleaned_data['email']): raise forms.ValidationError(_(u'This email address is already in use. Please supply a different email address.')) return self.cleaned_data['email']
def clean_username(self): """ Validate that the username is alphanumeric and is not already in use. """ if RegistrationProfile.objects( username__iexact=self.cleaned_data['username']): raise forms.ValidationError( _(u'This username is already taken. Please choose another.')) return self.cleaned_data['username']
def clean_username(self): """ Validate that the username is alphanumeric and is not already in use. """ if RegistrationProfile.objects( username__iexact=self.cleaned_data['username']): raise forms.ValidationError( _(u'This username is already taken. Please choose another.')) return self.cleaned_data['username']
def save(self): """ Create the new ``User`` and ``RegistrationProfile``, and returns the ``User``. This is essentially a light wrapper around ``RegistrationProfile.objects.create_inactive_user()``, feeding it the form data and a profile callback (see the documentation on ``create_inactive_user()`` for details) if supplied. """ new_user = RegistrationProfile.create_active_user( username=self.cleaned_data['username'], password=self.cleaned_data['password1'], email=self.cleaned_data['email']) return new_user
def save(self): """ Create the new ``User`` and ``RegistrationProfile``, and returns the ``User``. This is essentially a light wrapper around ``RegistrationProfile.objects.create_inactive_user()``, feeding it the form data and a profile callback (see the documentation on ``create_inactive_user()`` for details) if supplied. """ new_user = RegistrationProfile.create_inactive_user( username=self.cleaned_data['username'], password=self.cleaned_data['password1'], email=self.cleaned_data['email']) return new_user
def handle_noargs(self, **options): RegistrationProfile.delete_expired_users()