Example #1
0
    def test_setup_sqlalchemy(self):
        app_yml_file = FakeFile(
            textwrap.dedent("""
        global:
            engine.sqlalchemy.on: true
            environment: test_suite
        controllers:
            '':
                controller: !!python/name:blueberrypy.tests.test_command.Root
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
        sqlalchemy_engine:
            url: sqlite://
        """))

        path_file_mapping = {"/tmp/dev/app.yml": app_yml_file}
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()
        self.assertIsNone(blueberrypy.email._mailer)
        self.assertTrue(not hasattr(cherrypy.engine, "logging"))
        self.assertTrue(not hasattr(cherrypy.lib.sessions, "RedisSession"))
        self.assertIsInstance(cherrypy.engine.sqlalchemy,
                              blueberrypy.plugins.SQLAlchemyPlugin)
        self.assertIsInstance(cherrypy.tools.orm_session,
                              blueberrypy.tools.SQLAlchemySessionTool)
        self.assertIsNone(blueberrypy.template_engine.jinja2_env)
Example #2
0
    def test_setup_jinja2(self):
        app_yml_file = FakeFile(
            textwrap.dedent("""
        global:
            environment: test_suite
        controllers:
            '':
                controller: !!python/name:blueberrypy.tests.test_command.Root
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
        jinja2:
            loader: !!python/object:jinja2.loaders.DictLoader
                    mapping: {}
        """))

        path_file_mapping = {"/tmp/dev/app.yml": app_yml_file}
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()
        self.assertIsNone(blueberrypy.email._mailer)
        self.assertTrue(not hasattr(cherrypy.engine, "logging"))
        self.assertTrue(not hasattr(cherrypy.lib.sessions, "RedisSession"))
        self.assertTrue(not hasattr(cherrypy.engine, "sqlalchemy"))
        self.assertTrue(not hasattr(cherrypy.tools, "orm_session"))
        self.assertIsInstance(blueberrypy.template_engine.jinja2_env,
                              jinja2.Environment)
Example #3
0
    def test_setup_jinja2(self):
        app_yml_file = FakeFile(textwrap.dedent("""
        global:
            environment: test_suite
        controllers:
            '':
                controller: !!python/name:blueberrypy.tests.test_command.Root
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
        jinja2:
            loader: !!python/object:jinja2.loaders.DictLoader
                    mapping: {}
        """))

        path_file_mapping = {"/tmp/dev/app.yml": app_yml_file}
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()
        self.assertIsNone(blueberrypy.email._mailer)
        self.assertTrue(not hasattr(cherrypy.engine, "logging"))
        self.assertTrue(not hasattr(cherrypy.lib.sessions, "RedisSession"))
        self.assertTrue(not hasattr(cherrypy.engine, "sqlalchemy"))
        self.assertTrue(not hasattr(cherrypy.tools, "orm_session"))
        self.assertIsInstance(blueberrypy.template_engine.jinja2_env, jinja2.Environment)
Example #4
0
    def test_setup_cherrypy_redis(self):
        app_yml_file = FakeFile(
            textwrap.dedent("""
        global:
            environment: test_suite
        controllers:
            '':
                controller: !!python/name:blueberrypy.tests.test_command.Root
                /:
                    tools.sessions.storage_type: redis
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
        """))

        path_file_mapping = {"/tmp/dev/app.yml": app_yml_file}
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()
        self.assertIsNone(blueberrypy.email._mailer)
        self.assertTrue(not hasattr(cherrypy.engine, "logging"))
        self.assertEqual(cherrypy.lib.sessions.RedisSession,
                         blueberrypy.session.RedisSession)
        self.assertTrue(not hasattr(cherrypy.engine, "sqlalchemy"))
        self.assertTrue(not hasattr(cherrypy.tools, "orm_session"))
        self.assertIsNone(blueberrypy.template_engine.jinja2_env)
Example #5
0
    def test_setup_sqlalchemy(self):
        app_yml_file = FakeFile(textwrap.dedent("""
        global:
            engine.sqlalchemy.on: true
            environment: test_suite
        controllers:
            '':
                controller: !!python/name:blueberrypy.tests.test_command.Root
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
        sqlalchemy_engine:
            url: sqlite://
        """))

        path_file_mapping = {"/tmp/dev/app.yml": app_yml_file}
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()
        self.assertIsNone(blueberrypy.email._mailer)
        self.assertTrue(not hasattr(cherrypy.engine, "logging"))
        self.assertTrue(not hasattr(cherrypy.lib.sessions, "RedisSession"))
        self.assertIsInstance(cherrypy.engine.sqlalchemy, blueberrypy.plugins.SQLAlchemyPlugin)
        self.assertIsInstance(cherrypy.tools.orm_session, blueberrypy.tools.SQLAlchemySessionTool)
        self.assertIsNone(blueberrypy.template_engine.jinja2_env)
Example #6
0
    def test_setup_cherrypy_logging(self):
        app_yml_file = FakeFile(
            textwrap.dedent("""
        global:
            engine.logging.on: true
            environment: test_suite
        controllers:
            '':
                controller: !!python/name:blueberrypy.tests.test_command.Root
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
        """))
        logging_yml_file = FakeFile(
            textwrap.dedent("""
        version: 1
        disable_existing_loggers: false
        loggers:
            root:
                level: WARNING
                handlers: [stdout]
        handlers:
            stdout:
                class: logging.StreamHandler
                formatter: debug
                stream: ext://sys.stdout
        formatters:
            debug:
                format: '%(asctime)s [%(levelname)s] %(name)s Thread(id=%(thread)d, name="%(threadName)s") %(message)s'
        """))

        path_file_mapping = {
            "/tmp/dev/app.yml": app_yml_file,
            "/tmp/dev/logging.yml": logging_yml_file
        }
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()
        self.assertIsNone(blueberrypy.email._mailer)
        self.assertIsInstance(cherrypy.engine.logging,
                              blueberrypy.plugins.LoggingPlugin)
        self.assertTrue(not hasattr(cherrypy.lib.sessions, "RedisSession"))
        self.assertTrue(not hasattr(cherrypy.engine, "sqlalchemy"))
        self.assertTrue(not hasattr(cherrypy.tools, "orm_session"))
        self.assertIsNone(blueberrypy.template_engine.jinja2_env)
Example #7
0
    def test_bind(self):
        self._setup_basic_app_config()

        # hack to get around problem with not being able to acquire a port
        # after listening on a different ip/port in a previous test
        old_cherrypy_engine_start = cherrypy.engine.start

        def dummy_start(self):
            pass
        cherrypy.engine.start = dummy_start.__get__(cherrypy.engine, cherrypy.engine.__class__)

        sys.argv = "blueberrypy -C /tmp serve -b 0.0.0.0:9090".split()

        try:
            main()
            self.assertEqual(cherrypy.server.bind_addr, ("0.0.0.0", 9090))
        finally:
            cherrypy.engine.start = old_cherrypy_engine_start
Example #8
0
    def test_bind(self):
        self._setup_basic_app_config()

        # hack to get around problem with not being able to acquire a port
        # after listening on a different ip/port in a previous test
        old_cherrypy_engine_start = cherrypy.engine.start

        def dummy_start(self):
            pass

        cherrypy.engine.start = dummy_start.__get__(cherrypy.engine,
                                                    cherrypy.engine.__class__)

        sys.argv = "blueberrypy -C /tmp serve -b 0.0.0.0:9090".split()

        try:
            main()
            self.assertEqual(cherrypy.server.bind_addr, ("0.0.0.0", 9090))
        finally:
            cherrypy.engine.start = old_cherrypy_engine_start
Example #9
0
    def test_setup_rest_controller(self):
        app_yml_file = FakeFile(
            textwrap.dedent("""
        global:
            engine.sqlalchemy.on: true
            environment: test_suite
        controllers:
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
                /:
                    tools.sessions.storage_type: redis
                    tools.orm_session.on: true
        sqlalchemy_engine:
            url: sqlite://
        email:
            host: localhost
            port: 1025
        """))
        path_file_mapping = {"/tmp/dev/app.yml": app_yml_file}
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()

        app_config = load_yaml(app_yml_file.getvalue())
        controller_config = app_config["controllers"]['/api'].copy()
        controller = controller_config.pop("controller")
        controller_config["/"].update({"request.dispatch": controller})

        merged_app_config = cherrypy.tree.apps["/api"].config
        for k, v in list(controller_config.viewitems()):
            self.assertEqual(v, merged_app_config[k])

        for k, v in list(app_config["global"].viewitems()):
            self.assertEqual(v, merged_app_config["global"][k])

        for k, v in list(app_config["email"].viewitems()):
            self.assertEqual(v, merged_app_config["email"][k])

        for k, v in list(app_config["sqlalchemy_engine"].viewitems()):
            self.assertEqual(v, merged_app_config["sqlalchemy_engine"][k])
Example #10
0
    def test_setup_rest_controller(self):
        app_yml_file = FakeFile(textwrap.dedent("""
        global:
            engine.sqlalchemy.on: true
            environment: test_suite
        controllers:
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
                /:
                    tools.sessions.storage_type: redis
                    tools.orm_session.on: true
        sqlalchemy_engine:
            url: sqlite://
        email:
            host: localhost
            port: 1025
        """))
        path_file_mapping = {"/tmp/dev/app.yml": app_yml_file}
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()

        app_config = load_yaml(app_yml_file.getvalue())
        controller_config = app_config["controllers"]['/api'].copy()
        controller = controller_config.pop("controller")
        controller_config["/"].update({"request.dispatch": controller})

        merged_app_config = cherrypy.tree.apps["/api"].config
        for k, v in list(controller_config.viewitems()):
            self.assertEqual(v, merged_app_config[k])

        for k, v in list(app_config["global"].viewitems()):
            self.assertEqual(v, merged_app_config["global"][k])

        for k, v in list(app_config["email"].viewitems()):
            self.assertEqual(v, merged_app_config["email"][k])

        for k, v in list(app_config["sqlalchemy_engine"].viewitems()):
            self.assertEqual(v, merged_app_config["sqlalchemy_engine"][k])
Example #11
0
    def test_setup_cherrypy_logging(self):
        app_yml_file = FakeFile(textwrap.dedent("""
        global:
            engine.logging.on: true
            environment: test_suite
        controllers:
            '':
                controller: !!python/name:blueberrypy.tests.test_command.Root
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
        """))
        logging_yml_file = FakeFile(textwrap.dedent("""
        version: 1
        disable_existing_loggers: false
        loggers:
            root:
                level: WARNING
                handlers: [stdout]
        handlers:
            stdout:
                class: logging.StreamHandler
                formatter: debug
                stream: ext://sys.stdout
        formatters:
            debug:
                format: '%(asctime)s [%(levelname)s] %(name)s Thread(id=%(thread)d, name="%(threadName)s") %(message)s'
        """))

        path_file_mapping = {"/tmp/dev/app.yml": app_yml_file,
                             "/tmp/dev/logging.yml": logging_yml_file}
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()
        self.assertIsNone(blueberrypy.email._mailer)
        self.assertIsInstance(cherrypy.engine.logging, blueberrypy.plugins.LoggingPlugin)
        self.assertTrue(not hasattr(cherrypy.lib.sessions, "RedisSession"))
        self.assertTrue(not hasattr(cherrypy.engine, "sqlalchemy"))
        self.assertTrue(not hasattr(cherrypy.tools, "orm_session"))
        self.assertIsNone(blueberrypy.template_engine.jinja2_env)
Example #12
0
    def test_setup_cherrypy_redis(self):
        app_yml_file = FakeFile(textwrap.dedent("""
        global:
            environment: test_suite
        controllers:
            '':
                controller: !!python/name:blueberrypy.tests.test_command.Root
                /:
                    tools.sessions.storage_type: redis
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
        """))

        path_file_mapping = {"/tmp/dev/app.yml": app_yml_file}
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()
        self.assertIsNone(blueberrypy.email._mailer)
        self.assertTrue(not hasattr(cherrypy.engine, "logging"))
        self.assertEqual(cherrypy.lib.sessions.RedisSession, blueberrypy.session.RedisSession)
        self.assertTrue(not hasattr(cherrypy.engine, "sqlalchemy"))
        self.assertTrue(not hasattr(cherrypy.tools, "orm_session"))
        self.assertIsNone(blueberrypy.template_engine.jinja2_env)
Example #13
0
    def test_setup_cherrypy_all(self):
        app_yml_file = FakeFile(
            textwrap.dedent("""
        global:
            engine.logging.on: true
            engine.sqlalchemy.on: true
            environment: test_suite
        controllers:
            '':
                controller: !!python/name:blueberrypy.tests.test_command.Root
                /:
                    tools.sessions.storage_type: redis
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
        sqlalchemy_engine:
            url: sqlite://
        jinja2:
            loader: !!python/object:jinja2.loaders.DictLoader
                    mapping: {}
            use_webassets: true
        email:
            host: localhost
            port: 1025
        """))
        bundles_yml_file = FakeFile(
            textwrap.dedent("""
        directory: /tmp
        url: /
        bundles:
            nosuchjs.js:
                output: nosuchjs.js
                contents: nosuchjs.js
        """))
        logging_yml_file = FakeFile(
            textwrap.dedent("""
        version: 1
        disable_existing_loggers: false
        loggers:
            root:
                level: WARNING
                handlers: [stdout]
        handlers:
            stdout:
                class: logging.StreamHandler
                formatter: debug
                stream: ext://sys.stdout
        formatters:
            debug:
                format: '%(asctime)s [%(levelname)s] %(name)s Thread(id=%(thread)d, name="%(threadName)s") %(message)s'
        """))

        path_file_mapping = {
            "/tmp/dev/app.yml": app_yml_file,
            "/tmp/dev/bundles.yml": bundles_yml_file,
            "/tmp/dev/logging.yml": logging_yml_file
        }
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()
        self.assertIsInstance(blueberrypy.email._mailer,
                              blueberrypy.email.Mailer)
        self.assertIsInstance(cherrypy.engine.logging,
                              blueberrypy.plugins.LoggingPlugin)
        self.assertEqual(cherrypy.lib.sessions.RedisSession,
                         blueberrypy.session.RedisSession)
        self.assertIsInstance(cherrypy.engine.sqlalchemy,
                              blueberrypy.plugins.SQLAlchemyPlugin)
        self.assertIsInstance(cherrypy.tools.orm_session,
                              blueberrypy.tools.SQLAlchemySessionTool)
        self.assertIsInstance(blueberrypy.template_engine.jinja2_env,
                              jinja2.Environment)
        self.assertIsInstance(
            blueberrypy.template_engine.jinja2_env.assets_environment,
            webassets.Environment)
Example #14
0
    def test_setup_cherrypy_all(self):
        app_yml_file = FakeFile(textwrap.dedent("""
        global:
            engine.logging.on: true
            engine.sqlalchemy.on: true
            environment: test_suite
        controllers:
            '':
                controller: !!python/name:blueberrypy.tests.test_command.Root
                /:
                    tools.sessions.storage_type: redis
            /api:
                controller: !!python/name:blueberrypy.tests.test_command.rest_controller
        sqlalchemy_engine:
            url: sqlite://
        jinja2:
            loader: !!python/object:jinja2.loaders.DictLoader
                    mapping: {}
            use_webassets: true
        email:
            host: localhost
            port: 1025
        """))
        bundles_yml_file = FakeFile(textwrap.dedent("""
        directory: /tmp
        url: /
        bundles:
            nosuchjs.js:
                output: nosuchjs.js
                contents: nosuchjs.js
        """))
        logging_yml_file = FakeFile(textwrap.dedent("""
        version: 1
        disable_existing_loggers: false
        loggers:
            root:
                level: WARNING
                handlers: [stdout]
        handlers:
            stdout:
                class: logging.StreamHandler
                formatter: debug
                stream: ext://sys.stdout
        formatters:
            debug:
                format: '%(asctime)s [%(levelname)s] %(name)s Thread(id=%(thread)d, name="%(threadName)s") %(message)s'
        """))

        path_file_mapping = {"/tmp/dev/app.yml": app_yml_file,
                             "/tmp/dev/bundles.yml": bundles_yml_file,
                             "/tmp/dev/logging.yml": logging_yml_file}
        self._stub_out_path_and_open(path_file_mapping)

        sys.argv = ("blueberrypy -C /tmp serve").split()
        main()
        self.assertIsInstance(blueberrypy.email._mailer, blueberrypy.email.Mailer)
        self.assertIsInstance(cherrypy.engine.logging, blueberrypy.plugins.LoggingPlugin)
        self.assertEqual(cherrypy.lib.sessions.RedisSession, blueberrypy.session.RedisSession)
        self.assertIsInstance(cherrypy.engine.sqlalchemy, blueberrypy.plugins.SQLAlchemyPlugin)
        self.assertIsInstance(cherrypy.tools.orm_session, blueberrypy.tools.SQLAlchemySessionTool)
        self.assertIsInstance(blueberrypy.template_engine.jinja2_env, jinja2.Environment)
        self.assertIsInstance(blueberrypy.template_engine.jinja2_env.assets_environment,
                              webassets.Environment)