예제 #1
0
def test_instance_from_external_call(polar, load_policy, query):
    user = Actor(name="guest")
    resource = Widget(id="1", name="name")
    assert query(Predicate(name="allow", args=[user, "frob", resource]))

    resource = Widget(id="2", name="name")
    assert not query(Predicate(name="allow", args=[user, "frob", resource]))
예제 #2
0
파일: test_api.py 프로젝트: zmilan/oso
def test_is_allowed(polar, load_policy, query):
    actor = Actor(name="guest")
    resource = Widget(id="1")
    action = "get"
    assert query(Predicate(name="allow", args=[actor, action, resource]))
    actor = Actor(name="president")
    assert query(Predicate(name="actorInRole", args=[actor, "admin", resource]))
    assert query(Predicate(name="allowRole", args=["admin", "create", resource]))
예제 #3
0
파일: test_api.py 프로젝트: zmilan/oso
def test_cut(polar, load_policy, query):
    set_frobbed([])
    actor = Actor(name="guest")
    resource = Widget(id="1")
    action = "get"
    assert query(Predicate(name="allow_with_cut", args=[actor, action, resource]))
    assert get_frobbed() == ["Widget"]
    set_frobbed([])
    resource = DooDad(id="2")
    assert query(Predicate(name="allow_with_cut", args=[actor, action, resource]))
    assert get_frobbed() == ["DooDad"]
예제 #4
0
def test_method_resolution_order(polar, load_policy, query):
    set_frobbed([])
    actor = Actor(name="guest")
    resource = Widget(id="1")
    action = "get"
    assert query(Predicate(name="allow", args=[actor, action, resource]))
    assert get_frobbed() == ["Widget"]

    # DooDad is a Widget
    set_frobbed([])
    resource = DooDad(id="2")
    assert query(Predicate(name="allow", args=[actor, action, resource]))
    assert get_frobbed() == ["DooDad", "Widget"]
예제 #5
0
def test_patching(polar, widget_in_company, actor_in_role, load_policy, query):
    user = Actor("test")
    assert not query(
        Predicate(name="actorInRole", args=[user, "admin",
                                            Widget(id="1")]))
    with widget_in_company:
        with actor_in_role("admin"):
            assert query(
                Predicate(name="actorInRole",
                          args=[user, "admin", Widget(id="1")]))
    assert not query(
        Predicate(name="actorInRole", args=[user, "admin",
                                            Widget(id="1")]))
예제 #6
0
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]))
예제 #7
0
def test_querystring_resource_map(polar, load_policy, query):
    assert query(
        Predicate(
            name="allow",
            args=[
                Actor(name="sam"),
                "what",
                Http(path="/widget/12", query={"param": "foo"}),
            ],
        ))
    assert not query(
        Predicate(name="allow",
                  args=[Actor(name="sam"), "what",
                        Http(path="/widget/12")]))
예제 #8
0
def test_decorators(test_oso):
    actor = Actor(name="president")
    action = "create"
    resource = Company(id="1")
    assert list(
        test_oso.query(Predicate(name="allow",
                                 args=(actor, action, resource))))
예제 #9
0
파일: test_api.py 프로젝트: zmilan/oso
 def create_widget():
     if not query(
         Predicate(
             name="allow",
             args=[g.user, request.method.lower(), Http(path=request.path)],
         )
     ):
         return Response("Denied", status=403)
     return Response("Ok", status=204)
예제 #10
0
def test_query(load_file, polar, query):
    """Test that queries work with variable arguments"""

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

    assert query(Predicate(name="f", args=[Variable("a")])) == [
        {"a": 1},
        {"a": 2},
        {"a": 3},
    ]
예제 #11
0
def test_return_list(polar, query):
    class User:
        def groups(self):
            return ["engineering", "social", "admin"]

    polar.register_class(User)

    # for testing lists
    polar.load_str('allow(actor: User, "join", "party") if "social" in actor.groups();')

    assert query(Predicate(name="allow", args=[User(), "join", "party"]))
예제 #12
0
def test_datetime(polar, query):
    # test datetime comparison
    t1 = datetime(2020, 5, 25)
    t2 = datetime.now()
    t3 = datetime(2030, 5, 25)
    t4 = datetime(2020, 5, 26)

    polar.load_str("lt(a, b) if a < b;")
    assert query(Predicate("lt", [t1, t2]))
    assert not query(Predicate("lt", [t2, t1]))

    # test creating datetime from polar
    polar.load_str("dt(x) if x = new Datetime(year: 2020, month: 5, day: 25);")
    assert query(Predicate("dt", [Variable("x")])) == [{
        "x":
        datetime(2020, 5, 25)
    }]
    polar.load_str("ltnow(x) if x < Datetime.now();")
    assert query(Predicate("ltnow", [t1]))
    assert not query(Predicate("ltnow", [t3]))

    polar.load_str(
        "timedelta(a: Datetime, b: Datetime) if a.__sub__(b) == new Timedelta(days: 1);"
    )
    assert query(Predicate("timedelta", [t4, t1]))
예제 #13
0
def test_external_op(polar, query):
    class A:
        def __init__(self, a):
            self.a = a

        def __gt__(self, other):
            return self.a > other.a

        def __lt__(self, other):
            return self.a < other.a

        def __eq__(self, other):
            return self.a == other.a

    polar.register_class(A)

    a1 = A(1)
    a2 = A(2)

    polar.load_str("lt(a, b) if a < b;")
    polar.load_str("gt(a, b) if a > b;")
    assert query(Predicate("lt", [a1, a2]))
    assert not query(Predicate("lt", [a2, a1]))
    assert query(Predicate("gt", [a2, a1]))
예제 #14
0
def test_instance_cache(polar, qeval, query):
    class Counter:
        count = 0

        def __init__(self):
            self.__class__.count += 1

    polar.register_class(Counter)
    polar.load_str("f(c: Counter) if c.count > 0;")

    assert Counter.count == 0
    c = Counter()
    assert Counter.count == 1
    assert query(Predicate(name="f", args=[c]))
    assert Counter.count == 1
    assert c not in polar.host.instances.values()
예제 #15
0
def test_register_class(polar, load_policy, query):
    actor = Actor(name="guest")
    resource = Widget(id="1")
    action = "get"
    assert query(Predicate(name="allow", args=(actor, action, resource)))
예제 #16
0
def test_iter_fields(polar, load_policy, query):
    resource = Widget(id=1, name="stapler")
    actor = Actor(name="milton", id=1)
    assert query(Predicate(name="allow", args=[actor, "can_have", resource]))
예제 #17
0
def test_type_fields(polar, load_policy, query):
    resource = Widget(id=1, name="goldfish")
    actor = Actor(name="elmo", id=1, widget=resource)
    assert query(Predicate(name="allow", args=[actor, "keep", resource]))
예제 #18
0
def test_return_list(polar, load_policy, query):
    actor = Actor(name="guest")
    resource = Widget(id="1")
    action = "invite"
    assert query(Predicate(name="allow", args=[actor, action, resource]))
예제 #19
0
def test_nil(polar, query, qvar):
    """Test that nil is pre-registered as None."""
    polar.load_str("null(nil);")
    assert qvar("null(x)", "x") == [None]
    assert query(Predicate("null", [None])) == [{}]
    assert not query(Predicate("null", [[]]))
예제 #20
0
def test_iter_fields(polar, load_policy, query):
    resource = Widget(id=1, name="stapler")
    actor = Actor(name="milton", id=1)
    assert query(Predicate(name="allow", args=[actor, "can_have", resource]))
    with pytest.raises(InvalidIteratorError):
        query(Predicate(name="allow", args=[actor, "tries_to_get", resource]))
예제 #21
0
def test_predicate(polar, qvar, query):
    """Test that predicates can be converted to and from python."""
    polar.load_str("f(x) if x = pred(1, 2);")
    assert qvar("f(x)", "x") == [Predicate("pred", [1, 2])]

    assert query(Predicate(name="f", args=[Predicate("pred", [1, 2])])) == [{}]