Skip to content

Commit 2114f7e

Browse files
committed
Fix tests after updating base client
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 7c6ca37 commit 2114f7e

File tree

3 files changed

+5
-66
lines changed

3 files changed

+5
-66
lines changed

src/frequenz/client/dispatch/test/_service.py

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@
4040
from .._internal_types import DispatchCreateRequest
4141
from ..types import Dispatch, DispatchEvent, DispatchId, Event
4242

43-
ALL_KEY = "all"
44-
"""Key that has access to all resources in the FakeService."""
45-
46-
NONE_KEY = "none"
47-
"""Key that has no access to any resources in the FakeService."""
48-
4943
_logger = logging.getLogger(__name__)
5044

5145

@@ -84,63 +78,21 @@ def refresh_last_id_for(self, microgrid_id: MicrogridId) -> None:
8478

8579
self._last_id = max(self._last_id, max(dispatch.id for dispatch in dispatches))
8680

87-
def _check_access(self, metadata: grpc.aio.Metadata) -> None:
88-
"""Check if the access key is valid.
89-
90-
Args:
91-
metadata: The metadata.
92-
93-
Raises:
94-
grpc.RpcError: If the access key is invalid.
95-
"""
96-
# metadata is a weird tuple of tuples, we don't like it
97-
metadata_dict = dict(metadata)
98-
99-
if "key" not in metadata_dict:
100-
raise grpc.RpcError(
101-
grpc.StatusCode.UNAUTHENTICATED,
102-
"No access key provided",
103-
)
104-
105-
key = metadata_dict["key"]
106-
107-
if key is None:
108-
raise grpc.RpcError(
109-
grpc.StatusCode.UNAUTHENTICATED,
110-
"No access key provided",
111-
)
112-
113-
if key == NONE_KEY:
114-
raise grpc.RpcError(
115-
grpc.StatusCode.PERMISSION_DENIED,
116-
"Permission denied",
117-
)
118-
119-
if key != ALL_KEY:
120-
raise grpc.RpcError(
121-
grpc.StatusCode.UNAUTHENTICATED,
122-
"Invalid access key",
123-
)
124-
12581
# pylint: disable=invalid-name
12682
async def ListMicrogridDispatches(
12783
self,
12884
request: ListMicrogridDispatchesRequest,
129-
metadata: grpc.aio.Metadata,
13085
timeout: int = 5, # pylint: disable=unused-argument
13186
) -> ListMicrogridDispatchesResponse:
13287
"""List microgrid dispatches.
13388
13489
Args:
13590
request: The request.
136-
metadata: The metadata.
13791
timeout: timeout for the request, ignored in this mock.
13892
13993
Returns:
14094
The dispatch list.
14195
"""
142-
self._check_access(metadata)
143-
14496
grid_dispatches = self.dispatches.get(MicrogridId(request.microgrid_id), [])
14597

14698
return ListMicrogridDispatchesResponse(
@@ -159,14 +111,12 @@ async def ListMicrogridDispatches(
159111
async def StreamMicrogridDispatches(
160112
self,
161113
request: StreamMicrogridDispatchesRequest,
162-
metadata: grpc.aio.Metadata,
163114
timeout: int = 5, # pylint: disable=unused-argument
164115
) -> AsyncIterator[StreamMicrogridDispatchesResponse]:
165116
"""Stream microgrid dispatches changes.
166117
167118
Args:
168119
request: The request.
169-
metadata: The metadata.
170120
timeout: timeout for the request, ignored in this mock.
171121
172122
Returns:
@@ -175,8 +125,6 @@ async def StreamMicrogridDispatches(
175125
Yields:
176126
An event for each dispatch change.
177127
"""
178-
self._check_access(metadata)
179-
180128
receiver = self._stream_channel.new_receiver()
181129

182130
async for message in receiver:
@@ -231,11 +179,9 @@ def _filter_dispatch(
231179
async def CreateMicrogridDispatch(
232180
self,
233181
request: PBDispatchCreateRequest,
234-
metadata: grpc.aio.Metadata,
235182
timeout: int = 5, # pylint: disable=unused-argument
236183
) -> CreateMicrogridDispatchResponse:
237184
"""Create a new dispatch."""
238-
self._check_access(metadata)
239185
microgrid_id = MicrogridId(request.microgrid_id)
240186
self._last_id = DispatchId(int(self._last_id) + 1)
241187

@@ -261,12 +207,9 @@ async def CreateMicrogridDispatch(
261207
async def UpdateMicrogridDispatch(
262208
self,
263209
request: UpdateMicrogridDispatchRequest,
264-
metadata: grpc.aio.Metadata,
265210
timeout: int = 5, # pylint: disable=unused-argument
266211
) -> UpdateMicrogridDispatchResponse:
267212
"""Update a dispatch."""
268-
self._check_access(metadata)
269-
270213
microgrid_id = MicrogridId(request.microgrid_id)
271214
grid_dispatches = self.dispatches.get(microgrid_id, [])
272215
index = next(
@@ -355,11 +298,9 @@ async def UpdateMicrogridDispatch(
355298
async def GetMicrogridDispatch(
356299
self,
357300
request: GetMicrogridDispatchRequest,
358-
metadata: grpc.aio.Metadata,
359301
timeout: int = 5, # pylint: disable=unused-argument
360302
) -> GetMicrogridDispatchResponse:
361303
"""Get a single dispatch."""
362-
self._check_access(metadata)
363304
microgrid_id = MicrogridId(request.microgrid_id)
364305
grid_dispatches = self.dispatches.get(microgrid_id, [])
365306
dispatch = next(
@@ -380,11 +321,9 @@ async def GetMicrogridDispatch(
380321
async def DeleteMicrogridDispatch(
381322
self,
382323
request: DeleteMicrogridDispatchRequest,
383-
metadata: grpc.aio.Metadata,
384324
timeout: int = 5, # pylint: disable=unused-argument
385325
) -> Empty:
386326
"""Delete a given dispatch."""
387-
self._check_access(metadata)
388327
microgrid_id = MicrogridId(request.microgrid_id)
389328
grid_dispatches = self.dispatches.get(microgrid_id, [])
390329

src/frequenz/client/dispatch/test/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
from .. import DispatchApiClient
1111
from ..types import Dispatch
12-
from ._service import ALL_KEY, NONE_KEY, FakeService
12+
from ._service import FakeService
1313

14-
__all__ = ["FakeClient", "to_create_params", "ALL_KEY", "NONE_KEY"]
14+
__all__ = ["FakeClient", "to_create_params"]
1515

1616

1717
class FakeClient(DispatchApiClient):
@@ -24,7 +24,7 @@ def __init__(
2424
self,
2525
) -> None:
2626
"""Initialize the mock client."""
27-
super().__init__(server_url="mock", auth_key=ALL_KEY, connect=False)
27+
super().__init__(server_url="mock", auth_key="what", connect=False)
2828
self._stuba: FakeService = FakeService()
2929

3030
@property

tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
RecurrenceRule,
2222
Weekday,
2323
)
24-
from frequenz.client.dispatch.test.client import ALL_KEY, FakeClient
24+
from frequenz.client.dispatch.test.client import FakeClient
2525
from frequenz.client.dispatch.types import (
2626
Dispatch,
2727
DispatchId,
@@ -33,7 +33,7 @@
3333
TEST_NOW = datetime(2023, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
3434
"""Arbitrary time used as NOW for testing."""
3535

36-
ENVIRONMENT_VARIABLES = {"DISPATCH_API_KEY": ALL_KEY}
36+
ENVIRONMENT_VARIABLES = {"DISPATCH_API_KEY": "test_key"}
3737

3838

3939
@pytest.fixture

0 commit comments

Comments
 (0)