def test_should_raise_exception_when_user_does_not_exist(self):
        # given
        self.user_repository_mock.read_by_user_name.return_value = None

        # when then
        with self.assertRaises(BusinessError):
            user_roles_service.grant_roles("test_editor", "test_user",
                                           ["test_role_1"])
示例#2
0
    def __init__(self):

        # Making sure that all the entities has been imported (i.e. initialized)
        assert auth_model is not None
        assert user_model is not None
        assert campaign_model is not None

        self.flask_app = flask.Flask(__name__)

        properties.init()

        self.host = properties.get_app_host()
        self.port = properties.get_app_port()

        db_access.init()

        # Registering web-service endpoints
        self.flask_app.register_blueprint(auth_endpoint.AUTH_BLUEPRINT)
        self.flask_app.register_blueprint(users_endpoint.USERS_BLUEPRINT)
        self.flask_app.register_blueprint(
            my_campaigns_endpoint.MY_CAMPAIGNS_BLUEPRINT)

        # Registering views
        self.flask_app.register_blueprint(index_view.INDEX_BLUEPRINT)

        session = db_access.Session()
        try:
            if not startup_service.superuser_exists():
                startup_service.create_superuser()
            else:
                superuser_roles = user_roles_service.get_user_roles(
                    "superuser")

                grant_required = False
                for role in roles.ALL_ROLES:
                    if role not in superuser_roles:
                        grant_required = True
                        break

                if grant_required:
                    user_roles_service.grant_roles("SYSTEM", "superuser",
                                                   roles.ALL_ROLES)

            session.commit()

        except Exception as err:
            session.rollback()
            logger.log_error("Error on application startup: {}".format(
                str(err)))
            raise err
        finally:
            session.close()

        logger.log_info("Application initialized.")
    def test_should_grant_new_roles_to_user(self):
        # given
        test_user = UserEntity()
        test_user.user_name = "test_user"

        self.user_repository_mock.read_by_user_name.return_value = test_user

        # when
        user_roles_service.grant_roles("test_editor", "test_user",
                                       ["test_role_1", "test_role_2"])

        # then
        self.user_repository_mock.persist.assert_called_once_with(test_user)
        self.assertEqual(2, len(test_user.user_roles))
        self.assertEqual("test_role_1", test_user.user_roles[0].role_name)
        self.assertEqual("test_role_2", test_user.user_roles[1].role_name)
    def test_should_not_modify_old_role_when_granting_new_one(self):
        # given
        test_user = UserEntity()
        test_user.user_name = "test_user"

        old_role = UserRoleEntity()
        old_role.role_name = "old_role"
        old_role.created_on = datetime(2010, 1, 1)
        old_role.user = test_user

        self.user_repository_mock.read_by_user_name.return_value = test_user

        # when
        user_roles_service.grant_roles("test_editor", "test_user",
                                       ["old_role", "new_role"])

        # then
        self.assertEqual(2, len(test_user.user_roles))
        self.assertEqual("old_role", test_user.user_roles[0].role_name)
        self.assertEqual(datetime(2010, 1, 1),
                         test_user.user_roles[0].created_on)
 def test_should_raise_exception_when_user_name_is_not_str(self):
     # when then
     with self.assertRaises(ValueError):
         user_roles_service.grant_roles("test_editor", 1, ["test_role_1"])
 def test_should_raise_exception_when_editor_name_is_empty(self):
     # when then
     with self.assertRaises(BusinessError):
         user_roles_service.grant_roles("", "test_user", ["test_role_1"])
 def test_should_raise_exception_when_roles_arg_is_not_list(self):
     # when then
     with self.assertRaises(ValueError):
         user_roles_service.grant_roles("test_editor", "test_user", 1)
 def test_should_raise_exception_when_roles_arg_is_none(self):
     # when then
     with self.assertRaises(BusinessError):
         user_roles_service.grant_roles("test_editor", "test_user", None)