import asyncio import unittest from types import SimpleNamespace from unittest.mock import patch import story_client as client class ClientTests(unittest.TestCase): def test_create_and_update_use_request_body(self): self.assertEqual(client.call_args(SimpleNamespace(command='create', data={'type': 'Bug'})), ('rm_story_create', {'request_body': {'type': 'Bug'}})) self.assertEqual(client.call_args(SimpleNamespace(command='update', sys_id='abc', data={'story_points': '3'})), ('rm_story_update', {'sys_id': 'abc', 'request_body': {'story_points': '3'}})) def test_callback_checks_state(self): async def check(): async def request(query): reader, writer = await asyncio.open_connection('127.0.0.1', 18766) writer.write(('GET /oauth/callback?' + query + ' HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n').encode()) await writer.drain() response = await reader.read() writer.close(); await writer.wait_closed() return response async def send(): await asyncio.sleep(.02) self.assertIn(b'400 Bad Request', await request('code=wrong&state=bad')) self.assertIn(b'200 OK', await request('code=ok&state=expected')) with patch.object(client, 'REDIRECT', 'http://127.0.0.1:18766/oauth/callback'): task = asyncio.create_task(client.wait_for_callback('expected', lambda: None, 3)) await send() self.assertEqual(await task, 'ok') asyncio.run(check()) if __name__ == '__main__': unittest.main()