def test_can_get_user_as_dict_serialized(self): """ User object is convertible to dict""" user = User(**self.data) user.lock_account() data = user.to_dict(serialize=True) self.assertIsInstance(data['created'], str) self.assertIsInstance(data['locked_until'], str)
def test_lock_check_unlocks_after_timeout(self): """ Lock checker unlock after timeout if previously locked """ user = User(**self.data) user.lock_account(-1) self.assertIsNotNone(user.locked_until) self.assertFalse(user.is_locked()) self.assertIsNone(user.locked_until)
def test_can_add_role(self): """ Adding role to user """ role = Role(handle='demo', title='Demo role') role_service.save(role) user = User(**self.data) user.add_role(role) self.assertIn(role, user.roles)
def test_can_get_user_as_dict_with_roles(self): """ Include roles when getting a dictionary representation of user""" user = User(**self.data) user.lock_account() data = user.to_dict(roles=True) self.assertEqual(1, len(data['roles'])) self.assertEqual('user', data['roles'][0]['handle'])
def test_generate_hash(self): """ Generating random hash """ length = 40 u = User() hash = u.generate_hash(length) self.assertTrue(type(hash) is str) self.assertEqual(length, len(hash))
def test_initial_email_set_requires_confirmation(self): """ Require confirmation when initially setting email""" user = User() user.email_confirmed = True user.email = '*****@*****.**' self.assertFalse(user.email_confirmed) self.assertEqual('*****@*****.**', user.email)
def test_user_provides_default_principal_needs(self): """ User provides default needs to principal identity""" user = User() user.id = 123 needs = user.provide_principal_needs() self.assertEquals(1, len(needs)) for need in needs: self.assertIsInstance(need, Need)
def test_hash_password(self): """ Hashing user password """ u = User() password = '******' u.password = password self.assertIsNotNone(u.password) self.assertTrue(type(u.password) is str) self.assertNotEqual(password, u.password)
def test_obfuscating_email_via_property(self): """ Can obfuscate email via property """ u1 = User(email='*****@*****.**') u2 = User(email='*****@*****.**') u3 = User(email='*****@*****.**') u4 = User(email='*****@*****.**') self.assertEqual('*@gmail.com', u1.email_secure) self.assertEqual('**@gmail.com', u2.email_secure) self.assertEqual('t****[email protected]', u3.email_secure) self.assertEqual('t*****-*****[email protected]', u4.email_secure)
def test_require_confirmation(self): """ Can require email confirmation """ user = User() self.assertFalse(user.email_confirmed) user.email_confirmed = True user.require_email_confirmation() self.assertFalse(user.email_confirmed) self.assertTrue(type(user.email_link) is str) self.assertIsInstance(user.email_link_expires, datetime)
def test_gravatar(self): """ Getting gravatar url from email """ from hashlib import md5 email = '*****@*****.**' size = '120x120' user = User(email=email) url = 'http://www.gravatar.com/avatar/{}?d=mm&s={}' expected = url.format( md5(user.email.encode('utf-8')).hexdigest(), size) self.assertEqual(expected, user.gravatar(size))
def test_can_confirm_initial_email(self): """ Confirming initial email """ user = User() user.email = '*****@*****.**' self.assertFalse(user.email_confirmed) self.assertTrue(type(user.email_link) is str) self.assertIsInstance(user.email_link_expires, datetime) user.confirm_email() self.assertTrue(user.email_confirmed) self.assertIsNone(user.email_link) self.assertIsNone(user.email_link_expires)
def test_email_update_requires_confirmation(self): """ Updating email requires confirmation """ user = User() user.email = '*****@*****.**' user.email_confirmed = True with events.events.disconnect_receivers(): user.email = '*****@*****.**' self.assertFalse(user.email_confirmed) self.assertEqual('*****@*****.**', user.email) self.assertEqual('*****@*****.**', user.email_new)
def test_can_get_user_as_dict(self): """ User object is convertible to dict""" user = User(**self.data) user.lock_account() data = user.to_dict() self.assertIn('id', data) self.assertIsInstance(data['created'], datetime) self.assertEqual(0, data['failed_logins']) self.assertTrue(data['locked']) self.assertIsInstance(data['locked_until'], datetime) self.assertIn('email', data) self.assertFalse(data['email_confirmed']) self.assertIn('roles', data) self.assertEqual(0, len(data['roles']))
def test_verify_password(self): """ Can verify user password """ password = '******' u = User() u.password = password self.assertTrue(u.verify_password(password)) self.assertFalse(u.verify_password('not a password')) u = User() self.assertFalse(u.verify_password(None)) self.assertFalse(u.verify_password('not a password'))
def test_change_email_returns_validation_errors(self): """ Change email returns validation errors on bad data """ u = User(email='*****@*****.**', password='******') with events.events.disconnect_receivers(): res = user_service.change_email(u, 'not-an-email', send_message=False) self.assertIsInstance(res, Result)
def test_save_emits_event(self): """ Saving a user emits event """ user = User(email='*****@*****.**', password='******') with user_events.disconnect_receivers(): spy = mock.Mock() events.user_save_event.connect(spy, weak=False) user_service.save(user) spy.assert_called_with(user)
def test_login_count_limit_check(self): """ Checking for failed login limit being reached """ user = User(**self.data) user.failed_logins = 9 self.assertFalse(user.failed_login_limit_reached()) user.increment_failed_logins() self.assertTrue(user.failed_login_limit_reached())
def test_check_password_link_expiration(self): """ Checking for password link expiration """ user = User() user.generate_password_link() now = datetime.utcnow() self.assertFalse(user.password_link_expired()) self.assertFalse(user.password_link_expired(now)) user.password_link_expires = datetime.utcnow() - timedelta(days=2) self.assertTrue(user.password_link_expired())
def create(email, password): """ Creates a new user record """ with get_app().app_context(): user = User(email=email, password=password) result = user_service.save(user) if not isinstance(result, User): print_validation_errors(result) return click.echo(green('\nUser created:')) click.echo(green('-' * 40)) click.echo(str(user) + '\n')
def test_increment_locks_and_drops_counter_upon__limit(self): """ Lock user account and drop counter upon reaching a limit """ user = User(**self.data) user.failed_logins = 10 user.increment_failed_logins() self.assertEqual(0, user.failed_logins) self.assertTrue(user.is_locked())
def test_unlock_account(self): """ Unlocking account """ user = User(**self.data) self.assertIsNone(user.locked_until) user.lock_account() self.assertIsNotNone(user.locked_until) user.unlock_account() self.assertIsNone(user.locked_until)
def test_new_users_cant_cancel_email_change(self): """ New users can not request email change cancellation """ user = User(email='*****@*****.**') user.require_email_confirmation() self.assertIsNotNone(user.email_link) user.cancel_email_change() self.assertIsNotNone(user.email_link) # nothing should change
def test_check_email_link_expiration(self): """ Checking for email link expiration """ user = User() user.email = '*****@*****.**' self.assertFalse(user.email_confirmed) now = datetime.utcnow() self.assertFalse(user.email_link_expired()) self.assertFalse(user.email_link_expired(now)) user.email_link_expires = datetime.utcnow() - timedelta(days=2) self.assertTrue(user.email_link_expired())
def test_account_confirmation_message_send(self): """ Account confirmation message can be sent """ user = User(email='*****@*****.**', password='******') with mail.record_messages() as out: with self.app.test_request_context(): url = 'http://my.confirm.url/' user_service.require_confirmation = True user_service.send_welcome_message(user, url) msg = out[0] self.assertTrue('Confirm email' in msg.html) self.assertTrue('Confirm email' in msg.body) self.assertTrue(user.email_link in msg.html) self.assertTrue(user.email_link in msg.body)
def test_can_remove_role(self): """ Removing role from user """ role = Role(handle='demo', title='Demo role') role_service.save(role) user = User(**self.data) user.add_role(role) self.assertIn(role, user.roles) user.remove_role(role) self.assertNotIn(role, user.roles)
def test_can_check_if_user_has_role(self): """ Checking if user has role """ user = User(**self.data) role1 = Role(handle='testrole1', title='Test role 1') role_service.save(role1) user.add_role(role1) role2 = Role(handle='testrole2', title='Test role 2') role_service.save(role2) # check by handle self.assertTrue(user.has_role('testrole1')) self.assertFalse(user.has_role('testrole2')) # check by object self.assertTrue(user.has_role(role1)) self.assertFalse(user.has_role(role2))
def test_existing_users_can_cancel_email_changes(self): """ Existing users can cancel email change""" user = User(email='*****@*****.**') user.confirm_email() self.assertTrue(user.email_confirmed) self.assertIsNone(user.email_link) user.email = '*****@*****.**' self.assertFalse(user.email_confirmed) self.assertIsNotNone(user.email_link) # change and assert data rollback happened user.cancel_email_change() self.assertTrue(user.email_confirmed) self.assertIsNone(user.email_link) self.assertIsNone(user.email_new)
def test_can_confirm_updated_email(self): """ Confirming updated email """ user = User() user.email = '*****@*****.**' user.email_confirmed = True with events.events.disconnect_receivers(): user.email = '*****@*****.**' self.assertFalse(user.email_confirmed) self.assertEqual('*****@*****.**', user.email_new) user.confirm_email() self.assertTrue(user.email_confirmed) self.assertIsNone(user.email_new) self.assertIsNone(user.email_link) self.assertIsNone(user.email_link_expires) self.assertEqual('*****@*****.**', user.email)
def test_adding_invalid_role_raises_exception(self): """ Raise exception in adding bad role to user """ user = User(**self.data) role = Role(id=123) with self.assertRaises(x.UserException): user.add_role(role)