async def decorated_function(request, *args, **kwargs):
            is_authenticated = check_token(request)

            if is_authenticated:
                response = await f(request, *args, **kwargs)
                return response
            else:
                return text("You are unauthorized.", 401)
async def on_authorization(request, authentication_session):
    """
    Permissions authorization.
    """
    await check_roles(request, request.form.get("role"))
    if request.form.get("permissions_required"):
        await check_permissions(
            request,
            *request.form.get("permissions_required").split(", "))
    return text("Account permitted.")
async def on_role_assign(request, authentication_session):
    """
    Assigns authenticated account a role.
    """
    await assign_role(
        request.form.get("name"),
        "Role used for testing.",
        request.form.get("permissions"),
        authentication_session.bearer,
    )
    return text("Role assigned.")
Beispiel #4
0
def test_request_line(app):
    app.route("/")(lambda _: text(""))
    request, _ = app.test_client.get(
        "/",
        headers={
            "FOO": "bar",
            "Host": "example.com",
            "User-Agent": "Sanic-Testing",
        },
    )

    assert request.request_line == b"GET / HTTP/1.1"
Beispiel #5
0
def test_raw_headers(app):
    app.route("/")(lambda _: text(""))
    request, _ = app.test_client.get(
        "/",
        headers={
            "FOO": "bar",
            "Host": "example.com",
            "User-Agent": "Sanic-Testing",
        },
    )

    assert request.raw_headers == (
        b"Host: example.com\r\nAccept: */*\r\nAccept-Encoding: gzip, "
        b"deflate\r\nConnection: keep-alive\r\nUser-Agent: "
        b"Sanic-Testing\r\nFOO: bar")
Beispiel #6
0
async def http_receiver(request, protocol: T_BotProtocol):
    try:
        raw_event = request.json

        logger.debug(pformat(raw_event))
        await handle_event(protocol, raw_event)

    except EventHandleError as e:
        logger.error(e)

    except Exception:
        logger.exception("事件处理异常")

    finally:
        return text("")
Beispiel #7
0
def test_raw_headers(app):
    app.route("/")(lambda _: text(""))
    request, _ = app.test_client.get(
        "/",
        headers={
            "FOO": "bar",
            "Host": "example.com",
            "User-Agent": "Sanic-Testing",
        },
    )

    assert b"Host: example.com" in request.raw_headers
    assert b"Accept: */*" in request.raw_headers
    assert b"Accept-Encoding: gzip, deflate" in request.raw_headers
    assert b"Connection: keep-alive" in request.raw_headers
    assert b"User-Agent: Sanic-Testing" in request.raw_headers
    assert b"FOO: bar" in request.raw_headers
Beispiel #8
0
 async def handleStaticDirError(request, exception):
     if isinstance(exception, IsADirectoryError):
         return text(error_text, status=403)
     raise exception
Beispiel #9
0
async def handler(request):
    return text(request.ip)
Beispiel #10
0
def foo(request):
    return text("foo")
Beispiel #11
0
async def secret(request):
    return text("To go fast, you must be fast")
async def hello_handler(request) -> HTTPResponse:
    return text("Hello")
Beispiel #13
0
 async def base_handler(request):
     return text("111122223333444455556666777788889999")
Beispiel #14
0
def test(request):
    log.info("received request; responding with 'hey'")
    return text("hey")
Beispiel #15
0
 async def file_not_found(request, exception):
     return text(f"No file: {request.path}", status=404)
async def do_login(request):
    token = jwt.encode({}, request.app.config.SECRET)
    return text(token)
Beispiel #17
0
def bar(request):
    return text("bar")
Beispiel #18
0
 def handler(_):
     return text("It works")
Beispiel #19
0
 def handler(_):
     return text("Done.")