This repository was archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathtest_create_order.py
More file actions
executable file
·206 lines (147 loc) · 5.43 KB
/
Copy pathtest_create_order.py
File metadata and controls
executable file
·206 lines (147 loc) · 5.43 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import copy
import json
import boto3
import pytest
import requests
from fixtures import iam_auth, get_order, get_product # pylint: disable=import-error,no-name-in-module
from helpers import compare_dict, get_parameter # pylint: disable=import-error,no-name-in-module
@pytest.fixture(scope="module")
def delivery_api_url():
return get_parameter("/ecommerce/{Environment}/delivery-pricing/api/url")
@pytest.fixture(scope="module")
def payment_3p_api_url():
return get_parameter("/ecommerce/{Environment}/payment-3p/api/url")
@pytest.fixture(scope="module")
def product_table_name():
return get_parameter("/ecommerce/{Environment}/products/table/name")
@pytest.fixture(scope="module")
def table_name():
return get_parameter("/ecommerce/{Environment}/orders/table/name")
@pytest.fixture(scope="module")
def function_arn():
return get_parameter("/ecommerce/{Environment}/orders/create-order/arn")
@pytest.fixture(scope="function")
def order(get_order):
return get_order()
@pytest.fixture(scope="module")
def products(product_table_name, get_product):
"""
Create fake products
"""
table = boto3.resource("dynamodb").Table(product_table_name) # pylint: disable=no-member
products = [get_product() for _ in range(3)]
for product in products:
table.put_item(Item=product)
yield products
for product in products:
table.delete_item(Key={"productId": product["productId"]})
@pytest.fixture
def delivery_price(delivery_api_url, iam_auth, order, products):
res = requests.post(
url=delivery_api_url+"/backend/pricing",
auth=iam_auth(delivery_api_url),
json={
"products": products,
"address": order["address"]
}
)
body = res.json()
print(body)
return body["pricing"]
@pytest.fixture
def payment_token(payment_3p_api_url, delivery_price, products):
# Calculate the order total
amount = sum([p["price"]*p.get("quantity", 1) for p in products]) + delivery_price
# Generate a payment token
res = requests.post(payment_3p_api_url+"/preauth", json={
"cardNumber": "1234567890123456",
"amount": amount
})
payment_token = res.json()["paymentToken"]
yield payment_token
# Cancel the payment token
requests.post(payment_3p_api_url+"/cancelPayment", json={
"paymentToken": payment_token
})
@pytest.fixture(scope="function")
def order_request(order, products, delivery_price, payment_token):
return {
"userId": order["userId"],
"order": {
"products": products,
"address": order["address"],
"deliveryPrice": delivery_price,
"paymentToken": payment_token
}
}
def test_create_order(function_arn, table_name, order_request):
"""
Test the CreateOrder function
"""
order_request = copy.deepcopy(order_request)
table = boto3.resource("dynamodb").Table(table_name) #pylint: disable=no-member
lambda_ = boto3.client("lambda")
# Trigger the function
response = lambda_.invoke(
FunctionName=function_arn,
InvocationType="RequestResponse",
Payload=json.dumps(order_request).encode()
)
response = json.load(response["Payload"])
print(response)
# Check the output of the Function
assert response["success"] == True
assert "order" in response
assert len(response.get("errors", [])) == 0
del order_request["order"]["products"]
compare_dict(order_request["order"], response["order"])
assert response["order"]["userId"] == order_request["userId"]
# Check the table
ddb_response = table.get_item(Key={"orderId": response["order"]["orderId"]})
assert "Item" in ddb_response
mandatory_fields = [
"orderId", "userId", "createdDate", "modifiedDate", "status",
"products", "address", "deliveryPrice", "total"
]
for field in mandatory_fields:
assert field in ddb_response["Item"]
assert ddb_response["Item"]["status"] == "NEW"
compare_dict(order_request["order"], ddb_response["Item"])
# Cleanup the table
table.delete_item(Key={"orderId": response["order"]["orderId"]})
def test_create_order_fail_products(function_arn, table_name, order_request, get_product):
"""
Test the CreateOrder function
"""
order_request = copy.deepcopy(order_request)
order_request["order"]["products"] = [get_product()]
lambda_ = boto3.client("lambda")
# Trigger the function
response = lambda_.invoke(
FunctionName=function_arn,
InvocationType="RequestResponse",
Payload=json.dumps(order_request).encode()
)
response = json.load(response["Payload"])
print(response)
# Check the output of the Function
assert response["success"] == False
assert len(response.get("errors", [])) > 0
def test_create_order_fail_delivery_price(function_arn, table_name, order_request, get_product):
"""
Test the CreateOrder function
"""
order_request = copy.deepcopy(order_request)
order_request["order"]["deliveryPrice"] += 200
lambda_ = boto3.client("lambda")
# Trigger the function
response = lambda_.invoke(
FunctionName=function_arn,
InvocationType="RequestResponse",
Payload=json.dumps(order_request).encode()
)
response = json.load(response["Payload"])
print(response)
# Check the output of the Function
assert response["success"] == False
assert len(response.get("errors", [])) > 0