Skip to content

Commit

Permalink
Merge pull request #889 from DalgoT4D/888-error-while-getting-data-in…
Browse files Browse the repository at this point in the history
…sights-for-janaagraha

url-encode the username and password
  • Loading branch information
Ishankoradia authored Nov 15, 2024
2 parents ff670e4 + 5ed2335 commit 943915b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 7 additions & 1 deletion ddpui/datainsights/warehouse/postgres.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from urllib.parse import quote

from sqlalchemy.engine import create_engine
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy import inspect
Expand All @@ -14,7 +16,11 @@ def __init__(self, creds: dict):
Establish connection to the postgres database using sqlalchemy engine
Creds come from the secrets manager
"""
connection_string = "postgresql://{username}:{password}@{host}/{database}".format(**creds)
creds["encoded_username"] = quote(creds["username"].strip())
creds["encoded_password"] = quote(creds["password"].strip())
connection_string = (
"postgresql://{encoded_username}:{encoded_password}@{host}/{database}".format(**creds)
)

self.engine = create_engine(connection_string, pool_size=5, pool_timeout=30)
self.inspect_obj: Inspector = inspect(
Expand Down
15 changes: 13 additions & 2 deletions ddpui/tests/core/datainsights/factories/test_warehouse_factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
import django
from django.core.management import call_command
from django.apps import apps

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ddpui.settings")
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
Expand Down Expand Up @@ -45,3 +43,16 @@ def test_warehouse_factory():

with pytest.raises(ValueError):
WarehouseFactory.connect({}, "some-no-supported-warehouse-type")


def test_url_encoding():
"""tests url encoding of username and password"""

with patch("ddpui.datainsights.warehouse.postgres.inspect"):
with patch("ddpui.datainsights.warehouse.postgres.create_engine") as mock_create_engine:
PostgresClient(
{"username": "user name", "password": "pass word", "host": "host", "database": "db"}
)
mock_create_engine.assert_called_with(
"postgresql://user%20name:pass%20word@host/db", pool_size=5, pool_timeout=30
)

0 comments on commit 943915b

Please sign in to comment.