Пример #1
0
def test_ensure_dir_bad_base_dir(app):
    os.rmdir(app.config['DATA_DIR'])
    with open(app.config['DATA_DIR'], "w") as f:
        f.write("FOO\n")
    with pytest.raises(NotADirectoryError) as ex:
        ensure_dir(app, "test", "DATA_DIR", "bar", mode=0x700)
    assert "(DATA_DIR) is not a directory" in str(ex.value)
Пример #2
0
def test_ensure__dir_missing_base_dir(app, caplog):
    os.rmdir(app.config['DATA_DIR'])
    with pytest.raises(FileNotFoundError) as ex:
        ensure_dir(app, "test", "DATA_DIR", "foo", mode=0x700)
    assert (
        f"Directory '{app.config['DATA_DIR']}' (DATA_DIR) does not exist"
        in str(ex.value)
    )
Пример #3
0
def test_ensure_dir_err(app, tmp_path, monkeypatch):
    def fake_makedirs(name, mode=0o777, exist_ok=False):
        raise FileNotFoundError(errno.ENOENT, "BOO", name)

    monkeypatch.setattr(os, 'makedirs', fake_makedirs)
    d = os.path.join(app.config["ROOT_DIR"], "foo", "bar")
    with pytest.raises(FileNotFoundError) as ex:
        ensure_dir(app, "test", "ROOT_DIR", "foo", "bar")
    assert "Error creating test directory " in str(ex.value)
    assert f": BOO ({errno.ENOENT})" in str(ex.value)
    assert not os.path.exists(d)
Пример #4
0
def test_ensure_dir_ok(app, tmp_path):
    parent = os.path.join(app.config["ROOT_DIR"], "foo")
    d = os.path.join(parent, "bar")
    mode = 0o700
    umask = os.umask(0)
    d0 = ensure_dir(app, "test", "ROOT_DIR", "foo", "bar", mode=mode)
    os.umask(umask)
    assert d == d0
    for path, wanted_mode in ((parent, 0o777), (d, mode)):
        p_mode = os.stat(path).st_mode
        assert stat.S_ISDIR(p_mode) != 0
        assert stat.S_IMODE(p_mode) == wanted_mode
Пример #5
0
def _make_mako_lookup(app):
    # Make a Mako `TemplateLookup` from the app configuration.

    mod_dir = ensure_dir(app,
                         "Mako template cache",
                         "DATA_DIR",
                         "template-cache",
                         mode=0o770)

    kwargs = {
        'input_encoding':
        'utf-8',
        'output_encoding':
        'utf-8',
        'default_filters':
        app.config['MAKO_DEFAULT_FILTERS'],
        'imports': [
            ('from flask_babel import gettext as _, ngettext, '
             'pgettext, npgettext'),
        ],
        # `module_directory` points to a directory that is used to
        # cache Mako templates that have been compiled to Python code.
        'module_directory':
        mod_dir,
    }

    # Tokens can come with their own Mako templates, all of which are
    # in the `tokens` folder, but it seems overkill to add an
    # otherwise-empty blueprint for that just so the very contrived
    # blueprint-scanning code can find it. It's easier to add the folder
    # here and omit the blueprint-scanning loop (see below).

    dirs = [
        os.path.join(app.root_path, app.template_folder),
        os.path.join(app.root_path, "tokens")
    ]
    custom_templates_dir = app.config["CUSTOM_TEMPLATES_DIR"]
    if custom_templates_dir is not None:
        dirs.insert(0, custom_templates_dir)

    # We don't bother with `template_folder` directories of blueprints
    # because we're not using that feature in LinOTP (yet anyway).

    return TemplateLookup(directories=dirs, **kwargs)
Пример #6
0
def test_ensure_dir_bad_config(app, var):
    var = "FLORP_DIR"
    with pytest.raises(KeyError) as ex:
        ensure_dir(app, "test", var, "")
    assert f"Invalid LinOTP configuration setting '{var}'" in str(ex.value)