Esempio n. 1
0
def test_custom_display_text(test_client, init_database, menu_name, language, expecteds,
                             real_at_idx, length, unexpected, menu_nr):
    with db.session.no_autoflush:
        start_state = UssdSessionFactory(state="menu_name")

        mapping = fake_transfer_mapping(length)
        real_usage1 = TransferUsage.find_or_create('Education')
        real_usage1.translations = {'en': 'Education', 'sw': 'Elimu'}
        real_usage2 = TransferUsage.find_or_create('Health')
        real_usage2.translations = {'en': 'Health', 'sw': 'Afya'}
        mapping[real_at_idx] = KenyaUssdStateMachine.make_usage_mapping(real_usage1)
        mapping[real_at_idx + 1] = KenyaUssdStateMachine.make_usage_mapping(real_usage2)
        user = standard_user()
        user.preferred_language = language

        start_state.session_data = {
            'transfer_usage_mapping': mapping,
            'usage_menu': menu_nr,
            'usage_index_stack': [0, 8]
        }

        start_state.user = user

        menu = UssdMenu(name=menu_name, display_key="ussd.kenya.{}".format(menu_name))
        resulting_menu = KenyaUssdProcessor.custom_display_text(
            menu, start_state)

        for expected in expecteds:
            assert expected in resulting_menu
        if unexpected is not None:
            assert unexpected not in resulting_menu
Esempio n. 2
0
def test_send_directory_listing(mocker, test_client, init_database):
    session = UssdSessionFactory(state="directory_listing")
    session.session_data = {'transfer_usage_mapping': fake_transfer_mapping(6), 'usage_menu': 0}
    user = standard_user()
    user.phone = phone()
    state_machine = KenyaUssdStateMachine(session, user)
    transfer_usage = TransferUsage.find_or_create("Food")

    send_directory_listing = mocker.MagicMock()
    mocker.patch('server.ussd_tasker.send_directory_listing', send_directory_listing)

    state_machine.feed_char('2')
    assert state_machine.state == 'complete'
    send_directory_listing.assert_called_with(user, transfer_usage)
def test_save_directory_info(mocker, test_client, init_database):
    session = UssdSessionFactory(state="change_my_business_prompt")
    user = standard_user()
    user.phone = phone()
    assert next(filter(lambda x: x.name == 'bio', user.custom_attributes),
                None) is None
    state_machine = KenyaUssdStateMachine(session, user)
    state_machine.send_sms = mocker.MagicMock()

    state_machine.feed_char("My Bio")
    session.session_data = {'bio': "My Bio"}
    assert state_machine.state == "profile_info_change_pin_authorization"
    state_machine.feed_char("0000")
    bio = next(filter(lambda x: x.name == 'bio', user.custom_attributes), None)
    assert bio.value == "My Bio"
def test_exchange_token(mocker, test_client, init_database, create_transfer_account_user):
    agent_recipient = create_transfer_account_user
    agent_recipient.phone = make_kenyan_phone(agent_recipient.phone)
    agent_recipient.set_held_role('TOKEN_AGENT', 'grassroots_token_agent')

    exchange_token_confirmation = UssdSessionFactory(
        state="exchange_token_confirmation",
        session_data=json.loads(
            "{" +
            f'"agent_phone": "{agent_recipient.phone}",'
            '"exchange_amount": "50"'
            + "}"
        )
    )

    user = standard_user()
    user.phone = phone()

    state_machine = KenyaUssdStateMachine(exchange_token_confirmation, user)
    exchange_token = mocker.MagicMock()
    mocker.patch('server.ussd_tasker.exchange_token', exchange_token)

    state_machine.feed_char("1")
    assert state_machine.state == "complete"
    exchange_token.assert_called_with(user, agent_recipient, 50)
Esempio n. 5
0
def test_save_directory_info(mocker, test_client, init_database,
                             session_factory, user_factory, user_input,
                             expected):
    session = UssdSessionFactory(state="change_my_business_prompt")
    user = user_factory()
    user.phone = phone()
    assert next(filter(lambda x: x.name == 'bio', user.custom_attributes),
                None) is None
    state_machine = KenyaUssdStateMachine(session, user)
    state_machine.send_sms = mocker.MagicMock()

    state_machine.feed_char("My Bio")
    assert state_machine.state == expected
    session.session_data = {'bio': "My Bio"}
    state_machine.feed_char("0000")
    bio = next(filter(lambda x: x.name == 'bio', user.custom_attributes), None)
    assert bio.value == "My Bio"
def test_balance_inquiry(mocker, test_client, init_database):
    session = UssdSessionFactory(state="balance_inquiry_pin_authorization")
    user = standard_user()
    user.phone = phone()

    state_machine = KenyaUssdStateMachine(session, user)
    inquire_balance = mocker.MagicMock()
    mocker.patch('server.ussd_tasker.inquire_balance', inquire_balance)

    state_machine.feed_char('0000')
    assert state_machine.state == 'complete'
    inquire_balance.assert_called_with(user)
Esempio n. 7
0
def test_opt_out_of_marketplace(mocker, test_client, init_database):
    session = UssdSessionFactory(state="opt_out_of_market_place_pin_authorization")
    user = standard_user()
    user.phone = phone()
    assert next(filter(lambda x: x.name == 'market_enabled', user.custom_attributes), None) is None
    state_machine = KenyaUssdStateMachine(session, user)
    state_machine.send_sms = mocker.MagicMock()

    state_machine.feed_char("0000")
    assert state_machine.state == "complete"
    assert user.is_market_enabled == False
    state_machine.send_sms.assert_called_with(user.phone, "opt_out_of_market_place_sms")
Esempio n. 8
0
def test_send_token(mocker, test_client, init_database,
                    create_transfer_account_user, standard_user):
    recipient = create_transfer_account_user
    recipient.phone = make_kenyan_phone(recipient.phone)

    send_token_confirmation = UssdSessionFactory(
        state="send_token_confirmation",
        session_data=json.loads("{" +
                                f'"recipient_phone": "{recipient.phone}",'
                                '"transaction_amount": "10",'
                                '"transaction_reason_i18n": "A reason",'
                                '"transaction_reason_id": "1"' + "}"))

    state_machine = KenyaUssdStateMachine(send_token_confirmation,
                                          standard_user)
    send_token = mocker.MagicMock()
    mocker.patch('server.ussd_tasker.send_token', send_token)

    state_machine.feed_char("1")
    assert state_machine.state == "complete"
    send_token.assert_called_with(standard_user, recipient, 10, "A reason", 1)
def test_terms_only_sent_once(mocker, test_client, init_database, mock_sms_apis):
    session = UssdSessionFactory(state="balance_inquiry_pin_authorization")
    user = standard_user()
    user.phone = phone()

    inquire_balance = mocker.MagicMock()
    mocker.patch('server.ussd_tasker.inquire_balance', inquire_balance)

    state_machine = KenyaUssdStateMachine(session, user)
    state_machine.feed_char('0000')

    db.session.commit()

    messages = mock_sms_apis

    assert len(messages) == 1

    state_machine = KenyaUssdStateMachine(session, user)
    state_machine.feed_char('0000')

    assert len(messages) == 1
Esempio n. 10
0
def test_send_token(mocker, test_client, init_database,
                    create_transfer_account_user, standard_user):
    recipient = create_transfer_account_user
    recipient.phone = make_kenyan_phone(recipient.phone)

    send_token_pin_authorization = UssdSessionFactory(
        state="send_token_pin_authorization",
        session_data=json.loads("{" +
                                f'"recipient_phone": "{recipient.phone}",'
                                '"transaction_amount": "10"' + "}"))

    state_machine = KenyaUssdStateMachine(send_token_pin_authorization,
                                          standard_user)
    send_token = mocker.MagicMock()
    mocker.patch('server.ussd_tasker.send_token', send_token)

    state_machine.feed_char("0000")
    assert state_machine.state == "exit_successful_send_token"
    send_token.assert_called_with(standard_user,
                                  recipient=recipient,
                                  amount=10.0)
Esempio n. 11
0
def test_terms_only_sent_once(mocker, test_client, init_database):
    session = UssdSessionFactory(state="balance_inquiry_pin_authorization")
    user = standard_user()
    user.phone = phone()

    messages = []
    def mock_send_message(phone, message):
        messages.append({'phone': phone, 'message': message})
    mocker.patch('server.message_processor.send_message', mock_send_message)

    inquire_balance = mocker.MagicMock()
    mocker.patch('server.ussd_tasker.inquire_balance', inquire_balance)

    state_machine = KenyaUssdStateMachine(session, user)
    state_machine.feed_char('0000')

    db.session.commit()

    assert len(messages) == 1

    state_machine = KenyaUssdStateMachine(session, user)
    state_machine.feed_char('0000')

    assert len(messages) == 1