def test_load_input_checking(polar, query): with pytest.raises(PolarFileExtensionError): polar.load_file("unreal.py") with pytest.raises(PolarFileExtensionError): polar.load_file(Path(__file__).parent / "unreal.py") with pytest.raises(PolarFileNotFoundError): polar.load_file(Path(__file__).parent / "unreal.polar") with pytest.raises(PolarFileExtensionError): polar.load_file(Path(__file__).parent / "unreal.pol") polar.load_file(Path(__file__).parent / "policies" / "test_api.polar")
def test_load_input_checking(polar, query): with pytest.raises(PolarApiException): polar.load_file("unreal.py") with pytest.raises(PolarApiException): polar.load_file(Path(__file__).parent / "unreal.py") with pytest.raises(PolarApiException): polar.load_file(Path(__file__).parent / "unreal.polar") with pytest.raises(PolarApiException): polar.load_file(Path(__file__).parent / "unreal.pol") polar.load_file(Path(__file__).parent / "policies" / "test_api.polar")
def test_clear(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() 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() with pytest.raises(PolarRuntimeException): polar.load_file(fails) polar.clear() 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]))
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.PolarRuntimeException) 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.PolarRuntimeException) 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() 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}]
def load_policy(polar): polar.register_class(Http) polar.register_class(PathMapper) polar.load_file(Path(__file__).parent / "policies" / "test_api.polar")