def test_set_get_session(): from sqlalchemy_oso.session import set_get_session from oso import Oso def get_session(): engine = create_engine("sqlite://") Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() load_fixture_data(session) return session oso = Oso() set_get_session(oso, get_session) register_models(oso, Base) test_str = """get_repo(name: String) if session = OsoSession.get() and repo = session.query(Repository).filter_by(name: name).first() and repo.name = name; """ oso.load_str(test_str) results = oso.query_rule("get_repo", "Abbey Road") assert next(results) results = oso.query_rule("get_repo", "Abbey Road") assert next(results)
def test_multi(): oso = Oso() oso.load_str("allow(x, y) if x == y;") tp = ThreadPoolExecutor(max_workers=8) futures = [] for _ in range(32): futures.append(tp.submit(torch_oso, oso)) for i, future in enumerate(concurrent.futures.as_completed(futures)): future.result() # If we got here none of these crashed. assert True
class E: @staticmethod def sum(*args): return sum(*args) oso.register_class(E) polar_file = os.path.dirname(os.path.realpath(__file__)) + "/test.polar" oso.load_file(polar_file) assert oso.is_allowed("a", "b", "c") # Test that a built in string method can be called. oso.load_str("""?= x = "hello world!" and x.endswith("world!");""") # Test that a custom error type is thrown. exception_thrown = False try: oso.load_str("missingSemicolon()") except UnrecognizedEOF as e: exception_thrown = True assert ( str(e) == "hit the end of the file unexpectedly. Did you forget a semi-colon at line 1, column 19" ) assert exception_thrown assert list(oso.query_rule("specializers", D("hello"), B.C("hello"))) assert list(oso.query_rule("floatLists"))
from oso import Oso oso = Oso() oso.load_str("""allow("user", "can_use", "this_program");""") if oso.is_allowed("user", "can_use", "this_program"): print("Hello from oso") else: print("Access denied") if oso.is_allowed("user-123", "can_run", "this_program"): print("Hello again, but you probably can't run this line.") else: print("You weren't authorized by the rules!")