Skip to content

Commit

Permalink
Test client gets api token from env
Browse files Browse the repository at this point in the history
  • Loading branch information
pelletier committed Feb 1, 2024
1 parent e2b9e91 commit 00c02b2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions tests/task_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -85,15 +85,15 @@ 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()

service_grpc.add_ServiceServicer_to_server(self.servicer, self.server)
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):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)]
Expand Down

0 comments on commit 00c02b2

Please sign in to comment.