示例#1
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]))
示例#2
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")]))
示例#3
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]))
示例#4
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]))
示例#5
0
def test_instance_initialization(polar, query, qvar):
    # test round trip through kb query
    user = Actor("sam")
    env = query('new Actor{name:"sam"} = returned_user')[0]
    assert polar.host.to_python(env["returned_user"]) == user

    env = query('new Actor(name:"sam") = returned_user')[0]
    assert polar.host.to_python(env["returned_user"]) == user
示例#6
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"]
示例#7
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"]
示例#8
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")]))
示例#9
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)))
示例#10
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]))
示例#11
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]))
示例#12
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]))
示例#13
0
def test_instance_round_trip(polar, query, qvar):
    # direct round trip
    user = Actor("sam")
    assert polar.host.to_python(polar.host.to_polar(user)) is user
示例#14
0
 def set_user():
     g.user = Actor(name=request.headers["username"])
示例#15
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]))