Exemple #1
0
def test_jmespath_with_powertools_json(
        idempotency_config: IdempotencyConfig,
        persistence_store: DynamoDBPersistenceLayer, lambda_context):
    # GIVEN an event_key_jmespath with powertools_json custom function
    persistence_store.configure(idempotency_config)
    sub_attr_value = "cognito_user"
    key_attr_value = "some_key"
    expected_value = [sub_attr_value, key_attr_value]
    api_gateway_proxy_event = {
        "requestContext": {
            "authorizer": {
                "claims": {
                    "sub": sub_attr_value
                }
            }
        },
        "body": json.dumps({"id": key_attr_value}),
    }

    # WHEN calling _get_hashed_idempotency_key
    result = persistence_store._get_hashed_idempotency_key(
        api_gateway_proxy_event, lambda_context)

    # THEN the hashed idempotency key should match the extracted values generated hash
    assert result == "test-func#" + persistence_store._generate_hash(
        expected_value)
def test_custom_jmespath_function_overrides_builtin_functions(
    config_with_jmespath_options: IdempotencyConfig, persistence_store: DynamoDBPersistenceLayer, lambda_context
):
    # GIVEN an persistence store with a custom jmespath_options
    # AND use a builtin powertools custom function
    persistence_store.configure(config_with_jmespath_options)

    with pytest.raises(jmespath.exceptions.UnknownFunctionError, match="Unknown function: powertools_json()"):
        # WHEN calling _get_hashed_idempotency_key
        # THEN raise unknown function
        persistence_store._get_hashed_idempotency_key({}, lambda_context)
Exemple #3
0
def test_delete_from_cache_when_empty(
        idempotency_config: IdempotencyConfig,
        persistence_store: DynamoDBPersistenceLayer):
    # GIVEN use_local_cache is True AND the local cache is empty
    persistence_store.configure(idempotency_config)

    try:
        # WHEN we _delete_from_cache
        persistence_store._delete_from_cache("key_does_not_exist")
    except KeyError:
        # THEN we should not get a KeyError
        pytest.fail("KeyError should not happen")
Exemple #4
0
def test_in_progress_never_saved_to_cache(
        idempotency_config: IdempotencyConfig,
        persistence_store: DynamoDBPersistenceLayer):
    # GIVEN a data record with status "INPROGRESS"
    # and persistence_store has use_local_cache = True
    persistence_store.configure(idempotency_config)
    data_record = DataRecord("key", status="INPROGRESS")

    # WHEN saving to local cache
    persistence_store._save_to_cache(data_record)

    # THEN don't save to local cache
    assert persistence_store._cache.get("key") is None
Exemple #5
0
def test_default_no_raise_on_missing_idempotency_key(
        idempotency_config: IdempotencyConfig,
        persistence_store: DynamoDBPersistenceLayer):
    # GIVEN a persistence_store with use_local_cache = False and event_key_jmespath = "body"
    persistence_store.configure(idempotency_config)
    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
    hashed_key = persistence_store._get_hashed_idempotency_key({})

    # THEN return the hash of None
    assert md5(json.dumps(None).encode()).hexdigest() == hashed_key
def test_raise_on_no_idempotency_key(
    idempotency_config: IdempotencyConfig, persistence_store: DynamoDBPersistenceLayer, lambda_context
):
    # GIVEN a persistence_store with raise_on_no_idempotency_key and no idempotency key in the request
    persistence_store.configure(idempotency_config)
    persistence_store.raise_on_no_idempotency_key = True
    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(IdempotencyKeyError) as excinfo:
        persistence_store._get_hashed_idempotency_key({}, lambda_context)

    # THEN raise IdempotencyKeyError error
    assert "No data found to create a hashed idempotency_key" in str(excinfo.value)
def test_user_local_disabled(idempotency_config: IdempotencyConfig, persistence_store: DynamoDBPersistenceLayer):
    # GIVEN a persistence_store with use_local_cache = False
    persistence_store.configure(idempotency_config)

    # WHEN calling any local cache options
    data_record = DataRecord("key", status="COMPLETED")
    try:
        persistence_store._save_to_cache(data_record)
        cache_value = persistence_store._retrieve_from_cache("key")
        assert cache_value is None
        persistence_store._delete_from_cache("key")
    except AttributeError as e:
        pytest.fail(f"AttributeError should not be raised: {e}")

    # THEN raise AttributeError
    # AND don't have a _cache attribute
    assert not hasattr("persistence_store", "_cache")
Exemple #8
0
def persistence_store(config):
    return DynamoDBPersistenceLayer(table_name=TABLE_NAME, boto_config=config)