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