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
def test_kenya_state_machine(test_client, init_database, standard_user, session_factory, user_input, expected): session = session_factory() session.session_data = { 'transfer_usage_mapping': fake_transfer_mapping(10), 'usage_menu': 1, 'usage_index_stack': [0, 8] } db.session.commit() state_machine = KenyaUssdStateMachine(session, standard_user) state_machine.feed_char(user_input) assert state_machine.state == expected
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_kenya_state_machine(test_client, init_database, user_factory, session_factory, user_input, expected): from flask import g g.active_organisation = OrganisationFactory(country_code='AU') session = session_factory() session.session_data = { 'transfer_usage_mapping': fake_transfer_mapping(10), 'usage_menu': 1, 'usage_index_stack': [0, 8] } user = user_factory() user.phone = phone() db.session.commit() state_machine = KenyaUssdStateMachine(session, user) state_machine.feed_char(user_input) assert state_machine.state == expected