Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Changelog
=========

Unreleased
----------

* Preserve spaces in database, search, and Redis connection URL passwords.

1.0.1 (2020-15-01)
------------------

Expand Down
8 changes: 4 additions & 4 deletions pgsync/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_search_url(

auth: str = ""
if user and password:
auth = f"{user}:{quote_plus(password)}@"
auth = f"{user}:{quote(password, safe='')}@"
else:
logger.debug("Connecting to Search without password.")

Expand Down Expand Up @@ -142,7 +142,7 @@ def get_database_url(
)
)

auth: str = f"{user}:{quote_plus(password)}" if password else user
auth: str = f"{user}:{quote(password, safe='')}" if password else user
if not password:
logger.debug("Connecting to database without password.")

Expand Down Expand Up @@ -193,10 +193,10 @@ def get_redis_url(

auth: str = ""
if username and password:
auth = f"{quote_plus(username)}:{quote_plus(password)}@"
auth = f"{quote_plus(username)}:{quote(password, safe='')}@"
logger.debug("Connecting to Redis with custom username and password.")
elif password:
auth = f":{quote_plus(password)}@"
auth = f":{quote(password, safe='')}@"
logger.debug("Connecting to Redis with default password.")
else:
logger.debug("Connecting to Redis without password.")
Expand Down
26 changes: 26 additions & 0 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""URLS tests."""

from urllib.parse import unquote, urlsplit

import pytest
from mock import MagicMock, patch

Expand All @@ -16,6 +18,30 @@
class TestUrls(object):
"""URLS tests."""

@pytest.mark.parametrize(
"service", ["database", "search", "redis", "redis-default-user"]
)
@pytest.mark.parametrize(
"password", ["word with spaces", "plus+ @:/?#%", "päss 密码"]
)
def test_connection_password_round_trip(
self, monkeypatch, service, password
):
monkeypatch.setattr("pgsync.urls._get_auth", lambda key: None)
for variable in ("PG_URL", "ELASTICSEARCH_URL", "REDIS_URL"):
monkeypatch.setattr(f"pgsync.urls.{variable}", None)

if service == "database":
url = get_database_url("mydb", user="studio", password=password)
elif service == "search":
url = get_search_url(user="studio", password=password)
elif service == "redis":
url = get_redis_url(username="studio", password=password)
else:
url = get_redis_url(password=password)

assert unquote(urlsplit(url).password) == password

@patch("pgsync.urls.logger")
def test_get_search_url(self, mock_logger):
assert (
Expand Down