Example #1
0
def test_create_put_bot_no_prefix(validate_intent, mock_intent, mock_bot,
                                  cfn_create_no_prefix_event, setup,
                                  monkeypatch):
    """ test_create_puts_bot"""
    context, builder, _ = setup
    resources = cfn_create_no_prefix_event['ResourceProperties']
    messages = resources.get('messages')

    resources.pop('slotTypes')

    mock_bot.return_value = '1234'
    intent = Intent('a', 'b', 'c', 'd', 'e')
    mock_intent.return_value = intent

    builder.put.return_value = {"name": 'LexBot', "version": '$LATEST'}
    patch_builder(context, builder, monkeypatch)

    response = app.create(cfn_create_no_prefix_event, context)
    messages = resources['messages']

    builder.put.assert_called_once_with('1234')
    mock_bot.assert_called_once_with('LexBot', [intent, intent],
                                     messages,
                                     locale=resources.get('locale'),
                                     description=resources.get('description'))
    validate_intent.assert_called_once
    mock_intent.assert_called

    assert response['BotName'] == 'LexBot'
    assert response['BotVersion'] == BOT_VERSION
def test_create_intent_with_amazon_slot(put_intent_response, codehook_uri,
                                        mock_context, lex, aws_lambda,
                                        monkeypatch_account):

    with Stubber(aws_lambda) as lambda_stubber, Stubber(lex) as lex_stubber:
        stub_lambda_request(lambda_stubber, codehook_uri)

        intent_builder = IntentBuilder(Mock(),
                                       mock_context,
                                       lex_sdk=lex,
                                       lambda_sdk=aws_lambda)

        plaintext = {
            "confirmation": 'some confirmation message',
            'rejection': 'rejection message',
            'conclusion': 'concluded'
        }
        stub_not_found_get_request(lex_stubber)
        slot = Slot('person', 'AMAZON.Person', 'yo', ['one', 'two'])
        intent = Intent(BOT_NAME,
                        INTENT_NAME,
                        codehook_uri,
                        UTTERANCES, [slot],
                        plaintext=plaintext,
                        max_attempts=3)

        put_request = put_intent_slot_request(intent)
        put_request.update(put_request_conclusion(plaintext))

        stub_intent_creation(lex_stubber, put_intent_response, put_request)

        intent_builder.put_intent(intent)

        lex_stubber.assert_no_pending_responses()
        lambda_stubber.assert_no_pending_responses()
def test_create_intent_old_missing_rejection_plaintext(put_intent_response,
                                                       codehook_uri,
                                                       mock_context, lex,
                                                       aws_lambda,
                                                       monkeypatch_account):

    with Stubber(aws_lambda) as lambda_stubber, Stubber(lex) as lex_stubber:
        stub_lambda_request(lambda_stubber, codehook_uri)
        stub_not_found_get_request(lex_stubber)

        intent_builder = IntentBuilder(Mock(),
                                       mock_context,
                                       lex_sdk=lex,
                                       lambda_sdk=aws_lambda)
        plaintext = {"confirmation": 'some confirmation message'}
        intent = Intent(BOT_NAME,
                        INTENT_NAME,
                        codehook_uri,
                        UTTERANCES, [],
                        plaintext=plaintext)
        with pytest.raises(Exception) as excinfo:
            intent_builder.put_intent(intent)

        assert "Must have both rejection and confirmation or neither" in str(
            excinfo.value)
def test_validate_intent_slots(validate_slot, intent_defs):
    intent_def = intent_defs[0]
    del intent_def['Slots']

    intent = Intent.create_intent('botname', intent_def)
    intent.slots = [Slot('a', 'b', 'c', [])]
    intent.validate_intent()

    validate_slot.assert_called_once()
def setup():
    """ setup function """
    intent1 = Intent(BOT_NAME,
                     'greeting',
                     LAMBDA_ARN, ['farewell my friend'],
                     None,
                     max_attempts=3,
                     plaintext={'confirmation': 'a greeting confirmation'})

    intent2 = Intent(BOT_NAME,
                     'farewell',
                     LAMBDA_ARN, ['farewell my friend'],
                     None,
                     max_attempts=3,
                     plaintext={'confirmation': 'a farewell confirmation'})

    lex = botocore.session.get_session().create_client('lex-models')
    return lex, [intent1, intent2]
def test_create_intent(intent_defs):
    del intent_defs[0]['Slots']

    intent = Intent.create_intent('botname', intent_defs[0])

    assert intent.bot_name == 'botname'
    assert intent.intent_name == 'greeting'
    assert intent.utterances == ['greetings my friend', 'hello']
    assert intent.attrs['max_attempts'] == 5
    assert intent.attrs['plaintext'] == {"confirmation": 'a confirmation'}
def test_create_intent_default_max_attempts(intent_defs, mocker):
    intent_def = intent_defs[0]

    del intent_def['Slots']
    del intent_def['maxAttempts']

    intent = Intent.create_intent('botname', intent_def)
    assert intent.bot_name == 'botname'
    assert intent.intent_name == 'greeting'
    assert intent.utterances == ['greetings my friend', 'hello']
    assert intent.attrs['max_attempts'] == 3
    assert intent.attrs['plaintext'] == {"confirmation": 'a confirmation'}
Example #8
0
def test_delete_no_prefix(validate_intent, mock_intent, mock_bot,
                          cfn_delete_event, setup, monkeypatch):
    """ test_delete_no_prefix """
    context, builder, slot_builder = setup
    cfn_delete_event['ResourceProperties'].pop('NamePrefix')
    cfn_delete_event['ResourceProperties']['name'] = 'LexBot'
    intent = Intent('a', 'b', 'c', 'd', 'e')

    mock_intent.return_value = intent
    mock_bot.return_value = '1234'
    patch_builder(context, builder, monkeypatch)
    patch_slot_builder(context, slot_builder, monkeypatch)

    app.delete(cfn_delete_event, context)

    builder.delete.assert_called_once_with('1234')
    slot_builder.delete_slot_type.assert_called_once_with(SLOT_TYPE_NAME)
Example #9
0
def test_delete(validate_intent, mock_intent, mock_bot, cfn_delete_event,
                setup, monkeypatch):
    """ test_delete """
    context, builder, slot_builder = setup

    intent = Intent('a', 'b', 'c', 'd', None)
    mock_intent.return_value = intent
    mock_bot.return_value = '1234'

    patch_builder(context, builder, monkeypatch)
    patch_slot_builder(context, slot_builder, monkeypatch)

    app.delete(cfn_delete_event, context)

    builder.delete.assert_called_once_with('1234')
    slot_builder.delete_slot_type.assert_called_once_with(PREFIX +
                                                          SLOT_TYPE_NAME)
Example #10
0
def test_update_puts_no_prefix(mock_intent, mock_bot,
                               cfn_create_no_prefix_event, setup, monkeypatch):
    """ test_update_puts_bot_no_prefix """
    context, builder, _ = setup
    cfn_create_no_prefix_event['ResourceProperties'].pop('slotTypes')
    mock_bot.return_value = '1234'

    builder.put.return_value = {"name": 'LexBot', "version": '$LATEST'}
    intent = Intent('a', 'b', 'c', 'd', None)
    mock_intent.return_value = intent
    patch_builder(context, builder, monkeypatch)

    response = app.update(cfn_create_no_prefix_event, context)

    builder.put.assert_called_once_with('1234')

    assert response['BotName'] == 'LexBot'
    assert response['BotVersion'] == BOT_VERSION
def test_update_intent_plaintext_followUp(put_intent_response, codehook_uri,
                                          mock_context, lex, aws_lambda,
                                          monkeypatch_account):

    with Stubber(aws_lambda) as lambda_stubber, Stubber(lex) as stubber:
        stub_lambda_request(lambda_stubber, codehook_uri)

        intent_builder = IntentBuilder(Mock(),
                                       mock_context,
                                       lex_sdk=lex,
                                       lambda_sdk=aws_lambda)

        plaintext = {
            "confirmation": 'some confirmation message',
            'rejection': 'rejection message',
            'followUpPrompt': 'follow on',
            'followUpRejection': 'failed follow on'
        }
        put_request = put_intent_request(BOT_NAME,
                                         INTENT_NAME,
                                         UTTERANCES,
                                         plaintext=plaintext)

        put_request.update(put_request_followUp(plaintext))
        put_request.update({'checksum': 'chksum'})
        stub_intent_get(stubber, INTENT_NAME)

        stub_intent_creation(stubber, put_intent_response, put_request)

        intent = Intent(BOT_NAME,
                        INTENT_NAME,
                        codehook_uri,
                        UTTERANCES,
                        None,
                        plaintext=plaintext,
                        max_attempts=3)
        response = intent_builder.put_intent(intent)

        stubber.assert_no_pending_responses()
        lambda_stubber.assert_no_pending_responses()

        assert response['intentName'] == 'greeting'
        assert response['intentVersion'] == '1'
def test_create_intent_conclusion_and_followUp_errors(put_intent_response,
                                                      codehook_uri,
                                                      mock_context, lex,
                                                      aws_lambda,
                                                      monkeypatch_account):

    with Stubber(aws_lambda) as lambda_stubber, Stubber(lex) as stubber:
        stub_lambda_request(lambda_stubber, codehook_uri)
        intent_builder = IntentBuilder(Mock(),
                                       mock_context,
                                       lex_sdk=lex,
                                       lambda_sdk=aws_lambda)

        plaintext = {
            "confirmation": 'some confirmation message',
            'rejection': 'rejection message',
            'conclusion': 'the conclusion',
            'followUpPrompt': 'follow up'
        }

        stub_not_found_get_request(stubber)
        put_request = put_intent_request(BOT_NAME,
                                         INTENT_NAME,
                                         UTTERANCES,
                                         plaintext=plaintext)
        put_request.update(put_request_conclusion(plaintext))

        stub_intent_creation(stubber, put_intent_response, put_request)

        with pytest.raises(Exception) as excinfo:
            intent = Intent(BOT_NAME,
                            INTENT_NAME,
                            codehook_uri,
                            UTTERANCES,
                            None,
                            plaintext=plaintext,
                            max_attempts=3)
            intent_builder.put_intent(intent)

        assert "Can not have conclusion and followUpPrompt" in str(
            excinfo.value)
def test_create_intent_response(put_intent_response, codehook_uri,
                                mock_context, lex, aws_lambda,
                                monkeypatch_account):
    """ test the response from create intent """

    with Stubber(aws_lambda) as lambda_stubber, Stubber(lex) as stubber:
        stub_lambda_request(lambda_stubber, codehook_uri)
        intent_builder = IntentBuilder(Mock(),
                                       mock_context(mocker),
                                       lex_sdk=lex,
                                       lambda_sdk=aws_lambda)

        plaintext = {
            "confirmation": 'some confirmation message',
            'rejection': 'rejection message',
            'conclusion': 'concluded'
        }

        stub_not_found_get_request(stubber)
        put_request = put_intent_request(BOT_NAME,
                                         INTENT_NAME,
                                         UTTERANCES,
                                         plaintext=plaintext)
        put_request.update(put_request_conclusion(plaintext))

        stub_intent_creation(stubber, put_intent_response, put_request)

        intent = Intent(BOT_NAME,
                        INTENT_NAME,
                        codehook_uri,
                        UTTERANCES,
                        None,
                        plaintext=plaintext,
                        max_attempts=3)
        response = intent_builder.put_intent(intent)

        stubber.assert_no_pending_responses()
        lambda_stubber.assert_no_pending_responses()

        assert response['intentName'] == 'greeting'
        assert response['intentVersion'] == '1'
def test_create_intent_missing_followUp_plaintext(put_intent_response,
                                                  codehook_uri, mock_context,
                                                  lex, aws_lambda,
                                                  monkeypatch_account):

    with Stubber(aws_lambda) as lambda_stubber, Stubber(lex) as stubber:
        stub_lambda_request(lambda_stubber, codehook_uri)
        intent_builder = IntentBuilder(Mock(),
                                       mock_context,
                                       lex_sdk=lex,
                                       lambda_sdk=aws_lambda)

        plaintext = {
            "confirmation": 'some confirmation message',
            'rejection': 'rejection message',
            'conclusion': 'the conclusion'
        }

        stub_not_found_get_request(stubber)
        put_request = put_intent_request(BOT_NAME,
                                         INTENT_NAME,
                                         UTTERANCES,
                                         plaintext=plaintext)
        put_request.update(put_request_conclusion(plaintext))

        stub_intent_creation(stubber, put_intent_response, put_request)

        intent = Intent(BOT_NAME,
                        INTENT_NAME,
                        codehook_uri,
                        UTTERANCES,
                        None,
                        plaintext=plaintext,
                        max_attempts=3)
        intent_builder.put_intent(intent)

        stubber.assert_no_pending_responses()
        lambda_stubber.assert_no_pending_responses()
Example #15
0
def _extract_intents(bot_name, resources):
    intents = []
    for json_intent in resources.get('intents'):
        intents.append(Intent.create_intent(bot_name, json_intent))
    return intents
def test_validate_intent_fails(intent_defs):
    with pytest.raises(Exception) as excinfo:
        Intent.create_intent('botname', intent_defs[1]).validate_intent()

    assert "Utterances missing in intents" in str(excinfo.value)