Properly set database connection string in cloud.gov

This commit is contained in:
Ryan Ahearn
2022-11-01 11:34:00 -04:00
parent 3ceda169f7
commit b4256d0a6c
4 changed files with 18 additions and 7 deletions

View File

@@ -14,6 +14,10 @@ class CloudfoundryConfig:
'region': '' 'region': ''
} }
@property
def database_url(self):
return os.environ.get('DATABASE_URL', '').replace('postgres://', 'postgresql://')
@property @property
def redis_url(self): def redis_url(self):
try: try:

View File

@@ -87,7 +87,7 @@ class Config(object):
ROUTE_SECRET_KEY_2 = os.environ.get('ROUTE_SECRET_KEY_2', 'dev-route-secret-key-2') ROUTE_SECRET_KEY_2 = os.environ.get('ROUTE_SECRET_KEY_2', 'dev-route-secret-key-2')
# DB settings # DB settings
SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URI') SQLALCHEMY_DATABASE_URI = cloud_config.database_url
SQLALCHEMY_RECORD_QUERIES = False SQLALCHEMY_RECORD_QUERIES = False
SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_POOL_SIZE = int(os.environ.get('SQLALCHEMY_POOL_SIZE', 5)) SQLALCHEMY_POOL_SIZE = int(os.environ.get('SQLALCHEMY_POOL_SIZE', 5))

View File

@@ -28,14 +28,14 @@ AWS_US_TOLL_FREE_NUMBER=+18446120782
ADMIN_BASE_URL=http://admin:6012 ADMIN_BASE_URL=http://admin:6012
API_HOST_NAME=http://dev:6011 API_HOST_NAME=http://dev:6011
REDIS_URL=redis://redis:6380 REDIS_URL=redis://redis:6380
SQLALCHEMY_DATABASE_URI=postgresql://postgres:chummy@db:5432/notification_api DATABASE_URL=postgresql://postgres:chummy@db:5432/notification_api
SQLALCHEMY_DATABASE_TEST_URI=postgresql://postgres:chummy@db:5432/test_notification_api SQLALCHEMY_DATABASE_TEST_URI=postgresql://postgres:chummy@db:5432/test_notification_api
# Local direct setup, all overwritten in cloud.gov # Local direct setup, all overwritten in cloud.gov
# ADMIN_BASE_URL=http://localhost:6012 # ADMIN_BASE_URL=http://localhost:6012
# API_HOST_NAME=http://localhost:6011 # API_HOST_NAME=http://localhost:6011
# REDIS_URL=redis://localhost:6379 # REDIS_URL=redis://localhost:6379
# SQLALCHEMY_DATABASE_URI=postgresql://localhost:5432/notification_api # DATABASE_URL=postgresql://localhost:5432/notification_api
# SQLALCHEMY_DATABASE_TEST_URI=postgresql://localhost:5432/test_notification_api # SQLALCHEMY_DATABASE_TEST_URI=postgresql://localhost:5432/test_notification_api
############################################################# #############################################################

View File

@@ -5,12 +5,13 @@ import pytest
from app.cloudfoundry_config import CloudfoundryConfig from app.cloudfoundry_config import CloudfoundryConfig
bucket_credentials = { _bucket_credentials = {
'access_key_id': 'csv-access', 'access_key_id': 'csv-access',
'bucket': 'csv-upload-bucket', 'bucket': 'csv-upload-bucket',
'region': 'us-gov-west-1', 'region': 'us-gov-west-1',
'secret_access_key': 'csv-secret' 'secret_access_key': 'csv-secret'
} }
_postgres_url = 'postgres://postgres:password@localhost:5432/db_name'
@pytest.fixture @pytest.fixture
@@ -18,7 +19,7 @@ def vcap_services():
return { return {
'aws-rds': [{ 'aws-rds': [{
'credentials': { 'credentials': {
'uri': 'postgres uri' 'uri': _postgres_url
} }
}], }],
'aws-elasticache-redis': [{ 'aws-elasticache-redis': [{
@@ -29,7 +30,7 @@ def vcap_services():
's3': [ 's3': [
{ {
'name': 'notifications-api-csv-upload-bucket-test', 'name': 'notifications-api-csv-upload-bucket-test',
'credentials': bucket_credentials 'credentials': _bucket_credentials
}, },
{ {
'name': 'notifications-api-contact-list-bucket-test', 'name': 'notifications-api-contact-list-bucket-test',
@@ -45,6 +46,12 @@ def vcap_services():
} }
def test_database_url(vcap_services):
os.environ['DATABASE_URL'] = _postgres_url
assert CloudfoundryConfig().database_url == 'postgresql://postgres:password@localhost:5432/db_name'
def test_redis_url(vcap_services): def test_redis_url(vcap_services):
os.environ['VCAP_SERVICES'] = json.dumps(vcap_services) os.environ['VCAP_SERVICES'] = json.dumps(vcap_services)
@@ -62,7 +69,7 @@ def test_redis_url_falls_back_to_REDIS_URL():
def test_s3_bucket_credentials(vcap_services): def test_s3_bucket_credentials(vcap_services):
os.environ['VCAP_SERVICES'] = json.dumps(vcap_services) os.environ['VCAP_SERVICES'] = json.dumps(vcap_services)
assert CloudfoundryConfig().s3_credentials('notifications-api-csv-upload-bucket-test') == bucket_credentials assert CloudfoundryConfig().s3_credentials('notifications-api-csv-upload-bucket-test') == _bucket_credentials
def test_s3_bucket_credentials_falls_back_to_empty_creds(): def test_s3_bucket_credentials_falls_back_to_empty_creds():