Skip to content

Commit

Permalink
Rename configuration environment variables (#107)
Browse files Browse the repository at this point in the history
- Drop PYTHON_ prefix from configuration environment variables
- Original environment variable names are still supported
  • Loading branch information
rmartin16 authored Aug 13, 2022
1 parent 0b9517d commit 0351bec
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 41 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/tests-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 4 additions & 4 deletions docs/source/behavior&configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
**********************
Expand Down
53 changes: 28 additions & 25 deletions qbittorrentapi/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down
62 changes: 56 additions & 6 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0351bec

Please sign in to comment.