Skip to content

Commit

Permalink
feat(idempotencty): Raise error when event data is None
Browse files Browse the repository at this point in the history
GIVEN a persistence_store with event_key_jmespath = `body`
WHEN getting the hashed idempotency key with an event without a `body` key
THEN raise IdempotencyValidationError
  • Loading branch information
Michael Brewer committed Feb 22, 2021
1 parent f1a8832 commit 8308fe1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ def _get_hashed_idempotency_key(self, lambda_event: Dict[str, Any]) -> str:
data = lambda_event
if self.event_key_jmespath:
data = self.event_key_compiled_jmespath.search(lambda_event)

if data is None:
raise IdempotencyValidationError("No data found to create a hashed idempotency_key")

return self._generate_hash(data)

def _get_hashed_payload(self, lambda_event: Dict[str, Any]) -> str:
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/idempotency/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def hashed_validation_key(lambda_apigw_event):
@pytest.fixture
def persistence_store(config, request, default_jmespath):
persistence_store = DynamoDBPersistenceLayer(
event_key_jmespath=default_jmespath,
event_key_jmespath=request.param.get("event_key_jmespath") or default_jmespath,
table_name=TABLE_NAME,
boto_config=config,
use_local_cache=request.param["use_local_cache"],
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/idempotency/test_idempotency.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,17 @@ def test_user_local_disabled(persistence_store):
# THEN raise AttributeError
# AND don't have a _cache attribute
assert not hasattr("persistence_store", "_cache")


@pytest.mark.parametrize("persistence_store", [{"use_local_cache": False, "event_key_jmespath": "body"}], indirect=True)
def test_no_data_for_generate_hash(persistence_store):
# GIVEN a persistence_store with use_local_cache = False and event_key_jmespath = "body"
assert persistence_store.use_local_cache is False
assert "body" in persistence_store.event_key_jmespath

# WHEN getting the hashed idempotency key for an event with no `body` key
with pytest.raises(IdempotencyValidationError) as excinfo:
persistence_store._get_hashed_idempotency_key({})

# THEN raise IdempotencyValidationError error
assert "No data found to create a hashed idempotency_key" in str(excinfo.value)

0 comments on commit 8308fe1

Please sign in to comment.