Exemplo n.º 1
0
 def test_password_setter(self):
     old_password_hash = generate_password_hash('123456')
     user_data = {'password': old_password_hash}
     user = User(**user_data)
     self.assertEqual(user.password, old_password_hash)
     new_password_hash = generate_password_hash('new_pass')
     user.password = '******'
     self.assertEqual(user.password, new_password_hash)
Exemplo n.º 2
0
 def test_password_setter(self):
     old_password_hash = generate_password_hash('123456')
     user_data = {'password': old_password_hash}
     user = User(**user_data)
     self.assertEqual(user.password, old_password_hash)
     new_password_hash = generate_password_hash('new_pass')
     user.password = '******'
     self.assertEqual(user.password, new_password_hash)
Exemplo n.º 3
0
    def create_user(self, *args, **kwargs):
        """
        Create user in the database.

        :param args: tuple of arguments.
        :param kwargs: dictionary, where key is filled field of user model.
        """
        if 'username' not in kwargs or 'password' not in kwargs:
            raise RequiredModelFieldsNotDefined(
                "Username and password fields are required")

        kwargs['password'] = generate_password_hash(kwargs['password'])

        user_defined_fields = self.__user_defined_fields(kwargs)
        default_user_data = {
            'first_name': '',
            'last_name': '',
            'is_active': True,
            'is_superuser': False,
            'is_staff': False,
            'is_user': False,
        }

        default_user_data.update(user_defined_fields)
        user_data = [default_user_data[key] for key in self.fields_without_pk]

        try:
            self.db_manager.execute_sql(SQL_USER_ADD, user_data)
        except OperationalError as exc:
            logger.error(exc)
Exemplo n.º 4
0
    def update_user(self, *args, **kwargs):
        """Update user row in the database.

        :param args: tuple of arguments.
        :param kwargs: dictionary, where key is updated field of user model.
        """
        username = kwargs.pop('username', None)
        if not username:
            raise SearchCriteriaRequired(
                "Username for WHEN statement is required."
            )

        if len(kwargs) < 1:
            raise NotEnoughArguments()

        if 'password' in kwargs.keys():
            kwargs['password'] = generate_password_hash(kwargs['password'])

        updated_fields = self.__user_defined_fields(kwargs)
        update_query, query_args = construct_update_sql(**updated_fields)
        query_args.append(username)
        try:
            self.db_manager.execute_sql(update_query, query_args)
        except OperationalError as exc:
            logger.error(exc)
Exemplo n.º 5
0
    def create_user(self, *args, **kwargs):
        """Create user in the database.

        :param args: tuple of arguments.
        :param kwargs: dictionary, where key is filled field of user model.
        """
        if 'username' not in kwargs or 'password' not in kwargs:
            raise RequiredModelFieldsNotDefined(
                "Username and password fields are required"
            )

        kwargs['password'] = generate_password_hash(kwargs['password'])

        user_defined_fields = self.__user_defined_fields(kwargs)
        default_user_data = {
            'first_name': '',
            'last_name': '',
            'is_active': True,
            'is_superuser': False,
            'is_staff': False,
            'is_user': False,
        }

        default_user_data.update(user_defined_fields)
        user_data = [default_user_data[key] for key in self.fields_without_pk]

        try:
            self.db_manager.execute_sql(SQL_USER_ADD, user_data)
        except OperationalError as exc:
            logger.error(exc)
Exemplo n.º 6
0
    def password(self, password):
        """
        Set new password for user.

        :param password: password as a string.
        """
        self._password = generate_password_hash(password)
Exemplo n.º 7
0
    def check_password(self, password):
        """
        Check for a valid password has taken.

        :param password: password as a string.
        """
        return self.password == generate_password_hash(password)
Exemplo n.º 8
0
    def check_password(self, password):
        """
        Check for a valid password has taken.

        :param password: password as a string.
        """
        return self.password == generate_password_hash(password)
Exemplo n.º 9
0
    def password(self, password):
        """
        Set new password for user.

        :param password: password as a string.
        """
        self._password = generate_password_hash(password)
Exemplo n.º 10
0
    def update_user(self, *args, **kwargs):
        """
        Update user row in the database.

        :param args: tuple of arguments.
        :param kwargs: dictionary, where key is updated field of user model.
        """
        username = kwargs.pop('username', None)
        if not username:
            raise SearchCriteriaRequired(
                "Username for WHEN statement is required.")  # NOQA

        if len(kwargs) < 1:
            raise NotEnoughArguments()

        if 'password' in kwargs.keys():
            kwargs['password'] = generate_password_hash(kwargs['password'])

        updated_fields = self.__user_defined_fields(kwargs)
        update_query, query_args = construct_update_sql(**updated_fields)
        query_args.append(username)
        try:
            self.db_manager.execute_sql(update_query, query_args)
        except OperationalError as exc:
            logger.error(exc)
Exemplo n.º 11
0
    def test_has_permission(self):
        class DeleteAdminPermission(object):
            pass

        user_data = {
            'first_name': 'test',
            'last_name': 'user',
            'is_user': True,
            'password': generate_password_hash('123456'),
            'permissions': [DeleteAdminPermission, ]
        }
        user = User(**user_data)
        self.assertTrue(user.has_permission(DeleteAdminPermission))
Exemplo n.º 12
0
 def test_get_user_by_username_without_id(self):
     model = UserSQLiteModel()
     self._create_simple_user(model)
     user = model.get_user_by_username('testuser')
     self.assertIsNone(user.id)
     self.assertEquals(user.username, 'testuser')
     self.assertEquals(user.password, generate_password_hash('123456'))
     self.assertEquals(user.first_name, 'test')
     self.assertEquals(user.last_name, 'user')
     self.assertTrue(user.is_active)
     self.assertFalse(user.is_superuser)
     self.assertFalse(user.is_staff)
     self.assertTrue(user.is_user)
Exemplo n.º 13
0
    def test_has_permission_for_inactive_user(self):
        class ViewAdminSitePermission(object):
            pass

        user_data = {
            'first_name': 'test',
            'last_name': 'user',
            'is_active': False,
            'is_user': True,
            'password': generate_password_hash('123456'),
            'permissions': [ViewAdminSitePermission, ]
        }
        user = User(**user_data)
        self.assertFalse(user.has_permission(ViewAdminSitePermission))
Exemplo n.º 14
0
    def test_has_permission(self):
        class DeleteAdminPermission(object):
            pass

        user_data = {
            'first_name': 'test',
            'last_name': 'user',
            'is_user': True,
            'password': generate_password_hash('123456'),
            'permissions': [
                DeleteAdminPermission,
            ]
        }
        user = User(**user_data)
        self.assertTrue(user.has_permission(DeleteAdminPermission))
Exemplo n.º 15
0
    def test_has_permission_for_inactive_user(self):
        class ViewAdminSitePermission(object):
            pass

        user_data = {
            'first_name': 'test',
            'last_name': 'user',
            'is_active': False,
            'is_user': True,
            'password': generate_password_hash('123456'),
            'permissions': [
                ViewAdminSitePermission,
            ]
        }
        user = User(**user_data)
        self.assertFalse(user.has_permission(ViewAdminSitePermission))
Exemplo n.º 16
0
 def test_check_invalid_password(self):
     password = '******'
     password_hash = generate_password_hash(password)
     user_data = {'password': password_hash}
     user = User(**user_data)
     self.assertFalse(user.check_password('password'))
Exemplo n.º 17
0
 def test_password_getter_1(self):
     password_hash = generate_password_hash('123456')
     user_data = {'password': password_hash}
     user = User(**user_data)
     self.assertEqual(user.password, password_hash)
Exemplo n.º 18
0
def test_generate_password_hash(password, expected):
    assert generate_password_hash(password) == expected
Exemplo n.º 19
0
 def test_password_getter_1(self):
     password_hash = generate_password_hash('123456')
     user_data = {'password': password_hash}
     user = User(**user_data)
     self.assertEqual(user.password, password_hash)
Exemplo n.º 20
0
 def test_check_invalid_password(self):
     password = '******'
     password_hash = generate_password_hash(password)
     user_data = {'password': password_hash}
     user = User(**user_data)
     self.assertFalse(user.check_password('password'))