~homeworkprod/byceps

ref: fb66b66049d0567181a9f1007855f4e9cd7b913a byceps/tests/integration/services/shop/order/test_service_mark_order_as_paid.py -rw-r--r-- 2.3 KiB
fb66b660 — Jochen Kupperschmidt Introduce `get_order` 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

import pytest

from byceps.services.shop.order import event_service, service as order_service
from byceps.services.shop.order.transfer.models import (
    PaymentMethod,
    PaymentState,
)
from byceps.util.iterables import find

from tests.integration.services.shop.helpers import create_orderer


@pytest.fixture(scope='module')
def orderer(make_user_with_detail):
    return create_orderer(make_user_with_detail('PayingOrderer'))


@pytest.fixture
def order(admin_app, storefront, orderer, empty_cart):
    order, _ = order_service.place_order(storefront.id, orderer, empty_cart)
    yield order
    order_service.delete_order(order.id)


def test_mark_order_as_paid(order, admin_user):
    order_before = order
    assert order_before.payment_method is None
    assert order_before.payment_state == PaymentState.open
    assert order_before.is_open
    assert not order_before.is_paid

    order_service.mark_order_as_paid(
        order.id, PaymentMethod.cash, admin_user.id
    )

    order_after = order_service.get_order(order.id)
    assert order_after.payment_method == PaymentMethod.cash
    assert order_after.payment_state == PaymentState.paid
    assert not order_after.is_open
    assert order_after.is_paid


def test_additional_event_data(order, admin_user):
    additional_event_data = {
        # attempts to override internal properties
        'initiator_id': 'fake-initiator-id',
        'former_payment_state': 'random',
        'payment_method': 'telepathy',
        # custom properties
        'external_payment_id': '555-gimme-da-moneys',
    }

    paid_event = order_service.mark_order_as_paid(
        order.id,
        PaymentMethod.cash,
        admin_user.id,
        additional_event_data=additional_event_data,
    )

    events = event_service.get_events_for_order(order.id)
    paid_event = find(events, lambda e: e.event_type == 'order-paid')

    # Internal properties must not be overridden by additional event
    # data passed to the service.
    assert paid_event.data['initiator_id'] == str(admin_user.id)
    assert paid_event.data['former_payment_state'] == 'open'
    assert paid_event.data['payment_method'] == 'cash'

    # Other properties get passed unchanged.
    assert paid_event.data['external_payment_id'] == '555-gimme-da-moneys'