@@ -16,6 +16,7 @@
|
16 | 16 | import datetime |
17 | 17 | from functools import partial |
18 | 18 | import json |
| 19 | +import logging |
19 | 20 | import os |
20 | 21 | import re |
21 | 22 | import socket |
@@ -460,3 +461,38 @@ def test_local_server_socket_cleanup(
|
460 | 461 | instance.run_local_server() |
461 | 462 | |
462 | 463 | server_mock.server_close.assert_called_once() |
| 464 | + |
| 465 | +@mock.patch("builtins.print") |
| 466 | +@mock.patch("google_auth_oauthlib.flow.webbrowser", autospec=True) |
| 467 | +def test_run_local_server_logs_and_prints_url( |
| 468 | +self, webbrowser_mock, print_mock, instance, mock_fetch_token, port, caplog |
| 469 | + ): |
| 470 | +auth_redirect_url = urllib.parse.urljoin( |
| 471 | +f"http://localhost:{port}", self.REDIRECT_REQUEST_PATH |
| 472 | + ) |
| 473 | + |
| 474 | +# Configure caplog to capture INFO logs |
| 475 | +caplog.set_level(logging.INFO, logger="google_auth_oauthlib.flow") |
| 476 | + |
| 477 | +with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool: |
| 478 | +future = pool.submit(partial(instance.run_local_server, port=port)) |
| 479 | + |
| 480 | +while not future.done(): |
| 481 | +try: |
| 482 | +requests.get(auth_redirect_url) |
| 483 | +except requests.ConnectionError: |
| 484 | +pass |
| 485 | + |
| 486 | +future.result() |
| 487 | + |
| 488 | +# Verify log message |
| 489 | +assert "Please visit this URL" in caplog.text |
| 490 | +assert urllib.parse.quote(instance.redirect_uri, safe="") in caplog.text |
| 491 | + |
| 492 | +# Verify print message |
| 493 | +print_mock.assert_called_once() |
| 494 | +assert "Please visit this URL" in print_mock.call_args[0][0] |
| 495 | +assert ( |
| 496 | +urllib.parse.quote(instance.redirect_uri, safe="") |
| 497 | +in print_mock.call_args[0][0] |
| 498 | + ) |