forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconftest.py
210 lines (160 loc) · 6.42 KB
/
conftest.py
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
import datetime
import hashlib
import json
import os
from collections import namedtuple
from decimal import Decimal
from unittest import mock
import jmespath
import pytest
from botocore import stub
from botocore.config import Config
from jmespath import functions
from aws_lambda_powertools.shared.json_encoder import Encoder
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer
from aws_lambda_powertools.utilities.idempotency.idempotency import IdempotencyConfig
from aws_lambda_powertools.utilities.validation import envelopes
from aws_lambda_powertools.utilities.validation.base import unwrap_event_from_envelope
TABLE_NAME = "TEST_TABLE"
@pytest.fixture(scope="module")
def config() -> Config:
return Config(region_name="us-east-1")
@pytest.fixture(scope="module")
def lambda_apigw_event():
full_file_name = os.path.dirname(os.path.realpath(__file__)) + "/../../events/" + "apiGatewayProxyV2Event.json"
with open(full_file_name) as fp:
event = json.load(fp)
return event
@pytest.fixture
def lambda_context():
lambda_context = {
"function_name": "test-func",
"memory_limit_in_mb": 128,
"invoked_function_arn": "arn:aws:lambda:eu-west-1:809313241234:function:test-func",
"aws_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72",
}
return namedtuple("LambdaContext", lambda_context.keys())(*lambda_context.values())
@pytest.fixture
def timestamp_future():
return str(int((datetime.datetime.now() + datetime.timedelta(seconds=3600)).timestamp()))
@pytest.fixture
def timestamp_expired():
now = datetime.datetime.now()
period = datetime.timedelta(seconds=6400)
return str(int((now - period).timestamp()))
@pytest.fixture(scope="module")
def lambda_response():
return {"message": "test", "statusCode": 200, "decimal_val": Decimal("2.5"), "decimal_NaN": Decimal("NaN")}
@pytest.fixture(scope="module")
def serialized_lambda_response(lambda_response):
return json.dumps(lambda_response, cls=Encoder)
@pytest.fixture(scope="module")
def deserialized_lambda_response(lambda_response):
return json.loads(json.dumps(lambda_response, cls=Encoder))
@pytest.fixture
def default_jmespath():
return "[body, queryStringParameters]"
@pytest.fixture
def expected_params_update_item(serialized_lambda_response, hashed_idempotency_key):
return {
"ExpressionAttributeNames": {"#expiry": "expiration", "#response_data": "data", "#status": "status"},
"ExpressionAttributeValues": {
":expiry": stub.ANY,
":response_data": serialized_lambda_response,
":status": "COMPLETED",
},
"Key": {"id": hashed_idempotency_key},
"TableName": "TEST_TABLE",
"UpdateExpression": "SET #response_data = :response_data, " "#expiry = :expiry, #status = :status",
}
@pytest.fixture
def expected_params_update_item_with_validation(
serialized_lambda_response, hashed_idempotency_key, hashed_validation_key
):
return {
"ExpressionAttributeNames": {
"#expiry": "expiration",
"#response_data": "data",
"#status": "status",
"#validation_key": "validation",
},
"ExpressionAttributeValues": {
":expiry": stub.ANY,
":response_data": serialized_lambda_response,
":status": "COMPLETED",
":validation_key": hashed_validation_key,
},
"Key": {"id": hashed_idempotency_key},
"TableName": "TEST_TABLE",
"UpdateExpression": "SET #response_data = :response_data, "
"#expiry = :expiry, #status = :status, "
"#validation_key = :validation_key",
}
@pytest.fixture
def expected_params_put_item(hashed_idempotency_key):
return {
"ConditionExpression": "attribute_not_exists(id) OR expiration < :now",
"ExpressionAttributeValues": {":now": stub.ANY},
"Item": {"expiration": stub.ANY, "id": hashed_idempotency_key, "status": "INPROGRESS"},
"TableName": "TEST_TABLE",
}
@pytest.fixture
def expected_params_put_item_with_validation(hashed_idempotency_key, hashed_validation_key):
return {
"ConditionExpression": "attribute_not_exists(id) OR expiration < :now",
"ExpressionAttributeValues": {":now": stub.ANY},
"Item": {
"expiration": stub.ANY,
"id": hashed_idempotency_key,
"status": "INPROGRESS",
"validation": hashed_validation_key,
},
"TableName": "TEST_TABLE",
}
@pytest.fixture
def hashed_idempotency_key(lambda_apigw_event, default_jmespath, lambda_context):
compiled_jmespath = jmespath.compile(default_jmespath)
data = compiled_jmespath.search(lambda_apigw_event)
return "test-func#" + hashlib.md5(json.dumps(data).encode()).hexdigest()
@pytest.fixture
def hashed_idempotency_key_with_envelope(lambda_apigw_event):
event = unwrap_event_from_envelope(
data=lambda_apigw_event, envelope=envelopes.API_GATEWAY_HTTP, jmespath_options={}
)
return "test-func#" + hashlib.md5(json.dumps(event).encode()).hexdigest()
@pytest.fixture
def hashed_validation_key(lambda_apigw_event):
return hashlib.md5(json.dumps(lambda_apigw_event["requestContext"]).encode()).hexdigest()
@pytest.fixture
def persistence_store(config):
return DynamoDBPersistenceLayer(table_name=TABLE_NAME, boto_config=config)
@pytest.fixture
def idempotency_config(config, request, default_jmespath):
return IdempotencyConfig(
event_key_jmespath=request.param.get("event_key_jmespath") or default_jmespath,
use_local_cache=request.param["use_local_cache"],
)
@pytest.fixture
def config_without_jmespath(config, request):
return IdempotencyConfig(use_local_cache=request.param["use_local_cache"])
@pytest.fixture
def config_with_validation(config, request, default_jmespath):
return IdempotencyConfig(
event_key_jmespath=default_jmespath,
use_local_cache=request.param,
payload_validation_jmespath="requestContext",
)
@pytest.fixture
def config_with_jmespath_options(config, request):
class CustomFunctions(functions.Functions):
@functions.signature({"types": ["string"]})
def _func_echo_decoder(self, value):
return value
return IdempotencyConfig(
use_local_cache=False,
event_key_jmespath=request.param,
jmespath_options={"custom_functions": CustomFunctions()},
)
@pytest.fixture
def mock_function():
return mock.MagicMock()