Esempio n. 1
0
    def test_should_raise_error_when_password_is_not_str(self):
        # given
        self.user_repository_mock.read_by_user_name.return_value = None

        # when then
        with self.assertRaises(ValueError):
            user_service.create_user("test_editor", "test_user", 1)
Esempio n. 2
0
    def test_should_raise_error_when_password_is_empty_str(self):
        # given
        self.user_repository_mock.read_by_user_name = None

        # when then
        with self.assertRaises(BusinessError):
            user_service.create_user("test_editor", "test_user", "")
Esempio n. 3
0
def users_add():
    """
    Add a new user.

    Requires the following request body:
        {
            "userName": "******",
            "password": "******",
            "roles": [ (optional)
                <role1>,
                ...
            ]
        }
    """

    editor_name = None
    user_name = None
    password = None
    roles = None

    if flask.request.headers.get("Authorization") is not None:
        token = flask.request.headers.get("Authorization")
        editor_name = auth_service.get_user_name(token)

    if flask.request.json is not None:
        user_name = flask.request.json.get("userName")
        password = flask.request.json.get("password")
        roles = flask.request.json.get("roles")

    db_session = db_access.Session()

    try:
        user_service.create_user(editor_name, user_name, password, roles)
        db_session.commit()

        return flask.jsonify({
            "status": "success",
            "message": ""
        }), 200

    except BusinessError as err:
        db_session.rollback()

        return flask.jsonify({
            "status": "failure",
            "message": err.message
        }), 200

    except Exception as err:
        logger.log_error(str(err))
        db_session.rollback()

        return flask.jsonify({
            "status": "failure",
            "message": "Błąd aplikacji."
        }), 500

    finally:
        db_session.close()
Esempio n. 4
0
    def test_should_raise_error_when_user_already_exists(self):
        # given
        user = UserEntity()
        user.id = 1
        user.user_name = "test_user"

        self.user_repository_mock.read_by_user_name.return_value = user

        with self.assertRaises(BusinessError):
            user_service.create_user("test_editor", "test_user",
                                     "test_password")
Esempio n. 5
0
def create_superuser():
    """
    Create the superuser.
    """

    superuser_password = properties.get_superuser_password()

    if not superuser_password:
        raise RuntimeError("Property {} is missing!".format(
            properties.PROPERTY_SUPERUSER_PASSWORD))

    user_service.create_user("SYSTEM",
                             "superuser",
                             superuser_password,
                             roles=ALL_ROLES)
Esempio n. 6
0
    def test_should_create_new_user_with_roles(self):
        # given
        self.user_repository_mock.read_by_user_name.return_value = None
        self.security_mock.generate_password_hash.return_value = "test_hash"

        # when
        user_service.create_user("test_editor", "test_user", "test_password",
                                 ["role_1", "role_2"])

        # then
        self.user_repository_mock.persist.assert_called_once()

        user = self.user_repository_mock.persist.call_args[0][0]
        self.assertEqual(2, len(user.user_roles))
        self.assertEqual("role_1", user.user_roles[0].role_name)
        self.assertEqual("role_2", user.user_roles[1].role_name)
Esempio n. 7
0
    def test_should_create_new_user(self):
        # given
        self.user_repository_mock.read_by_user_name.return_value = None
        self.security_mock.generate_password_hash.return_value = "test_hash"

        # when
        user_service.create_user("test_editor", "test_user", "test_password")

        # then
        self.user_repository_mock.persist.assert_called_once()

        user = self.user_repository_mock.persist.call_args[0][0]
        self.assertIsInstance(user, UserEntity)
        self.assertEqual("test_user", user.user_name)
        self.assertEqual("test_hash", user.password_hash)

        self.assertEqual(1, len(user.user_history_entries))
        history_entry = user.user_history_entries[0]
        self.assertEqual("test_editor", history_entry.editor_name)
        self.assertEqual("Użytkownik test_user został utworzony.",
                         history_entry.message)
Esempio n. 8
0
 def test_should_raise_exception_when_roles_arg_is_not_list(self):
     # when then
     with self.assertRaises(ValueError):
         user_service.create_user("test_editor", "test_user",
                                  "test_password", 1)
Esempio n. 9
0
 def test_should_raise_exception_when_user_name_is_none(self):
     # when then
     with self.assertRaises(BusinessError):
         user_service.create_user("test_editor", None, "test_password")
Esempio n. 10
0
 def test_should_raise_exception_when_user_name_is_not_str(self):
     # when then
     with self.assertRaises(ValueError):
         user_service.create_user("test_editor", 1, "test_password")