1
+ import asyncio
1
2
import json
3
+ from contextlib import contextmanager
2
4
3
- import pytest
4
5
from aiohttp import web
5
- from aiohttp .test_utils import TestClient
6
- from aiohttp .test_utils import TestServer
6
+ from aiohttp .test_utils import TestClient as _TestClient
7
+ from aiohttp .test_utils import TestServer as _TestServer
7
8
from asynctest import patch
9
+ from asynctest import TestCase
8
10
9
11
from .config_exception import ConfigException
10
12
from .openid import OpenIDRequestor
@@ -27,77 +29,72 @@ def respond_json(data):
27
29
)
28
30
29
31
30
- @pytest .fixture
31
- def requestor ():
32
- return OpenIDRequestor (
33
- 'client-id' ,
34
- 'client-secret' ,
35
- '' ,
36
- )
37
-
32
+ @contextmanager
33
+ def working_client ():
34
+ loop = asyncio .get_event_loop ()
38
35
39
- @pytest .yield_fixture
40
- def working_client (loop , aiohttp_client ):
41
36
app = web .Application ()
42
37
43
38
app .router .add_get ('/.well-known/openid-configuration' , respond_json ({'token_endpoint' : '/token' }))
44
39
app .router .add_post ('/token' , respond_json ({'id-token' : 'id-token-data' , 'refresh-token' : 'refresh-token-data' }))
45
40
46
- with patch ('kubernetes_asyncio.config.openid.OpenIDRequestor._client_session ' ) as _client_session :
47
- client = TestClient ( TestServer (app , loop = loop ), loop = loop )
41
+ with patch ('kubernetes_asyncio.config.openid.aiohttp.ClientSession ' ) as _client_session :
42
+ client = _TestClient ( _TestServer (app , loop = loop ), loop = loop )
48
43
_client_session .return_value = client
49
- loop .run_until_complete (client .start_server ())
50
44
51
45
yield client
52
46
53
- loop .run_until_complete (client .close ())
54
47
55
-
56
- @ pytest . yield_fixture
57
- def fail_well_known_client ( loop , aiohttp_client ):
48
+ @ contextmanager
49
+ def fail_well_known_client ():
50
+ loop = asyncio . get_event_loop ()
58
51
app = web .Application ()
59
52
60
53
app .router .add_get ('/.well-known/openid-configuration' , make_responder (web .Response (status = 500 )))
61
54
62
- with patch ('kubernetes_asyncio.config.openid.OpenIDRequestor._client_session ' ) as _client_session :
63
- client = TestClient ( TestServer (app , loop = loop ), loop = loop )
55
+ with patch ('kubernetes_asyncio.config.openid.aiohttp.ClientSession ' ) as _client_session :
56
+ client = _TestClient ( _TestServer (app , loop = loop ), loop = loop )
64
57
_client_session .return_value = client
65
- loop .run_until_complete (client .start_server ())
66
-
67
58
yield client
68
59
69
- loop .run_until_complete (client .close ())
70
-
71
60
72
- @pytest .yield_fixture
73
- def fail_token_request_client (loop , aiohttp_client ):
61
+ @contextmanager
62
+ def fail_token_request_client ():
63
+ loop = asyncio .get_event_loop ()
74
64
app = web .Application ()
75
65
76
66
app .router .add_get ('/.well-known/openid-configuration' , respond_json ({'token_endpoint' : '/token' }))
77
67
app .router .add_post ('/token' , make_responder (web .Response (status = 500 )))
78
68
79
69
with patch ('kubernetes_asyncio.config.openid.aiohttp.ClientSession' ) as _client_session :
80
- client = TestClient ( TestServer (app , loop = loop ), loop = loop )
70
+ client = _TestClient ( _TestServer (app , loop = loop ), loop = loop )
81
71
_client_session .return_value = client
82
- loop .run_until_complete (client .start_server ())
83
72
84
73
yield client
85
74
86
- loop .run_until_complete (client .close ())
87
75
76
+ class OpenIDRequestorTest (TestCase ):
88
77
89
- async def test_refresh_token_success (requestor , working_client ):
90
- resp = await requestor .refresh_token ('my-refresh-token' )
91
-
92
- assert resp ['id-token' ] == 'id-token-data'
93
- assert resp ['refresh-token' ] == 'refresh-token-data'
78
+ def setUp (self ):
79
+ self .requestor = OpenIDRequestor (
80
+ 'client-id' ,
81
+ 'client-secret' ,
82
+ '' ,
83
+ )
94
84
85
+ async def test_refresh_token_success (self ):
86
+ with working_client ():
87
+ resp = await self .requestor .refresh_token ('my-refresh-token' )
95
88
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' )
89
+ assert resp ['id-token' ] == 'id-token-data'
90
+ assert resp ['refresh-token' ] == 'refresh-token-data'
99
91
92
+ async def test_failed_well_known (self ):
93
+ with fail_well_known_client ():
94
+ with self .assertRaises (ConfigException ):
95
+ await self .requestor .refresh_token ('my-refresh-token' )
100
96
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' )
97
+ async def test_failed_refresh_token (self ):
98
+ with fail_token_request_client ():
99
+ with self .assertRaises (ConfigException ):
100
+ await self .requestor .refresh_token ('my-refresh-token' )
0 commit comments