diff --git a/app/__init__.py b/app/__init__.py index 2dfa886a3..8b24a6623 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -77,11 +77,7 @@ def create_app(): application = Flask(__name__) - if os.getenv('VCAP_APPLICATION') is not None: - vcap_application = json.loads(os.environ.get('VCAP_APPLICATION')) - notify_environment = vcap_application['space_name'] - else: - notify_environment = os.environ['NOTIFY_ENVIRONMENT'] + notify_environment = os.environ['NOTIFY_ENVIRONMENT'] application.config.from_object(configs[notify_environment]) diff --git a/app/cloudfoundry_config.py b/app/cloudfoundry_config.py index e096122a9..efd554019 100644 --- a/app/cloudfoundry_config.py +++ b/app/cloudfoundry_config.py @@ -9,10 +9,15 @@ import json def extract_cloudfoundry_config(): vcap_services = json.loads(os.environ['VCAP_SERVICES']) + set_config_env_vars(vcap_services) def set_config_env_vars(vcap_services): + vcap_application = json.loads(os.environ.get('VCAP_APPLICATION')) + os.environ['NOTIFY_ENVIRONMENT'] = vcap_application['space_name'] + os.environ['LOGGING_STDOUT_JSON'] = '1' + for s in vcap_services['user-provided']: if s['name'] == 'notify-config': extract_notify_config(s) diff --git a/app/config.py b/app/config.py index 75e0a39a7..e6035e5da 100644 --- a/app/config.py +++ b/app/config.py @@ -20,7 +20,9 @@ class Config(object): # Hosted graphite statsd prefix STATSD_PREFIX = os.getenv('STATSD_PREFIX') + # Logging DEBUG = False + LOGGING_STDOUT_JSON = os.getenv('LOGGING_STDOUT_JSON') == '1' DESKPRO_DEPT_ID = 5 DESKPRO_ASSIGNED_AGENT_TEAM_ID = 5 @@ -122,9 +124,7 @@ class Live(Config): class CloudFoundryConfig(Config): - # debug on true is a less than ideal hack to enable stdout/stderr logging - # TODO: replace this! - DEBUG = True + pass # CloudFoundry sandbox diff --git a/requirements.txt b/requirements.txt index 219552cb8..9093e56d3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -26,6 +26,7 @@ wand==0.4.4 gunicorn==19.6.0 whitenoise==1.0.6 #manages static assets -git+https://github.com/alphagov/notifications-python-client.git@3.0.1#egg=notifications-python-client==3.0.1 +# pin to minor version 3.1.x +notifications-python-client>=3.1,<3.2 -git+https://github.com/alphagov/notifications-utils.git@13.0.1#egg=notifications-utils==13.0.1 +git+https://github.com/alphagov/notifications-utils.git@13.1.0#egg=notifications-utils==13.1.0 diff --git a/tests/app/test_cloudfoundry_config.py b/tests/app/test_cloudfoundry_config.py index b0804683c..ba17fdede 100644 --- a/tests/app/test_cloudfoundry_config.py +++ b/tests/app/test_cloudfoundry_config.py @@ -71,6 +71,15 @@ def cloudfoundry_config( @pytest.fixture def cloudfoundry_environ(monkeypatch, cloudfoundry_config): monkeypatch.setenv('VCAP_SERVICES', json.dumps(cloudfoundry_config)) + monkeypatch.setenv('VCAP_APPLICATION', '{"space_name":"🚀🌌"}') + + +@pytest.mark.usefixtures('os_environ', 'cloudfoundry_environ') +def test_extract_cloudfoundry_config_populates_other_vars(): + extract_cloudfoundry_config() + + assert os.environ['LOGGING_STDOUT_JSON'] == '1' + assert os.environ['NOTIFY_ENVIRONMENT'] == '🚀🌌' @pytest.mark.usefixtures('os_environ', 'cloudfoundry_environ') diff --git a/tests/app/test_config.py b/tests/app/test_config.py index 8c90c0b12..eb4dd1e5f 100644 --- a/tests/app/test_config.py +++ b/tests/app/test_config.py @@ -53,6 +53,22 @@ def test_load_config_if_cloudfoundry_not_available(monkeypatch, reload_config): assert config.Config.API_HOST_NAME == 'env' -def test_cloudfoundry_config_has_different_defaults(): - # these should always be set on Sandbox - assert config.Sandbox.DEBUG is True +def test_logging_stdout_json_defaults_to_off(reload_config): + os.environ.pop('LOGGING_STDOUT_JSON', None) + assert config.Config.LOGGING_STDOUT_JSON is False + + +def test_logging_stdout_json_sets_to_off_if_not_recognised(reload_config): + os.environ['LOGGING_STDOUT_JSON'] = 'foo' + + importlib.reload(config) + + assert config.Config.LOGGING_STDOUT_JSON is False + + +def test_logging_stdout_json_sets_to_on_if_set_to_1(reload_config): + os.environ['LOGGING_STDOUT_JSON'] = '1' + + importlib.reload(config) + + assert config.Config.LOGGING_STDOUT_JSON is True