Skip to content

Commit 47bccfa

Browse files
committed
feat(api): adds unit tests to open_auth_url
1 parent 38514a8 commit 47bccfa

2 files changed

Lines changed: 98 additions & 19 deletions

File tree

src/python_interface_to_workflows/auth/open_auth_url.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,27 @@
66
from typing import cast
77

88

9-
def open_auth_url(auth_url: str):
9+
class _ReusingHTTPServer(HTTPServer):
10+
allow_reuse_address = True
11+
auth_code: str
12+
1013

11-
class _ReusingHTTPServer(HTTPServer):
12-
allow_reuse_address = True
13-
auth_code: str
14+
class CallbackHandler(BaseHTTPRequestHandler):
15+
def do_GET(self):
16+
query = urllib.parse.urlparse(self.path).query
17+
params = urllib.parse.parse_qs(query)
18+
if "code" in params:
19+
cast(_ReusingHTTPServer, self.server).auth_code = params["code"][0]
20+
self.send_response(200)
21+
self.end_headers()
22+
self.wfile.write(b"Authorization successful. You can close this window.")
23+
else:
24+
self.send_response(400)
25+
self.end_headers()
26+
self.wfile.write(b"Missing authorization code.")
1427

15-
class CallbackHandler(BaseHTTPRequestHandler):
16-
def do_GET(self):
17-
query = urllib.parse.urlparse(self.path).query
18-
params = urllib.parse.parse_qs(query)
19-
if "code" in params:
20-
cast(_ReusingHTTPServer, self.server).auth_code = params["code"][0]
21-
self.send_response(200)
22-
self.end_headers()
23-
self.wfile.write(
24-
b"Authorization successful. You can close this window."
25-
)
26-
else:
27-
self.send_response(400)
28-
self.end_headers()
29-
self.wfile.write(b"Missing authorization code.")
3028

29+
def open_auth_url(auth_url: str):
3130
httpd = _ReusingHTTPServer(("localhost", 5173), CallbackHandler)
3231
webbrowser.open(auth_url)
3332
try:

tests/test_open_auth_url.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)