Esempio n. 1
0
def test_clear_rules(polar, query):
    class Test:
        pass

    polar.register_class(Test)
    polar.load_str("f(x) if x = 1;")
    assert len(query("f(1)")) == 1
    assert len(query("x = new Test()")) == 1
    polar.clear_rules()
    assert len(query("f(1)")) == 0
    assert len(query("x = new Test()")) == 1
Esempio n. 2
0
def test_clear_rules(polar, load_policy, query):
    old = Path(__file__).parent / "policies" / "load.pol"
    fails = Path(__file__).parent / "policies" / "reload_fail.pol"
    new = Path(__file__).parent / "policies" / "reload.pol"

    polar.clear_rules()
    polar.load_file(old)

    actor = Actor(name="milton", id=1)
    resource = Widget(id=1, name="thingy")
    assert query(Predicate(name="allow", args=[actor, "make", resource]))
    assert query(Predicate(name="allow", args=[actor, "get", resource]))
    assert query(Predicate(name="allow", args=[actor, "edit", resource]))
    assert query(Predicate(name="allow", args=[actor, "delete", resource]))

    # raises exception because new policy file specifies on a class defined in the old file,
    # but not in the new file
    polar.clear_rules()
    with pytest.raises(PolarRuntimeError):
        polar.load_file(fails)

    polar.clear_rules()
    polar.load_file(new)
    assert query(Predicate(name="allow", args=[actor, "make", resource]))
    assert not query(Predicate(name="allow", args=[actor, "get", resource]))
    assert not query(Predicate(name="allow", args=[actor, "edit", resource]))
    assert not query(Predicate(name="allow", args=[actor, "delete", resource]))
Esempio n. 3
0
def test_load_function(polar, query, qvar):
    """Make sure the load function works."""
    # Loading the same file twice doesn't mess stuff up.
    polar.load_file(Path(__file__).parent / "test_file.polar")
    with pytest.raises(exceptions.PolarRuntimeError) as e:
        polar.load_file(Path(__file__).parent / "test_file.polar")
    assert (
        str(e.value)
        == f"Problem loading file: File {Path(__file__).parent}/test_file.polar has already been loaded."
    )
    with pytest.raises(exceptions.PolarRuntimeError) as e:
        polar.load_file(Path(__file__).parent / "test_file_renamed.polar")
    assert (
        str(e.value)
        == f"Problem loading file: A file with the same contents as {Path(__file__).parent}/test_file_renamed.polar named {Path(__file__).parent}/test_file.polar has already been loaded."
    )
    assert query("f(x)") == [{"x": 1}, {"x": 2}, {"x": 3}]
    assert qvar("f(x)", "x") == [1, 2, 3]

    polar.clear_rules()
    polar.load_file(Path(__file__).parent / "test_file.polar")
    polar.load_file(Path(__file__).parent / "test_file_gx.polar")
    assert query("f(x)") == [{"x": 1}, {"x": 2}, {"x": 3}]
    assert query("g(x)") == [{"x": 1}, {"x": 2}, {"x": 3}]