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

Enable test data generation without mock verkkokauppa enabled #1444

Merged
merged 1 commit into from
Nov 19, 2024
Merged
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
20 changes: 15 additions & 5 deletions tests/factories/payment_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,22 @@ class PaymentOrderBuilder(ModelFactoryBuilder[PaymentOrder]):

def for_mock_order(self, reservation: Reservation) -> Self:
order_uuid = uuid.uuid4()
base_url = settings.MOCK_VERKKOKAUPPA_BACKEND_URL.strip("/")
checkout_url = reverse("mock_verkkokauppa:checkout", args=[order_uuid]).strip("/")
receipt_url = reverse("admin:tilavarauspalvelu_reservation_change", args=[reservation.id]).strip("/")

if settings.MOCK_VERKKOKAUPPA_API_ENABLED:
base_url = settings.MOCK_VERKKOKAUPPA_BACKEND_URL.strip("/")

checkout_path = reverse("mock_verkkokauppa:checkout", args=[order_uuid]).strip("/")
receipt_path = reverse("admin:tilavarauspalvelu_reservation_change", args=[reservation.id]).strip("/")

checkout_url = f"{base_url}/{checkout_path}/"
receipt_url = f"{base_url}/{receipt_path}/?"

else:
checkout_url = f"https://checkout-test.test.hel.ninja/{order_uuid}?user={reservation.user.uuid}"
receipt_url = f"https://checkout-test.test.hel.ninja/{order_uuid}/receipt?user={reservation.user.uuid}"

return self.set(
remote_id=order_uuid,
checkout_url=f"{base_url}/{checkout_url}/",
receipt_url=f"{base_url}/{receipt_url}/?",
checkout_url=checkout_url,
receipt_url=receipt_url,
)
38 changes: 0 additions & 38 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import uuid
from collections.abc import Callable
from functools import wraps
from typing import Any, NamedTuple, ParamSpec, Self, TypeVar
Expand All @@ -9,9 +8,6 @@
import pytest
from django.conf import settings
from django.http import HttpRequest
from django.test import override_settings
from django.test.signals import root_urlconf_changed
from django.urls import NoReverseMatch, URLResolver, include, path, reverse
from django.utils import translation
from django.utils.functional import lazy
from django.utils.translation import trans_real
Expand Down Expand Up @@ -213,37 +209,3 @@ def get_supported_language_variant(self, lang_code: Lang, strict: bool = False)

msg = f"Language '{lang_code}' is not supported"
raise LookupError(msg)


def with_mock_verkkokauppa(func: Callable[P, None]) -> Callable[P, None]:
"""Enables mock verkkokauppa API for the duration of the test."""

@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
from config.urls import urlpatterns

urls: URLResolver = path(
"mock_verkkokauppa/",
include("tilavarauspalvelu.api.mock_verkkokauppa_api.urls", namespace="mock_verkkokauppa"),
)

missing = False

# The mock verkkokauppa URLs are registered conditionally,
# so we need to check it they exist and add them to 'urlpatterns' if they don't.
try:
reverse("mock_verkkokauppa:checkout", args=[str(uuid.uuid4())])
except NoReverseMatch:
missing = True
urlpatterns.append(urls)
root_urlconf_changed(setting="ROOT_URLCONF")

try:
with override_settings(MOCK_VERKKOKAUPPA_API_ENABLED=True):
func(*args, **kwargs)
finally:
if missing:
urlpatterns.remove(urls)
root_urlconf_changed(setting="ROOT_URLCONF")

return wrapper
3 changes: 1 addition & 2 deletions tests/test_utils/test_create_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest
from django.db import models

from tests.helpers import with_mock_verkkokauppa
from tilavarauspalvelu.management.commands.create_test_data import create_test_data
from tilavarauspalvelu.models import (
AbilityGroup,
Expand Down Expand Up @@ -47,8 +46,8 @@

@pytest.mark.django_db
@pytest.mark.slow
@with_mock_verkkokauppa
def test_create_test_data(settings):
settings.MOCK_VERKKOKAUPPA_API_ENABLED = False
settings.UPDATE_RESERVATION_UNIT_THUMBNAILS = True

all_models = django.apps.apps.app_configs["tilavarauspalvelu"].get_models()
Expand Down