Beispiel #1
0
def real_api_session(token):
    """
    Create an API for testing, using TCP sockets, uses the token for auth
    """
    auth_interceptor = Auth().get_auth_interceptor(allow_jailed=False)

    with futures.ThreadPoolExecutor(1) as executor:
        server = grpc.server(executor, interceptors=[auth_interceptor])
        port = server.add_secure_port("localhost:0",
                                      grpc.local_server_credentials())
        servicer = API()
        api_pb2_grpc.add_APIServicer_to_server(servicer, server)
        server.start()

        call_creds = grpc.metadata_call_credentials(
            CookieMetadataPlugin(token))
        comp_creds = grpc.composite_channel_credentials(
            grpc.local_channel_credentials(), call_creds)

        try:
            with grpc.secure_channel(f"localhost:{port}",
                                     comp_creds) as channel:
                yield api_pb2_grpc.APIStub(channel)
        finally:
            server.stop(None).wait()
Beispiel #2
0
def api_session(token):
    """
    Create an API for testing, uses the token for auth
    """
    channel = fake_channel(token)
    api_pb2_grpc.add_APIServicer_to_server(API(), channel)
    yield api_pb2_grpc.APIStub(channel)
Beispiel #3
0
def api_session(db, token):
    """
    Create an API for testing, uses the token for auth
    """
    user_id, jailed = Auth(db).get_session_for_token(token)
    channel = FakeChannel(user_id=user_id)
    api_pb2_grpc.add_APIServicer_to_server(API(db), channel)
    yield api_pb2_grpc.APIStub(channel)