cic-internal-integration/apps/cic-ussd/tests/fixtures/config.py

108 lines
3.5 KiB
Python
Raw Normal View History

2021-02-06 16:13:47 +01:00
# standard imports
import logging
import os
# third party imports
2021-08-06 18:29:01 +02:00
import i18n
2021-02-06 16:13:47 +01:00
import pytest
from chainlib.chain import ChainSpec
2021-02-06 16:13:47 +01:00
from confini import Config
# local imports
2021-08-06 18:29:01 +02:00
from cic_ussd.account.chain import Chain
from cic_ussd.account.guardianship import Guardianship
from cic_ussd.encoder import PasswordEncoder
2021-02-06 16:13:47 +01:00
from cic_ussd.files.local_files import create_local_file_data_stores, json_file_parser
from cic_ussd.menu.ussd_menu import UssdMenu
2021-08-06 18:29:01 +02:00
from cic_ussd.phone_number import E164Format, Support
2021-02-06 16:13:47 +01:00
from cic_ussd.state_machine import UssdStateMachine
from cic_ussd.translation import generate_locale_files, Languages
2021-08-06 18:29:01 +02:00
from cic_ussd.validator import validate_presence
2021-02-06 16:13:47 +01:00
2021-08-06 18:29:01 +02:00
logg = logging.getLogger(__name__)
2021-02-06 16:13:47 +01:00
fixtures_dir = os.path.dirname(__file__)
root_directory = os.path.dirname(os.path.dirname(fixtures_dir))
@pytest.fixture(scope='session')
def alembic_config():
migrations_directory = os.path.join(root_directory, 'cic_ussd', 'db', 'migrations', 'default')
file = os.path.join(migrations_directory, 'alembic.ini')
return {
'file': file,
'script_location': migrations_directory
}
@pytest.fixture(scope='function')
2021-08-06 18:29:01 +02:00
def init_state_machine(load_config):
UssdStateMachine.states = json_file_parser(filepath=load_config.get('MACHINE_STATES'))
UssdStateMachine.transitions = json_file_parser(filepath=load_config.get('MACHINE_TRANSITIONS'))
2021-02-06 16:13:47 +01:00
@pytest.fixture(scope='function')
def load_languages(init_cache, load_config):
validate_presence(load_config.get('LANGUAGES_FILE'))
Languages.load_languages_dict(load_config.get('LANGUAGES_FILE'))
languages = Languages()
languages.cache_system_languages()
2021-08-06 18:29:01 +02:00
@pytest.fixture(scope='function')
def load_chain_spec(load_config):
chain_spec = ChainSpec.from_chain_str(load_config.get('CHAIN_SPEC'))
2021-08-06 18:29:01 +02:00
Chain.spec = chain_spec
2021-02-06 16:13:47 +01:00
2021-08-06 18:29:01 +02:00
@pytest.fixture(scope='session')
def load_config():
config_directory = os.path.join(root_directory, 'config/test')
config = Config(default_dir=config_directory)
config.process()
logg.debug('config loaded\n{}'.format(config))
return config
2021-02-06 16:13:47 +01:00
@pytest.fixture(scope='function')
2021-08-06 18:29:01 +02:00
def load_e164_region(load_config):
E164Format.region = load_config.get('E164_REGION')
2021-02-06 16:13:47 +01:00
2021-08-06 18:29:01 +02:00
@pytest.fixture(scope='session')
def load_support_phone(load_config):
Support.phone_number = load_config.get('OFFICE_SUPPORT_PHONE')
2021-02-06 16:13:47 +01:00
2021-08-06 18:29:01 +02:00
@pytest.fixture
def load_ussd_menu(load_config):
ussd_menu_db = create_local_file_data_stores(file_location=load_config.get('USSD_MENU_FILE'), table_name="ussd_menu")
UssdMenu.ussd_menu_db = ussd_menu_db
@pytest.fixture(scope='function')
2021-08-06 18:29:01 +02:00
def set_fernet_key(load_config):
PasswordEncoder.set_key(load_config.get('APP_PASSWORD_PEPPER'))
@pytest.fixture(scope='function')
def setup_guardianship(load_config):
guardians_file = os.path.join(root_directory, load_config.get('SYSTEM_GUARDIANS_FILE'))
validate_presence(guardians_file)
Guardianship.load_system_guardians(guardians_file)
@pytest.fixture(scope="session")
def set_locale_files(load_config, tmpdir_factory):
tmpdir = tmpdir_factory.mktemp("var")
tmpdir_path = str(tmpdir)
validate_presence(tmpdir_path)
import cic_translations
package_path = cic_translations.__path__
schema_files = os.path.join(package_path[0], load_config.get("SCHEMA_FILE_PATH"))
generate_locale_files(locale_dir=tmpdir_path,
schema_file_path=schema_files,
translation_builder_path=load_config.get('LOCALE_FILE_BUILDERS'))
i18n.load_path.append(tmpdir_path)
2021-08-06 18:29:01 +02:00
i18n.set('fallback', load_config.get('LOCALE_FALLBACK'))