1
+ import asyncio
1
2
import json
2
3
3
4
import pytest
@@ -36,14 +37,15 @@ def requestor():
36
37
)
37
38
38
39
39
- @pytest .yield_fixture
40
- def working_client (loop , aiohttp_client ):
40
+ def working_client ():
41
+ loop = asyncio .get_running_loop ()
42
+
41
43
app = web .Application ()
42
44
43
45
app .router .add_get ('/.well-known/openid-configuration' , respond_json ({'token_endpoint' : '/token' }))
44
46
app .router .add_post ('/token' , respond_json ({'id-token' : 'id-token-data' , 'refresh-token' : 'refresh-token-data' }))
45
47
46
- with patch ('kubernetes_asyncio.config.openid.OpenIDRequestor._client_session ' ) as _client_session :
48
+ with patch ('kubernetes_asyncio.config.openid.aiohttp.ClientSession ' ) as _client_session :
47
49
client = TestClient (TestServer (app , loop = loop ), loop = loop )
48
50
_client_session .return_value = client
49
51
loop .run_until_complete (client .start_server ())
@@ -53,13 +55,13 @@ def working_client(loop, aiohttp_client):
53
55
loop .run_until_complete (client .close ())
54
56
55
57
56
- @ pytest . yield_fixture
57
- def fail_well_known_client ( loop , aiohttp_client ):
58
+ def fail_well_known_client ():
59
+ loop = asyncio . get_running_loop ()
58
60
app = web .Application ()
59
61
60
62
app .router .add_get ('/.well-known/openid-configuration' , make_responder (web .Response (status = 500 )))
61
63
62
- with patch ('kubernetes_asyncio.config.openid.OpenIDRequestor._client_session ' ) as _client_session :
64
+ with patch ('kubernetes_asyncio.config.openid.aiohttp.ClientSession ' ) as _client_session :
63
65
client = TestClient (TestServer (app , loop = loop ), loop = loop )
64
66
_client_session .return_value = client
65
67
loop .run_until_complete (client .start_server ())
@@ -69,8 +71,8 @@ def fail_well_known_client(loop, aiohttp_client):
69
71
loop .run_until_complete (client .close ())
70
72
71
73
72
- @ pytest . yield_fixture
73
- def fail_token_request_client ( loop , aiohttp_client ):
74
+ def fail_token_request_client ():
75
+ loop = asyncio . get_running_loop ()
74
76
app = web .Application ()
75
77
76
78
app .router .add_get ('/.well-known/openid-configuration' , respond_json ({'token_endpoint' : '/token' }))
@@ -86,18 +88,21 @@ def fail_token_request_client(loop, aiohttp_client):
86
88
loop .run_until_complete (client .close ())
87
89
88
90
89
- async def test_refresh_token_success (requestor , working_client ):
90
- resp = await requestor .refresh_token ('my-refresh-token' )
91
+ async def test_refresh_token_success (requestor ):
92
+ with working_client ():
93
+ resp = await requestor .refresh_token ('my-refresh-token' )
91
94
92
- assert resp ['id-token' ] == 'id-token-data'
93
- assert resp ['refresh-token' ] == 'refresh-token-data'
95
+ assert resp ['id-token' ] == 'id-token-data'
96
+ assert resp ['refresh-token' ] == 'refresh-token-data'
94
97
95
98
96
- async def test_failed_well_known (requestor , fail_well_known_client ):
97
- with pytest .raises (ConfigException ):
98
- await requestor .refresh_token ('my-refresh-token' )
99
+ async def test_failed_well_known (requestor ):
100
+ with fail_well_known_client ():
101
+ with pytest .raises (ConfigException ):
102
+ await requestor .refresh_token ('my-refresh-token' )
99
103
100
104
101
- async def test_failed_refresh_token (requestor , fail_token_request_client ):
102
- with pytest .raises (ConfigException ):
103
- await requestor .refresh_token ('my-refresh-token' )
105
+ async def test_failed_refresh_token (requestor ):
106
+ with fail_token_request_client ():
107
+ with pytest .raises (ConfigException ):
108
+ await requestor .refresh_token ('my-refresh-token' )
0 commit comments