def test_activation_valid(self): """ Valid activation of an user. Activation of an user with a valid ``activation_key`` should activate the user and set a new invalid ``activation_key`` that is defined in the setting ``USERENA_ACTIVATED``. """ user = SignupManager.create_user(**self.user_info) active_user = SignupManager.activate_user(user.signup.activation_key) # The returned user should be the same as the one just created. self.failUnlessEqual(user, active_user) # The user should now be active. self.failUnless(active_user.is_active) # The user should have permission to view and change its profile #self.failUnless('view_profile' in get_perms(active_user, active_user.get_profile())) #self.failUnless('change_profile' in get_perms(active_user, active_user.get_profile())) # The activation key should be the same as in the settings self.assertEqual(active_user.signup.activation_key, auth_settings.BAPH_ACTIVATED)
def test_activation_expired(self): """ Activation with a key that's expired should also make ``UserenaSignup.objects.activation_user`` return ``False``. """ user = SignupManager.create_user(**self.user_info) # Set the date that the key is created a day further away than allowed user.date_joined -= datetime.timedelta(days=auth_settings.BAPH_ACTIVATION_DAYS + 1) session = orm.sessionmaker() session.add(user) session.commit() # Try to activate the user SignupManager.activate_user(user.signup.activation_key) active_user = session.query(User).filter(User.username=='alice').first() # UserenaSignup activation should have failed self.failIf(active_user.is_active) # The activation key should still be a hash self.assertEqual(user.signup.activation_key, active_user.signup.activation_key)
def test_activation_expired(self): """ Activation with a key that's expired should also make ``UserenaSignup.objects.activation_user`` return ``False``. """ user = SignupManager.create_user(**self.user_info) # Set the date that the key is created a day further away than allowed user.date_joined -= datetime.timedelta( days=auth_settings.BAPH_ACTIVATION_DAYS + 1) session = orm.sessionmaker() session.add(user) session.commit() # Try to activate the user SignupManager.activate_user(user.signup.activation_key) active_user = session.query(User).filter( User.username == 'alice').first() # UserenaSignup activation should have failed self.failIf(active_user.is_active) # The activation key should still be a hash self.assertEqual(user.signup.activation_key, active_user.signup.activation_key)
def test_delete_expired_users(self): """ Test if expired users are deleted from the database. """ expired_user = SignupManager.create_user(**self.user_info) expired_user.date_joined -= datetime.timedelta(days=auth_settings.BAPH_ACTIVATION_DAYS + 1) expired_user.save() deleted_users = SignupManager.delete_expired_users() self.failUnlessEqual(deleted_users[0].username, 'alice')
def test_activation_invalid(self): """ Activation with a key that's invalid should make ``UserenaSignup.objects.activate_user`` return ``False``. """ # Wrong key self.failIf(SignupManager.activate_user('wrong_key')) # At least the right length invalid_key = 10 * 'a1b2' self.failIf(SignupManager.activate_user(invalid_key))
def test_delete_expired_users(self): """ Test if expired users are deleted from the database. """ expired_user = SignupManager.create_user(**self.user_info) expired_user.date_joined -= datetime.timedelta( days=auth_settings.BAPH_ACTIVATION_DAYS + 1) expired_user.save() deleted_users = SignupManager.delete_expired_users() self.failUnlessEqual(deleted_users[0].username, 'alice')
def test_create_inactive_user(self): """ Test the creation of a new user. ``UserenaSignup.create_inactive_user`` should create a new user that is not active. The user should get an ``activation_key`` that is used to set the user as active. Every user also has a profile, so this method should create an empty profile. """ # Check that the fields are set. new_user = SignupManager.create_user(**self.user_info) self.assertEqual(new_user.username, self.user_info['username']) self.assertEqual(new_user.email, self.user_info['email']) self.failUnless(new_user.check_password(self.user_info['password'])) # User should be inactive self.failIf(new_user.is_active) # User has a valid SHA1 activation key self.failUnless(re.match('^[a-f0-9]{40}$', new_user.signup.activation_key)) # User should be saved session = orm.sessionmaker() self.failUnlessEqual(session.query(User).filter(User.email==self.user_info['email']).count(), 1)
def test_confirmation_invalid(self): """ Trying to confirm a new e-mail address when the ``confirmation_key`` is invalid. """ new_email = '*****@*****.**' session = orm.sessionmaker() user = session.query(User).get(1) user.signup.change_email(new_email) # Verify email with wrong SHA1 self.failIf(SignupManager.confirm_email('sha1')) # Correct SHA1, but non-existend in db. self.failIf(SignupManager.confirm_email(10 * 'a1b2'))
def test_create_inactive_user(self): """ Test the creation of a new user. ``UserenaSignup.create_inactive_user`` should create a new user that is not active. The user should get an ``activation_key`` that is used to set the user as active. Every user also has a profile, so this method should create an empty profile. """ # Check that the fields are set. new_user = SignupManager.create_user(**self.user_info) self.assertEqual(new_user.username, self.user_info['username']) self.assertEqual(new_user.email, self.user_info['email']) self.failUnless(new_user.check_password(self.user_info['password'])) # User should be inactive self.failIf(new_user.is_active) # User has a valid SHA1 activation key self.failUnless( re.match('^[a-f0-9]{40}$', new_user.signup.activation_key)) # User should be saved session = orm.sessionmaker() self.failUnlessEqual( session.query(User).filter( User.email == self.user_info['email']).count(), 1)
def activate_retry(request, activation_key, template_name='registration/activate_retry_success.html', extra_context=None): """ Reissue a new ``activation_key`` for the user with the expired ``activation_key``. If ``activation_key`` does not exists, or ``BAPH_ACTIVATION_RETRY`` is set to False and for any other error condition user is redirected to :func:`activate` for error message display. :param activation_key: String of a SHA1 string of 40 characters long. A SHA1 is always 160bit long, with 4 bits per character this makes it --160/4-- 40 characters long. :param template_name: String containing the template name that is used when new ``activation_key`` has been created. Defaults to ``userena/activate_retry_success.html``. :param extra_context: Dictionary containing variables which could be added to the template context. Default to an empty dictionary. """ if not settings.BAPH_ACTIVATION_RETRY: return redirect(reverse('baph_activate', args=(activation_key, ))) try: if SignupManager.check_expired_activation(activation_key): new_key = SignupManager.reissue_activation(activation_key) if new_key: if not extra_context: extra_context = dict() return render_to_response( template_name, extra_context, context_instance=RequestContext(request)) else: return redirect( reverse('baph_activate', args=(activation_key, ))) else: return redirect(reverse('baph_activate', args=(activation_key, ))) except NoResultFound: return redirect(reverse('baph_activate', args=(activation_key, )))
def activate_retry(request, activation_key, template_name='registration/activate_retry_success.html', extra_context=None): """ Reissue a new ``activation_key`` for the user with the expired ``activation_key``. If ``activation_key`` does not exists, or ``BAPH_ACTIVATION_RETRY`` is set to False and for any other error condition user is redirected to :func:`activate` for error message display. :param activation_key: String of a SHA1 string of 40 characters long. A SHA1 is always 160bit long, with 4 bits per character this makes it --160/4-- 40 characters long. :param template_name: String containing the template name that is used when new ``activation_key`` has been created. Defaults to ``userena/activate_retry_success.html``. :param extra_context: Dictionary containing variables which could be added to the template context. Default to an empty dictionary. """ if not settings.BAPH_ACTIVATION_RETRY: return redirect(reverse('baph_activate', args=(activation_key,))) try: if SignupManager.check_expired_activation(activation_key): new_key = SignupManager.reissue_activation(activation_key) if new_key: if not extra_context: extra_context = dict() return render_to_response(template_name, extra_context, context_instance=RequestContext(request)) else: return redirect(reverse('baph_activate', args=(activation_key,))) else: return redirect(reverse('baph_activate', args=(activation_key,))) except NoResultFound: return redirect(reverse('baph_activate', args=(activation_key,)))
def save(self): """ Creates a new user and account. Returns the newly created user. """ username, email, password = (self.cleaned_data[User.USERNAME_FIELD], self.cleaned_data['email'], self.cleaned_data['password1']) extra_kwargs = dict(i for i in self.cleaned_data.items() if i[0] not in [User.USERNAME_FIELD, 'email', 'password1', 'password2']) new_user = SignupManager.create_user(username, email, password, not settings.BAPH_ACTIVATION_REQUIRED, settings.BAPH_ACTIVATION_REQUIRED, **extra_kwargs) return new_user
def email_confirm(request, confirmation_key, template_name='registration/email_confirm_fail.html', success_url=None, extra_context=None): """ Confirms an email address with a confirmation key. Confirms a new email address by running :func:`User.objects.confirm_email` method. If the method returns an :class:`User` the user will have his new e-mail address set and redirected to ``success_url``. If no ``User`` is returned the user will be represented with a fail message from ``template_name``. :param confirmation_key: String with a SHA1 representing the confirmation key used to verify a new email address. :param template_name: String containing the template name which should be rendered when confirmation fails. When confirmation is successful, no template is needed because the user will be redirected to ``success_url``. :param success_url: String containing the URL which is redirected to after a successful confirmation. Supplied argument must be able to be rendered by ``reverse`` function. :param extra_context: Dictionary of variables that are passed on to the template supplied by ``template_name``. """ user = SignupManager.confirm_email(confirmation_key) if user: messages.success(request, _('Your email address has been changed.'), fail_silently=True) if success_url: redirect_to = success_url else: redirect_to = reverse('baph_email_confirm_complete') return redirect(redirect_to) else: if not extra_context: extra_context = dict() return render_to_response(template_name, extra_context, context_instance=RequestContext(request))
def activate(request, activation_key, template_name='registration/activate_fail.html', retry_template_name='registration/activate_retry.html', success_url=django_settings.LOGIN_REDIRECT_URL, extra_context=None): session = orm.sessionmaker() signup = session.query(UserRegistration) \ .filter_by(activation_key=activation_key) \ .first() if not signup: if not extra_context: extra_context = dict() return render_to_response(template_name, extra_context, context_instance=RequestContext(request)) if (not signup.activation_key_expired() or not settings.BAPH_ACTIVATION_RETRY): user = SignupManager.activate_user(activation_key) if user: auth_user = authenticate(identification=user.email, check_password=False) login(request, auth_user) messages.success(request, _('Your account has been activated and ' 'you have been signed in.'), fail_silently=True) if success_url: redirect_to = success_url % {'username': user.username} else: redirect_to = reverse('userena_profile_detail', kwargs={'username': user.username}) #TODO this is broken return redirect(redirect_to) else: if not extra_context: extra_context = dict() return render_to_response(template_name, extra_context, context_instance=RequestContext(request)) else: if not extra_context: extra_context = dict() extra_context['activation_key'] = activation_key return render_to_response(retry_template_name, extra_context, context_instance=RequestContext(request))
def test_confirmation_valid(self): """ Confirmation of a new e-mail address with turns out to be valid. """ new_email = '*****@*****.**' session = orm.sessionmaker() user = session.query(User).get(1) user.signup.change_email(new_email) # Confirm email confirmed_user = SignupManager.confirm_email(user.signup.email_confirmation_key) self.failUnlessEqual(user, confirmed_user) # Check the new email is set. self.failUnlessEqual(confirmed_user.email, new_email) # ``email_new`` and ``email_verification_key`` should be empty self.failIf(confirmed_user.signup.email_unconfirmed) self.failIf(confirmed_user.signup.email_confirmation_key)
def test_confirmation_valid(self): """ Confirmation of a new e-mail address with turns out to be valid. """ new_email = '*****@*****.**' session = orm.sessionmaker() user = session.query(User).get(1) user.signup.change_email(new_email) # Confirm email confirmed_user = SignupManager.confirm_email( user.signup.email_confirmation_key) self.failUnlessEqual(user, confirmed_user) # Check the new email is set. self.failUnlessEqual(confirmed_user.email, new_email) # ``email_new`` and ``email_verification_key`` should be empty self.failIf(confirmed_user.signup.email_unconfirmed) self.failIf(confirmed_user.signup.email_confirmation_key)
def activate(request, activation_key, template_name='registration/activate_fail.html', retry_template_name='registration/activate_retry.html', success_url=django_settings.LOGIN_REDIRECT_URL, extra_context=None): session = orm.sessionmaker() signup = session.query(UserRegistration) \ .filter_by(activation_key=activation_key) \ .first() if not signup: if not extra_context: extra_context = dict() return render_to_response(template_name, extra_context, context_instance=RequestContext(request)) if (not signup.activation_key_expired() or not settings.BAPH_ACTIVATION_RETRY): user = SignupManager.activate_user(activation_key) if user: auth_user = authenticate(identification=user.email, check_password=False) login(request, auth_user) messages.success(request, _('Your account has been activated and ' 'you have been signed in.'), fail_silently=True) if success_url: redirect_to = success_url % {'username': user.username } else: redirect_to = reverse('userena_profile_detail', kwargs={'username': user.username}) #TODO this is broken return redirect(redirect_to) else: if not extra_context: extra_context = dict() return render_to_response(template_name, extra_context, context_instance=RequestContext(request)) else: if not extra_context: extra_context = dict() extra_context['activation_key'] = activation_key return render_to_response(retry_template_name, extra_context, context_instance=RequestContext(request))