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 391
Expand file tree
/
Copy pathtest_get_order.py
More file actions
executable file
·230 lines (193 loc) · 6.12 KB
/
Copy pathtest_get_order.py
File metadata and controls
executable file
·230 lines (193 loc) · 6.12 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import copy
import datetime
import decimal
import json
import uuid
import pytest
from boto3.dynamodb.types import TypeSerializer
from botocore import stub
from fixtures import context, lambda_module # pylint: disable=import-error
from helpers import compare_dict # pylint: disable=import-error,no-name-in-module
lambda_module = pytest.fixture(scope="module", params=[{
"function_dir": "get_order",
"module_name": "main",
"environ": {
"ENVIRONMENT": "test",
"TABLE_NAME": "TABLE_NAME",
"USER_INDEX_NAME": "USER_INDEX_NAME",
"ORDERS_LIMIT": "20",
"POWERTOOLS_TRACE_DISABLED": "true"
}
}])(lambda_module)
context = pytest.fixture(context)
@pytest.fixture
def apigateway_event(order):
"""
API Gateway Lambda Proxy event
See https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
"""
return {
"resource": "/backend/{orderId}",
"path": "/backend/"+order["orderId"],
"httpMethod": "GET",
"headers": {},
"multiValueHeaders": {},
"queryStringParameters": None,
"multiValueQueryStringParameters": {},
"pathParameters": {
"orderId": order["orderId"]
},
"stageVariables": {},
"requestContext": {
"identity": {
"accountId": "123456789012",
"caller": "CALLER",
"sourceIp": "127.0.0.1",
"accessKey": "ACCESS_KEY",
"userArn": "arn:aws:iam::123456789012:user/alice",
"userAgent": "PostmanRuntime/7.1.1",
"user": "CALLER"
}
},
"body": {},
"isBase64Encoded": False
}
@pytest.fixture
def order():
"""
Single order
"""
now = datetime.datetime.now()
return {
"orderId": str(uuid.uuid4()),
"userId": str(uuid.uuid4()),
"createdDate": now.isoformat(),
"modifiedDate": now.isoformat(),
"status": "NEW",
"products": [{
"productId": str(uuid.uuid4()),
"name": "Test Product",
"package": {
"width": 1000,
"length": 900,
"height": 800,
"weight": 700
},
"price": 300,
"quantity": 4
}],
"address": {
"name": "John Doe",
"companyName": "Company Inc.",
"streetAddress": "123 Street St",
"postCode": "12345",
"city": "Town",
"state": "State",
"country": "SE",
"phoneNumber": "+123456789"
},
"deliveryPrice": 200,
"total": 1400
}
def test_get_order(lambda_module, order):
"""
Test get_order()
"""
# Stub boto3
table = stub.Stubber(lambda_module.table.meta.client)
response = {
"Item": {k: TypeSerializer().serialize(v) for k, v in order.items()},
# We do not use ConsumedCapacity
"ConsumedCapacity": {}
}
expected_params = {
"TableName": lambda_module.TABLE_NAME,
"Key": {"orderId": order["orderId"]}
}
table.add_response("get_item", response, expected_params)
table.activate()
# Gather orders
ddb_order = lambda_module.get_order(order["orderId"])
# Remove stub
table.assert_no_pending_responses()
table.deactivate()
# Check response
compare_dict(order, ddb_order)
def test_handler(lambda_module, apigateway_event, order, context):
"""
Test handler()
"""
# Stub boto3
table = stub.Stubber(lambda_module.table.meta.client)
response = {
"Item": {k: TypeSerializer().serialize(v) for k, v in order.items()},
# We do not use ConsumedCapacity
"ConsumedCapacity": {}
}
expected_params = {
"TableName": lambda_module.TABLE_NAME,
"Key": {"orderId": order["orderId"]}
}
table.add_response("get_item", response, expected_params)
table.activate()
# Send request
response = lambda_module.handler(apigateway_event, context)
# Remove stub
table.assert_no_pending_responses()
table.deactivate()
assert response["statusCode"] == 200
assert "body" in response
body = json.loads(response["body"])
compare_dict(order, body)
def test_handler_not_found(lambda_module, apigateway_event, order, context):
"""
Test handler() with an unknown order ID
"""
# Stub boto3
table = stub.Stubber(lambda_module.table.meta.client)
response = {
# We do not use ConsumedCapacity
"ConsumedCapacity": {}
}
expected_params = {
"TableName": lambda_module.TABLE_NAME,
"Key": {"orderId": order["orderId"]}
}
table.add_response("get_item", response, expected_params)
table.activate()
# Send request
response = lambda_module.handler(apigateway_event, context)
# Remove stub
table.assert_no_pending_responses()
table.deactivate()
assert response["statusCode"] == 404
assert "body" in response
body = json.loads(response["body"])
assert "message" in body
assert isinstance(body["message"], str)
def test_handler_forbidden(lambda_module, apigateway_event, context):
"""
Test handler() without claims
"""
apigateway_event = copy.deepcopy(apigateway_event)
del apigateway_event["requestContext"]["identity"]
# Send request
response = lambda_module.handler(apigateway_event, context)
assert response["statusCode"] == 401
assert "body" in response
body = json.loads(response["body"])
assert "message" in body
assert isinstance(body["message"], str)
def test_handler_missing_order(lambda_module, apigateway_event, context):
"""
Test handler() without orderId
"""
apigateway_event = copy.deepcopy(apigateway_event)
apigateway_event["pathParameters"] = None
# Send request
response = lambda_module.handler(apigateway_event, context)
assert response["statusCode"] == 400
assert "body" in response
body = json.loads(response["body"])
assert "message" in body
assert isinstance(body["message"], str)