Skip to content

Commit

Permalink
secret envs
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Oct 2, 2023
1 parent 6bd9908 commit 67f2b8b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
35 changes: 33 additions & 2 deletions services/payments/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


from collections.abc import AsyncIterator, Callable, Iterator
from pathlib import Path

import httpx
import pytest
Expand All @@ -16,6 +17,7 @@
from fastapi.encoders import jsonable_encoder
from pytest_mock import MockerFixture
from pytest_simcore.helpers.typing_env import EnvVarsDict
from pytest_simcore.helpers.utils_envs import EnvVarsDict, load_dotenv
from respx import MockRouter
from simcore_service_payments.core.application import create_app
from simcore_service_payments.core.settings import ApplicationSettings
Expand Down Expand Up @@ -44,6 +46,25 @@ def _doit():
return _doit


@pytest.fixture
def external_secret_envs(project_tests_dir: Path) -> EnvVarsDict:
"""
If a file under test prefixed `.env-secret` is present,
then some mocks are disabled and real external services are used.
This technique allows reusing the same tests to check against
external development/production servers
"""
envs = {}
env_files = list(project_tests_dir.glob(".env-secret*"))
if env_files:
assert len(env_files) == 1
envs = load_dotenv(env_files[0])
assert "PAYMENTS_GATEWAY_API_SECRET" in envs
assert "PAYMENTS_GATEWAY_URL" in envs
return envs


@pytest.fixture
async def app(app_environment: EnvVarsDict) -> AsyncIterator[FastAPI]:
test_app = create_app()
Expand All @@ -57,11 +78,21 @@ async def app(app_environment: EnvVarsDict) -> AsyncIterator[FastAPI]:

@pytest.fixture
def mock_payments_gateway_service_api_base(
app: FastAPI,
app: FastAPI, external_secret_envs: EnvVarsDict
) -> Iterator[MockRouter]:
"""
If external_secret_envs is present, then this mock is not really used
and instead the test runs against some real services
"""
settings: ApplicationSettings = app.state.settings
mock_base_url = settings.PAYMENTS_GATEWAY_URL

if external_secret_envs.get("PAYMENTS_GATEWAY_URL") == mock_base_url:
print("WARNING: Bypassing mock, and using external service at", mock_base_url)
mock_base_url = "https://httpbin.org/"

with respx.mock(
base_url=settings.PAYMENTS_GATEWAY_URL,
base_url=mock_base_url,
assert_all_called=False,
assert_all_mocked=True, # IMPORTANT: KEEP always True!
) as respx_mock:
Expand Down
23 changes: 6 additions & 17 deletions services/payments/tests/unit/test_services_payments_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@


from collections.abc import Callable
from pathlib import Path

import pytest
from faker import Faker
from fastapi import FastAPI, status
from pytest_simcore.helpers.utils_envs import (
EnvVarsDict,
load_dotenv,
setenvs_from_dict,
)
from pytest_simcore.helpers.utils_envs import EnvVarsDict, setenvs_from_dict
from respx import MockRouter
from simcore_service_payments.core.settings import ApplicationSettings
from simcore_service_payments.models.payments_gateway import InitPayment
Expand All @@ -39,21 +34,16 @@ def app_environment(
app_environment: EnvVarsDict,
disable_rabbitmq_and_rpc_setup: Callable,
disable_db_setup: Callable,
project_tests_dir: Path,
external_secret_envs: EnvVarsDict,
):
# mocks setup
disable_rabbitmq_and_rpc_setup()
disable_db_setup()

secret_envs = {}
env_file = project_tests_dir / ".env-secret.ignore.keep"
if env_file.exists():
secret_envs = load_dotenv(env_file)

# set environs
return setenvs_from_dict(
monkeypatch,
{**app_environment, **secret_envs},
{**app_environment, **external_secret_envs},
)


Expand Down Expand Up @@ -82,17 +72,16 @@ async def test_payment_gateway_responsiveness(
assert await payment_gateway_api.is_healhy()


@pytest.mark.testit
@pytest.mark.acceptance_test(
"https://github.com/ITISFoundation/osparc-simcore/pull/4715"
)
async def test_one_time_payment_workflow(
app: FastAPI,
faker: Faker,
# mock_payments_gateway_service_api_base: MockRouter,
# mock_init_payment_route: Callable,
mock_payments_gateway_service_api_base: MockRouter,
mock_init_payment_route: Callable,
):
# mock_init_payment_route(mock_payments_gateway_service_api_base)
mock_init_payment_route(mock_payments_gateway_service_api_base)

payment_gateway_api = PaymentsGatewayApi.get_from_state(app)
assert payment_gateway_api
Expand Down

0 comments on commit 67f2b8b

Please sign in to comment.