From c3ec6efc2b44ebfc959a6a3ce93241df95bb4bfe Mon Sep 17 00:00:00 2001 From: "Martin J. Hsu" Date: Tue, 2 Oct 2018 15:25:41 +0800 Subject: [PATCH 1/3] added getconnectionstring with password from pgpass added --- pgpasslib.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/pgpasslib.py b/pgpasslib.py index 3cfd221..04b6806 100644 --- a/pgpasslib.py +++ b/pgpasslib.py @@ -26,7 +26,7 @@ import sys import platform -__version__ = '1.1.0' +__version__ = '1.1.1' LOGGER = logging.getLogger(__name__) @@ -78,6 +78,41 @@ def getpass(host=DEFAULT_HOST, port=DEFAULT_PORT, dbname=DEFAULT_DBNAME, return None +def getconnectionstring( + host=DEFAULT_HOST, + port=DEFAULT_PORT, + dbname=DEFAULT_DBNAME, + user=DEFAULT_USER, +): + """Return the connection string for the specified host, port, dbname and user. + :py:const:`None` will be returned if a password can not be found for the + specified connection parameters. + + :param str host: PostgreSQL hostname + :param port: PostgreSQL port + :type port: int or str + :param str dbname: Database name + :param str user: Database role/user + :rtype: str + :raises: FileNotFound + :raises: InvalidPermissions + :raises: InvalidEntry + + """ + return 'postgresql://{user}:{password}@{host}:{port}/{dbname}'.format( + host=host, + port=port, + dbname=dbname, + user=user, + password=getpass( + host=host, + port=port, + dbname=dbname, + user=user, + ) + ) + + class PgPassException(Exception): """Base exception for all pgpasslib exceptions""" MESSAGE = 'Base Exception: {}' From 7167b7124a4c1045072c5ab6531cf29f93435386 Mon Sep 17 00:00:00 2001 From: "Martin J. Hsu" Date: Tue, 2 Oct 2018 17:14:18 +0800 Subject: [PATCH 2/3] added tests for getconnectionstring --- tests.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests.py b/tests.py index f4f68cd..516f867 100644 --- a/tests.py +++ b/tests.py @@ -2,7 +2,10 @@ Tests for pgpasslib """ -import mock +try: + import mock +except ImportError: + from unittest import mock import os from os import path import stat @@ -268,3 +271,40 @@ class InvalidPermissionsExceptionStrFormatting(unittest.TestCase): def test_str_matches_expectation(self): self.assertEqual(str(pgpasslib.InvalidPermissions('foo', '0x000')), 'Invalid Permissions for foo: 0x000') + + +class GetConnectionStringMatch1Test(unittest.TestCase): + + def test_getconnectionstring_returns_expected_result(self): + with mock.patch('pgpasslib._read_file') as read_file: + read_file.return_value = MOCK_CONTENT + self.assertEqual(pgpasslib.getconnectionstring('localhost', 5432, + 'foo', 'kermit'), '') + + +class GetConnectionStringMatch2Test(unittest.TestCase): + + def test_getconnectionstring_returns_expected_result(self): + with mock.patch('pgpasslib._read_file') as read_file: + read_file.return_value = MOCK_CONTENT + self.assertEqual(pgpasslib.getconnectionstring('bouncer', 6000, + 'bumpers', 'rubber'), 'postgresql://rubber:buggy@bouncer:6000/bumpers') + + +class GetConnectionStringMatch3Test(unittest.TestCase): + + def test_getconnectionstring_returns_expected_result(self): + with mock.patch('pgpasslib._read_file') as read_file: + read_file.return_value = MOCK_CONTENT + self.assertEqual(pgpasslib.getconnectionstring('foo.abjdite.us-east-1.' + 'redshift.amazonaws.com', 5439, + 'redshift', 'fonzy'), 'postgresql://fonzy:b3ar@foo.abjdite.us-east-1.redshift.amazonaws.com:5439/redshift') + + +class GetConnectionStringMatch4Test(unittest.TestCase): + + def test_getconnectionstring_returns_expected_result(self): + with mock.patch('pgpasslib._read_file') as read_file: + read_file.return_value = MOCK_CONTENT + self.assertEqual(pgpasslib.getconnectionstring('foo:bar', '6000', + 'corgie', 'baz'), 'postgresql://baz:qux@foo:bar:6000/corgie') From cefc033f607f2a80771f5fb457b19e4b71278924 Mon Sep 17 00:00:00 2001 From: "Martin J. Hsu" Date: Tue, 2 Oct 2018 17:17:41 +0800 Subject: [PATCH 3/3] getconnectionstring returns empty string when getpass does not return a password --- pgpasslib.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pgpasslib.py b/pgpasslib.py index 04b6806..7f92ee1 100644 --- a/pgpasslib.py +++ b/pgpasslib.py @@ -99,18 +99,21 @@ def getconnectionstring( :raises: InvalidEntry """ - return 'postgresql://{user}:{password}@{host}:{port}/{dbname}'.format( + password=getpass( host=host, port=port, dbname=dbname, user=user, - password=getpass( + ) + if password: + return 'postgresql://{user}:{password}@{host}:{port}/{dbname}'.format( host=host, port=port, dbname=dbname, user=user, + password=password ) - ) + return '' class PgPassException(Exception):