Skip to content

Commit

Permalink
Enable test data generation without mock verkkokauppa enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
matti-lamppu committed Nov 19, 2024
1 parent a6c8db1 commit b18956e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 45 deletions.
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

0 comments on commit b18956e

Please sign in to comment.