Example #1
0
def run_fake_rlp_gateway(envelopes):
    app = Sanic(name="".join(random.choices(string.ascii_lowercase, k=16)))

    expected_token = "good-token"

    def set_token(token):
        nonlocal expected_token
        expected_token = token

    @app.route("/v2/read", stream=True)
    async def stream_envelopes(req):  # pylint:disable=unused-variable
        auth_value = req.headers.get("Authorization")

        def is_auth_valid():
            expected_auth = b"bearer " + expected_token.encode("utf-8")
            return expected_auth == auth_value.encode("utf-8")

        if not is_auth_valid():
            return response.text("Unauthorized (bad token)", status=401)

        async def streaming(resp):
            while True:
                if not is_auth_valid():
                    return

                for e in envelopes:
                    data = b"data: " + json.dumps(e).encode("utf-8") + b"\n\n"
                    await resp.write(data)
                await asyncio.sleep(1)

        return response.stream(streaming)

    with run_simple_sanic_app(app) as url:
        yield url, set_token
Example #2
0
    @app.post("/oauth/token")
    async def get_token(req):  # pylint:disable=unused-variable
        auth_value = req.headers.get("Authorization")
        expected_auth = b"Basic " + b64encode(b"myusername:mypassword")
        if expected_auth == auth_value.encode("utf-8"):
            json_data = {
                "access_token": token,
                "token_type": "bearer",
                "expires_in": 1_000_000,
                "scope": "",
                "jti": "28edda5c-4e37-4a63-9ba3-b32f48530a51",
            }
            return response.json(json_data)
        return response.text("Unauthorized", status=401)

    with run_simple_sanic_app(app) as url:
        yield url, set_token


@contextmanager
def run_fake_rlp_gateway(envelopes):
    app = Sanic(name="".join(random.choices(string.ascii_lowercase, k=16)))

    expected_token = "good-token"

    def set_token(token):
        nonlocal expected_token
        expected_token = token

    @app.route("/v2/read", stream=True)
    async def stream_envelopes(req):  # pylint:disable=unused-variable