@@ -34,18 +34,29 @@ def test_can_be_constructed_on_https():
34
34
Client (api_url = "https://example.com" , api_key = "foo" )
35
35
36
36
37
+ # On Python 3.8/3.9, pytest.mark.asyncio doesn't work with mock.patch.dict,
38
+ # so we have to use the old-fashioned way of setting the environment variable
39
+ # and then cleaning it up manually.
40
+ #
41
+ # @mock.patch.dict(os.environ, {"DISPATCH_API_KEY": "0000000000000000"})
37
42
@pytest .mark .asyncio
38
- @mock .patch .dict (os .environ , {"DISPATCH_API_KEY" : "0000000000000000" })
39
43
async def test_api_key_from_env ():
40
- async with server () as api :
41
- client = Client (api_url = api .url )
42
-
43
- with pytest .raises (
44
- PermissionError ,
45
- match = r"Dispatch received an invalid authentication token \(check DISPATCH_API_KEY is correct\)" ,
46
- ) as mc :
47
- await client .dispatch ([Call (function = "my-function" , input = 42 )])
48
-
44
+ prev_api_key = os .environ .get ("DISPATCH_API_KEY" )
45
+ try :
46
+ os .environ ["DISPATCH_API_KEY" ] = "0000000000000000"
47
+ async with server () as api :
48
+ client = Client (api_url = api .url )
49
+
50
+ with pytest .raises (
51
+ PermissionError ,
52
+ match = r"Dispatch received an invalid authentication token \(check DISPATCH_API_KEY is correct\)" ,
53
+ ) as mc :
54
+ await client .dispatch ([Call (function = "my-function" , input = 42 )])
55
+ finally :
56
+ if prev_api_key is None :
57
+ del os .environ ["DISPATCH_API_KEY" ]
58
+ else :
59
+ os .environ ["DISPATCH_API_KEY" ] = prev_api_key
49
60
50
61
@pytest .mark .asyncio
51
62
async def test_api_key_from_arg ():
0 commit comments