Repository: freedomofpress/securedrop

Issue:
development server no longer hot-reloads on file changes
## Description

After #6563, `make dev` no longer hot-reloads on file changes, because the immutable `SDConfig` doesn't carry the `env` attribute from `config.py`.  (This regression was last encountered in #5594.)

## Steps to Reproduce

Same as #5594.

## Expected Behavior

Same as #5594.

## Actual Behavior

Same as #5594.

## Comments

The fix in <https://github.com/freedomofpress/securedrop/issues/6669#issuecomment-1526129678> is trivial, but I want to take a moment to see if there's an easy way to test for this regression (fool me once, fool me twice, etc.).


Discussion:
Can I work on this?
On Wed, May 10, 2023 at 08:55:40AM -0700, Siddhant Ota wrote:
> Can I work on this?

Sure; thanks your interest!  You'll find the basic fix in
<https://github.com/freedomofpress/securedrop/issues/6669#issuecomment-1526129678>.
The goal here is to find out if we can add a test that checks that "env"
is passed through as expected.  Let us know if you have any questions.

Thanks, I'm new to open source but I'll give me best. I'll let you know if I need anything.
I have applied the patch you mentioned in #6669 and reload works in my machine. But I don't know how to write tests (I don't understand how it is implemented in this repo to be specific). Can you point me to some resources where I can learn? Thanks



> On Wed, May 10, 2023 at 08:55:40AM -0700, Siddhant Ota wrote: Can I work on this?
> Sure; thanks your interest! You'll find the basic fix in <[#6669 (comment)](https://github.com/freedomofpress/securedrop/issues/6669#issuecomment-1526129678)>. The goal here is to find out if we can add a test that checks that "env" is passed through as expected. Let us know if you have any questions.

I have applied the patch you mentioned in https://github.com/freedomofpress/securedrop/issues/6669 and reload works in my machine. But I don't know how to write tests (I don't understand how it is implemented in this repo to be specific). Can you point me to some resources where I can learn? Thanks

Resolving patch:
diff --git a/securedrop/sdconfig.py b/securedrop/sdconfig.py
--- a/securedrop/sdconfig.py
+++ b/securedrop/sdconfig.py
@@ -71,6 +71,8 @@ class SecureDropConfig:
 
     RQ_WORKER_NAME: str
 
+    env: str = "prod"
+
     @property
     def TEMP_DIR(self) -> Path:
         # We use a directory under the SECUREDROP_DATA_ROOT instead of `/tmp` because
@@ -115,6 +117,8 @@ def _parse_config_from_file(config_module_name: str) -> SecureDropConfig:
         config_from_local_file, "SCRYPT_PARAMS", dict(N=2**14, r=8, p=1)
     )
 
+    env = getattr(config_from_local_file, "env", "prod")
+
     try:
         final_securedrop_root = Path(config_from_local_file.SECUREDROP_ROOT)
     except AttributeError:
@@ -188,6 +192,7 @@ def _parse_config_from_file(config_module_name: str) -> SecureDropConfig:
     )
 
     return SecureDropConfig(
+        env=env,
         JOURNALIST_APP_FLASK_CONFIG_CLS=parsed_journ_flask_config,
         SOURCE_APP_FLASK_CONFIG_CLS=parsed_source_flask_config,
         GPG_KEY_DIR=final_gpg_key_dir,


Test patch:
diff --git a/securedrop/tests/test_config.py b/securedrop/tests/test_config.py
--- a/securedrop/tests/test_config.py
+++ b/securedrop/tests/test_config.py
@@ -19,8 +19,12 @@ def test_parse_current_config():
     try:
         current_config_file.write_text(current_sample_config.read_text())
 
-        # When trying to parse it, it succeeds
-        assert _parse_config_from_file(f"tests.{current_config_module}")
+        # When trying to parse it, it succeeds...
+        parsed_config = _parse_config_from_file(f"tests.{current_config_module}")
+        assert parsed_config
+
+        # ...and has one of our `env` values rather than one of Flask's:
+        assert parsed_config.env in ["prod", "dev", "test"]
 
     finally:
         current_config_file.unlink(missing_ok=True)