Beispiel #1
0
def test_mw_config_wrong_rule_condition():
    mw_config = {
        'TOKEN': 'token',
        'MIDDLEWARE': {
            'CHAT_ID': -1001339325227,
            'RULES': [{
                'view': 'reports-fail',
                'trigger_codes': [204],
                'conditions': {
                    'type': '',
                    'field': 'template.status',
                    'field_value': 'blocked',
                },
                'message': 'Template is blocked due to report failed',
            }],
        },
    }

    c = TelegramBotConfigurator(mw_config, [MW_DEF])

    with pytest.raises(ImproperlyConfigured) as err:
        c._check_mw_settings()

    err_expected = (
        '"MIDDLEWARE[RULES]" position "0" error: Condition "type"'
        ' key must be one of "[\'function\', \'value\']"'
    )

    assert err_expected == str(err.value)
Beispiel #2
0
def test_mw_config_wrong_rule_trigger_codes_not_all_integers():
    mw_config = {
        'TOKEN': 'token',
        'MIDDLEWARE': {
            'CHAT_ID': -1001339325227,
            'RULES': [{
                'view': 'view',
                'trigger_codes': [1, 2, "3"],
                'conditions': {
                    'type': 'value',
                    'field': 'template.status',
                    'field_value': 'blocked',
                },
                'message': 'msg',
            }],
        },
    }

    c = TelegramBotConfigurator(mw_config, [MW_DEF])

    with pytest.raises(ImproperlyConfigured) as err:
        c._check_mw_settings()

    err_expected = (
        '"MIDDLEWARE[RULES]" position "0" error: '
        '"trigger_codes" contains non-integer values'
    )

    assert err_expected == str(err.value)
Beispiel #3
0
def test_mw_config_wrong_rule_message_empty():
    mw_config = {
        'TOKEN': 'token',
        'MIDDLEWARE': {
            'CHAT_ID': -1001339325227,
            'RULES': [{
                'view': 'view',
                'trigger_codes': [204],
                'conditions': {
                    'type': 'value',
                    'field': 'template.status',
                    'field_value': 'blocked',
                },
                'message': '',
            }],
        },
    }

    c = TelegramBotConfigurator(mw_config, [MW_DEF])

    with pytest.raises(ImproperlyConfigured) as err:
        c._check_mw_settings()

    err_expected = '"MIDDLEWARE[RULES]" position "0" error: "message" key has not been set'

    assert err_expected == str(err.value)
Beispiel #4
0
def test_condition_config_function_ok():
    c = TelegramBotConfigurator({}, [])
    condition = {
        'type': 'function',
        'function': 'tests.test_configurator.cond_fn',
    }

    assert c._check_mw_config_rule_condition(condition) is None
Beispiel #5
0
def test_condition_config_value_ok():
    c = TelegramBotConfigurator({}, [])
    condition = {
        'type': 'value',
        'field': 'f1',
        'field_value': 'f1_value',
    }

    assert c._check_mw_config_rule_condition(condition) is None
Beispiel #6
0
def test_condition_config_type_value_no_field_value():
    c = TelegramBotConfigurator({}, [])
    condition = {
        'type': 'value',
        'field': 'f',
    }

    with pytest.raises(ImproperlyConfigured) as err:
        c._check_mw_config_rule_condition(condition)

    assert 'Condition "field_value" key must be set' == str(err.value)
Beispiel #7
0
def test_mw_config_no_mw_key():
    mw_config = {
        'TOKEN': 'token',
    }

    c = TelegramBotConfigurator(mw_config, [MW_DEF])

    with pytest.raises(ImproperlyConfigured) as err:
        c._check_mw_settings()

    assert f'"{MW_DEF}" is enabled, however "MIDDLEWARE" key has not been set.' == str(err.value)
Beispiel #8
0
def test_condition_config_no_type():
    c = TelegramBotConfigurator({}, [])
    condition = {
        'field': 'f1',
        'field_value': 'f1_value',
    }

    with pytest.raises(ImproperlyConfigured) as err:
        c._check_mw_config_rule_condition(condition)

    assert 'Condition "type" key has not been set' == str(err.value)
Beispiel #9
0
def test_condition_config_type_empty():
    c = TelegramBotConfigurator({}, [])
    condition = {
        'type': '',
        'field': 'f1',
        'field_value': 'f1_value',
    }

    with pytest.raises(ImproperlyConfigured) as err:
        c._check_mw_config_rule_condition(condition)

    assert 'Condition "type" key must be one of "[\'function\', \'value\']"' == str(err.value)
Beispiel #10
0
def test_condition_config_function_no_function():
    c = TelegramBotConfigurator({}, [])
    condition = {
        'type': 'function',
    }

    with pytest.raises(ImproperlyConfigured) as err:
        c._check_mw_config_rule_condition(condition)

    assert str((
        'Condition "function" key must be set and have value. ',
        'Or specified function could be found.',
    )) == str(err.value)
Beispiel #11
0
def test_mw_config_no_rules():
    mw_config = {
        'TOKEN': 'token',
        'MIDDLEWARE': {
            'CHAT_ID': 123,
        },
    }

    c = TelegramBotConfigurator(mw_config, [MW_DEF])

    with pytest.raises(ImproperlyConfigured) as err:
        c._check_mw_settings()

    assert '"MIDDLEWARE[RULES]" key has not been set.' == str(err.value)
Beispiel #12
0
 def ready(self):
     telegram_settings = getattr(settings, 'TELEGRAM_BOT', None)
     if not telegram_settings:
         raise ImproperlyConfigured(
             "Error obtaining telegram bot settings. ",
             "Please check the documentation on how to configure settings.py ",
             "in your project. TELEGRAM_BOT object is missing.",
         )
     if not isinstance(telegram_settings, dict):
         raise ImproperlyConfigured(
             "TELEGRAM_BOT object must be a dictionary.",
         )
     checker = TelegramBotConfigurator(telegram_settings, settings.MIDDLEWARE)
     checker.run_check()
Beispiel #13
0
def test_mw_config_ok_rule_no_conditions():
    mw_config = {
        'TOKEN': 'token',
        'MIDDLEWARE': {
            'CHAT_ID': -1001339325227,
            'RULES': [{
                'view': 'view',
                'trigger_codes': [1, 2],
                'message': 'msg',
            }],
        },
    }

    c = TelegramBotConfigurator(mw_config, [MW_DEF])

    assert c._check_mw_settings() is None
Beispiel #14
0
def test_mw_config_not_enabled():
    mw_config = {
        'TOKEN': 'token',
        'CHAT_ID': -1001339325227,
        'CONFIG': [{
            'view': 'reports-fail',
            'trigger_codes': [204],
            'conditions': {
                'type': 'value',
                'field': 'template.status',
                'field_value': 'blocked',
            },
            'message': 'Template is blocked due to report failed',
        }],
    }

    c = TelegramBotConfigurator(mw_config, [])

    assert c._check_mw_settings() is None
Beispiel #15
0
def test_global_config_conversations_not_empty():
    mw_config = {
        'TOKEN': 'token',
        'COMMANDS_SUFFIX': '',
        'HISTORY_LOOKUP_MODEL_PROPERTY': 'prop',
        'CONVERSATIONS': ['test'],
        'MIDDLEWARE': {
            'CHAT_ID': -1001339325227,
            'RULES': [{
                'view': 'view',
                'trigger_codes': [1, 2],
                'message': 'msg',
            }],
        },
    }

    c = TelegramBotConfigurator(mw_config, [MW_DEF])

    assert c.run_check() is None
Beispiel #16
0
def test_global_config_no_conversations():
    mw_config = {
        'TOKEN': 'token',
        'COMMANDS_SUFFIX': '',
        'HISTORY_LOOKUP_MODEL_PROPERTY': 'prop',
        'MIDDLEWARE': {
            'CHAT_ID': -1001339325227,
            'RULES': [{
                'view': 'view',
                'trigger_codes': [1, 2],
                'message': 'msg',
            }],
        },
    }

    c = TelegramBotConfigurator(mw_config, [MW_DEF])

    with pytest.raises(ImproperlyConfigured) as err:
        c.run_check()

    assert '"CONVERSATIONS" key has not been set.' == str(err.value)
Beispiel #17
0
def test_mw_config_no_chat_id():
    mw_config = {
        'TOKEN': 'token',
        'MIDDLEWARE': {
            'RULES': [{
                'view': 'reports-fail',
                'trigger_codes': [204],
                'conditions': {
                    'type': 'value',
                    'field': 'template.status',
                    'field_value': 'blocked',
                },
                'message': 'Template is blocked due to report failed',
            }],
        },
    }

    c = TelegramBotConfigurator(mw_config, [MW_DEF])

    with pytest.raises(ImproperlyConfigured) as err:
        c._check_mw_settings()

    assert '"MIDDLEWARE[CHAT_ID]" key has not been set.' == str(err.value)