Files
notifications-api/tests/app/test_cloudfoundry_config.py

83 lines
2.2 KiB
Python
Raw Normal View History

2016-12-08 12:12:45 +00:00
import json
2021-03-10 13:55:06 +00:00
import os
2016-12-08 12:12:45 +00:00
import pytest
2022-10-31 15:37:12 -04:00
from app.cloudfoundry_config import CloudfoundryConfig
_bucket_credentials = {
2023-08-29 14:54:30 -07:00
"access_key_id": "csv-access",
"bucket": "csv-upload-bucket",
"region": "us-gov-west-1",
"secret_access_key": "csv-secret",
2022-10-31 15:37:12 -04:00
}
2023-08-29 14:54:30 -07:00
_postgres_url = "postgres://postgres:password@localhost:5432/db_name"
2016-12-08 12:12:45 +00:00
@pytest.fixture
def vcap_services():
return {
2023-08-29 14:54:30 -07:00
"aws-rds": [{"credentials": {"uri": _postgres_url}}],
"aws-elasticache-redis": [{"credentials": {"uri": "redis://xxx:6379"}}],
"s3": [
{
2023-08-29 14:54:30 -07:00
"name": "notify-api-csv-upload-bucket-test",
"credentials": _bucket_credentials,
},
{
2023-08-29 14:54:30 -07:00
"name": "notify-api-contact-list-bucket-test",
"credentials": {
"access_key_id": "contact-access",
"bucket": "contact-list-bucket",
"region": "us-gov-west-1",
"secret_access_key": "contact-secret",
},
},
],
2023-08-29 14:54:30 -07:00
"user-provided": [],
2016-12-08 12:12:45 +00:00
}
def test_database_url(vcap_services):
2023-08-29 14:54:30 -07:00
os.environ["DATABASE_URL"] = _postgres_url
2023-08-29 14:54:30 -07:00
assert (
CloudfoundryConfig().database_url
== "postgresql://postgres:password@localhost:5432/db_name"
)
2022-10-31 15:37:12 -04:00
def test_redis_url(vcap_services):
2023-08-29 14:54:30 -07:00
os.environ["VCAP_SERVICES"] = json.dumps(vcap_services)
2016-12-08 12:12:45 +00:00
2023-08-29 14:54:30 -07:00
assert CloudfoundryConfig().redis_url == "rediss://xxx:6379"
2022-10-31 15:37:12 -04:00
def test_redis_url_falls_back_to_REDIS_URL():
2023-08-29 14:54:30 -07:00
expected = "redis://yyy:6379"
os.environ["REDIS_URL"] = expected
os.environ["VCAP_SERVICES"] = ""
2022-10-31 15:37:12 -04:00
assert CloudfoundryConfig().redis_url == expected
def test_s3_bucket_credentials(vcap_services):
2023-08-29 14:54:30 -07:00
os.environ["VCAP_SERVICES"] = json.dumps(vcap_services)
2022-10-31 15:37:12 -04:00
2023-08-29 14:54:30 -07:00
assert (
CloudfoundryConfig().s3_credentials("notify-api-csv-upload-bucket-test")
== _bucket_credentials
)
2022-10-31 15:37:12 -04:00
def test_s3_bucket_credentials_falls_back_to_empty_creds():
2023-08-29 14:54:30 -07:00
os.environ["VCAP_SERVICES"] = ""
2022-10-31 15:37:12 -04:00
expected = {
2023-08-29 14:54:30 -07:00
"bucket": "",
"access_key_id": "",
"secret_access_key": "",
"region": "",
2022-10-31 15:37:12 -04:00
}
2023-08-29 14:54:30 -07:00
assert CloudfoundryConfig().s3_credentials("bucket") == expected