def test_render_string():
    data_dict = {
        'processor': 'template.render_string',
        'name': 'datastream_label',
        'test_var': 'test_val',
        'value': '{{ test_var }}',
        'on_fail': 500
    }

    # Test for positive scenario
    with app.app_context():
        evaluation = template.render_string(data_dict)
        assert isinstance(evaluation, str)
        assert evaluation == 'test_val'

        # Test for invalid template passed
        data_dict['value'] = "{{ forgot to close"
        evaluation = template.render_string(data_dict)
        assert evaluation is None

        # Test for invalid variable in valid jinja
        data_dict['value'] = "{{ invalid_var_name }}"
        evaluation = template.render_string(data_dict)
        assert isinstance(evaluation, str)
        assert evaluation == ''

        # Test not providing a 'value'
        del data_dict['value']
        evaluation = template.render_string(data_dict)
        assert evaluation is None
Пример #2
0
def app_context():
    """
    Create a flask app context if not already present.
    Use example:
        with context.app_context():
            ...
    """
    class NullContext: # pylint: disable=all
        def __enter__(self): return None
        def __exit__(self, exc_type, exc_value, traceback): return None

    return app.app_context() if not has_app_context() else NullContext()
Пример #3
0
def test_load_matched_json():
    # Test matching etd config file successfully
    data_dict = {
        'location': 'config/metadata_1',
        'view_args': {
            'namespace': 'etd'
        },
        'item': {
            'model_type': 'info:fedora/islandora:sp_pdf'
        }
    }
    with app.app_context():
        file_data = file.load_matched_json(data_dict)
        assert isinstance(file_data, dict)
        assert file_data
        assert 'test_filename' in file_data
        assert file_data['test_filename'] == "etd.json"

    # Test loading directory with invalid match conditions
    data_dict = {
        'location': 'config/metadata_2',
        'view_args': {
            'namespace': 'etd'
        },
        'item': {
            'model_type': 'info:fedora/islandora:sp_pdf'
        }
    }
    with app.app_context():
        file_data = file.load_matched_json(data_dict)
        assert not isinstance(file_data, dict)
        assert not file_data

    # Testing the on_fail functionality in load_matched_json when the config path is invalid
    data_dict['location'] = "metadata_config_invalid_path"
    data_dict['on_fail'] = 404
    with app.app_context():
        file_data = file.load_matched_json(data_dict)
        assert file_data is None  # expect None return, as base processor will throw the abort
Пример #4
0
def test_render_template_string():
    # Test if the template variables are rendered as expected
    context = {"var1": "val1", "var2": "val2", "date": "3029-01-01"}
    with app.app_context():
        assert template.render_template_string("{{ var1 }}", context) == "val1"
        assert template.render_template_string("{{ var3 }}", context) == ""
        assert template.render_template_string("{{ date | datepassed }}",
                                               context) == "False"
        assert template.render_template_string("{{ var2 | myfilter }}",
                                               context) == "myval2"

        # Test raising a template error
        with raises(TemplateError):
            template.render_template_string("{{ var1 }", context)
def test_conditions():
    data_dict = {
        "name": "restriction",
        "processor": "evaluate.conditions",
        "on_fail": 401,
        "conditions": "metadata_conf.restriction_conditions",
        "match_all": True,
        "metadata_conf": {
            "restriction_conditions": [{
                "evaluate": "test",
                "match_when": ["test"]
            }]
        }
    }

    with app.app_context():
        # Test for positive evalution match in the data_dict
        evaluation = evaluate.conditions(data_dict)
        assert evaluation is True

    # Test for negative unmatched condition
    del data_dict['metadata_conf']
    data_dict['conditions'] = "other_conf.other_cons"
    data_dict['other_conf'] = {
        "other_cons": [{
            "evaluate": "notmatched",
            "match_when": ["will", "fail"]
        }]
    }
    with app.app_context():
        evaluation = evaluate.conditions(data_dict)
        assert evaluation is False

    # Test for matching only 1 of multiple conditions with match_all = True
    data_dict['other_conf'] = {
        "other_cons": [{
            "evaluate": "test",
            "match_when": ["test"]
        }, {
            "evaluate": "notmatched",
            "match_when": ["will", "fail"]
        }]
    }
    with app.app_context():
        evaluation = evaluate.conditions(data_dict)
        assert evaluation is False

        # Test for matching any of multiple conditions
        data_dict['match_all'] = False
        evaluation = evaluate.conditions(data_dict)
        assert evaluation is True

        # Test for abort on match
        data_dict['abort_on_match'] = True
        evaluation = evaluate.conditions(data_dict)
        assert evaluation is None

        # Test for missing definition of 'match_all'
        del data_dict['match_all']
        evaluation = evaluate.conditions(data_dict)
        assert evaluation is None

        # Test for missing 'conditions' definitions
        data_dict['match_all'] = False
        del data_dict['conditions']
        evaluation = evaluate.conditions(data_dict)
        assert evaluation is None

        # Test referencing an invalid condition key
        data_dict['conditions'] = "other_conf.invalid_key"
        evaluation = evaluate.conditions(data_dict)
        assert evaluation is None

        # Test for missing conditions 'evaluate'/'match_when' dict
        data_dict['conditions'] = "other_conf.other_cons"
        del data_dict['other_conf']
        evaluation = evaluate.conditions(data_dict)
        assert evaluation is None
Пример #6
0
def test_evaluate_conditions():
    conditions = [{
        "evaluate":
        "{{ item.embargo_end_date_ss | head | datepassed if item.embargo_end_date_ss is defined else True }}",
        "match_when": ["False"]
    }, {
        "evaluate":
        "{{ 'embargoed' if item.embargo_datastream_ss is not defined or datastream_label in item.embargo_datastream_ss else 'accessible' }}",
        "match_when": ["embargoed"]
    }]
    context = {
        "item": {
            "embargo_end_date_ss": ["3029-01-01"],
            "embargo_datastream_ss": ["OBJ", "FULL_TEXT"]
        },
        "datastream_label": "OBJ"
    }
    with app.app_context():
        assert template.evaluate_conditions(conditions,
                                            context,
                                            match_all=True) == 2

        # change the datastream_label to an allowed datastream
        context["datastream_label"] = "TN"
        assert template.evaluate_conditions(conditions,
                                            context,
                                            match_all=True) == 0
        assert template.evaluate_conditions(conditions,
                                            context,
                                            match_all=False) == 1

    # invalid keys in conditions
    conditions = [{
        "value": "{{ Value is not an allowed key }}",
        "allowed": ["False"]
    }, {
        "value": "{{ Value is not an allowed key }}",
        "allowed": ["embargoed"]
    }]
    with app.app_context():
        with raises(KeyError):
            template.evaluate_conditions(conditions, context, match_all=True)

        # when conditions are empty
        conditions = []
        assert template.evaluate_conditions(conditions,
                                            context,
                                            match_all=True) == 0

    # when all the conditions dont match
    conditions = [{
        "evaluate":
        "{{ item.embargo_end_date_ss | head | datepassed if item.embargo_end_date_ss is defined else True }}",
        "match_when": ["True"]
    }, {
        "evaluate":
        "{{ 'embargoed' if item.embargo_datastream_ss is not defined or datastream_label in item.embargo_datastream_ss else 'accessible' }}",
        "match_when": ["embargoed"]
    }]
    with app.app_context():
        assert template.evaluate_conditions(conditions,
                                            context,
                                            match_all=True) == 0

    # use of match_when_not
    conditions = [{
        "evaluate": "{{ datastream_label }}",
        "match_when_not": ["TN"]
    }]
    with app.app_context():
        assert template.evaluate_conditions(conditions,
                                            context,
                                            match_all=True) == 0
        conditions[0]['match_when_not'] = ['OBJ']
        assert template.evaluate_conditions(conditions,
                                            context,
                                            match_all=True) == 1

    # attempted use of both match_when and match_when_not
    conditions = [{
        "evaluate": "{{ datastream_label }}",
        "match_when_not": ["TN"],
        "match_when": ["OBJ"]
    }]
    with app.app_context():
        with raises(KeyError):
            template.evaluate_conditions(conditions, context, match_all=True)