def tag_nested_many_many_fixtures(): user = User(username="******") user.save() other_user = User(username="******") other_user.save() eng = Tag(name="eng") eng.save() eng.users.set([user]) user_posts = Tag(name="user_posts") user_posts.save() user_posts.users.set([user]) random = Tag(name="random", is_public=True) random.save() random.users.set([other_user]) user_eng_post = Post(contents="user eng post", access_level="public", created_by=user) user_user_post = Post( contents="user user post", access_level="public", created_by=user, ) random_post = Post( contents="other random post", access_level="public", created_by=other_user, ) not_tagged_post = Post(contents="not tagged post", access_level="public", created_by=user) all_tagged_post = Post( contents="not tagged post", access_level="public", created_by=user, ) posts = { "user_eng_post": user_eng_post, "user_user_post": user_user_post, "random_post": random_post, "not_tagged_post": not_tagged_post, "all_tagged_post": all_tagged_post, } for post in posts.values(): post.save() user_eng_post.tags.set([eng]) user_user_post.tags.set([user_posts]) random_post.tags.set([random]) all_tagged_post.tags.set([eng, user_posts, random]) user.posts.set( [user_eng_post, user_user_post, not_tagged_post, all_tagged_post]) other_user.posts.set([random_post]) return posts
def post_fixtures(): foo = User(username="******") foo.save() admin = User(username="******", is_moderator=True) admin.save() banned = User(username="******", is_banned=True) banned.save() Post(contents="foo public post", access_level="public", created_by=foo).save() Post( contents="foo public post 2", access_level="public", created_by=foo, ).save() Post( contents="foo private post", created_by=foo, ).save() Post( contents="foo private post 2", created_by=foo, ).save() Post( contents="private for moderation", needs_moderation=True, created_by=foo, ).save() Post( contents="public for moderation", access_level="public", needs_moderation=True, created_by=foo, ).save() Post( contents="public admin post", access_level="public", needs_moderation=True, created_by=admin, ).save() Post( contents="private admin post", needs_moderation=True, created_by=admin, ).save() Post(contents="banned post", access_level="public", created_by=banned).save()
def tag_fixtures(): """Test data for tests with tags.""" user = User(username="******") user.save() other_user = User(username="******") other_user.save() moderator = User(username="******", is_moderator=True) moderator.save() eng = Tag(name="eng") eng.save() foo = Tag(name="foo") foo.save() random = Tag(name="random", is_public=True) random.save() user_public_post = Post(contents="public post", created_by=user, access_level="public") user_private_post = Post(contents="private user post", created_by=user) other_user_public_post = Post(contents="other user public", created_by=other_user, access_level="public") other_user_private_post = Post(contents="other user private", created_by=other_user) other_user_random_post = Post(contents="other user random", created_by=other_user) other_user_foo_post = Post(contents="other user foo", created_by=other_user) posts = { "user_public_post": user_public_post, "user_private_post": user_private_post, "other_user_public_post": other_user_public_post, "other_user_private_post": other_user_private_post, "other_user_random_post": other_user_random_post, "other_user_foo_post": other_user_foo_post, } for post in posts.values(): post.save() other_user_random_post.tags.set([random]) other_user_foo_post.tags.set([foo]) return posts
def test_all_objects_collection_condition(oso, engine): public_tag = Tag(name="public", is_public=True) private_tag = Tag(name="private", is_public=False) post0 = Post(id=0, contents="public tag", tags=[public_tag]) post1 = Post(id=1, contents="no tags", tags=[]) post2 = Post(id=2, contents="both tags", tags=[public_tag, private_tag]) post3 = Post(id=3, contents="public tag 2", tags=[public_tag]) post4 = Post(id=4, contents="private tag", tags=[private_tag]) public_tag.save() private_tag.save() post0.save() post1.save() post2.save() post3.save() post4.save() post0.tags.set([public_tag]) post2.tags.set([public_tag, private_tag]) post3.tags.set([public_tag]) post4.tags.set([private_tag]) oso.load_str( """ allow(_, _, post: test_app2::Post) if forall(tag in post.tags, tag.is_public = true); """ ) posts = Post.objects.authorize(None, actor="u", action="r").all() assert len(posts) == 2 assert post0 in posts assert post3 in posts
def test_ground_object_in_collection(): tag = Tag(name="tag") post0 = Post(id=0, contents="tag post") post1 = Post(id=1, contents="no tag post") post2 = Post(id=2, contents="tag 2 post") tag.save() post0.save() post1.save() post2.save() post0.tags.set([tag]) post2.tags.set([tag]) Oso.register_constant(tag, "allowed_tag") Oso.load_str( """ allow(_, _, post: test_app2::Post) if allowed_tag in post.tags; """ ) posts = Post.objects.authorize(None, actor="u", action="r").all() assert len(posts) == 2 assert post0 in posts assert post2 in posts
def test_scalar_in_list(): post0 = Post(id=0, contents="private post", title="not private post") post1 = Post(id=1, contents="allowed posts", title="private post") post2 = Post(id=2, contents="post", title="post") post0.save() post1.save() post2.save() Oso.load_str( """ allow(_, _, post: test_app2::Post) if post.contents in ["post", "allowed posts"]; """ ) posts = Post.objects.authorize(None, actor="u", action="r").all() assert len(posts) == 2 assert post1 in posts assert post2 in posts
def test_field_comparison(): post0 = Post(id=0, contents="private post", title="not private post") post1 = Post(id=1, contents="private post", title="private post") post2 = Post(id=2, contents="post", title="post") post0.save() post1.save() post2.save() Oso.load_str( """ allow(_, _, post: test_app2::Post) if post.title = post.contents; """ ) posts = Post.objects.authorize(None, actor="u", action="r").all() assert len(posts) == 2 assert post1 in posts assert post2 in posts