From ae4d10228865ca5e1d065747f4de7a7dff2399a3 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Sun, 18 Oct 2015 18:02:52 +0200 Subject: [PATCH] Added tests for the checkoutflow module --- src/requirements/testing.txt | 1 + src/tests/presale/test_checkoutflow.py | 104 +++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/tests/presale/test_checkoutflow.py diff --git a/src/requirements/testing.txt b/src/requirements/testing.txt index 11690e820f..98362b0bce 100644 --- a/src/requirements/testing.txt +++ b/src/requirements/testing.txt @@ -9,6 +9,7 @@ selenium pytest pytest-django isort +pytest-mock # PyVirtualDisplay # -e git+https://github.com/pretix/sauceclient.git@master#egg=sauceclient # travis diff --git a/src/tests/presale/test_checkoutflow.py b/src/tests/presale/test_checkoutflow.py new file mode 100644 index 0000000000..bfafa91045 --- /dev/null +++ b/src/tests/presale/test_checkoutflow.py @@ -0,0 +1,104 @@ +import pytest +from django.test import RequestFactory +from django.utils.timezone import now + +from pretix.base.models import Event, Organizer +from pretix.multidomain.middlewares import SessionMiddleware +from pretix.presale import checkoutflow + + +@pytest.fixture +def event(): + o = Organizer.objects.create(name='MRMCD', slug='mrmcd') + e = Event.objects.create( + organizer=o, name='MRMCD2015', slug='2015', + date_from=now() + ) + return e + + +@pytest.fixture +def req_with_session(): + factory = RequestFactory() + r = factory.get('/') + SessionMiddleware().process_request(r) + r.session.save() + return r + + +@pytest.mark.django_db +def test_flow_order(event): + orig_flow = checkoutflow.DEFAULT_FLOW + checkoutflow.DEFAULT_FLOW = ( + checkoutflow.ConfirmStep, checkoutflow.PaymentStep, checkoutflow.QuestionsStep + ) + flow = checkoutflow.get_checkout_flow(event) + assert all(flow[i].priority <= flow[i + 1].priority for i in range(len(flow) - 1)) + checkoutflow.DEFAULT_FLOW = orig_flow + + +@pytest.mark.django_db +def test_double_linked_list(event): + flow = checkoutflow.get_checkout_flow(event) + assert all(flow[i]._next is flow[i + 1] for i in range(len(flow) - 1)) + assert all(flow[i + 1]._previous is flow[i] for i in range(len(flow) - 1)) + + +@pytest.mark.django_db +def test_plugins_called(event, mocker): + from pretix.presale.signals import checkout_flow_steps + mocker.patch('pretix.presale.signals.checkout_flow_steps.send') + checkoutflow.get_checkout_flow(event) + checkout_flow_steps.send.assert_called_once_with(event) + + +def with_mocked_step(mocker, step, event): + from pretix.presale.signals import checkout_flow_steps + mocker.patch('pretix.presale.signals.checkout_flow_steps.send') + checkout_flow_steps.send.return_value = [(None, step)] + return checkoutflow.get_checkout_flow(event) + + +@pytest.mark.django_db +def test_plugins_max_priority(event, mocker): + class MockingStep(checkoutflow.BaseCheckoutFlowStep): + identifier = 'mocking' + priority = 1001 + + with pytest.raises(ValueError): + with_mocked_step(mocker, MockingStep, event) + + +@pytest.mark.django_db +def test_plugin_in_order(event, mocker): + class MockingStep(checkoutflow.BaseCheckoutFlowStep): + identifier = 'mocking' + priority = 100 + + flow = with_mocked_step(mocker, MockingStep, event) + assert isinstance(flow[0], checkoutflow.QuestionsStep) + assert isinstance(flow[1], MockingStep) + assert isinstance(flow[2], checkoutflow.PaymentStep) + assert isinstance(flow[3], checkoutflow.ConfirmStep) + + +@pytest.mark.django_db +def test_step_ignored(event, mocker, req_with_session): + class MockingStep(checkoutflow.BaseCheckoutFlowStep): + identifier = 'mocking' + priority = 100 + + def is_applicable(self, request): + return False + + flow = with_mocked_step(mocker, MockingStep, event) + req_with_session.event = event + assert flow[0].get_next_applicable(req_with_session) is flow[2] + assert flow[0] is flow[2].get_prev_applicable(req_with_session) + + +@pytest.mark.django_db +def test_step_first_last(event): + flow = checkoutflow.get_checkout_flow(event) + assert flow[0].get_prev_applicable(req_with_session) is None + assert flow[-1].get_next_applicable(req_with_session) is None