db: Drop None and empty-string checking in arguments.

psycopg2 treats None and "" the same as not-provided:

```
assert connect(user="zulip", dbname="zulip")
assert connect(user="zulip", dbname="zulip", host="")
assert connect(user="zulip", dbname="zulip", host=None)
with Raises("no password supplied"):
    connect(user="zulip", dbname="zulip", host="localhost")

assert connect(user="zulip", dbname="zulip", port="")
assert connect(user="zulip", dbname="zulip", port=None)
assert connect(user="zulip", dbname="zulip", port=5432)
with Raises("could not connect to server"):
    connect(user="zulip", dbname="zulip", port=5000)

assert connect(dbname="zulip", host="localhost", password="right-password")
with Raises("no password supplied"):
    connect(dbname="zulip", host="localhost", password="")
with Raises("no password supplied"):
    connect(dbname="zulip", host="localhost", password=None)
with Raises("password authentication failed"):
    connect(dbname="zulip", host="localhost", password="wrong")
```

Co-authored-by: Adam Birds <adam.birds@adbwebdesigns.co.uk>
This commit is contained in:
Alex Vandiver 2021-05-24 22:10:54 -07:00 committed by Tim Abbott
parent 9c652eb16b
commit 02fc0d3e1d
1 changed files with 3 additions and 7 deletions

View File

@ -97,13 +97,9 @@ try:
os.environ["DJANGO_SETTINGS_MODULE"] = "zproject.settings"
from django.conf import settings
if settings.DATABASES["default"]["HOST"] != "":
pg_args["host"] = settings.DATABASES["default"]["HOST"]
if settings.DATABASES["default"].get("PORT"):
pg_args["port"] = settings.DATABASES["default"]["PORT"]
pg_args["password"] = ""
if settings.DATABASES["default"]["PASSWORD"] is not None:
pg_args["password"] = settings.DATABASES["default"]["PASSWORD"]
pg_args["host"] = settings.DATABASES["default"]["HOST"]
pg_args["port"] = settings.DATABASES["default"].get("PORT")
pg_args["password"] = settings.DATABASES["default"].get("PASSWORD")
pg_args["user"] = settings.DATABASES["default"]["USER"]
pg_args["dbname"] = settings.DATABASES["default"]["NAME"]
pg_args["sslmode"] = settings.DATABASES["default"]["OPTIONS"].get("sslmode")