From 00c02b25830a8fb7f8f3abce895b92b9ae3b17bf Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Wed, 31 Jan 2024 21:57:14 -0500 Subject: [PATCH] Test client gets api token from env --- tests/task_service.py | 6 +++--- tests/test_client.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/task_service.py b/tests/task_service.py index f83779ed..b8558b35 100644 --- a/tests/task_service.py +++ b/tests/task_service.py @@ -32,7 +32,7 @@ def _validate_authentication(self, context: grpc.ServicerContext): return context.abort( grpc.StatusCode.UNAUTHENTICATED, - f"Invalid authorization header. Expected '{expected}', got '{value!r}'", + f"Invalid authorization header. Expected '{expected}', got {value!r}", ) context.abort(grpc.StatusCode.UNAUTHENTICATED, "Missing authorization header") @@ -85,7 +85,7 @@ def __init__(self): self.thread_pool = concurrent.futures.thread.ThreadPoolExecutor() self.server = grpc.server(self.thread_pool) - port = self.server.add_insecure_port("127.0.0.1:0") + self.port = self.server.add_insecure_port("127.0.0.1:0") self.servicer = FakeRing() @@ -93,7 +93,7 @@ def __init__(self): self.server.start() self.client = Client( - api_key=_test_auth_token, api_url=f"http://127.0.0.1:{port}" + api_key=_test_auth_token, api_url=f"http://127.0.0.1:{self.port}" ) def stop(self): diff --git a/tests/test_client.py b/tests/test_client.py index c6ff45a6..de91c946 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,5 +1,8 @@ +import os import unittest +from unittest import mock +import grpc from google.protobuf import wrappers_pb2, any_pb2 from dispatch import Client, TaskInput, TaskID @@ -17,6 +20,16 @@ def setUp(self): def tearDown(self): self.server.stop() + @mock.patch.dict(os.environ, {"DISPATCH_API_KEY": "WHATEVER"}) + def test_api_key_from_env(self): + client = Client(api_url=f"http://127.0.0.1:{self.server.port}") + + with self.assertRaises(grpc._channel._InactiveRpcError) as mc: + client.create_tasks( + [TaskInput(coroutine_uri="my-cool-coroutine", input=42)] + ) + self.assertTrue("got 'Bearer WHATEVER'" in str(mc.exception)) + def test_create_one_task_pickle(self): results = self.client.create_tasks( [TaskInput(coroutine_uri="my-cool-coroutine", input=42)]