Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# python-dotenv environment file for test account credentials.
.env


# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ dev = [
"types-setuptools",
"pytest-asyncio",
"validators",
"python-dotenv",
]

[build-system]
Expand Down
23 changes: 13 additions & 10 deletions tests/tests_transfers/aws/aws_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
from datashuttle import DataShuttle
from datashuttle.utils import aws, utils

from .. import transfer_test_utils


def setup_project_for_aws(project: DataShuttle):
"""Update the config file for an AWS connection.

The connection credentials are fetched from the environment which
the developer shall set themselves to test locally. In the CI, these
are set using the github secrets. A random string is added to the
are set using the GitHub secrets. A random string is added to the
central path so that the test project paths do not interfere while
running multiple test instances simultaneously in CI.
"""
Expand All @@ -35,6 +37,7 @@ def setup_aws_connection(project: DataShuttle):
testing. For testing locally, the developer must set it themselves.
"""
original_get_secret = copy.deepcopy(aws.get_aws_secret_access_key)

aws.get_aws_secret_access_key = lambda *args, **kwargs: os.environ[
"AWS_SECRET_ACCESS_KEY"
]
Expand All @@ -45,17 +48,17 @@ def setup_aws_connection(project: DataShuttle):


def has_aws_environment_variables():
for key in [
"""Check for environment variables needed to run AWS tests.

Environment variables can be stored in a `.env` file in the
project root, for use with `python-dotenv`. Otherwise,
they are set up in GitHub actions.
"""
required_variables = [
"AWS_BUCKET_NAME",
"AWS_ACCESS_KEY_ID",
"AWS_REGION",
"AWS_SECRET_ACCESS_KEY",
]:
if key not in os.environ:
return False

# On CI triggered by forked repositories, secrets are empty
if os.environ[key].strip() == "":
return False
]

return True
return transfer_test_utils.check_if_env_vars_are_loaded(required_variables)
23 changes: 12 additions & 11 deletions tests/tests_transfers/gdrive/gdrive_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from datashuttle import DataShuttle
from datashuttle.utils import gdrive, utils

from .. import transfer_test_utils


def setup_project_for_gdrive(project: DataShuttle):
"""Set up a project with configs for Google Drive transfers.

The connection credentials are fetched from the environment which
the developer shall set themselves to test locally. In the CI, these
are set using the github secrets. A random string is added to the
are set using the GitHub secrets. A random string is added to the
central path so that the test project paths do not interfere while
running multiple test instances simultaneously in CI.
"""
Expand Down Expand Up @@ -51,25 +53,24 @@ def mock_input(_: str) -> str:
gdrive.get_client_secret = lambda *args, **kwargs: os.environ[
"GDRIVE_CLIENT_SECRET"
]

project.setup_gdrive_connection()

builtins.input = original_input
gdrive.get_client_secret = original_get_secret


def has_gdrive_environment_variables():
for key in [
"""Check for environment variables needed to run GDrive tests.

Environment variables can be stored in a `.env` file in the
project root, for use with `python-dotenv`. Otherwise,
they are set up in GitHub actions.
"""
required_variables = [
"GDRIVE_CLIENT_ID",
"GDRIVE_ROOT_FOLDER_ID",
"GDRIVE_CONFIG_TOKEN",
"GDRIVE_CLIENT_SECRET",
]:
if key not in os.environ:
return False

# On CI triggered by forked repositories, secrets are empty
if os.environ[key].strip() == "":
return False
]

return True
return transfer_test_utils.check_if_env_vars_are_loaded(required_variables)
40 changes: 40 additions & 0 deletions tests/tests_transfers/transfer_test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os

from dotenv import load_dotenv


def check_if_env_vars_are_loaded(required_variables):
"""Check if environment variables required to run tests are loaded.

If we are on GitHub, these should be loaded in the
workflow `.yaml` files. Otherwise, these can be set manually
or (more conveniently) added to a `.env` file in the project root.

Parameters
----------
required_variables :
A list of required variables to check for.

Returns
-------
bool
True if all required environment variables are loaded, False otherwise.
"""
# Outside of GitHub actions, if env vars are not
# loaded try and load using dotenv.
if (
not os.getenv("GITHUB_ACTIONS")
and not all(var in os.environ for var in required_variables)
and not load_dotenv()
):
return False

for var in required_variables:
if var not in os.environ:
return False

# On CI triggered by forked repositories, secrets are empty
if os.environ[var].strip() == "":
return False

return True
Loading