Files
notifications-admin/tests/app/test_config.py

64 lines
1.7 KiB
Python
Raw Normal View History

2016-12-08 16:50:37 +00:00
import importlib
2022-08-05 00:25:03 -07:00
import json
import os
2016-12-08 16:50:37 +00:00
from unittest import mock
import pytest
from app import config
def cf_conf():
os.environ['API_HOST_NAME'] = 'cf'
@pytest.fixture
def reload_config(os_environ):
2016-12-08 16:50:37 +00:00
"""
Reset config, by simply re-running config.py from a fresh environment
"""
old_env = os.environ.copy()
os.environ.clear()
2016-12-08 16:50:37 +00:00
yield
os.environ.clear()
for k, v in old_env.items():
os.environ[k] = v
2016-12-08 16:50:37 +00:00
importlib.reload(config)
def test_load_cloudfoundry_config_if_available(reload_config):
2016-12-08 16:50:37 +00:00
os.environ['API_HOST_NAME'] = 'env'
2022-08-05 00:25:03 -07:00
os.environ['REDIS_URL'] = 'some uri'
os.environ['VCAP_APPLICATION'] = 'some json blob'
2022-08-05 00:25:03 -07:00
os.environ['VCAP_SERVICES'] = json.dumps({
'aws-elasticache-redis': [{
'credentials': {
2022-09-07 21:32:33 -07:00
'uri': 'redis://xxx:6379'
2022-08-05 00:25:03 -07:00
}
}],
})
2016-12-08 16:50:37 +00:00
2022-08-09 14:14:49 +00:00
with mock.patch('app.cloudfoundry_config.extract_cloudfoundry_config', side_effect=cf_conf):
2016-12-08 16:50:37 +00:00
# reload config so that its module level code (ie: all of it) is re-instantiated
importlib.reload(config)
2022-09-07 21:32:33 -07:00
assert os.environ['REDIS_URL'] == 'rediss://xxx:6379'
assert config.Config.REDIS_URL == 'rediss://xxx:6379'
2016-12-08 16:50:37 +00:00
def test_load_config_if_cloudfoundry_not_available(reload_config):
2016-12-08 16:50:37 +00:00
os.environ['API_HOST_NAME'] = 'env'
os.environ.pop('VCAP_APPLICATION', None)
2016-12-08 16:50:37 +00:00
with mock.patch('app.cloudfoundry_config.extract_cloudfoundry_config') as cf_config:
# reload config so that its module level code (ie: all of it) is re-instantiated
importlib.reload(config)
assert not cf_config.called
assert os.environ['API_HOST_NAME'] == 'env'
assert config.Config.API_HOST_NAME == 'env'