|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | + |
| 6 | +from localstack.services.awslambda.lambda_utils import LAMBDA_RUNTIME_PYTHON39 |
| 7 | +from localstack.utils.aws.arns import parse_arn |
| 8 | +from localstack.utils.strings import short_uid |
| 9 | +from tests.integration.apigateway.apigateway_fixtures import api_invoke_url |
| 10 | +from tests.integration.awslambda.test_lambda import TEST_LAMBDA_AWS_PROXY |
| 11 | + |
| 12 | + |
| 13 | +class TestApiGatewayCommon: |
| 14 | + """ |
| 15 | + In this class we won't test individual CRUD API calls but how those will affect the integrations and |
| 16 | + requests/responses from the API. |
| 17 | + """ |
| 18 | + |
| 19 | + @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 | + ) |
| 30 | + def test_api_gateway_request_validator( |
| 31 | + self, |
| 32 | + apigateway_client, |
| 33 | + create_lambda_function, |
| 34 | + create_rest_apigw, |
| 35 | + apigw_redeploy_api, |
| 36 | + lambda_client, |
| 37 | + snapshot, |
| 38 | + ): |
| 39 | + # TODO: create fixture which will provide basic integrations where we can test behaviour |
| 40 | + # see once we have more cases how we can regroup functionality into one or several fixtures |
| 41 | + # 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 | + ) |
| 50 | + |
| 51 | + fn_name = f"test-{short_uid()}" |
| 52 | + create_lambda_function( |
| 53 | + func_name=fn_name, |
| 54 | + handler_file=TEST_LAMBDA_AWS_PROXY, |
| 55 | + runtime=LAMBDA_RUNTIME_PYTHON39, |
| 56 | + ) |
| 57 | + lambda_arn = lambda_client.get_function(FunctionName=fn_name)["Configuration"][ |
| 58 | + "FunctionArn" |
| 59 | + ] |
| 60 | + # matching on lambda id for reference replacement in snapshots |
| 61 | + snapshot.match("register-lambda", {"fn_name": fn_name, "fn_arn": lambda_arn}) |
| 62 | + |
| 63 | + parsed_arn = parse_arn(lambda_arn) |
| 64 | + region = parsed_arn["region"] |
| 65 | + account_id = parsed_arn["account"] |
| 66 | + |
| 67 | + api_id, _, root = create_rest_apigw(name="aws lambda api") |
| 68 | + |
| 69 | + resource_1 = apigateway_client.create_resource( |
| 70 | + restApiId=api_id, parentId=root, pathPart="test" |
| 71 | + )["id"] |
| 72 | + |
| 73 | + resource_id = apigateway_client.create_resource( |
| 74 | + restApiId=api_id, parentId=resource_1, pathPart="{test}" |
| 75 | + )["id"] |
| 76 | + |
| 77 | + validator_id = apigateway_client.create_request_validator( |
| 78 | + restApiId=api_id, |
| 79 | + name="test-validator", |
| 80 | + validateRequestParameters=True, |
| 81 | + validateRequestBody=True, |
| 82 | + )["id"] |
| 83 | + |
| 84 | + apigateway_client.put_method( |
| 85 | + restApiId=api_id, |
| 86 | + resourceId=resource_id, |
| 87 | + httpMethod="POST", |
| 88 | + authorizationType="NONE", |
| 89 | + requestValidatorId=validator_id, |
| 90 | + requestParameters={"method.request.path.test": True}, |
| 91 | + ) |
| 92 | + |
| 93 | + apigateway_client.put_integration( |
| 94 | + restApiId=api_id, |
| 95 | + resourceId=resource_id, |
| 96 | + httpMethod="POST", |
| 97 | + integrationHttpMethod="POST", |
| 98 | + type="AWS_PROXY", |
| 99 | + uri=f"arn:aws:apigateway:{region}:lambda:path//2015-03-31/functions/" |
| 100 | + f"{lambda_arn}/invocations", |
| 101 | + ) |
| 102 | + apigateway_client.put_method_response( |
| 103 | + restApiId=api_id, |
| 104 | + resourceId=resource_id, |
| 105 | + httpMethod="POST", |
| 106 | + statusCode="200", |
| 107 | + ) |
| 108 | + apigateway_client.put_integration_response( |
| 109 | + restApiId=api_id, |
| 110 | + resourceId=resource_id, |
| 111 | + httpMethod="POST", |
| 112 | + statusCode="200", |
| 113 | + ) |
| 114 | + |
| 115 | + stage_name = "local" |
| 116 | + deploy_1 = apigateway_client.create_deployment(restApiId=api_id, stageName=stage_name) |
| 117 | + snapshot.match("deploy-1", deploy_1) |
| 118 | + |
| 119 | + source_arn = f"arn:aws:execute-api:{region}:{account_id}:{api_id}/*/*/test/*" |
| 120 | + |
| 121 | + lambda_client.add_permission( |
| 122 | + FunctionName=lambda_arn, |
| 123 | + StatementId=str(short_uid()), |
| 124 | + Action="lambda:InvokeFunction", |
| 125 | + Principal="apigateway.amazonaws.com", |
| 126 | + SourceArn=source_arn, |
| 127 | + ) |
| 128 | + |
| 129 | + url = api_invoke_url(api_id, stage=stage_name, path="/test/value") |
| 130 | + response = requests.post(url, json={"test": "test"}) |
| 131 | + assert response.ok |
| 132 | + assert json.loads(response.json()["body"]) == {"test": "test"} |
| 133 | + |
| 134 | + response = apigateway_client.update_method( |
| 135 | + restApiId=api_id, |
| 136 | + resourceId=resource_id, |
| 137 | + httpMethod="POST", |
| 138 | + patchOperations=[ |
| 139 | + { |
| 140 | + "op": "add", |
| 141 | + "path": "/requestParameters/method.request.path.issuer", |
| 142 | + "value": "true", |
| 143 | + }, |
| 144 | + { |
| 145 | + "op": "remove", |
| 146 | + "path": "/requestParameters/method.request.path.test", |
| 147 | + "value": "true", |
| 148 | + }, |
| 149 | + ], |
| 150 | + ) |
| 151 | + snapshot.match("change-request-path-names", response) |
| 152 | + |
| 153 | + apigw_redeploy_api(rest_api_id=api_id, stage_name=stage_name) |
| 154 | + |
| 155 | + response = requests.post(url, json={"test": "test"}) |
| 156 | + # FIXME: for now, not implemented in LocalStack, we don't validate RequestParameters yet |
| 157 | + # assert response.status_code == 400 |
| 158 | + if response.status_code == 400: |
| 159 | + snapshot.match("missing-required-request-params", response.json()) |
| 160 | + |
| 161 | + # create Model schema to validate body |
| 162 | + apigateway_client.create_model( |
| 163 | + restApiId=api_id, |
| 164 | + name="testSchema", |
| 165 | + contentType="application/json", |
| 166 | + schema=json.dumps( |
| 167 | + { |
| 168 | + "title": "testSchema", |
| 169 | + "type": "object", |
| 170 | + "properties": { |
| 171 | + "a": {"type": "number"}, |
| 172 | + "b": {"type": "number"}, |
| 173 | + }, |
| 174 | + "required": ["a", "b"], |
| 175 | + } |
| 176 | + ), |
| 177 | + ) |
| 178 | + # then attach the schema to the method |
| 179 | + response = apigateway_client.update_method( |
| 180 | + restApiId=api_id, |
| 181 | + resourceId=resource_id, |
| 182 | + httpMethod="POST", |
| 183 | + patchOperations=[ |
| 184 | + {"op": "add", "path": "/requestModels/application~1json", "value": "testSchema"}, |
| 185 | + ], |
| 186 | + ) |
| 187 | + snapshot.match("add-schema", response) |
| 188 | + |
| 189 | + response = apigateway_client.update_method( |
| 190 | + restApiId=api_id, |
| 191 | + resourceId=resource_id, |
| 192 | + httpMethod="POST", |
| 193 | + patchOperations=[ |
| 194 | + { |
| 195 | + "op": "add", |
| 196 | + "path": "/requestParameters/method.request.path.test", |
| 197 | + "value": "true", |
| 198 | + }, |
| 199 | + { |
| 200 | + "op": "remove", |
| 201 | + "path": "/requestParameters/method.request.path.issuer", |
| 202 | + "value": "true", |
| 203 | + }, |
| 204 | + ], |
| 205 | + ) |
| 206 | + snapshot.match("revert-request-path-names", response) |
| 207 | + |
| 208 | + apigw_redeploy_api(rest_api_id=api_id, stage_name=stage_name) |
| 209 | + |
| 210 | + # the validator should then check against this schema and fail |
| 211 | + response = requests.post(url, json={"test": "test"}) |
| 212 | + assert response.status_code == 400 |
| 213 | + snapshot.match("invalid-request-body", response.json()) |
| 214 | + |
| 215 | + # remove the validator from the method |
| 216 | + response = apigateway_client.update_method( |
| 217 | + restApiId=api_id, |
| 218 | + resourceId=resource_id, |
| 219 | + httpMethod="POST", |
| 220 | + patchOperations=[ |
| 221 | + { |
| 222 | + "op": "replace", |
| 223 | + &
3E26
quot;path": "/requestValidatorId", |
| 224 | + "value": "", |
| 225 | + }, |
| 226 | + ], |
| 227 | + ) |
| 228 | + snapshot.match("remove-validator", response) |
| 229 | + |
| 230 | + apigw_redeploy_api(rest_api_id=api_id, stage_name=stage_name) |
| 231 | + |
| 232 | + response = requests.post(url, json={"test": "test"}) |
| 233 | + assert response.ok |
| 234 | + assert json.loads(response.json()["body"]) == {"test": "test"} |
0 commit comments