Example #1
0
class ModelUserTD(unittest.TestCase):
    def setUp(self):
        self.db_handle = db_util.create_db_handle_no_app()
        self.tables = ImportedTables(self.db_handle)
        self.user_with_roles = self.tables.User(
            username='******')
        self.role = self.tables.Role()
        self.role.name = "new_role"
        self.role.role_id = 1
        self.user_with_roles.roles = [self.role]
        self.user = self.tables.User(username='******')
        self.user_with_roles.user_id = 1
        self.user.user_id = 2

    def test_crypt_password(self):
        password = self.user.crypt_password('password_test')
        self.assertTrue(password != "password_test")
        self.user.password = self.user.crypt_password('password_test')
        self.assertTrue(self.user.verify_password('password_test'))
        self.assertFalse(self.user.verify_password('password_not_test'))

    def test_user_auth(self):
        self.assertTrue(self.user.is_authenticated())
        self.assertTrue(self.user.is_active())
        self.assertFalse(self.user.is_anonymous())

    def test_to_dict_simple(self):
        self.user.password = self.user.crypt_password('password_two')
        simple_dict_with_roles = self.user_with_roles.to_dict_simple()
        for value in self.user.__table__.columns:
            key = str(value)[str(value).index('.') + 1:]
            if key != 'password_crypt':
                self.assertTrue(key in simple_dict_with_roles,
                                "oops - did not find %s" % key)
        self.assertTrue('password_crypt' not in simple_dict_with_roles)
        self.assertTrue('roles' in simple_dict_with_roles)
        self.assertEquals(len(simple_dict_with_roles['roles']), 1)
        simple_dict_no_roles = self.user.to_dict_simple()
        self.assertTrue('roles' not in simple_dict_no_roles)
Example #2
0
class UtilAuthTD(unittest.TestCase):
    def setUp(self):
        self.app = Flask("dummy")
        self.app.secret_key = 'poop'
        LoginManager().init_app(self.app)
        self.db_handle = db_util.create_db_handle_no_app()
        self.tables = ImportedTables(self.db_handle)
        self.app.tables = self.tables
        self.user_with_roles = self.tables.User()
        self.role = self.tables.Role()
        self.role.name = "new_role"
        self.user_with_roles.roles = [self.role]
        self.id = Identity('dummy')

    def test_on_identity_loaded_callback(self):
        with self.app.test_client() as c:
            with self.app.test_request_context('/') as req_c:
                login_user(self.user_with_roles)
                self.assertIsNone(getattr(self.id, 'user', None))
                on_identity_loaded_callback = auth.generate_identity_loaded(
                    self.app)
                self.assertEqual(len(self.id.provides), 0)
                on_identity_loaded_callback(None, self.id)
                self.assertEqual(len(self.id.provides), 2)

    def test_generate_user_loader(self):
        self.app.tables = MagicMock()
        self.app.tables.User.query.get.return_value = self.user_with_roles
        self.app.td_config = {'PLAYER_LOGIN': 0}

        with self.app.test_client() as c:
            with self.app.test_request_context('/') as req_c:
                user_loader = auth.generate_user_loader(self.app)
                returned_user = user_loader(1)
                self.assertEquals(returned_user, self.user_with_roles)
        self.app.td_config = {'PLAYER_LOGIN': 1}

        with self.app.test_client() as c:
            with self.app.test_request_context('/') as req_c:
                user_loader = auth.generate_user_loader(self.app)
                returned_user = user_loader(1)
                self.assertEquals(returned_user, self.user_with_roles)