Skip to content

Commit 12e6987

Browse files
committed
Fix tests after updating base client
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 9be621d commit 12e6987

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

5044
class FakeService:
5145
"""Dispatch mock service for testing."""
@@ -82,63 +76,21 @@ def refresh_last_id_for(self, microgrid_id: MicrogridId) -> None:
8276

8377
self._last_id = max(self._last_id, max(dispatch.id for dispatch in dispatches))
8478

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

14496
return ListMicrogridDispatchesResponse(
@@ -157,14 +109,12 @@ async def ListMicrogridDispatches(
157109
async def StreamMicrogridDispatches(
158110
self,
159111
request: StreamMicrogridDispatchesRequest,
160-
metadata: grpc.aio.Metadata,
161112
timeout: int = 5, # pylint: disable=unused-argument
162113
) -> AsyncIterator[StreamMicrogridDispatchesResponse]:
163114
"""Stream microgrid dispatches changes.
164115
165116
Args:
166117
request: The request.
167-
metadata: The metadata.
168118
timeout: timeout for the request, ignored in this mock.
169119
170120
Returns:
@@ -173,8 +123,6 @@ async def StreamMicrogridDispatches(
173123
Yields:
174124
An event for each dispatch change.
175125
"""
176-
self._check_access(metadata)
177-
178126
receiver = self._stream_channel.new_receiver()
179127

180128
async for message in receiver:
@@ -229,11 +177,9 @@ def _filter_dispatch(
229177
async def CreateMicrogridDispatch(
230178
self,
231179
request: PBDispatchCreateRequest,
232-
metadata: grpc.aio.Metadata,
233180
timeout: int = 5, # pylint: disable=unused-argument
234181
) -> CreateMicrogridDispatchResponse:
235182
"""Create a new dispatch."""
236-
self._check_access(metadata)
237183
microgrid_id = MicrogridId(request.microgrid_id)
238184
self._last_id = DispatchId(int(self._last_id) + 1)
239185

@@ -259,12 +205,9 @@ async def CreateMicrogridDispatch(
259205
async def UpdateMicrogridDispatch(
260206
self,
261207
request: UpdateMicrogridDispatchRequest,
262-
metadata: grpc.aio.Metadata,
263208
timeout: int = 5, # pylint: disable=unused-argument
264209
) -> UpdateMicrogridDispatchResponse:
265210
"""Update a dispatch."""
266-
self._check_access(metadata)
267-
268211
microgrid_id = MicrogridId(request.microgrid_id)
269212
grid_dispatches = self.dispatches.get(microgrid_id, [])
270213
index = next(
@@ -353,11 +296,9 @@ async def UpdateMicrogridDispatch(
353296
async def GetMicrogridDispatch(
354297
self,
355298
request: GetMicrogridDispatchRequest,
356-
metadata: grpc.aio.Metadata,
357299
timeout: int = 5, # pylint: disable=unused-argument
358300
) -> GetMicrogridDispatchResponse:
359301
"""Get a single dispatch."""
360-
self._check_access(metadata)
361302
microgrid_id = MicrogridId(request.microgrid_id)
362303
grid_dispatches = self.dispatches.get(microgrid_id, [])
363304
dispatch = next(
@@ -378,11 +319,9 @@ async def GetMicrogridDispatch(
378319
async def DeleteMicrogridDispatch(
379320
self,
380321
request: DeleteMicrogridDispatchRequest,
381-
metadata: grpc.aio.Metadata,
382322
timeout: int = 5, # pylint: disable=unused-argument
383323
) -> Empty:
384324
"""Delete a given dispatch."""
385-
self._check_access(metadata)
386325
microgrid_id = MicrogridId(request.microgrid_id)
387326
grid_dispatches = self.dispatches.get(microgrid_id, [])
388327

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)