예제 #1
0
 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)
예제 #2
0
 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)
예제 #3
0
 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)
예제 #4
0
 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'])
예제 #5
0
 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))
예제 #6
0
 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)
예제 #7
0
 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)
예제 #8
0
    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)
예제 #9
0
 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)
예제 #10
0
    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)
예제 #11
0
 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))
예제 #12
0
    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)
예제 #13
0
    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)
예제 #14
0
 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']))
예제 #15
0
    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'))
예제 #16
0
 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)
예제 #17
0
 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)
예제 #18
0
 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())
예제 #19
0
    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())
예제 #20
0
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')
예제 #21
0
 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())
예제 #22
0
 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)
예제 #23
0
    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
예제 #24
0
    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())
예제 #25
0
    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)
예제 #26
0
    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)
예제 #27
0
    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))
예제 #28
0
    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)
예제 #29
0
    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)
예제 #30
0
 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)