10000 added test cases and fixed issues for RequestValidator apigateway (#7… · codeperl/localstack@805b4a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 805b4a5

Browse files
added test cases and fixed issues for RequestValidator apigateway (localstack#7917)
1 parent 5f13258 commit 805b4a5

File tree

4 files changed

+496
-47
lines changed

4 files changed

+496
-47
lines changed

localstack/services/apigateway/provider.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,9 +1029,7 @@ def get_request_validator(
10291029
)
10301030

10311031
if validator is None:
1032-
raise NotFoundException(
1033-
f"Validator {request_validator_id} for API Gateway {rest_api_id} not found"
1034-
)
1032+
raise NotFoundException("Invalid Request Validator identifier specified")
10351033

10361034
result = to_validator_response_json(rest_api_id, validator)
10371035
return result
@@ -1054,8 +1052,8 @@ def create_request_validator(
10541052
validator = RequestValidator(
10551053
id=validator_id,
10561054
name=name,
1057-
validateRequestBody=validate_request_body,
1058-
validateRequestParameters=validate_request_parameters,
1055+
validateRequestBody=validate_request_body or False,
1056+
validateRequestParameters=validate_request_parameters or False,
10591057
)
10601058

10611059
rest_api_container.validators[validator_id] = validator
@@ -1083,11 +1081,33 @@ def update_request_validator(
10831081
f"Validator {request_validator_id} for API Gateway {rest_api_id} not found"
10841082
)
10851083

1086-
patched_validator = apply_json_patch_safe(validator, patch_operations)
1087-
rest_api_container.validators[request_validator_id] = patched_validator
1084+
for patch_operation in patch_operations:
1085+
path = patch_operation.get("path")
1086+
operation = patch_operation.get("op")
1087+
if operation != "replace":
1088+
raise BadRequestException(
1089+
f"Invalid patch path '{path}' specified for op '{operation}'. "
1090+
f"Please choose supported operations"
1091+
)
1092+
if path not in ("/name", "/validateRequestBody", "/validateRequestParameters"):
1093+
raise BadRequestException(
1094+
f"Invalid patch path '{path}' specified for op 'replace'. "
1095+
f"Must be one of: [/name, /validateRequestParameters, /validateRequestBody]"
1096+
)
1097+
1098+
key = path[1:]
1099+
value = patch_operation.get("value")
1100+
if key == "name" and not value:
1101+
raise BadRequestException("Request Validator name cannot be blank")
10881102

1089-
result = to_validator_response_json(rest_api_id, patched_validator)
1090-
return result
1103+
elif key in ("validateRequestParameters", "validateRequestBody"):
1104+
value = value and value.lower() == "true" or False
1105+
1106+
rest_api_container.validators[request_validator_id][key] = value
1107+
1108+
return to_validator_response_json(
1109+
rest_api_id, rest_api_container.validators[request_validator_id]
1110+
)
10911111

10921112
def delete_request_validator(
10931113
self, context: RequestContext, rest_api_id: String, request_validator_id: String
@@ -1096,13 +1116,11 @@ def delete_request_validator(
10961116
store = get_apigateway_store(account_id=context.account_id, region=context.region)
10971117
rest_api_container = store.rest_apis.get(rest_api_id)
10981118
if not rest_api_container:
1099-
return
1119+
raise NotFoundException("Invalid Request Validator identifier specified")
11001120

11011121
validator = rest_api_container.validators.pop(request_validator_id, None)
11021122
if not validator:
1103-
raise NotFoundException(
1104-
f"Validator {request_validator_id} for API Gateway {rest_api_id} not found"
1105-
)
1123+
raise NotFoundException("Invalid Request Validator identifier specified")
11061124

11071125
# tags
11081126

tests/integration/apigateway/test_apigateway_api.py

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,3 +1528,196 @@ def test_update_model(
15281528
patchOperations=patch_operations,
15291529
)
15301530
snapshot.match("update-model-empty-schema", e.value.response)
1531+
1532+
1533+
class TestApiGatewayApiRequestValidator:
1534+
@pytest.mark.aws_validated
1535+
def test_request_validator_lifecycle(self, apigateway_client, apigw_create_rest_api, snapshot):
1536+
response = apigw_create_rest_api(
1537+
name=f"test-api-{short_uid()}",
1538+
description="my api",
1539+
)
1540+
snapshot.match("create-rest-api", response)
1541+
api_id = response["id"]
1542+
1543+
# create a request validator for an API
1544+
response = apigateway_client.create_request_validator(
1545+
restApiId=api_id, name=f"test-validator-{short_uid()}"
1546+
)
1547+
snapshot.match("create-request-validator", response)
1548+
validator_id = response["id"]
1549+
1550+
# get detail of a specific request validator corresponding to an API
1551+
response = apigateway_client.get_request_validator(
1552+
restApiId=api_id, requestValidatorId=validator_id
1553+
)
1554+
snapshot.match("get-request-validator", response)
1555+
1556+
# get list of all request validators in the API
1557+
response = apigateway_client.get_request_validators(restApiId=api_id)
1558+
snapshot.match("get-request-validators", response)
1559+
1560+
# update request validators with different set of patch operations
1561+
patch_operations = [
1562+
{"op": "replace", "path": "/validateRequestBody", "value": "true"},
1563+
]
1564+
response = apigateway_client.update_request_validator(
1565+
restApiId=api_id, requestValidatorId=validator_id, patchOperations=patch_operations
1566+
)
1567+
snapshot.match("update-request-validator-with-value", response)
1568+
1569+
patch_operations = [
1570+
{"op": "replace", "path": "/validateRequestBody"},
1571+
]
1572+
response = apigateway_client.update_request_validator(
1573+
restApiId=api_id, requestValidatorId=validator_id, patchOperations=patch_operations
1574+
)
1575+
snapshot.match("update-request-validator-without-value", response)
1576+
1577+
response = apigateway_client.get_request_validator(
1578+
restApiId=api_id, requestValidatorId=validator_id
1579+
)
1580+
snapshot.match("get-request-validators-after-update-operation", response)
1581+
1582+
# delete request validator
1583+
response = apigateway_client.delete_request_validator(
1584+
restApiId=api_id, requestValidatorId=validator_id
1585+
)
1586+
snapshot.match("delete-request-validator", response)
1587+
1588+
# try fetching details of the deleted request validator
1589+
with pytest.raises(ClientError) as e:
1590+
apigateway_client.get_request_validator(
1591+
restApiId=api_id, requestValidatorId=validator_id
1592+
)
1593+
snapshot.match("get-deleted-request-validator", e.value.response)
1594+
1595+
# check list of all request validators in the API
1596+
response = apigateway_client.get_request_validators(restApiId=api_id)
1597+
snapshot.match("get-request-validators-after-delete", response)
1598+
1599+
@pytest.mark.aws_validated
1600+
def test_invalid_get_request_validator(
1601+
self, apigateway_client, apigw_create_rest_api, snapshot
1602+
):
1603+
response = apigw_create_rest_api(
1604+
name=f"test-api-{short_uid()}",
1605+
description="my api",
1606+
)
1607+
api_id = response["id"]
1608+
1609+
response = apigateway_client.create_request_validator(
1610+
restApiId=api_id, name=f"test-validator-{short_uid()}"
1611+
)
1612+
validator_id = response["id"]
1613+
1614+
with pytest.raises(ClientError) as e:
1615+
apigateway_client.get_request_validator(
1616+
restApiId="api_id", requestValidatorId=validator_id
1617+
)
1618+
snapshot.match("get-request-validators-invalid-api-id", e.value.response)
1619+
1620+
with pytest.raises(ClientError) as e:
1621+
apigateway_client.get_request_validator(
1622+
restApiId=api_id, requestValidatorId="validator_id"
1623+
)
1624+
snapshot.match("get-request-validators-invalid-validator-id", e.value.response)
1625+
1626+
@pytest.mark.aws_validated
1627+
def test_invalid_get_request_validators(
1628+
self, apigateway_client, apigw_create_rest_api, snapshot
1629+
):
1630+
with pytest.raises(ClientError) as e:
1631+
apigateway_client.get_request_validators(restApiId="api_id")
1632+
snapshot.match("get-invalid-request-validators", e.value.response)
1633+
1634+
@pytest.mark.aws_validated
1635+
def test_invalid_delete_request_validator(
1636+
self, apigateway_client, apigw_create_rest_api, snapshot
1637+
):
1638+
response = apigw_create_rest_api(
1639+
name=f"test-api-{short_uid()}",
1640+
description="my api",
1641+
)
1642+
api_id = response["id"]
1643+
1644+
response = apigateway_client.create_request_validator(
1645+
restApiId=api_id, name=f"test-validator-{short_uid()}"
1646+
)
1647+
validator_id = response["id"]
1648+
1649+
with pytest.raises(ClientError) as e:
1650+
apigateway_client.delete_request_validator(
1651+
restApiId="api_id", requestValidatorId=validator_id
1652+
)
1653+
snapshot.match("delete-request-validator-invalid-api-id", e.value.response)
1654+
1655+
with pytest.raises(ClientError) as e:
1656+
apigateway_client.delete_request_validator(
1657+
restApiId=api_id, requestValidatorId="validator_id"
1658+
)
1659+
snapshot.match("delete-request-validator-invalid-validator-id", e.value.response)
1660+
1661+
@pytest.mark.aws_validated
1662+
def test_create_request_validator_invalid_api_id(
1663+
self, apigateway_client, apigw_create_rest_api, snapshot
1664+
):
1665+
with pytest.raises(ClientError) as e:
1666+
apigateway_client.create_request_validator(
1667+
restApiId="api_id", name=f"test-validator-{short_uid()}"
1668+
)
1669+
snapshot.match("invalid-create-request-validator", e.value.response)
1670+
1671+
@pytest.mark.aws_validated
1672+
def test_invalid_update_request_validator_operations(
1673+
self, apigateway_client, apigw_create_rest_api, snapshot
1674+
):
1675+
response = apigw_create_rest_api(
1676+
name=f"test-api-{short_uid()}",
1677+
description="my api",
1678+
)
1679+
snapshot.match("create-rest-api", response)
1680+
api_id = response["id"]
1681+
1682+
response = apigateway_client.create_request_validator(
1683+
restApiId=api_id, name=f"test-validator-{short_uid()}"
1684+
)
1685+
snapshot.match("create-request-validator", response)
1686+
validator_id = response["id"]
1687+
1688+
patch_operations = [
1689+
{"op": "add", "path": "/validateRequestBody", "value": "true"},
1690+
]
1691+
with pytest.raises(ClientError) as e:
1692+
apigateway_client.update_request_validator(
1693+
restApiId=api_id, requestValidatorId=validator_id, patchOperations=patch_operations
1694+
)
1695+
snapshot.match("update-request-validator-invalid-add-operation", e.value.response)
1696+
1697+
patch_operations = [
1698+
{"op": "remove", "path": "/validateRequestBody", "value": "true"},
1699+
]
1700+
with pytest.raises(ClientError) as e:
1701+
apigateway_client.update_request_validator(
1702+
restApiId=api_id, requestValidatorId=validator_id, patchOperations=patch_operations
1703+
)
1704+
snapshot.match("update-request-validator-invalid-remove-operation", e.value.response)
1705+
1706+
patch_operations = [
1707+
{"op": "replace", "path": "/invalidPath", "value": "true"},
1708+
]
1709+
with pytest.raises(ClientError) as e:
1710+
apigateway_client.update_request_validator(
1711+
restApiId=api_id, requestValidatorId=validator_id, patchOperations=patch_operations
1712+
)
1713+
snapshot.match("update-request-validator-invalid-path", e.value.response)
1714+
1715+
patch_operations = [
1716+
{"op": "replace", "path": "/name"},
1717+
]
1718+
1719+
with pytest.raises(ClientError) as e:
1720+
apigateway_client.update_request_validator(
1721+
restApiId=api_id, requestValidatorId=validator_id, patchOperations=patch_operations
1722+
)
1723+
snapshot.match("update-request-validator-empty-name-value", e.value.response)

0 commit comments

Comments
 (0)
0