Add flag testmode_supported to sales channels (#1455)

* Add testmode-support-flag to SalesChannels

* Make saleschannels/testmode-warnings even more dangerous!

* Add warning for payment-methods that do support testmode but are being used in a non-testmode order caused by a saleschannel in a testmode-shop.

* Remove redundant testmode_supported-flag for WebshopSalesChannel

* Raise error on API when sales_channel does not support testmode

* Tests

* Fix style issue after merge
This commit is contained in:
Martin Gross
2019-10-21 10:07:02 +02:00
committed by Raphael Michel
parent e8a2f7e349
commit 2b18621c76
14 changed files with 171 additions and 24 deletions

View File

@@ -6,6 +6,7 @@ from unittest import mock
import pytest
from django.core import mail as djmail
from django.dispatch import receiver
from django.utils.timezone import now
from django_countries.fields import Country
from django_scopes import scopes_disabled
@@ -13,6 +14,7 @@ from pytz import UTC
from stripe.error import APIConnectionError
from tests.plugins.stripe.test_provider import MockedCharge
from pretix.base.channels import SalesChannel
from pretix.base.models import (
InvoiceAddress, Order, OrderPosition, Question, SeatingPlan,
)
@@ -22,6 +24,21 @@ from pretix.base.models.orders import (
from pretix.base.services.invoices import (
generate_cancellation, generate_invoice,
)
from pretix.base.signals import register_sales_channels
class FoobarSalesChannel(SalesChannel):
identifier = "bar"
verbose_name = "Foobar"
icon = "home"
testmode_supported = False
@receiver(register_sales_channels, dispatch_uid="test_orders_register_sales_channels")
def base_sales_channels(sender, **kwargs):
return (
FoobarSalesChannel(),
)
@pytest.fixture
@@ -1536,6 +1553,22 @@ def test_order_create_in_test_mode(token_client, organizer, event, item, quota,
assert o.testmode
@pytest.mark.django_db
def test_order_create_in_test_mode_saleschannel_limited(token_client, organizer, event, item, quota, question):
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
res['positions'][0]['item'] = item.pk
res['positions'][0]['answers'][0]['question'] = question.pk
res['testmode'] = True
res['sales_channel'] = 'bar'
resp = token_client.post(
'/api/v1/organizers/{}/events/{}/orders/'.format(
organizer.slug, event.slug
), format='json', data=res
)
assert resp.status_code == 400
assert resp.data == {'testmode': ['This sales channel does not provide support for testmode.']}
@pytest.mark.django_db
def test_order_create_attendee_name_optional(token_client, organizer, event, item, quota, question):
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)

View File

@@ -4,11 +4,13 @@ from decimal import Decimal
import pytest
import pytz
from django.core import mail as djmail
from django.dispatch import receiver
from django.test import TestCase
from django.utils.timezone import make_aware, now
from django_countries.fields import Country
from django_scopes import scope
from pretix.base.channels import SalesChannel
from pretix.base.decimal import round_decimal
from pretix.base.models import (
CartPosition, Event, InvoiceAddress, Item, Order, OrderPosition, Organizer,
@@ -23,10 +25,25 @@ from pretix.base.services.orders import (
OrderChangeManager, OrderError, _create_order, approve_order, cancel_order,
deny_order, expire_orders, send_download_reminders, send_expiry_warnings,
)
from pretix.base.signals import register_sales_channels
from pretix.plugins.banktransfer.payment import BankTransfer
from pretix.testutils.scope import classscope
class FoobarSalesChannel(SalesChannel):
identifier = "bar"
verbose_name = "Foobar"
icon = "home"
testmode_supported = False
@receiver(register_sales_channels, dispatch_uid="test_orders_register_sales_channels")
def base_sales_channels(sender, **kwargs):
return (
FoobarSalesChannel(),
)
@pytest.fixture(scope='function')
def event():
o = Organizer.objects.create(name='Dummy', slug='dummy')
@@ -1960,6 +1977,38 @@ def test_autocheckin(clist_autocheckin, event):
assert order.positions.first().checkins.count() == 0
@pytest.mark.django_db
def test_saleschannel_testmode_restriction(event):
today = now()
tr7 = event.tax_rules.create(rate=Decimal('17.00'))
ticket = Item.objects.create(event=event, name='Early-bird ticket', tax_rule=tr7,
default_price=Decimal('23.00'), admission=True)
cp1 = CartPosition.objects.create(
item=ticket, price=23, expires=now() + timedelta(days=1), event=event, cart_id="123"
)
order = _create_order(event, email='dummy@example.org', positions=[cp1],
now_dt=today, payment_provider=FreeOrderProvider(event),
locale='de', sales_channel='web')[0]
assert not order.testmode
order = _create_order(event, email='dummy@example.org', positions=[cp1],
now_dt=today, payment_provider=FreeOrderProvider(event),
locale='de', sales_channel=FoobarSalesChannel.identifier)[0]
assert not order.testmode
event.testmode = True
order = _create_order(event, email='dummy@example.org', positions=[cp1],
now_dt=today, payment_provider=FreeOrderProvider(event),
locale='de', sales_channel='web')[0]
assert order.testmode
order = _create_order(event, email='dummy@example.org', positions=[cp1],
now_dt=today, payment_provider=FreeOrderProvider(event),
locale='de', sales_channel=FoobarSalesChannel.identifier)[0]
assert not order.testmode
@pytest.mark.django_db
def test_giftcard_multiple(event):
ticket = Item.objects.create(event=event, name='Early-bird ticket',

View File

@@ -9,6 +9,7 @@ from django.utils.timezone import now
from django_countries.fields import Country
from django_scopes import scopes_disabled
from pretix.base.channels import SalesChannel
from pretix.base.decimal import round_decimal
from pretix.base.models import (
CartPosition, Event, InvoiceAddress, Item, ItemCategory, ItemVariation,
@@ -24,6 +25,13 @@ from pretix.testutils.scope import classscope
from pretix.testutils.sessions import get_cart_session_key
class FoobarSalesChannel(SalesChannel):
identifier = "bar"
verbose_name = "Foobar"
icon = "home"
testmode_supported = True
class CartTestMixin:
@scopes_disabled()
def setUp(self):
@@ -759,7 +767,7 @@ class CartTest(CartTestMixin, TestCase):
self.ticket.save()
self.client.post('/%s/%s/cart/add' % (self.orga.slug, self.event.slug), {
'item_%d' % self.ticket.id: '1',
}, follow=True, PRETIX_SALES_CHANNEL='bar')
}, follow=True, PRETIX_SALES_CHANNEL=FoobarSalesChannel)
with scopes_disabled():
self.assertEqual(CartPosition.objects.filter(cart_id=self.session_key, event=self.event).count(), 1)

View File

@@ -12,6 +12,7 @@ from django_scopes import scopes_disabled
from pytz import timezone
from tests.base import SoupTest
from pretix.base.channels import SalesChannel
from pretix.base.models import (
Event, Item, ItemCategory, ItemVariation, Order, Organizer, Quota, Team,
User, WaitingListEntry,
@@ -19,6 +20,13 @@ from pretix.base.models import (
from pretix.base.models.items import SubEventItem, SubEventItemVariation
class FoobarSalesChannel(SalesChannel):
identifier = "bar"
verbose_name = "Foobar"
icon = "home"
testmode_supported = True
class EventTestMixin:
@scopes_disabled()
def setUp(self):
@@ -109,7 +117,7 @@ class ItemDisplayTest(EventTestMixin, SoupTest):
q.items.add(item)
html = self.client.get('/%s/%s/' % (self.orga.slug, self.event.slug)).rendered_content
self.assertNotIn("Early-bird", html)
html = self.client.get('/%s/%s/' % (self.orga.slug, self.event.slug), PRETIX_SALES_CHANNEL="bar").rendered_content
html = self.client.get('/%s/%s/' % (self.orga.slug, self.event.slug), PRETIX_SALES_CHANNEL=FoobarSalesChannel).rendered_content
self.assertIn("Early-bird", html)
def test_timely_available(self):