diff --git a/pydrive2/auth.py b/pydrive2/auth.py index ec881308..b188adf1 100644 --- a/pydrive2/auth.py +++ b/pydrive2/auth.py @@ -1,3 +1,4 @@ +import os import json import webbrowser import httplib2 @@ -224,6 +225,9 @@ def LocalWebserverAuth( This function is not for web server application. It creates local web server for user from standalone application. + If GDRIVE_NON_INTERACTIVE environment variable is set, this function + raises AuthenticationError. + :param host_name: host name of the local web server. :type host_name: str. :param port_numbers: list of port numbers to be tried to used. @@ -237,6 +241,11 @@ def LocalWebserverAuth( :returns: str -- code returned from local web server :raises: AuthenticationRejected, AuthenticationError """ + if os.getenv("GDRIVE_NON_INTERACTIVE"): + raise AuthenticationError( + "Non interactive mode (GDRIVE_NON_INTERACTIVE env) is enabled" + ) + if port_numbers is None: port_numbers = [ 8080, diff --git a/pydrive2/test/test_oauth.py b/pydrive2/test/test_oauth.py index b2b0739a..b20670de 100644 --- a/pydrive2/test/test_oauth.py +++ b/pydrive2/test/test_oauth.py @@ -1,9 +1,11 @@ import json import os +import re import time import pytest +from pytest import MonkeyPatch -from pydrive2.auth import GoogleAuth +from pydrive2.auth import AuthenticationError, GoogleAuth from pydrive2.test.test_util import ( setup_credentials, delete_file, @@ -188,6 +190,27 @@ def test_12_ServiceAuthFromJsonDictNoCredentialsSaving(): time.sleep(1) +def test_13_LocalWebServerAuthNonInterativeRaises(): + settings = { + "client_config_backend": "file", + "client_config_file": "client_secrets.json", + "oauth_scope": ["https://www.googleapis.com/auth/drive"], + } + ga = GoogleAuth(settings=settings) + + with MonkeyPatch.context() as m: + m.setenv("GDRIVE_NON_INTERACTIVE", "true") + # Test that exception is raised on trying to do browser auth if + # we are running in a non interactive environment. + with pytest.raises( + AuthenticationError, + match=re.escape( + "Non interactive mode (GDRIVE_NON_INTERACTIVE env) is enabled" + ), + ): + ga.LocalWebserverAuth() + + def CheckCredentialsFile(credentials, no_file=False): ga = GoogleAuth(settings_file_path("test_oauth_default.yaml")) ga.LoadCredentialsFile(credentials)