summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmjith Ramanujam <amjith.r@gmail.com>2019-05-21 21:27:47 -0700
committerGitHub <noreply@github.com>2019-05-21 21:27:47 -0700
commita5e607b6fc889afd3f8960ca3903ae16b641c304 (patch)
treeb1e53aa2f08ac45489859422efafad07c9246c45
parent300febccdd9f6ac36dd2124d35bf974cf950a703 (diff)
parentd661f63eec393359600ff1277faeb2ccede66f51 (diff)
Merge pull request #1056 from dbcli/handle_password_spaces
Handle password spaces
-rw-r--r--pgcli/main.py6
-rw-r--r--pgcli/pgexecute.py5
-rw-r--r--tests/features/basic_commands.feature31
-rw-r--r--tests/features/environment.py3
-rw-r--r--tests/features/fixture_data/mock_pg_service.conf4
-rw-r--r--tests/features/specials.feature1
-rw-r--r--tests/features/steps/basic_commands.py28
-rw-r--r--tests/features/steps/crud_database.py3
-rw-r--r--tests/features/steps/wrappers.py7
9 files changed, 76 insertions, 12 deletions
diff --git a/pgcli/main.py b/pgcli/main.py
index 865c4f3a..2a000cd9 100644
--- a/pgcli/main.py
+++ b/pgcli/main.py
@@ -391,8 +391,8 @@ class PGCli(object):
except Exception as e: # ImportError for Python 2, ModuleNotFoundError for Python 3
self.logger.warning('import keyring failed: %r.', e)
- def connect_dsn(self, dsn):
- self.connect(dsn=dsn)
+ def connect_dsn(self, dsn, **kwargs):
+ self.connect(dsn=dsn, **kwargs)
def connect_uri(self, uri):
kwargs = psycopg2.extensions.parse_dsn(uri)
@@ -1048,7 +1048,7 @@ def cli(dbname, username_opt, host, port, prompt_passwd, never_prompt,
elif '://' in database:
pgcli.connect_uri(database)
elif "=" in database:
- pgcli.connect_dsn(database)
+ pgcli.connect_dsn(database, user=user)
elif os.environ.get('PGSERVICE', None):
pgcli.connect_dsn('service={0}'.format(os.environ['PGSERVICE']))
else:
diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py
index fd2c18cc..337be145 100644
--- a/pgcli/pgexecute.py
+++ b/pgcli/pgexecute.py
@@ -241,9 +241,8 @@ class PGExecute(object):
}
if new_params['password']:
- new_params['dsn'] = "{0} password={1}".format(
- new_params['dsn'], new_params.pop('password')
- )
+ new_params['dsn'] = make_dsn(
+ new_params['dsn'], password=new_params.pop('password'))
conn_params.update({
k: unicode2utf8(v) for k, v in new_params.items() if v
diff --git a/tests/features/basic_commands.feature b/tests/features/basic_commands.feature
index 8c9ce437..1f63a8ad 100644
--- a/tests/features/basic_commands.feature
+++ b/tests/features/basic_commands.feature
@@ -21,4 +21,33 @@ Feature: run the cli,
Scenario: list databases
When we list databases
then we see list of databases
-
+
+ Scenario: run the cli with --username
+ When we launch dbcli using --username
+ and we send "\?" command
+ then we see help output
+
+ Scenario: run the cli with --user
+ When we launch dbcli using --user
+ and we send "\?" command
+ then we see help output
+
+ Scenario: run the cli with --port
+ When we launch dbcli using --port
+ and we send "\?" command
+ then we see help output
+
+ Scenario: run the cli with --password
+ When we launch dbcli using --password
+ then we send password
+ and we see dbcli prompt
+ when we send "\?" command
+ then we see help output
+
+ @wip
+ Scenario: run the cli with dsn and password
+ When we launch dbcli using dsn_password
+ then we send password
+ and we see dbcli prompt
+ when we send "\?" command
+ then we see help output
diff --git a/tests/features/environment.py b/tests/features/environment.py
index 25f5c6ba..6b4d4241 100644
--- a/tests/features/environment.py
+++ b/tests/features/environment.py
@@ -86,6 +86,7 @@ def before_all(context):
'PGPASSWORD': os.environ.get('PGPASSWORD', None),
'PGPORT': os.environ.get('PGPORT', None),
'XDG_CONFIG_HOME': os.environ.get('XDG_CONFIG_HOME', None),
+ 'PGSERVICEFILE': os.environ.get('PGSERVICEFILE', None),
}
# Set new env vars.
@@ -93,6 +94,8 @@ def before_all(context):
os.environ['PGUSER'] = context.conf['user']
os.environ['PGHOST'] = context.conf['host']
os.environ['PGPORT'] = context.conf['port']
+ os.environ['PGSERVICEFILE'] = os.path.join(
+ fixture_dir, 'mock_pg_service.conf')
if context.conf['pass']:
os.environ['PGPASSWORD'] = context.conf['pass']
diff --git a/tests/features/fixture_data/mock_pg_service.conf b/tests/features/fixture_data/mock_pg_service.conf
new file mode 100644
index 00000000..15f98113
--- /dev/null
+++ b/tests/features/fixture_data/mock_pg_service.conf
@@ -0,0 +1,4 @@
+[mock_postgres]
+dbname=postgres
+host=localhost
+user=postgres
diff --git a/tests/features/specials.feature b/tests/features/specials.feature
index bb367578..63c5cdc9 100644
--- a/tests/features/specials.feature
+++ b/tests/features/specials.feature
@@ -1,6 +1,5 @@
Feature: Special commands
- @wip
Scenario: run refresh command
When we refresh completions
and we wait for prompt
diff --git a/tests/features/steps/basic_commands.py b/tests/features/steps/basic_commands.py
index ab46089e..71b591a0 100644
--- a/tests/features/steps/basic_commands.py
+++ b/tests/features/steps/basic_commands.py
@@ -33,6 +33,28 @@ def step_run_cli(context):
wrappers.run_cli(context)
+@when('we launch dbcli using {arg}')
+def step_run_cli_using_arg(context, arg):
+ prompt_check = False
+ currentdb = None
+ if arg == '--username':
+ arg = '--username={}'.format(context.conf['user'])
+ if arg == '--user':
+ arg = '--user={}'.format(context.conf['user'])
+ if arg == '--port':
+ arg = '--port={}'.format(context.conf['port'])
+ if arg == '--password':
+ arg = '--password'
+ prompt_check = False
+ # This uses the mock_pg_service.conf file in fixtures folder.
+ if arg == 'dsn_password':
+ arg = 'service=mock_postgres --password'
+ prompt_check = False
+ currentdb = "postgres"
+ wrappers.run_cli(context, run_args=[
+ arg], prompt_check=prompt_check, currentdb=currentdb)
+
+
@when('we wait for prompt')
def step_wait_prompt(context):
wrappers.wait_prompt(context)
@@ -98,3 +120,9 @@ def step_confirm_destructive_command(context):
wrappers.expect_exact(
context, 'You\'re about to run a destructive command.\r\nDo you want to proceed? (y/n):', timeout=2)
context.cli.sendline('y')
+
+
+@then(u'we send password')
+def step_send_password(context):
+ wrappers.expect_exact(context, 'Password for', timeout=5)
+ context.cli.sendline(context.conf['pass'] or 'DOES NOT MATTER')
diff --git a/tests/features/steps/crud_database.py b/tests/features/steps/crud_database.py
index 0a0928fc..3ec27c96 100644
--- a/tests/features/steps/crud_database.py
+++ b/tests/features/steps/crud_database.py
@@ -65,7 +65,8 @@ def step_see_prompt(context):
"""
Wait to see the prompt.
"""
- wrappers.expect_exact(context, '{0}> '.format(context.conf['dbname']), timeout=5)
+ db_name = getattr(context, "currentdb", context.conf['dbname'])
+ wrappers.expect_exact(context, '{0}> '.format(db_name), timeout=5)
context.atprompt = True
diff --git a/tests/features/steps/wrappers.py b/tests/features/steps/wrappers.py
index 66dd483c..23f9cc2b 100644
--- a/tests/features/steps/wrappers.py
+++ b/tests/features/steps/wrappers.py
@@ -49,7 +49,7 @@ def expect_pager(context, expected, timeout):
context.conf['pager_boundary'], expected), timeout=timeout)
-def run_cli(context, run_args=None):
+def run_cli(context, run_args=None, prompt_check=True, currentdb=None):
"""Run the process using pexpect."""
run_args = run_args or []
cli_cmd = context.conf.get('cli_command')
@@ -59,9 +59,10 @@ def run_cli(context, run_args=None):
context.logfile = StringIO()
context.cli.logfile = context.logfile
context.exit_sent = False
- context.currentdb = context.conf['dbname']
+ context.currentdb = currentdb or context.conf['dbname']
context.cli.sendline('\pset pager always')
- wait_prompt(context)
+ if prompt_check:
+ wait_prompt(context)
def wait_prompt(context):