def test_method_not_implement():
    app = Starlette()
    router = StarletteRouter(app)

    async def handle(request):
        return PlainTextResponse()

    with pytest.raises(TypeError):
        router.handle("/h", handle)
def test_route_methods(method, pattern):
    app = Starlette()
    router = StarletteRouter(app)
    http_method = getattr(router, method.lower())
    http_method(pattern, make_request_factory(method))

    router.export()

    client = TestClient(app)
    caller = getattr(client, method.lower())
    resp = caller(pattern)
    assert resp.status_code == 200
    assert resp.text == method
def test_route_use_class_middleware():
    class ExampleMiddleware(BaseHTTPMiddleware):
        async def dispatch(self, request, call_next):
            response = await call_next(request)
            response.headers["m1"] = "Example"
            return response

    class InfoMiddleware(BaseHTTPMiddleware):
        async def dispatch(self, request, call_next):
            response = await call_next(request)
            response.headers["m2"] = "info"
            return response

    class CustomMiddleware3(BaseHTTPMiddleware):
        async def dispatch(self, request, call_next):
            response = await call_next(request)
            response.headers["m3"] = "inline"
            return response

    async def handler(request):
        return PlainTextResponse(content="test")

    app = Starlette()
    r = StarletteRouter(app).use(ExampleMiddleware).use(InfoMiddleware)
    r.include(CustomMiddleware3).get("/hello", handler)
    r.export()
    client = TestClient(app)

    resp = client.get("/hello")
    assert resp.status_code == 200
    assert resp.headers["m1"] == "Example"
    assert resp.headers["m2"] == "info"
    assert resp.headers["m3"] == "inline"
Esempio n. 4
0
    def wrapper(health_service):
        app = Starlette()
        ExceptionHandlers(app)
        app_handler = AppHandler(health_service)
        router = StarletteRouter(app)
        app_routers = AppRouter(app_handler, router)
        router.mount("/v1/app", app_routers.init())
        router.export()

        return app
def test_route_mount():
    app = Starlette()
    router = StarletteRouter(app)

    router.get("/hello", make_request_factory("hello"))

    sub_router = router.make_router()
    sub_router.get("/sub-hello", make_request_factory("hello"))

    router.mount("/sub/", sub_router)
    router.export()

    client = TestClient(app)

    resp = client.get("/hello")
    assert resp.status_code == 200
    assert resp.text == "hello"

    resp = client.get("/sub/sub-hello")
    assert resp.status_code == 200
    assert resp.text == "hello"
Esempio n. 6
0
    async def init_caffeine(self):
        jwt_helper = JwtHelper(str(self.settings.JWT_SECRET))
        e = casbin.Enforcer(self.settings.CASBIN_MODEL,
                            self.settings.CASBIN_POLICY)
        enforcer = Enforcer(e)
        security_container = SecurityContainer(
            jwt_helper,
            enforcer,
            self.settings.JWT_COOKIE_KEY,
            self.settings.JWT_COOKIE_REFRESH_KEY,
            self.settings.JWT_COOKIE_EXPIRE,
        )

        recaptcha = Recaptcha(self.settings.RECAPTCHA_SECRET)
        self.shutdown_events.put(recaptcha.shutdown)

        templater = Templater(self.settings.TEMPLATE_PATH)
        # PubSub
        pubsub_store = PubSubStore(self.db)
        pubsub = PostgresPubSub(pubsub_store)
        # User
        user_store = PostgreSQLUserStore(self.db)
        user_service = UserService(self.settings, user_store, pubsub,
                                   templater)
        user_handler = UserHandler(self.settings, user_service,
                                   security_container, recaptcha)

        # App
        health_service = HealthService(self.db)
        app_handler = AppHandler(health_service)

        auth_middleware = AuthenticationMiddleware(
            self.app,
            backend=JwtAuthBackend(user_service, security_container.jwt_helper,
                                   security_container.jwt_cookie_key),
        )
        router = StarletteRouter(self.app)
        user_routers = UserRouter(user_handler, auth_middleware, router)
        app_routers = AppRouter(app_handler, router)

        router.mount("/v1", app_routers.init())
        router.mount("/v1", user_routers.init())
        router.export()
Esempio n. 7
0
def app(user_service, recaptcha=True):
    starlette = Starlette()
    r = Mock(Recaptcha(""))
    r.check.return_value = recaptcha
    ExceptionHandlers(starlette)
    settings = Settings()
    settings.JWT_TOKEN_EXPIRE = 10
    settings.JWT_TOKEN_REFRESH_EXPIRE = 50
    user_handler = UserHandler(settings, user_service, security_container, r)
    router = StarletteRouter(starlette)
    auth_middleware = AuthenticationMiddleware(
        starlette,
        backend=JwtAuthBackend(user_service, security_container.jwt_helper,
                               security_container.jwt_cookie_key),
    )
    user_routers = UserRouter(user_handler, auth_middleware, router)
    router.mount("/v1", user_routers.init())
    router.export()

    return starlette