Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the existing GPG fixture in the functional tests (CryptoUtil 2/3) #6184

Conversation

nabla-c0d3
Copy link
Contributor

@nabla-c0d3 nabla-c0d3 commented Nov 22, 2021

Status

Ready.

Description of Changes

This PR updates the functional tests to use the same fixture for GPG as the other tests. It also updates the config fixture.

  • This is a prerequisite for the refactoring of the GPG code in Refactor encryption logic to fix type-checking and improve tests (CryptoUtil 3/3) #6160 which uses the updated config fixture.
  • This is a small step forward for Convert functional tests to use pytest/fixtures #3836. More work is needed to refactor the functional tests and their fixtures.
  • Because the GPG fixture that is now being used is session-scoped, it is only called once during the whole test suite, and only one GPG home directory is used for all the tests. This helps a little bit with GPG agent processes are not cleaned up during testing #4332 (only 1 GPG process should get created instead of ~80).
  • There is more work to be done with the config fixture itself, and I might take a look at it eventually. The biggest problem is that tests might either use the config from the config() fixture, or the config from the config.py file generated by make test right before the tests run.
    • This creates really tricky interactions and bugs when working on the test suite, because it's unclear which config is being used. It's also sometime unclear what config is needed for a specific test to succeed, and some tests also tend to mutate the config object (for example changing the time it takes for a session to expire).
    • This PR makes small changes to slightly improve this situation.


# WARNING: This variable must be set before any import of the securedrop code/app
# or most tests will fail
os.environ["SECUREDROP_ENV"] = "test"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting this here ensures that the SECUREDROP_ENV will always be set before any application code is imported.

@@ -110,37 +107,39 @@ def insecure_scrypt() -> Generator[None, None, None]:
yield


def setup_gpg(gpg_dir: Path) -> str:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All I did here was move the logic in setup_gpg() to setup_journalist_key_and_gpg_folder().

@@ -172,7 +171,10 @@ def config(

config.SUPPORTED_LOCALES = i18n.get_test_locales()

yield config
# Set this newly-created config as the "global" config
with mock.patch.object(sdconfig, "config", config):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important change here: this ensures that the application code "sees" the same config as the one created in the fixture (when the code imports the config).

It's not perfect and more work is needed with the config fixture, but it's a step forward.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SECUREDROP_ENV does not seem to be set here when make test is run locally . It's used to set the database URI to the appropriate value for the environment, so without it the alembic tests in particular will fail. (Said failure isn't happening in CI for some reason).

One solution is to add os.environ["SECUREDROP_ENV"] = "test" before filesystem setup, but I'm sure there are more elegant ones :)

config.SOURCE_APP_FLASK_CONFIG_CLS.USE_X_SENDFILE = False

# Disable CSRF checks to make writing tests easier
config.SOURCE_APP_FLASK_CONFIG_CLS.WTF_CSRF_ENABLED = False
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was copied from the config.py created by make test. These settings are needed for the tests to pass.

@@ -202,7 +199,7 @@ def set_default_driver(self):
logging.error("Error stopping Firefox driver: %s", e)

@pytest.fixture(autouse=True)
def sd_servers(self):
def sd_servers(self, setup_journalist_key_and_gpg_folder):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "main" change in this PR.

# Then check the directory was already deleted
assert not os.path.exists(config.TEMP_DIR)
try:
shutil.rmtree(config.SECUREDROP_DATA_ROOT)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am no longer deleting the SECUREDROP_DATA_ROOT between each test because it will delete the GPG_KEY_DIR too (as it's a subfolder of SECUREDROP_DATA_ROOT).

@nabla-c0d3 nabla-c0d3 marked this pull request as ready for review November 25, 2021 02:55
@nabla-c0d3 nabla-c0d3 requested a review from a team as a code owner November 25, 2021 02:55
@zenmonkeykstop zenmonkeykstop self-assigned this Nov 29, 2021
Copy link
Contributor

@zenmonkeykstop zenmonkeykstop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI looks good, but the test_alembic.py tests fail when make test is run locally as the db URI is being set to the prod value instead of test's.

@@ -172,7 +171,10 @@ def config(

config.SUPPORTED_LOCALES = i18n.get_test_locales()

yield config
# Set this newly-created config as the "global" config
with mock.patch.object(sdconfig, "config", config):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SECUREDROP_ENV does not seem to be set here when make test is run locally . It's used to set the database URI to the appropriate value for the environment, so without it the alembic tests in particular will fail. (Said failure isn't happening in CI for some reason).

One solution is to add os.environ["SECUREDROP_ENV"] = "test" before filesystem setup, but I'm sure there are more elegant ones :)

@eloquence eloquence added this to In Development in SecureDrop Team Board Dec 1, 2021
from db import db
from models import (
if not os.environ.get("SECUREDROP_ENV"):
os.environ['SECUREDROP_ENV'] = 'dev' # noqa
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zenmonkeykstop This fixes the alembic tests. Thanks for explaining what the problem was.

zenmonkeykstop
zenmonkeykstop previously approved these changes Dec 9, 2021
@zenmonkeykstop
Copy link
Contributor

(rebased to pull in a dependency update and get tests passing)

Copy link
Contributor

@zenmonkeykstop zenmonkeykstop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, tests passing locally as well now!

@zenmonkeykstop zenmonkeykstop merged commit 90b3a08 into freedomofpress:develop Dec 10, 2021
SecureDrop Team Board automation moved this from In Development to Done Dec 10, 2021
@nabla-c0d3 nabla-c0d3 deleted the consolidate-gpg-test-fixtures branch June 11, 2022 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants