Skip to content

Commit

Permalink
fix(parser): Make ApiGateway version, authorizer fields optional (#532)
Browse files Browse the repository at this point in the history
Co-authored-by: Walmsley, Michael <m.walmsley@accenture.com>
  • Loading branch information
walmsles and m-walmsley authored Jul 17, 2021
1 parent 5b87bb1 commit 89337a2
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 2 deletions.
4 changes: 2 additions & 2 deletions aws_lambda_powertools/utilities/parser/models/apigw.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class APIGatewayEventAuthorizer(BaseModel):
class APIGatewayEventRequestContext(BaseModel):
accountId: str
apiId: str
authorizer: APIGatewayEventAuthorizer
authorizer: Optional[APIGatewayEventAuthorizer]
stage: str
protocol: str
identity: APIGatewayEventIdentity
Expand All @@ -70,7 +70,7 @@ class APIGatewayEventRequestContext(BaseModel):


class APIGatewayProxyEventModel(BaseModel):
version: str
version: Optional[str]
resource: str
path: str
httpMethod: Literal["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
Expand Down
75 changes: 75 additions & 0 deletions tests/events/apiGatewayProxyEvent_noVersionAuth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"resource": "/my/path",
"path": "/my/path",
"httpMethod": "GET",
"headers": {
"Header1": "value1",
"Header2": "value2"
},
"multiValueHeaders": {
"Header1": [
"value1"
],
"Header2": [
"value1",
"value2"
]
},
"queryStringParameters": {
"parameter1": "value1",
"parameter2": "value"
},
"multiValueQueryStringParameters": {
"parameter1": [
"value1",
"value2"
],
"parameter2": [
"value"
]
},
"requestContext": {
"accountId": "123456789012",
"apiId": "id",
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"extendedRequestId": "request-id",
"httpMethod": "GET",
"identity": {
"accessKey": null,
"accountId": null,
"caller": null,
"cognitoAuthenticationProvider": null,
"cognitoAuthenticationType": null,
"cognitoIdentityId": null,
"cognitoIdentityPoolId": null,
"principalOrgId": null,
"sourceIp": "192.168.0.1/32",
"user": null,
"userAgent": "user-agent",
"userArn": null,
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"path": "/my/path",
"protocol": "HTTP/1.1",
"requestId": "id=",
"requestTime": "04/Mar/2020:19:15:17 +0000",
"requestTimeEpoch": 1583349317135,
"resourceId": null,
"resourcePath": "/my/path",
"stage": "$default"
},
"pathParameters": null,
"stageVariables": null,
"body": "Hello from Lambda!",
"isBase64Encoded": true
}
64 changes: 64 additions & 0 deletions tests/functional/test_data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,70 @@ def test_seq_trigger_event():
assert record.aws_region == "us-east-2"


def test_default_api_gateway_proxy_event():
event = APIGatewayProxyEvent(load_event("apiGatewayProxyEvent_noVersionAuth.json"))

assert event.get("version") is None
assert event.resource == event["resource"]
assert event.path == event["path"]
assert event.http_method == event["httpMethod"]
assert event.headers == event["headers"]
assert event.multi_value_headers == event["multiValueHeaders"]
assert event.query_string_parameters == event["queryStringParameters"]
assert event.multi_value_query_string_parameters == event["multiValueQueryStringParameters"]

request_context = event.request_context
assert request_context.account_id == event["requestContext"]["accountId"]
assert request_context.api_id == event["requestContext"]["apiId"]

assert request_context.get("authorizer") is None

assert request_context.domain_name == event["requestContext"]["domainName"]
assert request_context.domain_prefix == event["requestContext"]["domainPrefix"]
assert request_context.extended_request_id == event["requestContext"]["extendedRequestId"]
assert request_context.http_method == event["requestContext"]["httpMethod"]

identity = request_context.identity
assert identity.access_key == event["requestContext"]["identity"]["accessKey"]
assert identity.account_id == event["requestContext"]["identity"]["accountId"]
assert identity.caller == event["requestContext"]["identity"]["caller"]
assert (
identity.cognito_authentication_provider == event["requestContext"]["identity"]["cognitoAuthenticationProvider"]
)
assert identity.cognito_authentication_type == event["requestContext"]["identity"]["cognitoAuthenticationType"]
assert identity.cognito_identity_id == event["requestContext"]["identity"]["cognitoIdentityId"]
assert identity.cognito_identity_pool_id == event["requestContext"]["identity"]["cognitoIdentityPoolId"]
assert identity.principal_org_id == event["requestContext"]["identity"]["principalOrgId"]
assert identity.source_ip == event["requestContext"]["identity"]["sourceIp"]
assert identity.user == event["requestContext"]["identity"]["user"]
assert identity.user_agent == event["requestContext"]["identity"]["userAgent"]
assert identity.user_arn == event["requestContext"]["identity"]["userArn"]

assert request_context.path == event["requestContext"]["path"]
assert request_context.protocol == event["requestContext"]["protocol"]
assert request_context.request_id == event["requestContext"]["requestId"]
assert request_context.request_time == event["requestContext"]["requestTime"]
assert request_context.request_time_epoch == event["requestContext"]["requestTimeEpoch"]
assert request_context.resource_id == event["requestContext"]["resourceId"]
assert request_context.resource_path == event["requestContext"]["resourcePath"]
assert request_context.stage == event["requestContext"]["stage"]

assert event.path_parameters == event["pathParameters"]
assert event.stage_variables == event["stageVariables"]
assert event.body == event["body"]
assert event.is_base64_encoded == event["isBase64Encoded"]

assert request_context.connected_at is None
assert request_context.connection_id is None
assert request_context.event_type is None
assert request_context.message_direction is None
assert request_context.message_id is None
assert request_context.route_key is None
assert request_context.operation_name is None
assert identity.api_key is None
assert identity.api_key_id is None


def test_api_gateway_proxy_event():
event = APIGatewayProxyEvent(load_event("apiGatewayProxyEvent.json"))

Expand Down

0 comments on commit 89337a2

Please sign in to comment.