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)
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()
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)
logging.info(f"Adding dummy data") try: add_dummy_data("src/dummy_data.json") except IntegrityError as e: print("Failed to insert dummy data, is it already inserted?") with session_scope(Session) as session: for user in session.query(User).all(): print(user) auth = Auth(Session) auth_server = grpc.server(futures.ThreadPoolExecutor(2)) auth_server.add_insecure_port("[::]:1752") auth_pb2_grpc.add_AuthServicer_to_server(auth, auth_server) auth_server.start() server = grpc.server(futures.ThreadPoolExecutor(2)) server = intercept_server(server, LoggingInterceptor()) server = intercept_server(server, auth.get_auth_interceptor()) server.add_insecure_port("[::]:1751") servicer = API(Session) api_pb2_grpc.add_APIServicer_to_server(servicer, server) server.start() logging.info(f"Serving on 1751 and 1752 (auth)") server.wait_for_termination() auth_server.wait_for_termination()