2015-06-17 13:37:09 +00:00
|
|
|
import os
|
2017-01-19 17:16:04 +00:00
|
|
|
from unittest import mock
|
2015-06-17 13:37:09 +00:00
|
|
|
|
2015-08-05 14:08:56 +00:00
|
|
|
from django.db.backends.postgresql.client import DatabaseClient
|
2017-01-19 17:16:04 +00:00
|
|
|
from django.test import SimpleTestCase
|
2015-06-17 13:37:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PostgreSqlDbshellCommandTestCase(SimpleTestCase):
|
|
|
|
|
|
|
|
def _run_it(self, dbinfo):
|
|
|
|
"""
|
|
|
|
That function invokes the runshell command, while mocking
|
|
|
|
subprocess.call. It returns a 2-tuple with:
|
|
|
|
- The command line list
|
2017-01-11 22:17:25 +00:00
|
|
|
- The content of the file pointed by environment PGPASSFILE, or None.
|
2015-06-17 13:37:09 +00:00
|
|
|
"""
|
|
|
|
def _mock_subprocess_call(*args):
|
|
|
|
self.subprocess_args = list(*args)
|
|
|
|
if 'PGPASSFILE' in os.environ:
|
2017-01-11 22:17:25 +00:00
|
|
|
with open(os.environ['PGPASSFILE'], 'r') as f:
|
2015-07-06 20:17:56 +00:00
|
|
|
self.pgpass = f.read().strip() # ignore line endings
|
2015-06-17 13:37:09 +00:00
|
|
|
else:
|
|
|
|
self.pgpass = None
|
|
|
|
return 0
|
|
|
|
self.subprocess_args = None
|
|
|
|
self.pgpass = None
|
|
|
|
with mock.patch('subprocess.call', new=_mock_subprocess_call):
|
|
|
|
DatabaseClient.runshell_db(dbinfo)
|
|
|
|
return self.subprocess_args, self.pgpass
|
|
|
|
|
|
|
|
def test_basic(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self._run_it({
|
2016-06-02 21:03:03 +00:00
|
|
|
'database': 'dbname',
|
|
|
|
'user': 'someuser',
|
|
|
|
'password': 'somepassword',
|
|
|
|
'host': 'somehost',
|
|
|
|
'port': '444',
|
2015-06-17 13:37:09 +00:00
|
|
|
}), (
|
|
|
|
['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'],
|
2017-01-11 22:17:25 +00:00
|
|
|
'somehost:444:dbname:someuser:somepassword',
|
2015-06-17 13:37:09 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_nopass(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self._run_it({
|
2016-06-02 21:03:03 +00:00
|
|
|
'database': 'dbname',
|
|
|
|
'user': 'someuser',
|
|
|
|
'host': 'somehost',
|
|
|
|
'port': '444',
|
2015-06-17 13:37:09 +00:00
|
|
|
}), (
|
|
|
|
['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'],
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_column(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self._run_it({
|
2016-06-02 21:03:03 +00:00
|
|
|
'database': 'dbname',
|
|
|
|
'user': 'some:user',
|
|
|
|
'password': 'some:password',
|
|
|
|
'host': '::1',
|
|
|
|
'port': '444',
|
2015-06-17 13:37:09 +00:00
|
|
|
}), (
|
|
|
|
['psql', '-U', 'some:user', '-h', '::1', '-p', '444', 'dbname'],
|
2017-01-11 22:17:25 +00:00
|
|
|
'\\:\\:1:444:dbname:some\\:user:some\\:password',
|
2015-06-17 13:37:09 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_escape_characters(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self._run_it({
|
2016-06-02 21:03:03 +00:00
|
|
|
'database': 'dbname',
|
|
|
|
'user': 'some\\user',
|
|
|
|
'password': 'some\\password',
|
|
|
|
'host': 'somehost',
|
|
|
|
'port': '444',
|
2015-06-17 13:37:09 +00:00
|
|
|
}), (
|
|
|
|
['psql', '-U', 'some\\user', '-h', 'somehost', '-p', '444', 'dbname'],
|
2017-01-11 22:17:25 +00:00
|
|
|
'somehost:444:dbname:some\\\\user:some\\\\password',
|
2015-06-17 13:37:09 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_accent(self):
|
|
|
|
username = 'rôle'
|
|
|
|
password = 'sésame'
|
2017-01-11 22:17:25 +00:00
|
|
|
pgpass_string = 'somehost:444:dbname:%s:%s' % (username, password)
|
2015-06-17 13:37:09 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self._run_it({
|
2016-06-02 21:03:03 +00:00
|
|
|
'database': 'dbname',
|
2017-01-11 22:17:25 +00:00
|
|
|
'user': username,
|
|
|
|
'password': password,
|
2016-06-02 21:03:03 +00:00
|
|
|
'host': 'somehost',
|
|
|
|
'port': '444',
|
2015-06-17 13:37:09 +00:00
|
|
|
}), (
|
2017-01-11 22:17:25 +00:00
|
|
|
['psql', '-U', username, '-h', 'somehost', '-p', '444', 'dbname'],
|
|
|
|
pgpass_string,
|
2015-06-17 13:37:09 +00:00
|
|
|
)
|
|
|
|
)
|