diff --git a/.github/workflows/tests-release.yml b/.github/workflows/tests-release.yml index f3352319a..b84d1f986 100644 --- a/.github/workflows/tests-release.yml +++ b/.github/workflows/tests-release.yml @@ -25,11 +25,11 @@ env: QT_USE_DEFAULT_PAA: "4.3.9, 4.3.8, 4.3.7, 4.3.6, 4.3.5, 4.3.4.1, 4.3.3, 4.3.2, 4.3.1, 4.3.0.1" LIBTORRENT_VER: "2.0.7" SUBMIT_COVERAGE_VERSIONS: "2.7, 3.10" - PYTHON_QBITTORRENTAPI_HOST: localhost:8080 - PYTHON_QBITTORRENTAPI_PASSWORD: adminadmin - PYTHON_QBITTORRENTAPI_USERNAME: admin - LIBTORRENT_INSTALLS: ${{ github.workspace }}/resources/libtorrent_installs - QBITTORRENT_INSTALLS: ${{ github.workspace }}/resources/qbittorrent_installs + QBITTORRENTAPI_HOST: "localhost:8080" + QBITTORRENTAPI_PASSWORD: "adminadmin" + QBITTORRENTAPI_USERNAME: "admin" + LIBTORRENT_INSTALLS: "${{ github.workspace }}/resources/libtorrent_installs" + QBITTORRENT_INSTALLS: "${{ github.workspace }}/resources/qbittorrent_installs" GH_CI_CACHE_VERSION: "1" # useful to effectively evict caches jobs: diff --git a/CHANGELOG.txt b/CHANGELOG.txt index dc8b9bce3..75cd728a3 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,6 @@ +Version 2022.8.35 (13 aug 2022) + - Remove PYTHON_ prefix for configuration environment variables + Version 2022.8.34 (11 aug 2022) - Add setuptools as an explicit dependency for pkg_resources.parse_version diff --git a/docs/source/behavior&configuration.rst b/docs/source/behavior&configuration.rst index 80c583628..fce231019 100644 --- a/docs/source/behavior&configuration.rst +++ b/docs/source/behavior&configuration.rst @@ -4,7 +4,7 @@ Behavior & Configuration Untrusted WebUI Certificate *************************** * qBittorrent allows you to configure HTTPS with an untrusted certificate; this commonly includes self-signed certificates. -* When using such a certificate, instantiate Client with ``VERIFY_WEBUI_CERTIFICATE=False`` or set environment variable ``PYTHON_QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE`` to a non-null value. +* When using such a certificate, instantiate Client with ``VERIFY_WEBUI_CERTIFICATE=False`` or set environment variable ``QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE`` to a non-null value. * Failure to do this for will cause connections to qBittorrent to fail. * As a word of caution, doing this actually does turn off certificate verification. Therefore, for instance, potential man-in-the-middle attacks will not be detected and reported (since the error is suppressed). However, the connection will remain encrypted. @@ -18,9 +18,9 @@ Host, Username and Password * Alternatively, set environment variables: - * ``PYTHON_QBITTORRENTAPI_HOST`` - * ``PYTHON_QBITTORRENTAPI_USERNAME`` - * ``PYTHON_QBITTORRENTAPI_PASSWORD`` + * ``QBITTORRENTAPI_HOST`` + * ``QBITTORRENTAPI_USERNAME`` + * ``QBITTORRENTAPI_PASSWORD`` Requests Configuration ********************** diff --git a/qbittorrentapi/request.py b/qbittorrentapi/request.py index 1c303824f..1b73159bf 100644 --- a/qbittorrentapi/request.py +++ b/qbittorrentapi/request.py @@ -272,34 +272,37 @@ def _initialize_lesser( if getLogger(logger_).level < 20: getLogger(logger_).setLevel("INFO") - # Environment variables have lowest priority - if self.host == "" and environ.get("PYTHON_QBITTORRENTAPI_HOST") is not None: - logger.debug( - "Using PYTHON_QBITTORRENTAPI_HOST env variable for qBittorrent hostname" + # Environment variables have the lowest priority + if not self.host: + env_host = environ.get( + "QBITTORRENTAPI_HOST", environ.get("PYTHON_QBITTORRENTAPI_HOST") ) - self.host = environ["PYTHON_QBITTORRENTAPI_HOST"] - if ( - self.username == "" - and environ.get("PYTHON_QBITTORRENTAPI_USERNAME") is not None - ): - logger.debug( - "Using PYTHON_QBITTORRENTAPI_USERNAME env variable for username" + if env_host is not None: + logger.debug( + "Using QBITTORRENTAPI_HOST env variable for qBittorrent host" + ) + self.host = env_host + if not self.username: + env_username = environ.get( + "QBITTORRENTAPI_USERNAME", environ.get("PYTHON_QBITTORRENTAPI_USERNAME") + ) + if env_username is not None: + logger.debug("Using QBITTORRENTAPI_USERNAME env variable for username") + self.username = env_username + if not self._password: + env_password = environ.get( + "QBITTORRENTAPI_PASSWORD", environ.get("PYTHON_QBITTORRENTAPI_PASSWORD") ) - self.username = environ["PYTHON_QBITTORRENTAPI_USERNAME"] - if ( - self._password == "" - and environ.get("PYTHON_QBITTORRENTAPI_PASSWORD") is not None - ): - logger.debug( - "Using PYTHON_QBITTORRENTAPI_PASSWORD env variable for password" + if env_password is not None: + logger.debug("Using QBITTORRENTAPI_PASSWORD env variable for password") + self._password = env_password + if self._VERIFY_WEBUI_CERTIFICATE is True: + env_verify_cert = environ.get( + "QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE", + environ.get("PYTHON_QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE"), ) - self._password = environ["PYTHON_QBITTORRENTAPI_PASSWORD"] - if ( - self._VERIFY_WEBUI_CERTIFICATE is True - and environ.get("PYTHON_QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE") - is not None - ): - self._VERIFY_WEBUI_CERTIFICATE = False + if env_verify_cert is not None: + self._VERIFY_WEBUI_CERTIFICATE = False # Mocking variables until better unit testing exists self._MOCK_WEB_API_VERSION = MOCK_WEB_API_VERSION diff --git a/setup.cfg b/setup.cfg index e74231267..ef9a9ce92 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = qbittorrent-api -version = 2022.8.34 +version = 2022.8.35 url = https://github.com/rmartin16/qbittorrent-api author = Russell Martin author_email = rmartin16@gmail.com diff --git a/tests/test_request.py b/tests/test_request.py index 8c89eaa0e..202dc0b41 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -46,8 +46,8 @@ def test_log_in_via_auth(): assert ( client_good.auth_log_in( - username=environ.get("PYTHON_QBITTORRENTAPI_USERNAME"), - password=environ.get("PYTHON_QBITTORRENTAPI_PASSWORD"), + username=environ.get("QBITTORRENTAPI_USERNAME"), + password=environ.get("QBITTORRENTAPI_PASSWORD"), ) is None ) @@ -94,7 +94,7 @@ def test_hostname_user_base_path(hostname): def test_port_from_host(app_version): - host, port = environ.get("PYTHON_QBITTORRENTAPI_HOST").split(":") + host, port = environ.get("QBITTORRENTAPI_HOST").split(":") client = Client(host=host, port=port, VERIFY_WEBUI_CERTIFICATE=False) assert client.app.version == app_version @@ -116,7 +116,7 @@ def _enable_disable_https(client, use_https): @pytest.mark.parametrize("use_https", (True, False)) def test_force_user_scheme(client, app_version, use_https): - default_host = environ["PYTHON_QBITTORRENTAPI_HOST"] + default_host = environ["QBITTORRENTAPI_HOST"] _enable_disable_https(client, use_https) @@ -161,7 +161,7 @@ def test_force_user_scheme(client, app_version, use_https): @pytest.mark.parametrize("scheme", ("http://", "https://")) def test_both_https_http_not_working(client, app_version, scheme): - default_host = environ["PYTHON_QBITTORRENTAPI_HOST"] + default_host = environ["QBITTORRENTAPI_HOST"] _enable_disable_https(client, use_https=True) # rerun with verify=True @@ -176,6 +176,56 @@ def test_both_https_http_not_working(client, app_version, scheme): _enable_disable_https(client, use_https=False) +def test_legacy_env_vars(): + save_env_host = environ.get("QBITTORRENTAPI_HOST") + save_env_username = environ.get("QBITTORRENTAPI_USERNAME") + save_env_password = environ.get("QBITTORRENTAPI_PASSWORD") + save_env_verify = environ.get("QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE") + del environ["QBITTORRENTAPI_HOST"] + del environ["QBITTORRENTAPI_USERNAME"] + del environ["QBITTORRENTAPI_PASSWORD"] + if "QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE" in environ: + del environ["QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE"] + + client = Client() + + assert client.host == "" + assert client.username == "" + assert client._password == "" + assert client._VERIFY_WEBUI_CERTIFICATE is True + + environ["PYTHON_QBITTORRENTAPI_HOST"] = excepted_host = "legacy:8090" + environ["PYTHON_QBITTORRENTAPI_USERNAME"] = excepted_username = "legacyuser" + environ["PYTHON_QBITTORRENTAPI_PASSWORD"] = expected_password = "legacypassword" + environ["PYTHON_QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE"] = "true" + + client = Client() + + try: + assert client.host == excepted_host + assert client.username == excepted_username + assert client._password == expected_password + assert client._VERIFY_WEBUI_CERTIFICATE is False + finally: + environ["QBITTORRENTAPI_HOST"] = save_env_host + environ["QBITTORRENTAPI_USERNAME"] = save_env_username + environ["QBITTORRENTAPI_PASSWORD"] = save_env_password + if save_env_verify is not None: + environ["QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE"] = save_env_verify + + client = Client() + + assert client.host != excepted_host + assert client.username != excepted_username + assert client._password != expected_password + assert client._VERIFY_WEBUI_CERTIFICATE is False + + del environ["PYTHON_QBITTORRENTAPI_HOST"] + del environ["PYTHON_QBITTORRENTAPI_USERNAME"] + del environ["PYTHON_QBITTORRENTAPI_PASSWORD"] + del environ["PYTHON_QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE"] + + def test_log_out(client): client.auth_log_out() with pytest.raises(exceptions.Forbidden403Error): @@ -348,7 +398,7 @@ def test_verify_cert(app_version): # assert client._VERIFY_WEBUI_CERTIFICATE is True # noqa: E800 # assert client.app.version == app_version # noqa: E800 - environ["PYTHON_QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE"] = "true" + environ["QBITTORRENTAPI_DO_NOT_VERIFY_WEBUI_CERTIFICATE"] = "true" client = Client(VERIFY_WEBUI_CERTIFICATE=True) assert client._VERIFY_WEBUI_CERTIFICATE is False assert client.app.version == app_version