Example #1
0
def _calculate_stage_strictness(stage, test_block_config, test_spec):
    """Figure out the strictness for this stage

    Can be overridden per stage, or per test

    Priority is global (see pytest util file) <= test <= stage
    """
    stage_options = None

    if test_spec.get("strict", None) is not None:
        stage_options = test_spec["strict"]

    if stage.get("response", {}).get("strict", None) is not None:
        stage_options = stage["response"]["strict"]
    elif stage.get("mqtt_response", {}).get("strict", None) is not None:
        stage_options = stage["mqtt_response"]["strict"]

    if stage_options is not None:
        logger.debug("Overriding global strictness")
        if stage_options is True:
            strict_level = StrictLevel.all_on()
        elif stage_options is False:
            strict_level = StrictLevel.all_off()
        else:
            strict_level = StrictLevel.from_options(stage_options)

        test_block_config["strict"] = strict_level
    else:
        logger.debug("Global default strictness used for this stage")

    logger.debug("Strict key checking for this stage is '%s'",
                 test_block_config["strict"])
Example #2
0
    def test_defaults(self, section):
        level = StrictLevel([])

        if section == "json":
            assert level.setting_for(section)
        else:
            assert not level.setting_for(section)
Example #3
0
def check_strict_key(value, rule_obj, path):
    """Make sure the 'strict' key is either a bool or a list"""
    # pylint: disable=unused-argument

    if not isinstance(value, list) and not is_bool_like(value):
        raise BadSchemaError("'strict' has to be either a boolean or a list")
    elif isinstance(value, list):
        try:
            # Reuse validation here
            StrictLevel.from_options(value)
        except exceptions.InvalidConfigurationException as e:
            raise BadSchemaError from e

    # Might be a bool as well, in which case it's processed further down the line - no validation required

    return True
def test_nothing_returned_fails():
    """Raises an error if no message was received"""
    fake_client = Mock(spec=MQTTClient, message_received=Mock(return_value=None))

    expected = {"topic": "/a/b/c", "payload": "hello"}

    verifier = MQTTResponse(
        fake_client, "Test stage", expected, {"strict": StrictLevel.all_on()}
    )

    with pytest.raises(exceptions.TestFailError):
        verifier.verify(expected)

    assert not verifier.received_messages
Example #5
0
def fix_example_includes():
    includes = {
        "variables": {
            "request": {"prefix": "www.", "url": "google.com"},
            "test_auth_token": "abc123",
            "code": "def456",
            "callback_url": "www.yahoo.co.uk",
            "request_topic": "/abc",
        },
        "backends": {"mqtt": "paho-mqtt", "http": "requests"},
        "strict": StrictLevel.all_on(),
        "tavern_internal": {"pytest_hook_caller": Mock()},
    }

    return includes.copy()
Example #6
0
    def test_unset(self, section):
        level = StrictLevel.from_options([section])

        assert level.setting_for(section).setting == StrictSetting.UNSET
Example #7
0
    def test_set_off(self, section):
        level = StrictLevel.from_options([section + ":off"])

        assert level.setting_for(section).setting == StrictSetting.OFF
        assert not level.setting_for(section).is_on()
Example #8
0
    def test_defaults(self, section):
        level = StrictLevel([])

        with pytest.raises(exceptions.InvalidConfigurationException):
            level.setting_for(section)
Example #9
0
    def test_set_on(self, section):
        level = StrictLevel.from_options([section + ":on"])

        assert level.setting_for(section).setting == _StrictSetting.ON
        assert level.setting_for(section).is_on()
Example #10
0
def _load_global_strictness(pytest_config):
    """Load the global 'strictness' setting"""

    options = get_option_generic(pytest_config, "tavern-strict", [])

    return StrictLevel.from_options(options)