def test_model_registration(): """Test that models are automatically registered with the policy.""" from test_app import models from oso import Variable assert (next( Oso.query_rule("models", models.TestRegistration(), Variable("x")))["bindings"]["x"] == 1) assert (next( Oso.query_rule("models", models.TestRegistration2(), Variable("x")))["bindings"]["x"] == 2)
def straightHelper(numbers): temp1 = numbers.copy() temp2 = numbers.copy() straightQuery = list( oso.query_rule("straight", temp1, temp2, Variable("top"))) if len(straightQuery) == 0: return 0 else: return straightQuery[0]['bindings']['top']
def compare_expr(expr: Expression, model: Model, path=(), **kwargs): assert expr.operator in COMPARISONS (left, right) = expr.args left_path = dot_path(left) if left_path: return COMPARISONS[expr.operator]("__".join(path + left_path), right) else: assert left == Variable("_this") if not isinstance(right, model): return FALSE_FILTER if expr.operator not in ("Eq", "Unify"): raise UnsupportedError( f"Unsupported comparison: {expr}. Models can only be compared" " with `=` or `==`") return COMPARISONS[expr.operator]("__".join(path + ("pk", )), right.pk)
def setHand(cards): #Establish the pattern and the top 5 cards #fiveCards = [] list_of_all_cards = cards list_of_all_faces = toNumbers(cards) list_of_all_suits = toSuits(cards) duplicate_list_of_all_faces = toNumbers(cards) top_cards = [] pattern = 0 #: Pattern check_for_flush = "" polar_result = list( oso.query_rule("hand", list_of_all_cards, list_of_all_faces, duplicate_list_of_all_faces, list_of_all_suits, Variable("result"))) #POLAR QUERIES if len(polar_result) > 0: #print(polar_result) result = polar_result[0]["bindings"]["result"] for i in range(len(result)): if i == 0: pattern = result[i] #print(pattern) check_for_flush = str(pattern) #print(check_for_flush) else: top_cards.append(result[i]) #print("(" + str(result[i].number) + "," + result[i].suit + ")" + "\n") else: print( "Unexpected Error occurred while running the Poker Hand Analyzer") return Hand(cards, Pattern.HighCard, top_cards) if "Pattern.Flush" == check_for_flush: def sort_flush(x): return x.number top_cards.sort(key=sort_flush, reverse=True) return Hand(cards, pattern, top_cards)
def test_enable_roles(test_db_session, oso_with_session, john, ringo, abbey_road, beatles): oso = oso_with_session enable_roles(oso) # Get test data read_repo_role = (test_db_session.query(RepositoryRole).filter_by( user=john, repository=abbey_road).first()) org_owner_role = (test_db_session.query(OrganizationRole).filter_by( user=john, organization=beatles).first()) # test base `resource_role_applies_to` results = list( oso.query_rule("resource_role_applies_to", abbey_road, Variable("role_resource"))) assert len(results) == 1 assert results[0].get("bindings").get("role_resource") == abbey_road # test custom `resource_role_applies_to` rules (for nested resources) resource_role_applies_to_str = """resource_role_applies_to(repo: Repository, parent_org) if parent_org = repo.organization and parent_org matches Organization; """ oso.load_str(resource_role_applies_to_str) results = list( oso.query_rule("resource_role_applies_to", abbey_road, Variable("role_resource"))) results.sort(key=lambda x: x.get("bindings").get("role_resource").name) assert len(results) == 2 assert results[0].get("bindings").get("role_resource") == abbey_road assert results[1].get("bindings").get("role_resource") == beatles # test `user_in_role` for RepositoryRole results = list( oso.query_rule("user_in_role", john, Variable("role"), abbey_road)) assert len(results) == 1 assert results[0].get("bindings").get("role").name == "READ" # test `user_in_role` for OrganizationRole results = list( oso.query_rule("user_in_role", john, Variable("role"), beatles)) assert len(results) == 1 assert results[0].get("bindings").get("role").name == "OWNER" # test `inherits_role` and `resource_role_order` # make sure `inherits_role` returns nothing without a role order rule results = list( oso.query_rule("inherits_role", org_owner_role, Variable("inherited_role"))) assert len(results) == 0 # test role_order rule role_order_str = 'organization_role_order(["OWNER", "MEMBER", "BILLING"]);' oso.load_str(role_order_str) results = list( oso.query_rule("inherits_role", org_owner_role, Variable("inherited_role"))) results.sort(key=lambda x: x.get("bindings").get("inherited_role").name) assert len(results) == 2 assert results[0].get("bindings").get("inherited_role").name == "BILLING" assert results[1].get("bindings").get("inherited_role").name == "MEMBER" # make sure this query fails before any rules are added results = list(oso.query_rule("role_allow", john, "READ", abbey_road)) assert len(results) == 0 # test basic `role_allow` rule role_allow_str = ( 'role_allow(role: RepositoryRole{name: "READ"}, "READ", repo: Repository);' ) oso.load_str(role_allow_str) results = list( oso.query_rule("role_allow", read_repo_role, "READ", abbey_road)) assert len(results) == 1 # test `role_allow` rule using nested resource nested_role_allow_str = ( 'role_allow(role: OrganizationRole{name: "MEMBER"}, "READ", repo: Repository);' ) oso.load_str(nested_role_allow_str) results = list( oso.query_rule("role_allow", org_owner_role, "READ", abbey_road)) assert len(results) == 1 # test top-level `allow` results = list(oso.query_rule("allow", john, "READ", abbey_road)) assert len(results) == 2 results = list(oso.query_rule("allow", ringo, "READ", abbey_road)) assert len(results) == 1
# Test built-in type specializers. assert list(oso.query('builtinSpecializers(true, "Boolean")')) assert not list(oso.query('builtinSpecializers(false, "Boolean")')) assert list(oso.query('builtinSpecializers(2, "Integer")')) assert list(oso.query('builtinSpecializers(1, "Integer")')) assert not list(oso.query('builtinSpecializers(0, "Integer")')) assert not list(oso.query('builtinSpecializers(-1, "Integer")')) assert list(oso.query('builtinSpecializers(1.0, "Float")')) assert not list(oso.query('builtinSpecializers(0.0, "Float")')) assert not list(oso.query('builtinSpecializers(-1.0, "Float")')) assert list(oso.query('builtinSpecializers(["foo", "bar", "baz"], "List")')) assert not list(oso.query('builtinSpecializers(["bar", "foo", "baz"], "List")')) assert list(oso.query('builtinSpecializers({foo: "foo"}, "Dictionary")')) assert not list(oso.query('builtinSpecializers({foo: "bar"}, "Dictionary")')) assert list(oso.query('builtinSpecializers("foo", "String")')) assert not list(oso.query('builtinSpecializers("bar", "String")')) # Test deref works oso.load_str("?= x = 1 and E.sum([x, 2, x]) = 4 and [3, 2, x].index(1) = 2;") # Test unspecialized rule ordering result = oso.query_rule("testUnspecializedRuleOrder", "foo", Variable("y")) assert next(result)["bindings"]["y"] == 1 assert next(result)["bindings"]["y"] == 2 result = oso.query_rule("testUnspecializedRuleOrder", "foo", Variable("x")) assert next(result)["bindings"]["x"] == 1 assert next(result)["bindings"]["x"] == 2 result = oso.query_rule("testUnspecializedRuleOrder", "foo", "bar", Variable("z")) assert next(result)["bindings"]["z"] == 1 assert next(result)["bindings"]["z"] == 2
assert not list(oso.query('builtinSpecializers(-1.0, "Float")')) assert list(oso.query('builtinSpecializers(["foo", "bar", "baz"], "List")')) assert not list( oso.query('builtinSpecializers(["bar", "foo", "baz"], "List")')) assert list(oso.query('builtinSpecializers({foo: "foo"}, "Dictionary")')) assert not list(oso.query('builtinSpecializers({foo: "bar"}, "Dictionary")')) assert list(oso.query('builtinSpecializers("foo", "String")')) assert not list(oso.query('builtinSpecializers("bar", "String")')) assert list(oso.query('builtinSpecializers(1, "IntegerWithFields")')) assert not list( oso.query('builtinSpecializers(2, "IntegerWithGarbageFields")')) assert not list( oso.query_rule("builtinSpecializers", {}, "DictionaryWithFields")) assert not list( oso.query_rule("builtinSpecializers", {"z": 1}, "DictionaryWithFields")) assert list( oso.query_rule("builtinSpecializers", {"y": 1}, "DictionaryWithFields")) # test iterables work assert list(oso.query_rule("testIterables")) # Test deref works oso.load_str("?= x = 1 and E.sum([x, 2, x]) = 4 and [3, 2, x].index(1) = 2;") # Test unspecialized rule ordering result = oso.query_rule("testUnspecializedRuleOrder", "foo", "bar", Variable("z")) assert next(result)["bindings"]["z"] == 1 assert next(result)["bindings"]["z"] == 2 assert next(result)["bindings"]["z"] == 3