def test_get_permissions_as_seen_by(collection, user, superuser): """Test getting viewable permissions as 'user'.""" # If there are no permissions, we se no permissions. assert collection.get_permissions_as_seen_by(user) == [] # If 'user' is not cataloging admin, they don't see regular user permissions on a collection. other_user = UserFactory() others_non_cataloging_admin_permission = Permission(user=other_user, collection=collection, cataloging_admin=False).save_as(superuser) assert others_non_cataloging_admin_permission not in \ collection.get_permissions_as_seen_by(user) # 'user' sees own and cataloging admin permissions on a collection. users_own_permission = Permission(user=user, collection=collection, cataloging_admin=False).save_as(superuser) third_user = UserFactory() thirds_cataloging_admin_permission = Permission(user=third_user, collection=collection, cataloging_admin=True).save_as(superuser) assert len(collection.get_permissions_as_seen_by(user)) == 2 assert users_own_permission in collection.get_permissions_as_seen_by(user) assert thirds_cataloging_admin_permission in collection.get_permissions_as_seen_by(user) # When 'user' becomes a cataloging admin on a collection, they sees all permissions. users_own_permission.cataloging_admin = True users_own_permission.save() assert len(collection.get_permissions_as_seen_by(user)) == 3 assert others_non_cataloging_admin_permission in collection.get_permissions_as_seen_by(user) assert users_own_permission in collection.get_permissions_as_seen_by(user) assert thirds_cataloging_admin_permission in collection.get_permissions_as_seen_by(user) # As a system admin, you see all permissions on a collection. assert len(superuser.permissions) == 0 assert len(collection.get_permissions_as_seen_by(superuser)) == 3
def test_is_cataloging_admin_for(user, collection, superuser): """Test is_cataloging_admin_for return value.""" other_collection = CollectionFactory() not_admin_permission = Permission(user=user, collection=collection, cataloging_admin=False).save_as(superuser) admin_permission = Permission(user=user, collection=other_collection, cataloging_admin=True).save_as(superuser) assert user.is_cataloging_admin_for(not_admin_permission.collection) is False assert user.is_cataloging_admin_for(admin_permission.collection) is True assert user.is_cataloging_admin_for(admin_permission.collection, not_admin_permission.collection) is False not_admin_permission.cataloging_admin = True now_also_admin_permission = not_admin_permission.save() assert user.is_cataloging_admin_for(admin_permission.collection, now_also_admin_permission.collection) is True