1
|
|
|
""" |
2
|
|
|
:Copyright: 2006-2022 Jochen Kupperschmidt |
3
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
4
|
|
|
""" |
5
|
|
|
|
6
|
|
|
from datetime import datetime |
7
|
|
|
from decimal import Decimal |
8
|
|
|
|
9
|
|
|
from flask import Flask |
10
|
|
|
from freezegun import freeze_time |
11
|
|
|
import pytest |
12
|
|
|
|
13
|
|
|
from byceps.services.shop.article.transfer.models import Article, ArticleNumber |
14
|
|
|
from byceps.services.shop.cart.models import Cart |
15
|
|
|
from byceps.services.shop.order import service as order_service |
16
|
|
|
from byceps.services.shop.order.transfer.order import Order, Orderer |
17
|
|
|
from byceps.services.shop.shop.transfer.models import Shop |
18
|
|
|
from byceps.services.shop.storefront.transfer.models import Storefront |
19
|
|
|
from byceps.services.user.transfer.models import User |
20
|
|
|
|
21
|
|
|
from tests.helpers import log_in_user |
22
|
|
|
from tests.integration.services.shop.conftest import make_article |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
@pytest.fixture(scope='package') |
26
|
|
|
def shop_order_admin(make_admin) -> User: |
27
|
|
|
permission_ids = {'admin.access', 'shop_order.view'} |
28
|
|
|
return make_admin('ShopOrderExportAdmin', permission_ids) |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
@pytest.fixture |
32
|
|
|
def article_bungalow(make_article, shop: Shop) -> Article: |
33
|
|
|
return make_article( |
34
|
|
|
shop.id, |
35
|
|
|
item_number=ArticleNumber('LR-08-A00003'), |
36
|
|
|
description='LANresort 2015: Bungalow 4 Plätze', |
37
|
|
|
price=Decimal('355.00'), |
38
|
|
|
tax_rate=Decimal('0.07'), |
39
|
|
|
total_quantity=10, |
40
|
|
|
) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
@pytest.fixture |
44
|
|
|
def article_guest_fee(make_article, shop: Shop) -> Article: |
45
|
|
|
return make_article( |
46
|
|
|
shop.id, |
47
|
|
|
item_number=ArticleNumber('LR-08-A00006'), |
48
|
|
|
description='Touristische Gästeabgabe (BispingenCard), pauschal für 4 Personen', |
49
|
|
|
price=Decimal('6.00'), |
50
|
|
|
tax_rate=Decimal('0.19'), |
51
|
|
|
total_quantity=10, |
52
|
|
|
) |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
@pytest.fixture |
56
|
|
|
def article_table(make_article, shop: Shop) -> Article: |
57
|
|
|
return make_article( |
58
|
|
|
shop.id, |
59
|
|
|
item_number=ArticleNumber('LR-08-A00002'), |
60
|
|
|
description='Tisch (zur Miete), 200 x 80 cm', |
61
|
|
|
price=Decimal('20.00'), |
62
|
|
|
tax_rate=Decimal('0.19'), |
63
|
|
|
total_quantity=10, |
64
|
|
|
) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
@pytest.fixture |
68
|
|
|
def cart( |
69
|
|
|
article_bungalow: Article, |
70
|
|
|
article_guest_fee: Article, |
71
|
|
|
article_table: Article, |
72
|
|
|
) -> Cart: |
73
|
|
|
cart = Cart() |
74
|
|
|
|
75
|
|
|
cart.add_item(article_bungalow, 1) |
76
|
|
|
cart.add_item(article_guest_fee, 1) |
77
|
|
|
cart.add_item(article_table, 2) |
78
|
|
|
|
79
|
|
|
return cart |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
@pytest.fixture |
83
|
|
|
def orderer(make_user) -> Orderer: |
84
|
|
|
user = make_user(email_address='[email protected]') |
85
|
|
|
|
86
|
|
|
return Orderer( |
87
|
|
|
user.id, |
88
|
|
|
'Hans-Werner', |
89
|
|
|
'Mustermann', |
90
|
|
|
'Deutschland', |
91
|
|
|
'42000', |
92
|
|
|
'Hauptstadt', |
93
|
|
|
'Nebenstraße 23a', |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
@pytest.fixture |
98
|
|
|
def storefront( |
99
|
|
|
shop: Shop, make_order_number_sequence, make_storefront |
100
|
|
|
) -> Storefront: |
101
|
|
|
order_number_sequence = make_order_number_sequence( |
102
|
|
|
shop.id, prefix='LR-08-B', value=26 |
103
|
|
|
) |
104
|
|
|
|
105
|
|
|
return make_storefront(shop.id, order_number_sequence.id) |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
@pytest.fixture |
109
|
|
|
def order(storefront: Storefront, cart: Cart, orderer: Orderer): |
110
|
|
|
created_at = datetime(2015, 2, 26, 12, 26, 24) # UTC |
111
|
|
|
|
112
|
|
|
order, _ = order_service.place_order( |
113
|
|
|
storefront.id, orderer, cart, created_at=created_at |
114
|
|
|
) |
115
|
|
|
|
116
|
|
|
yield order |
117
|
|
|
|
118
|
|
|
order_service.delete_order(order.id) |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
@freeze_time('2015-04-15 07:54:18') # UTC |
122
|
|
|
def test_serialize_existing_order( |
123
|
|
|
request, admin_app: Flask, shop_order_admin: User, make_client, order: Order |
124
|
|
|
): |
125
|
|
|
filename = request.fspath.dirpath('order_export.xml') |
126
|
|
|
expected = filename.read_text('iso-8859-1').rstrip() |
127
|
|
|
|
128
|
|
|
log_in_user(shop_order_admin.id) |
129
|
|
|
client = make_client(admin_app, user_id=shop_order_admin.id) |
130
|
|
|
|
131
|
|
|
url = f'/admin/shop/orders/{order.id}/export' |
132
|
|
|
response = client.get(url) |
133
|
|
|
|
134
|
|
|
assert response.status_code == 200 |
135
|
|
|
assert response.content_type == 'application/xml; charset=iso-8859-1' |
136
|
|
|
|
137
|
|
|
body = response.get_data().decode('utf-8') |
138
|
|
|
assert body == expected |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
@freeze_time('2015-04-15 07:54:18') # UTC |
142
|
|
|
def test_serialize_unknown_order( |
143
|
|
|
admin_app: Flask, shop_order_admin: User, make_client |
144
|
|
|
): |
145
|
|
|
unknown_order_id = '00000000-0000-0000-0000-000000000000' |
146
|
|
|
|
147
|
|
|
log_in_user(shop_order_admin.id) |
148
|
|
|
client = make_client(admin_app, user_id=shop_order_admin.id) |
149
|
|
|
|
150
|
|
|
url = f'/admin/shop/orders/{unknown_order_id}/export' |
151
|
|
|
response = client.get(url) |
152
|
|
|
|
153
|
|
|
assert response.status_code == 404 |
154
|
|
|
|