Ejemplo n.º 1
0
def test_controller_cache_is_server_cache():
    clear_expectations()
    clear()

    fake_server = Fake('server')
    fake_server.cache = "cache"

    controller = Controller()
    controller.server = fake_server

    assert controller.cache == "cache"
Ejemplo n.º 2
0
def test_send_template_by_mail():
    clear_expectations()
    clear()

    email_fake_server.has_attr(template_path="/some/path")

    controller = Controller()
    controller.server = email_fake_server

    status = controller.send_template_by_mail("some@user", ["to@user"], "some email", "email.html", some="argument")

    assert status == 0
Ejemplo n.º 3
0
def test_logging_calls_cherrypy_log_if_verbose_is_true():
    clear_expectations()
    clear()

    ctrl = Controller()
    ctrl.server = Fake('server')
    ctrl.server.context = Fake('context')
    ctrl.server.context.has_attr(settings=Fake('settings'))
    ctrl.server.context.settings.has_attr(Ion=Fake('Ion'))
    ctrl.server.context.settings.Ion.expects('as_bool').with_args('verbose').returns(True)

    ctrl.log('bla')
Ejemplo n.º 4
0
def test_logging_does_nothing_if_verbose_is_false():
    clear_expectations()
    clear()

    ctrl = Controller()
    ctrl.server = Fake('server')
    ctrl.server.context = Fake('context')
    ctrl.server.context.has_attr(settings=Fake('settings'))
    ctrl.server.context.settings.has_attr(Ion=Fake('Ion'))
    ctrl.server.context.settings.Ion.expects('as_bool').with_args('verbose').returns(False)

    ctrl.log('bla')
Ejemplo n.º 5
0
def test_healthcheck_action_returns_working_if_no_text_in_config():
    clear_expectations()
    clear()

    settings_health_check.Ion = Fake('Ion')
    settings_health_check.Ion.healthcheck_text = None

    fake_server_health_check = Fake('server')
    fake_server_health_check.expects('test_connection').returns(True)

    controller = Controller()
    controller.server = fake_server_health_check

    assert controller.healthcheck() == "WORKING"
Ejemplo n.º 6
0
def test_healthcheck_action():
    clear_expectations()
    clear()

    settings_health_check.Ion = Fake('Ion')
    settings_health_check.Ion.healthcheck_text = "CUSTOMTEXT"

    fake_server_health_check = Fake('server')
    fake_server_health_check.expects('test_connection').returns(True)

    controller = Controller()
    controller.server = fake_server_health_check

    assert controller.healthcheck() == "CUSTOMTEXT"
Ejemplo n.º 7
0
def test_render_template_in_folder_with_null_package():
    clear_expectations()
    clear()

    ctrl = Controller()
    ctrl.server = Fake('server')
    ctrl.server.has_attr(root_dir="some/root")
    ctrl.server.has_attr(template_path="some/root/templates")
    ctrl.server.apps = []
    ctrl.server.template_filters = {}
    ctrl.server.context = template_context3
    content = ctrl.render_template("some_file.html", some="args")

    assert content == "expected"
Ejemplo n.º 8
0
    def get_dispatcher(self):
        routes_dispatcher = cherrypy.dispatch.RoutesDispatcher()

        class MediaController():
            def __init__(self, apps):
                self.apps = apps

            def serve_media(self, media_url):
                for app in self.apps:
                    extension = splitext(media_url)[-1]
                    app_module = reduce(getattr, app.split('.')[1:], __import__(app))
                    path = inspect.getfile(app_module)
                    media_path = abspath(join("/".join(split(path)[:-1]), 'media', media_url))

                    if exists(media_path):
                        if extension == ".jpg":
                            content_type = "image/jpeg"
                        elif extension == ".gif":
                            content_type = "image/gif"
                        elif extension == ".png":
                            content_type = "image/png"
                        elif extension == ".js":
                            content_type = "text/javascript"
                        elif extension == ".css":
                            content_type = "text/css"
                        elif extension.startswith('.htm'):
                            content_type = "text/html"
                        else:
                            content_type = "text/plain"

                        return serve_file(media_path, content_type=content_type)

                raise cherrypy.HTTPError(404)

        for controller_type in Controller.all():
            controller = controller_type()
            controller.server = self
            controller.register_routes(routes_dispatcher)

        media_controller = MediaController(self.apps)
        routes_dispatcher.connect("media", "/media/{media_url:(.+)}", controller=media_controller, action="serve_media")

        route_name = "healthcheck"
        controller = Controller()
        controller.server = self
        routes_dispatcher.connect("healthcheck", "/healthcheck", controller=controller, action="healthcheck")

        dispatcher = routes_dispatcher
        return dispatcher
Ejemplo n.º 9
0
def test_render_template_uses_all_server_template_filters():
    clear_expectations()
    clear()

    fake_server = Fake('server')
    fake_server.template_path = "template_path"
    fake_server.template_filters = {"some":lambda x: "y"}

    controller = Controller()
    controller.server = fake_server
    controller.server.apps = []

    result = controller.render_template("template_file")

    assert result == "expected"
Ejemplo n.º 10
0
    def get_dispatcher(self):
        routes_dispatcher = cherrypy.dispatch.RoutesDispatcher()

        for controller_type in Controller.all():
            controller = controller_type()
            controller.server = self
            controller.register_routes(routes_dispatcher)

        route_name = "healthcheck"
        controller = Controller()
        controller.server = self
        routes_dispatcher.connect("healthcheck", "/healthcheck", controller=controller, action="healthcheck")

        dispatcher = routes_dispatcher
        return dispatcher
Ejemplo n.º 11
0
def test_healthcheck_action_fails_if_database_not_found():
    clear_expectations()
    clear()

    settings_health_check.Ion = Fake('Ion')
    settings_health_check.Ion.healthcheck_text = None

    fake_server_health_check = Fake('server')
    controller = Controller()
    controller.server = fake_server_health_check

    fake_server_health_check.expects('test_connection').returns(False)
    fake_server_health_check.test_connection_error = ValueError('Fake error')

    try:
        controller.healthcheck()
    except RuntimeError, err:
        assert str(err) == "The connection to the database failed with error: Fake error"
        return
Ejemplo n.º 12
0
def test_all_controllers_returns_all_imported_controllers():
    clear_expectations()
    clear()
    class TestController2(Controller):
        pass

    controllers = Controller.all()

    assert controllers
    assert len(controllers) == 1
    assert controllers[0] == TestController2