10000 test requestValidator integration · localstack/localstack@dd242d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd242d7

Browse files
committed
test requestValidator integration
1 parent 7c2ca3d commit dd242d7

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

localstack/services/apigateway/invocations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def validate_body(self, resource):
102102
validate(instance=json.loads(self.context.data), schema=json.loads(model["schema"]))
103103
return True
104104
except ValidationError as e:
105-
LOG.warning("failed to validate request body", e)
105+
LOG.warning("failed to validate request body %s", e)
106106
return False
107107

108108
# TODO implement parameters and headers

localstack/services/apigateway/provider.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,11 @@ def update_method(
513513
continue
514514

515515
elif path == "/requestValidatorId" and value not in rest_api.validators:
516+
if not value:
517+
# you can remove a requestValidator by passing an empty string as a value
518+
patch_op = {"op": "remove", "path": path, "value": value}
519+
applicable_patch_operations.append(patch_op)
520+
continue
516521
raise BadRequestException("Invalid Request Validator identifier specified")
517522

518523
elif path.startswith("/requestModels/"):

tests/integration/apigateway/test_apigateway_common.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ class TestApiGatewayCommon:
1717
"""
1818

1919
@pytest.mark.aws_validated
20+
@pytest.mark.skip_snapshot_verify(
21+
paths=[
22+
"$.invalid-request-body.Type",
23+
"$..methodIntegration.cacheNamespace",
24+
"$..methodIntegration.integrationResponses",
25+
"$..methodIntegration.passthroughBehavior",
26+
"$..methodIntegration.requestParameters",
27+
"$..methodIntegration.timeoutInMillis",
28+
]
29+
)
2030
def test_api_gateway_request_validator(
2131
self,
2232
apigateway_client,
@@ -29,6 +39,14 @@ def test_api_gateway_request_validator(
2939
# TODO: create fixture which will provide basic integrations where we can test behaviour
3040
# see once we have more cases how we can regroup functionality into one or several fixtures
3141
# example: create a basic echo lambda + integrations + deploy stage
42+
snapshot.add_transformers_list(
43+
[
44+
snapshot.transform.key_value("requestValidatorId"),
45+
snapshot.transform.key_value("id"), # deployment id
46+
snapshot.transform.key_value("fn_name"), # lambda name
47+
snapshot.transform.key_value("fn_arn"), # lambda arn
48+
]
49+
)
3250

3351
fn_name = f"test-{short_uid()}"
3452
create_lambda_function(
@@ -39,6 +57,9 @@ def test_api_gateway_request_validator(
3957
lambda_arn = lambda_client.get_function(FunctionName=fn_name)["Configuration"][
4058
"FunctionArn"
4159
]
60+
# matching on lambda id for reference replacement in snapshots
61+
snapshot.match("register-lambda", {"fn_name": fn_name, "fn_arn": lambda_arn})
62+
4263
parsed_arn = parse_arn(lambda_arn)
4364
region = parsed_arn["region"]
4465
account_id = parsed_arn["account"]
@@ -134,7 +155,8 @@ def test_api_gateway_request_validator(
134155
response = requests.post(url, json={"test": "test"})
135156
# FIXME: for now, not implemented in LocalStack, we don't validate RequestParameters yet
136157
# assert response.status_code == 400
137-
snapshot.match("missing-required-request-params", response.json())
158+
if response.status_code == 400:
159+
snapshot.match("missing-required-request-params", response.json())
138160

139161
# create Model schema to validate body
140162
apigateway_client.create_model(

tests/integration/apigateway/test_apigateway_common.snapshot.json

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{
22
"tests/integration/apigateway/test_apigateway_common.py::TestApiGatewayCommon::test_api_gateway_request_validator": {
3-
"recorded-date": "15-03-2023, 00:42:34",
3+
"recorded-date": "15-03-2023, 01:01:50",
44
"recorded-content": {
5+
"register-lambda": {
6+
"fn_arn": "arn:aws:lambda:<region>:111111111111:function:<fn_name:1>",
7+
"fn_name": "<fn_name:1>"
8+
},
59
"deploy-1": {
610
"createdDate": "datetime",
7-
"id": "ehob5m",
11+
"id": "<id:1>",
812
"ResponseMetadata": {
913
"HTTPHeaders": {},
1014
"HTTPStatusCode": 201
@@ -16,7 +20,7 @@
1620
"httpMethod": "POST",
1721
"methodIntegration": {
1822
"cacheKeyParameters": [],
19-
"cacheNamespace": "9qcl57",
23+
"cacheNamespace": "5l4d79",
2024
"httpMethod": "POST",
2125
"integrationResponses": {
2226
"200": {
@@ -26,7 +30,7 @@
2630
"passthroughBehavior": "WHEN_NO_MATCH",
2731
"timeoutInMillis": 29000,
2832
"type": "AWS_PROXY",
29-
"uri": "arn:aws:apigateway:<region>:lambda:path//2015-03-31/functions/arn:aws:lambda:<region>:111111111111:function:test-ae38daf7/invocations"
33+
"uri": "arn:aws:apigateway:<region>:lambda:path//2015-03-31/functions/arn:aws:lambda:<region>:111111111111:function:<fn_name:1>/invocations"
3034
},
3135
"methodResponses": {
3236
"200": {
@@ -36,7 +40,7 @@
3640
"requestParameters": {
3741
"method.request.path.issuer": true
3842
},
39-
"requestValidatorId": "f0uyyb",
43+
"requestValidatorId": "<request-validator-id:1>",
4044
"ResponseMetadata": {
4145
"HTTPHeaders": {},
4246
"HTTPStatusCode": 200
@@ -51,7 +55,7 @@
5155
"httpMethod": "POST",
5256
"methodIntegration": {
5357
"cacheKeyParameters": [],
54-
"cacheNamespace": "9qcl57",
58+
"cacheNamespace": "5l4d79",
5559
"httpMethod": "POST",
5660
"integrationResponses": {
5761
"200": {
@@ -61,7 +65,7 @@
6165
"passthroughBehavior": "WHEN_NO_MATCH",
6266
"timeoutInMillis": 29000,
6367
"type": "AWS_PROXY",
64-
"uri": "arn:aws:apigateway:<region>:lambda:path//2015-03-31/functions/arn:aws:lambda:<region>:111111111111:function:test-ae38daf7/invocations"
68+
"uri": "arn:aws:apigateway:<region>:lambda:path//2015-03-31/functions/arn:aws:lambda:<region>:111111111111:function:<fn_name:1>/invocations"
6569
},
6670
"methodResponses": {
6771
"200": {
@@ -74,7 +78,7 @@
7478
"requestParameters": {
7579
"method.request.path.issuer": true
7680
},
77-
"requestValidatorId": "f0uyyb",
81+
"requestValidatorId": "<request-validator-id:1>",
7882
"ResponseMetadata": {
7983
"HTTPHeaders": {},
8084
"HTTPStatusCode": 200
@@ -86,7 +90,7 @@
8690
"httpMethod": "POST",
8791
"methodIntegration": {
8892
"cacheKeyParameters": [],
89-
"cacheNamespace": "9qcl57",
93+
"cacheNamespace": "5l4d79",
9094
"httpMethod": "POST",
9195
"integrationResponses": {
9296
"200": {
@@ -96,7 +100,7 @@
96100
"passthroughBehavior": "WHEN_NO_MATCH",
97101
"timeoutInMillis": 29000,
98102
"type": "AWS_PROXY",
99-
"uri": "arn:aws:apigateway:<region>:lambda:path//2015-03-31/functions/arn:aws:lambda:<region>:111111111111:function:test-ae38daf7/invocations"
103+
"uri": "arn:aws:apigateway:<region>:lambda:path//2015-03-31/functions/arn:aws:lambda:<region>:111111111111:function:<fn_name:1>/invocations"
100104
},
101105
"methodResponses": {
102106
"200": {
@@ -109,7 +113,7 @@
109113
"requestParameters": {
110114
"method.request.path.test": true
111115
},
112-
"requestValidatorId": "f0uyyb",
116+
"requestValidatorId": "<request-validator-id:1>",
113117
"ResponseMetadata": {
114118
"HTTPHeaders": {},
115119
"HTTPStatusCode": 200
@@ -124,7 +128,7 @@
124128
"httpMethod": "POST",
125129
"methodIntegration": {
126130
"cacheKeyParameters": [],
127-
"cacheNamespace": "9qcl57",
131+
"cacheNamespace": "5l4d79",
128132
"httpMethod": "POST",
129133
"integrationResponses": {
130134
"200": {
@@ -134,7 +138,7 @@
134138
"passthroughBehavior": "WHEN_NO_MATCH",
135139
"timeoutInMillis": 29000,
136140
"type": "AWS_PROXY",
137-
"uri": "arn:aws:apigateway:<region>:lambda:path//2015-03-31/functions/arn:aws:lambda:<region>:111111111111:function:test-ae38daf7/invocations"
141+
"uri": "arn:aws:apigateway:<region>:lambda:path//2015-03-31/functions/arn:aws:lambda:<region>:111111111111:function:<fn_name:1>/invocations"
138142
},
139143
"methodResponses": {
140144
"200": {

0 commit comments

Comments
 (0)
0