Skip to content

Commit

Permalink
✅test: add initial pytest suite
Browse files Browse the repository at this point in the history
  • Loading branch information
gtempus committed Nov 19, 2024
1 parent 27ef81a commit 5433f9e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,3 @@ def replace_implementation(path_parts):
implementation = path_parts[impl_path_location]
path_parts[impl_path_location] = "dynamic"
return implementation


def test_handler():
# Mock event and context
event = {
"Records": [
{
"cf": {
"response": {
"status": "404",
"headers": {
"content-type": [
{"key": "Content-Type", "value": "text/plain"}
]
},
},
"request": {
"uri": "/sbtn_natural_forests_map/v202310/natural_forest/10/20/30.png",
"querystring": "some_param=30",
},
}
}
]
}
context = {}

# Call the handler function
response = handler(event, context)

# Assertions
assert response["status"] == "307"
assert response["headers"]["location"][0]["key"] == "Location"
assert (
response["headers"]["location"][0]["value"]
== "/sbtn_natural_forests_map/v202310/dynamic/10/20/30.png?some_param=30&implementation=natural_forest"
)
assert response["headers"]["content-type"][0]["value"] == "application/json"
115 changes: 115 additions & 0 deletions tests/lambda/test_redirect_s3_404.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from terraform.modules.content_delivery_network.lambda_functions.redirect_s3_404.src.lambda_function import (
handler,
)


def create_event(status="404", uri="not important for this test", querystring=""):
"""Helper method to create the base event dictionary with customizable
status, URI, and query string."""
return {
"Records": [
{
"cf": {
"response": {
"status": status,
"headers": {
"content-type": [
{"key": "Content-Type", "value": "application/json"}
]
},
},
"request": {
"uri": uri,
"querystring": querystring,
},
}
}
]
}


class TestRedirectOnlyTileRequestsThatAreNotFound:
def test_handler_does_not_modify_response_if_status_is_something_other_than_404(
self,
):
event = create_event(status="200")
context = {}

response = handler(event, context)

assert response == {
"status": "200",
"headers": {
"content-type": [{"key": "Content-Type", "value": "application/json"}]
},
}

def test_handler_creates_a_redirect_response_if_status_is_404_and_is_a_tile(self):
event = create_event(
uri="/sbtn_natural_forests_map/v202310/natural_forest/10/20/30.png"
)
context = {}

response = handler(event, context)

assert (
response.items()
>= {
"status": "307",
"statusDescription": "Temporary Redirect",
}.items()
)

def test_handler_does_not_modify_response_if_request_is_a_resource_other_than_a_tile(
self,
):
event = create_event(
uri="/sbtn_natural_forests_map/v202310/natural_forest/10/20/30.txt"
)
context = {}

response = handler(event, context)

assert response == {
"status": "404",
"headers": {
"content-type": [{"key": "Content-Type", "value": "application/json"}]
},
}


def test_handler():
# Mock event and context
event = {
"Records": [
{
"cf": {
"response": {
"status": "404",
"headers": {
"content-type": [
{"key": "Content-Type", "value": "text/plain"}
]
},
},
"request": {
"uri": "/sbtn_natural_forests_map/v202310/natural_forest/10/20/30.png",
"querystring": "some_param=30",
},
}
}
]
}
context = {}

# Call the handler function
response = handler(event, context)

# Assertions
assert response["status"] == "307"
assert response["headers"]["location"][0]["key"] == "Location"
assert (
response["headers"]["location"][0]["value"]
== "/sbtn_natural_forests_map/v202310/dynamic/10/20/30.png?some_param=30&implementation=natural_forest"
)
assert response["headers"]["content-type"][0]["value"] == "application/json"

0 comments on commit 5433f9e

Please sign in to comment.