Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Convert pyperclip & webbrowser errors to warnings #26

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions msal_requests_auth/auth/device_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Module for handling the Device Code flow with MSAL and credential refresh.
"""
import os
import warnings
import webbrowser
from typing import Dict, List, Optional

Expand Down Expand Up @@ -67,6 +68,15 @@ def _get_access_token(self) -> Dict[str, str]:
print(flow["message"])
if not self._headless:
# copy code to clipboard
pyperclip.copy(flow["user_code"])
webbrowser.open(flow["verification_uri"])
try:
pyperclip.copy(flow["user_code"])
webbrowser.open(flow["verification_uri"])
except Exception as error: # pylint: disable=broad-exception-caught
warnings.warn(
"Error encountered while copying code to clipboard "
f"and opening a webbrowser ({error})."
"To hide this message, set headless=True "
"or set the MSAL_REQUESTS_AUTH_HEADLESS "
"environment variable to 'true'."
)
return self.client.acquire_token_by_device_flow(flow)
42 changes: 42 additions & 0 deletions test/test_devide_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,45 @@ def test_device_code_auth__accounts(pyperclip_patch, webbrowser_patch, pca_mock)
assert returned_request.headers == {"Authorization": "Bearer TEST TOKEN"}
webbrowser_patch.open.assert_not_called()
pyperclip_patch.copy.assert_not_called()


@patch.dict(os.environ, {}, clear=True)
@patch("msal.PublicClientApplication", autospec=True)
@patch("msal_requests_auth.auth.device_code.webbrowser")
@patch("msal_requests_auth.auth.device_code.pyperclip")
def test_device_code_auth__pyperclip_error(pyperclip_patch, webbrowser_patch, pca_mock):
pyperclip_patch.copy.side_effect = AttributeError
pca_mock.get_accounts.return_value = None
pca_mock.initiate_device_flow.return_value = {
"message": "TEST MESSAGE",
"verification_uri": "TEST URL",
"user_code": "TEST CODE",
}
pca_mock.acquire_token_by_device_flow.return_value = {
"token_type": "Bearer",
"access_token": "TEST TOKEN",
}
request_mock = MagicMock()
request_mock.headers = {}
with pytest.warns(
UserWarning,
match="Error encountered while copying code to clipboard and opening a webbrowser.",
):
returned_request = DeviceCodeAuth(client=pca_mock, scopes=["TEST SCOPE"])(
request_mock
)

pca_mock.acquire_token_silent.assert_not_called()
pca_mock.initiate_device_flow.assert_called_with(scopes=["TEST SCOPE"])
pca_mock.acquire_token_by_device_flow.assert_called_with(
{
"message": "TEST MESSAGE",
"verification_uri": "TEST URL",
"user_code": "TEST CODE",
}
)

assert returned_request.headers == {"Authorization": "Bearer TEST TOKEN"}

pyperclip_patch.copy.assert_called_with("TEST CODE")
webbrowser_patch.open.assert_not_called()
Loading