From 97a0d7f31441681b5096c066999b14a88c6175e8 Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: Mar 02 2020 14:05:43 +0000
Subject: Omit overriding custom config file values with env variables


Signed-off-by: Lukas Holecek <hluk@email.cz>

---

diff --git a/greenwave/tests/conftest.py b/greenwave/tests/conftest.py
index aec4009..4c61cca 100644
--- a/greenwave/tests/conftest.py
+++ b/greenwave/tests/conftest.py
@@ -5,7 +5,7 @@ from greenwave.app_factory import create_app
 
 @pytest.fixture(autouse=True)
 def mock_env_config(monkeypatch):
-    monkeypatch.delenv('GREENWAVE_CONFIG')
+    monkeypatch.delenv('GREENWAVE_CONFIG', raising=False)
 
 
 @pytest.fixture
diff --git a/greenwave/tests/settings_empty.py b/greenwave/tests/settings_empty.py
new file mode 100644
index 0000000..21e2bfa
--- /dev/null
+++ b/greenwave/tests/settings_empty.py
@@ -0,0 +1 @@
+# Empty settings file for tests.
diff --git a/greenwave/tests/settings_override.py b/greenwave/tests/settings_override.py
new file mode 100644
index 0000000..02a56c8
--- /dev/null
+++ b/greenwave/tests/settings_override.py
@@ -0,0 +1,3 @@
+# Settings file for tests.
+POLICIES_DIR = '/src/conf/policies'
+SUBJECT_TYPES_DIR = '/src/conf/subject_types'
diff --git a/greenwave/tests/test_utils.py b/greenwave/tests/test_utils.py
index c3abd12..d8733b0 100644
--- a/greenwave/tests/test_utils.py
+++ b/greenwave/tests/test_utils.py
@@ -1,15 +1,19 @@
-
 # SPDX-License-Identifier: GPL-2.0+
 
-import pytest
-
 import json
+import os
+
+import pytest
 
 from requests import ConnectionError, ConnectTimeout, Timeout
 from werkzeug.exceptions import InternalServerError
 
 import greenwave.app_factory
-from greenwave.utils import json_error
+from greenwave.utils import json_error, load_config
+
+
+SETTINGS_DIR = os.path.abspath(os.path.dirname(__file__))
+SETTINGS_BASE_NAME = os.path.join(SETTINGS_DIR, 'settings')
 
 
 @pytest.mark.parametrize(('error, expected_status_code,'
@@ -28,3 +32,30 @@ def test_json_connection_error(error, expected_status_code,
             data = json.loads(r.get_data())
             assert r.status_code == expected_status_code
             assert expected_error_message_part in data['message']
+
+
+def test_load_config_defaults(monkeypatch):
+    monkeypatch.setenv('GREENWAVE_CONFIG', SETTINGS_BASE_NAME + '_empty.py')
+    monkeypatch.delenv('GREENWAVE_POLICIES_DIR', raising=False)
+    monkeypatch.delenv('GREENWAVE_SUBJECT_TYPES_DIR', raising=False)
+    config = load_config('greenwave.config.ProductionConfig')
+    assert config['POLICIES_DIR'] == '/etc/greenwave/policies'
+    assert config['SUBJECT_TYPES_DIR'] == '/etc/greenwave/subject_types'
+
+
+def test_load_config_override_with_env(monkeypatch):
+    monkeypatch.setenv('GREENWAVE_CONFIG', SETTINGS_BASE_NAME + '_empty.py')
+    monkeypatch.setenv('GREENWAVE_POLICIES_DIR', '/policies')
+    monkeypatch.setenv('GREENWAVE_SUBJECT_TYPES_DIR', '/subject_types')
+    config = load_config('greenwave.config.ProductionConfig')
+    assert config['POLICIES_DIR'] == '/policies'
+    assert config['SUBJECT_TYPES_DIR'] == '/subject_types'
+
+
+def test_load_config_override_with_custom_config(monkeypatch):
+    monkeypatch.setenv('GREENWAVE_CONFIG', SETTINGS_BASE_NAME + '_override.py')
+    monkeypatch.setenv('GREENWAVE_POLICIES_DIR', '/policies')
+    monkeypatch.setenv('GREENWAVE_SUBJECT_TYPES_DIR', '/subject_types')
+    config = load_config('greenwave.config.ProductionConfig')
+    assert config['POLICIES_DIR'] == '/src/conf/policies'
+    assert config['SUBJECT_TYPES_DIR'] == '/src/conf/subject_types'
diff --git a/greenwave/utils.py b/greenwave/utils.py
index 658dd14..240c7d7 100644
--- a/greenwave/utils.py
+++ b/greenwave/utils.py
@@ -88,9 +88,18 @@ def load_config(config_obj=None):
     else:
         default_config_file = '/etc/greenwave/settings.py'
 
+    # 1. Load default configuration.
     log.debug("config: Loading config from %r", config_obj)
     config.from_object(config_obj)
 
+    # 2. Override default configuration with environment variables.
+    if os.environ.get('GREENWAVE_SUBJECT_TYPES_DIR'):
+        config['SUBJECT_TYPES_DIR'] = os.environ['GREENWAVE_SUBJECT_TYPES_DIR']
+
+    if os.environ.get('GREENWAVE_POLICIES_DIR'):
+        config['POLICIES_DIR'] = os.environ['GREENWAVE_POLICIES_DIR']
+
+    # 3. Override default configuration and environment variables with custom config file.
     config_file = os.environ.get('GREENWAVE_CONFIG', default_config_file)
     log.debug("config: Extending config with %r", config_file)
     config.from_pyfile(config_file)
@@ -98,12 +107,6 @@ def load_config(config_obj=None):
     if os.environ.get('SECRET_KEY'):
         config['SECRET_KEY'] = os.environ['SECRET_KEY']
 
-    if os.environ.get('GREENWAVE_SUBJECT_TYPES_DIR'):
-        config['SUBJECT_TYPES_DIR'] = os.environ['GREENWAVE_SUBJECT_TYPES_DIR']
-
-    if os.environ.get('GREENWAVE_POLICIES_DIR'):
-        config['POLICIES_DIR'] = os.environ['GREENWAVE_POLICIES_DIR']
-
     return config
 
 

