|
| 1 | +import os |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +from python_interface_to_workflows.auth.open_auth_url import ( |
| 5 | + CallbackHandler, |
| 6 | + open_auth_url, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +@patch("python_interface_to_workflows.auth.open_auth_url.webbrowser.open") |
| 11 | +@patch("python_interface_to_workflows.auth.open_auth_url._ReusingHTTPServer") |
| 12 | +def test_open_auth_url_normal_function( |
| 13 | + mock_http_server: MagicMock, |
| 14 | + mock_open_browser: MagicMock, |
| 15 | +): |
| 16 | + server = mock_http_server.return_value |
| 17 | + server.auth_code = "this_is_your_code" |
| 18 | + open_auth_url("url") |
| 19 | + server.handle_request.assert_called_once() |
| 20 | + mock_open_browser.assert_called_once_with("url") |
| 21 | + server.socket.shutdown.assert_called_once() |
| 22 | + server.server_close.assert_called_once() |
| 23 | + assert os.environ["AUTH"] == "this_is_your_code" |
| 24 | + |
| 25 | + |
| 26 | +@patch("python_interface_to_workflows.auth.open_auth_url.exit") |
| 27 | +@patch("python_interface_to_workflows.auth.open_auth_url.webbrowser.open") |
| 28 | +@patch("python_interface_to_workflows.auth.open_auth_url._ReusingHTTPServer") |
| 29 | +def test_open_auth_url_raises_error( |
| 30 | + mock_http_server: MagicMock, |
| 31 | + mock_open_browser: MagicMock, |
| 32 | + mock_exit: MagicMock, |
| 33 | +): |
| 34 | + server = mock_http_server.return_value |
| 35 | + server.auth_code = "this_is_your_code" |
| 36 | + server.handle_request.side_effect = OSError |
| 37 | + open_auth_url("url") |
| 38 | + mock_open_browser.assert_called_once_with("url") |
| 39 | + assert os.environ["AUTH"] == "" |
| 40 | + mock_exit.assert_called_once_with(1) |
| 41 | + server.socket.shutdown.assert_called_once() |
| 42 | + server.server_close.assert_called_once() |
| 43 | + |
| 44 | + |
| 45 | +def test_handler_normal_function(): |
| 46 | + handler = CallbackHandler.__new__(CallbackHandler) |
| 47 | + handler.path = "/?code=this_is_your_code" |
| 48 | + |
| 49 | + handler.server = MagicMock() |
| 50 | + |
| 51 | + handler.send_response = MagicMock() |
| 52 | + handler.end_headers = MagicMock() |
| 53 | + handler.wfile = MagicMock() |
| 54 | + handler.wfile.write = MagicMock() |
| 55 | + |
| 56 | + handler.do_GET() |
| 57 | + |
| 58 | + assert handler.server.auth_code == "this_is_your_code" |
| 59 | + handler.send_response.assert_called_once_with(200) |
| 60 | + handler.wfile.write.assert_called_once_with( |
| 61 | + b"Authorization successful. You can close this window." |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +def test_handler_error_response(): |
| 66 | + handler = CallbackHandler.__new__(CallbackHandler) |
| 67 | + handler.path = "/" |
| 68 | + |
| 69 | + handler.server = MagicMock() |
| 70 | + |
| 71 | + handler.send_response = MagicMock() |
| 72 | + handler.end_headers = MagicMock() |
| 73 | + handler.wfile = MagicMock() |
| 74 | + handler.wfile.write = MagicMock() |
| 75 | + |
| 76 | + handler.do_GET() |
| 77 | + |
| 78 | + handler.send_response.assert_called_once_with(400) |
| 79 | + handler.end_headers.assert_called_once() |
| 80 | + handler.wfile.write.assert_called_once_with(b"Missing authorization code.") |
0 commit comments