~homeworkprod/byceps

2fac21a37901423f812bd6554d20223de2cf8308 — Jochen Kupperschmidt 9 months ago 37c62f7
Use actual `CurrentUser` object in order email tests
M tests/helpers.py => tests/helpers.py +5 -2
@@ 16,6 16,9 @@ from flask import appcontext_pushed, g

from byceps.application import create_app, init_app
from byceps.database import db, generate_uuid
from byceps.services.authentication.session.models.current_user import (
    CurrentUser,
)
from byceps.services.authentication.session import service as session_service
from byceps.services.authorization import service as authz_service
from byceps.services.party import service as party_service


@@ 67,9 70,9 @@ def current_party_set(app, party):


@contextmanager
def current_user_set(app, user):
def current_user_set(app, current_user: CurrentUser):
    def handler(sender, **kwargs):
        g.user = user
        g.user = current_user

    with appcontext_pushed.connected_to(handler, app):
        yield

M tests/integration/services/shop/order/email/helpers.py => tests/integration/services/shop/order/email/helpers.py +13 -0
@@ 3,12 3,25 @@
:License: Revised BSD (see `LICENSE` file for details)
"""

from byceps.services.authentication.session.models.current_user import (
    CurrentUser,
)
from byceps.services.authentication.session import service as session_service
from byceps.services.shop.cart.models import Cart
from byceps.services.shop.order import service as order_service

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


def get_current_user_for_user(user) -> CurrentUser:
    permissions = set()
    locale = None

    return session_service.get_authenticated_current_user(
        user, permissions, locale
    )


def place_order_with_items(
    storefront_id, user, created_at=None, items_with_quantity=None
):

M tests/integration/services/shop/order/email/test_email_on_order_canceled.py => tests/integration/services/shop/order/email/test_email_on_order_canceled.py +3 -2
@@ 19,7 19,7 @@ from byceps.services.snippet import service as snippet_service
from tests.helpers import current_user_set
from tests.integration.services.shop.helpers import create_shop_fragment

from .helpers import place_order_with_items
from .helpers import get_current_user_for_user, place_order_with_items


@pytest.fixture


@@ 84,7 84,8 @@ def test_email_on_order_canceled(
    reason = 'Du hast nicht rechtzeitig bezahlt.'
    order_service.cancel_order(order.id, order_admin.id, reason)

    with current_user_set(app, customer), app.app_context():
    current_user = get_current_user_for_user(customer)
    with current_user_set(app, current_user), app.app_context():
        order_email_service.send_email_for_canceled_order_to_orderer(order.id)

    expected_sender = 'noreply@acmecon.test'

M tests/integration/services/shop/order/email/test_email_on_order_paid.py => tests/integration/services/shop/order/email/test_email_on_order_paid.py +3 -2
@@ 20,7 20,7 @@ from byceps.services.snippet import service as snippet_service
from tests.helpers import current_user_set
from tests.integration.services.shop.helpers import create_shop_fragment

from .helpers import place_order_with_items
from .helpers import get_current_user_for_user, place_order_with_items


@pytest.fixture


@@ 86,7 86,8 @@ def test_email_on_order_paid(
        order.id, PaymentMethod.bank_transfer, order_admin.id
    )

    with current_user_set(app, customer), app.app_context():
    current_user = get_current_user_for_user(customer)
    with current_user_set(app, current_user), app.app_context():
        order_email_service.send_email_for_paid_order_to_orderer(order.id)

    expected_sender = 'noreply@acmecon.test'

M tests/integration/services/shop/order/email/test_email_on_order_placed.py => tests/integration/services/shop/order/email/test_email_on_order_placed.py +3 -2
@@ 24,7 24,7 @@ from tests.integration.services.shop.helpers import (
    create_shop_fragment,
)

from .helpers import place_order_with_items
from .helpers import get_current_user_for_user, place_order_with_items


@pytest.fixture(scope='package')


@@ 127,7 127,8 @@ def order(storefront, article1, article2, customer, order_admin):
def test_email_on_order_placed(send_email_mock, site_app, customer, order):
    app = site_app

    with current_user_set(app, customer), app.app_context():
    current_user = get_current_user_for_user(customer)
    with current_user_set(app, current_user), app.app_context():
        order_email_service.send_email_for_incoming_order_to_orderer(order.id)

    expected_to_orderer_sender = 'noreply@acmecon.test'