Exemple #1
0
    def test_should_persist_new_user_with_history_entry(self):
        # given
        user = UserEntity()
        user.user_name = "test_user"
        user.created_on = datetime.utcnow()
        user.modified_on = datetime.utcnow()
        user.opt_lock = 0

        user_history_entry = UserHistoryEntity()
        user_history_entry.created_on = datetime.utcnow()
        user_history_entry.modified_on = datetime.utcnow()
        user_history_entry.opt_lock = 0
        user_history_entry.editor_name = "test"
        user_history_entry.message = "test message"
        user_history_entry.user = user

        # when
        user_repository.persist(user)

        # then
        self.assertIsNotNone(
            self.session.query(UserEntity).filter_by(
                user_name="test_user").first())
        self.assertIsNotNone(
            self.session.query(UserHistoryEntity).filter_by(user=user).first())
Exemple #2
0
    def test_should_raise_value_error_when_argument_is_of_wrong_type(self):
        # given
        user = str()

        # when then
        with self.assertRaises(ValueError):
            user_repository.persist(user)
Exemple #3
0
    def test_should_raise_persistence_error_on_constraint_violation(self):
        # given
        user = UserEntity()

        # when then
        with self.assertRaises(PersistenceError):
            user_repository.persist(user)
Exemple #4
0
    def test_should_persist_new_user(self):
        # given
        user = UserEntity()
        user.user_name = "test_user"
        user.created_on = datetime.utcnow()
        user.modified_on = datetime.utcnow()
        user.opt_lock = 0

        # when
        user_repository.persist(user)

        # then
        self.assertIn(user, self.session.query(UserEntity).all())
Exemple #5
0
def create_user(editor_name, user_name, password, roles=None):
    """
    Create a new user with the given name and password, granting him/her the given roles.
    """

    validate_str_arg(editor_name, "Nazwa użytkownika wprowadzającego zmianę")
    validate_str_arg(user_name, "Nazwa użytkownika")
    validate_str_arg(password, "Hasło")
    validate_list_arg_nullable(roles, "Role")

    if user_repository.read_by_user_name(user_name) is not None:
        raise BusinessError("Użytkownik {} już istnieje!".format(user_name))

    timestamp = datetime.utcnow()
    user = _create_user_entity(user_name, password, timestamp)
    _grant_roles(user, roles, timestamp)
    _create_and_assing_history_entry_creation(user, editor_name, timestamp)

    user_repository.persist(user)
Exemple #6
0
    def test_should_update_user(self):
        # given
        user = UserEntity()
        user.user_name = "test_user"
        user.created_on = datetime.utcnow()
        user.modified_on = datetime.utcnow()
        user.opt_lock = 0

        self.session.add(user)
        self.session.flush()

        # when
        user.password_hash = "test_hash"
        user_repository.persist(user)

        # then
        self.assertEqual(
            "test_hash",
            self.session.query(UserEntity).filter_by(
                user_name="test_user").first().password_hash)
Exemple #7
0
    def test_should_persist_new_user_with_role(self):
        # given
        user = UserEntity()
        user.user_name = "test_user"
        user.created_on = datetime.utcnow()
        user.modified_on = datetime.utcnow()
        user.opt_lock = 0

        user_role = UserRoleEntity()
        user_role.created_on = datetime.utcnow()
        user_role.modified_on = datetime.utcnow()
        user_role.opt_lock = 0
        user_role.role_name = "test_role"
        user_role.user = user

        # when
        user_repository.persist(user)

        # then
        self.assertIsNotNone(
            self.session.query(UserEntity).filter_by(
                user_name="test_user").first())
        self.assertIsNotNone(
            self.session.query(UserRoleEntity).filter_by(user=user).first)
def grant_roles(editor_name, user_name, roles):
    """
    Grant the roles to the given user.
    """

    validate_str_arg(editor_name, "Editor name")
    validate_str_arg(user_name, "User name")
    validate_list_arg(roles, "Roles")

    user = user_repository.read_by_user_name(user_name)

    if not user:
        raise BusinessError(f"User {user_name} does not exist!")

    previous_user_roles = [role.role_name for role in user.user_roles]

    timestamp = datetime.utcnow()

    for role in roles:
        if role not in previous_user_roles:
            new_role = UserRoleEntity()
            new_role.created_on = timestamp
            new_role.modified_on = timestamp
            new_role.opt_lock = 0
            new_role.role_name = role
            new_role.user = user

            user_history_entry = UserHistoryEntity()
            user_history_entry.created_on = timestamp
            user_history_entry.modified_on = timestamp
            user_history_entry.opt_lock = 0
            user_history_entry.editor_name = editor_name
            user_history_entry.message = f"Przyznano rolę '{role}'."
            user_history_entry.user = user

    user_repository.persist(user)