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 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)