def test_init_human_interaction_fails_on_generic_exceptions():
    # test init_human_interaction() to make it fail on exceptions other than KeyError
    bad_context = {
        'task_token':
        1.0,  #It expects a string, and a float number should make it fail
        'execution_id': gen_id(),
        'artifacts': {
            'event': {
                'id': gen_id(),
                'created_at': gen_datetimenow(),
                'data_types': {},
                'details': {
                    "some": "randon text"
                },
                'event_type': 'Test sfn',
                'event_meta': {},
                'investigation_id': gen_id(),
                'status_': 'open',
                'is_duplicate': False
            },
            'execution_id': gen_id()
        },
        'state_name': gen_id(),
        'Parameters': {}
    }
    test_message = {"greeting": "Hello, World"}

    with pytest.raises(Exception):
        message_id = init_human_interaction(bad_context, test_message)
def test_end_human_interaction_fails_on_update_item_exception():
    # moved blocks of code that can't be tested automatically to here
    # test end_human_interaction() to fail on update item. Expecting it to raise an exception

    test_response = {"response": "Hello, back"}
    test_message = {"greeting": "Hello, World"}
    sfn_item_metadata = mock_sfn_db_context()
    sfn_context = sfn_item_metadata['sfn_context']
    message_id = gen_id()
    date_time = gen_datetimenow()
    response_table = boto3.resource('dynamodb').Table(
        os.environ['SOCLESS_MESSAGE_RESPONSE_TABLE'])
    response_table.put_item(
        Item={
            "message_id": message_id,
            "datetime": date_time,
            "investigation_id": sfn_item_metadata['investigation_id'],
            "message": test_message,
            "fulfilled": False,
            "execution_id": sfn_item_metadata['execution_id'],
            "receiver": sfn_context['sfn_context']['State_Config']['Name'],
            "await_token": sfn_item_metadata['task_token']
        })
    try:
        response_table.update_item(
            Key={"message_id": message_id},
            UpdateExpression=
            "SET fulfilled = :fulfilled, response_payload = :response_payload",
            ExpressionAttributeValues={
                ":fulfilled": True,
                ":response_payload": 1.0
            })
    except Exception as e:
        triggered_exception = True
    assert triggered_exception == True
Ejemplo n.º 3
0
def mock_execution_results_table_entry():
    # setup db context for execution results table entry

    random_id = gen_id()
    execution_id = gen_id()
    investigation_id = gen_id()
    date_time = gen_datetimenow()
    context = {
        "datetime": date_time,
        "execution_id": execution_id,
        "investigation_id": investigation_id,
        "results": {
            'artifacts': {
                'event': {
                    'id': random_id,
                    'created_at': date_time,
                    'data_types': {},
                    'details': {
                        "some": "randon text"
                    },
                    'event_type': 'Test integrations',
                    'event_meta': {},
                    'investigation_id': investigation_id,
                    'status_': 'open',
                    'is_duplicate': False
                },
                'execution_id': execution_id
            },
            "errors": {},
            "results": {}
        }
    }
    results_table_name = os.environ['SOCLESS_RESULTS_TABLE']
    client = boto3.client('dynamodb')
    client.put_item(TableName=results_table_name,
                    Item=dict_to_item(context, convert_root=False))
    return {
        'id': random_id,
        "execution_id": execution_id,
        "investigation_id": investigation_id,
        "datetime": date_time,
        'context': context
    }
def test_end_human_interaction_fails_on_receiver_not_found():
    # test end_human_interaction() fails on receiver doesn't exist in the saved item. Expecting it to raise an exception

    test_response = {"response": "Hello, back"}
    test_message = {"greeting": "Hello, World"}
    sfn_item_metadata = mock_sfn_db_context()
    message_id = gen_id()
    response_table = boto3.resource('dynamodb').Table(
        os.environ['SOCLESS_MESSAGE_RESPONSE_TABLE'])
    response_table.put_item(
        Item={
            "message_id": message_id,
            "datetime": gen_datetimenow(),
            "investigation_id": sfn_item_metadata['investigation_id'],
            "message": test_message,
            "fulfilled": False,
            "execution_id": sfn_item_metadata['execution_id'],
            "await_token": sfn_item_metadata['task_token']
        })
    with pytest.raises(Exception, match='receiver_not_found'):
        end_human_interaction(message_id, test_response)
Ejemplo n.º 5
0
def test_gen_datetimenow():
    """Testing the gen_datetimenow util"""
    response = gen_datetimenow()
    assert type(response) == str
    assert response.endswith('Z')