Skip to content

Commit 34b112d

Browse files
committed
documentation
1 parent 58737ea commit 34b112d

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

README.md

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ def grpc_servicer():
7272

7373

7474
@pytest.fixture(scope='module')
75-
def grpc_stub(grpc_channel):
75+
def grpc_stub_cls(grpc_channel):
7676
from stub.test_pb2_grpc import EchoServiceStub
7777

78-
return EchoServiceStub(grpc_channel)
78+
return EchoServiceStub
7979
```
8080

8181
Write little test:
@@ -94,6 +94,100 @@ def test_example(grpc_stub):
9494
assert response.name == f'test-{request.name}'
9595
```
9696

97+
#### Testing secure server
98+
99+
```python
100+
from pathlib import Path
101+
import pytest
102+
import grpc
103+
104+
@pytest.fixture(scope='module')
105+
def grpc_add_to_server():
106+
from stub.test_pb2_grpc import add_EchoServiceServicer_to_server
107+
108+
return add_EchoServiceServicer_to_server
109+
110+
111+
@pytest.fixture(scope='module')
112+
def grpc_servicer():
113+
from servicer import Servicer
114+
115+
return Servicer()
116+
117+
118+
@pytest.fixture(scope='module')
119+
def grpc_stub_cls(grpc_channel):
120+
from stub.test_pb2_grpc import EchoServiceStub
121+
122+
return EchoServiceStub
123+
124+
125+
@pytest.fixture(scope='session')
126+
def my_ssl_key_path():
127+
return Path('/path/to/key.pem')
128+
129+
130+
@pytest.fixture(scope='session')
131+
def my_ssl_cert_path():
132+
return Path('/path/to/cert.pem')
133+
134+
135+
@pytest.fixture(scope='module')
136+
def grpc_server(_grpc_server, grpc_addr, my_ssl_key_path, my_ssl_cert_path):
137+
"""
138+
Overwrites default `grpc_server` fixture with ssl credentials
139+
"""
140+
credentials = grpc.ssl_server_credentials([
141+
(my_ssl_key_path.read_bytes(),
142+
my_ssl_cert_path.read_bytes())
143+
])
144+
145+
_grpc_server.add_secure_port(grpc_addr, server_credentials=credentials)
146+
_grpc_server.start()
147+
yield _grpc_server
148+
_grpc_server.stop(grace=None)
149+
150+
151+
@pytest.fixture(scope='module')
152+
def my_channel_ssl_credentials(my_ssl_cert_path):
153+
# If we're using self-signed certificate it's necessarily to pass root certificate to channel
154+
return grpc.ssl_channel_credentials(
155+
root_certificates=my_ssl_cert_path.read_bytes()
156+
)
157+
158+
159+
@pytest.fixture(scope='module')
160+
def grpc_channel(my_channel_ssl_credentials, create_channel):
161+
"""
162+
Overwrites default `grpc_channel` fixture with ssl credentials
163+
"""
164+
with create_channel(my_channel_ssl_credentials) as channel:
165+
yield channel
166+
167+
168+
@pytest.fixture(scope='module')
169+
def grpc_authorized_channel(my_channel_ssl_credentials, create_channel):
170+
"""
171+
Channel with authorization header passed
172+
"""
173+
grpc_channel_credentials = grpc.access_token_call_credentials("some_token")
174+
composite_credentials = grpc.composite_channel_credentials(
175+
my_channel_ssl_credentials,
176+
grpc_channel_credentials
177+
)
178+
with create_channel(composite_credentials) as channel:
179+
yield channel
180+
181+
182+
@pytest.fixture(scope='module')
183+
def my_authorized_stub(grpc_stub_cls, grpc_channel):
184+
"""
185+
Stub with authorized channel
186+
"""
187+
return grpc_stub_cls(grpc_channel)
188+
189+
```
190+
97191
## Run tests agains real gRPC server
98192
Run tests against read grpc server worked in another thread:
99193

pytest_grpc/plugin.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def fake_handler(request):
7171
context = FakeContext()
7272

7373
def metadata_callbak(metadata, error):
74-
context._invocation_metadata += [_Metadatum(k, v) for k, v in metadata]
74+
context._invocation_metadata.extend((_Metadatum(k, v) for k, v in metadata))
7575

7676
if self._credentials and isinstance(self._credentials._credentials, CompositeChannelCredentials):
7777
for call_cred in self._credentials._credentials._call_credentialses:
@@ -141,7 +141,13 @@ def _create_channel(credentials=None, options=None):
141141

142142
@pytest.fixture(scope='module')
143143
def grpc_channel(create_channel):
144-
yield create_channel()
144+
with create_channel() as channel:
145+
yield channel
146+
147+
148+
@pytest.fixture(scope='module')
149+
def grpc_stub(grpc_stub_cls, grpc_channel):
150+
return grpc_stub_cls(grpc_channel)
145151

146152

147153
def pytest_addoption(parser):

0 commit comments

Comments
 (0)