def test_equals(self, role, parent_role): r = Role(name='test1') r2 = Role(name='test2') r3 = Role(name='test3', chat_ids=[1, 2]) r4 = Role(name='test4') assert role.equals(parent_role) role.add_child_role(r) assert not role.equals(parent_role) parent_role.add_child_role(r2) assert role.equals(parent_role) parent_role.add_child_role(r3) role.add_child_role(r4) assert not role.equals(parent_role) role.remove_child_role(r4) parent_role.remove_child_role(r3) role.add_member(1) assert not role.equals(parent_role) parent_role.add_member(1) assert role.equals(parent_role) role.add_member(2) assert not role.equals(parent_role) parent_role.add_member(2) assert role.equals(parent_role) role.kick_member(2) assert not role.equals(parent_role) parent_role.kick_member(2) assert role.equals(parent_role) r.add_member(1) assert not role.equals(parent_role) r2.add_member(1) assert role.equals(parent_role)
def test_filter_merged_roles(self, update, role): role.add_member(0) r = Role(0) assert not (role & (~r))(update) r = Role(1) assert not (role & r)(update) assert (role | r)(update)
def test_deepcopy(self, role, parent_role): child = Role(name='cr', chat_ids=[1, 2, 3]) role.add_child_role(child) role.add_member(7) copied_role = deepcopy(role) assert role is not copied_role assert role.equals(copied_role) assert role.chat_ids is not copied_role.chat_ids assert role.chat_ids == copied_role.chat_ids (copied_child, ) = copied_role.child_roles assert child is not copied_child assert child.equals(copied_child)
def test_pickle(self, role, parent_role): role.add_member([0, 1, 3]) parent_role.add_member([4, 5, 6]) child_role = Role(name='child_role', chat_ids=[7, 8, 9]) role.add_child_role(child_role) parent_role.add_child_role(child_role) data = { 'role': role, 'parent': parent_role, 'child': child_role, } with open('pickle', 'wb') as file: pickle.dump(data, file) with open('pickle', 'rb') as file: data = pickle.load(file) assert data['role'].equals(role) assert data['parent'].equals(parent_role) assert data['child'].equals(child_role) (child_1, ) = data['role'].child_roles (child_2, ) = data['parent'].child_roles assert child_1 is child_2 assert data['role'] in Role._admin.child_roles assert data['parent'] in Role._admin.child_roles assert data['child'] in Role._admin.child_roles assert data['child'] <= data['role'] assert data['child'] <= data['parent'] assert not data['role'] <= data['parent']
def test_creation(self, parent_role): r = Role(child_roles=[parent_role, parent_role]) assert r.chat_ids == set() assert str(r) == 'Role({})' assert r.child_roles == {parent_role} assert isinstance(r._admin, Role) assert str(r._admin) == f'Role({Role._DEFAULT_ADMIN_NAME})' r = Role(1) assert r.chat_ids == {1} assert str(r) == 'Role({1})' r = Role([1, 2]) assert r.chat_ids == {1, 2} assert str(r) == 'Role({1, 2})' r = Role([1, 2], name='role') assert r.chat_ids == {1, 2} assert str(r) == 'Role(role)'
def test_handler_error_message(self, cdp, update): handler = MessageHandler(Filters.all, callback=lambda u, c: 1) roles_handler = RolesHandler(handler, roles=Role(0)) cdp.add_handler(roles_handler) self.test_flag = False def error_handler(_, context: CallbackContext): self.test_flag = 'You must set a Roles instance' in str( context.error) cdp.add_error_handler(error_handler) cdp.process_update(update) assert self.test_flag
def test_add_remove_child_role(self, role, parent_role): assert role.child_roles == set() parent2 = Role(chat_ids=456, name='pr2') role.add_child_role(parent_role) assert role.child_roles == {parent_role} role.add_child_role(parent2) assert role.child_roles == {parent_role, parent2} role.remove_child_role(parent_role) assert role.child_roles == {parent2} role.remove_child_role(parent2) assert role.child_roles == set() with pytest.raises(ValueError, match='You must not add a role as its own child!'): role.add_child_role(role) parent_role.add_child_role(role) with pytest.raises(ValueError, match='You must not add a parent role as a child!'): role.add_child_role(parent_role)
def role(): return Role(name='role')
def parent_role(): return Role(name='parent_role')
def test_always_allow_admin(self, update, role): role._admin.add_member(0) try: assert (~Role(0))(update) assert (Role(0) & ~Role(0))(update) assert (Role(1) & ~Role(0))(update) assert (Role(1) & ~Role(2))(update) assert (Role(0) | ~Role(0))(update) assert (Role(1) | ~Role(0))(update) assert (Role(1) | ~Role(2))(update) finally: role._admin.kick_member(0)