|
40 | 40 | AUTH_METHODS = {} |
41 | 41 |
|
42 | 42 |
|
| 43 | +class TestRequest: |
| 44 | + """Simple request stub for doctests.""" |
| 45 | + pass |
| 46 | + |
| 47 | + |
43 | 48 | def register_event_auth(name): |
44 | 49 | ''' |
45 | 50 | Decorator to register a method to authenticate events |
@@ -135,7 +140,9 @@ async def guest_auth(request, event, source): |
135 | 140 | We assign a cookie on first visit, but we have no guarantee |
136 | 141 | the browser will keep cookies around. |
137 | 142 |
|
138 | | - >>> a = asyncio.run(guest_auth(TestRequest(), [], {}, 'org.mitros.test')) |
| 143 | + >>> from unittest.mock import AsyncMock, patch |
| 144 | + >>> with patch('aiohttp_session.get_session', new=AsyncMock(return_value={})): |
| 145 | + ... a = asyncio.run(guest_auth(TestRequest(), {}, 'org.mitros.test')) |
139 | 146 | >>> a['user_id'] = len(a['user_id']) # Different user_id each time, and we want doctest to match exact string. |
140 | 147 | >>> a |
141 | 148 | {'sec': 'none', 'user_id': 32, 'providence': 'guest'} |
@@ -163,12 +170,16 @@ async def local_storage_auth(request, event, source): |
163 | 170 | unauthenticated (if we don't), or allow for both, with a tag for |
164 | 171 | guest versus non-guest accounts. |
165 | 172 |
|
| 173 | + >>> from unittest.mock import patch |
166 | 174 | >>> auth_event = {'event': 'local_storage', 'user_tag': 'bob'} |
167 | | - >>> a = asyncio.run(local_storage_auth(TestRequest(), [], auth_event, 'org.mitros.test')) |
| 175 | + >>> with patch('learning_observer.auth.events.token_authorize_user', return_value='authenticated'): |
| 176 | + ... a = asyncio.run(local_storage_auth(TestRequest(), auth_event, 'org.mitros.test')) |
168 | 177 | >>> a |
169 | 178 | {'sec': 'authenticated', 'user_id': 'ls-bob', 'providence': 'ls'} |
170 | 179 | >>> auth_event['user_tag'] = 'jim' |
171 | | - >>> a = asyncio.run(local_storage_auth(TestRequest(), [auth_event], {}, 'org.mitros.test')) |
| 180 | + >>> with patch('learning_observer.auth.events.token_authorize_user', return_value='unauthenticated'): |
| 181 | + ... a = asyncio.run(local_storage_auth(TestRequest(), auth_event, 'org.mitros.test')) |
| 182 | +
|
172 | 183 | >>> a |
173 | 184 | {'sec': 'unauthenticated', 'user_id': 'ls-jim', 'providence': 'ls'} |
174 | 185 | ''' |
@@ -344,9 +355,6 @@ def check_event_auth_config(): |
344 | 355 | import doctest |
345 | 356 | print("Running tests") |
346 | 357 |
|
347 | | - class TestRequest: |
348 | | - pass |
349 | | - |
350 | 358 | session = {} |
351 | 359 |
|
352 | 360 | async def get_session(request): |
|
0 commit comments