async def test_search_partial_name_arg( compare_kelvin_obj_with_test_data, new_school, kelvin_session_kwargs, new_school_user, ): school = new_school(1)[0] user = await new_school_user(school=school.name) name = user.name name_len = len(name) name_begin = name[: int(name_len / 2)] name_end = name[len(name_begin) :] async with Session(**kelvin_session_kwargs) as session: objs1 = [ obj async for obj in UserResource(session=session).search( school=school.name, name=f"{name_begin}*" ) ] objs2 = [ obj async for obj in UserResource(session=session).search( school=school.name, name=f"*{name_end}" ) ] assert objs1, f"No User for school={school.name!r} and name='{name_begin}*' found." assert len(objs1) == 1 assert user.dn == objs1[0].dn assert objs2, f"No User for school={school.name!r} and name='*{name_end}' found." assert len(objs2) == 1 assert user.dn == objs2[0].dn
async def test_modify( check_password, compare_kelvin_obj_with_test_data, kelvin_session_kwargs, new_school_user, new_user_test_obj, ): user = await new_school_user() new_data = asdict( new_user_test_obj( name=user.name, roles=user.roles, school=user.school, schools=user.schools, ucsschool_roles=user.ucsschool_roles, ) ) async with Session(**kelvin_session_kwargs) as session: user_resource = UserResource(session=session) obj: User = await user_resource.get(school=user.school, name=user.name) compare_kelvin_obj_with_test_data(obj, **asdict(user)) await check_password(obj.dn, user.password) for k, v in new_data.items(): if k not in ("dn", "url"): setattr(obj, k, v) new_obj: User = await obj.save() assert new_obj is obj compare_kelvin_obj_with_test_data(new_obj, **obj.as_dict()) # load fresh object fresh_obj: User = await user_resource.get(school=user.school, name=user.name) compare_kelvin_obj_with_test_data(fresh_obj, **new_obj.as_dict()) compare_kelvin_obj_with_test_data(fresh_obj, **obj.as_dict()) await check_password(fresh_obj.dn, new_data["password"])
async def test_move_change_name( compare_kelvin_obj_with_test_data, kelvin_session_kwargs, new_school_user, schedule_delete_obj, ): user = await new_school_user() old_name = user.name new_name = fake.first_name() assert old_name != new_name async with Session(**kelvin_session_kwargs) as session: user_resource = UserResource(session=session) obj: User = await user_resource.get(school=user.school, name=old_name) assert obj.name == old_name compare_kelvin_obj_with_test_data(obj, **asdict(user)) obj.name = new_name old_url = obj.url schedule_delete_obj(object_type="user", name=new_name) new_obj: User = await obj.save() assert new_obj is obj assert new_obj.name == new_name assert f"uid={new_name}" in new_obj.dn assert new_obj.url != old_url # load fresh object fresh_obj: User = await user_resource.get(school=user.school, name=new_name) assert fresh_obj.name == new_name assert fresh_obj.url != old_url compare_kelvin_obj_with_test_data(fresh_obj, **new_obj.as_dict())
async def test_search_inexact_school(new_school, kelvin_session_kwargs): school = new_school(1)[0] school_name = school.name school_name_len = len(school_name) school_name_begin = school_name[: int(school_name_len / 2)] async with Session(**kelvin_session_kwargs) as session: with pytest.raises(InvalidRequest): assert [ obj async for obj in UserResource(session=session).search(school=f"{school_name_begin}*") ]
async def test_search_inexact( compare_kelvin_obj_with_test_data, new_school, kelvin_session_kwargs, new_school_user, attr, ): user = await new_school_user() value = getattr(user, attr) value_len = len(value) value_begin = value[: int(value_len / 2)] value_end = value[len(value_begin) :] async with Session(**kelvin_session_kwargs) as session: objs1 = [ obj async for obj in UserResource(session=session).search( school=user.school, **{attr: f"{value_begin}*"} ) ] objs2 = [ obj async for obj in UserResource(session=session).search( school=user.school, **{attr: f"*{value_end}"} ) ] assert objs1, f"No User for school={user.school!r} and {attr!r}='{value_begin}*' found." assert objs2, f"No User for school={user.school!r} and {attr!r}='*{value_end}' found." if attr == "source_uid": assert len(objs1) >= 1 assert user.dn in [o.dn for o in objs1] assert len(objs2) >= 1 assert user.dn in [o.dn for o in objs2] else: assert len(objs1) == 1 assert user.dn == objs1[0].dn assert len(objs2) == 1 assert user.dn == objs2[0].dn
async def fetch_obj(self, search_params: Dict[str, Any]) -> User: """Retrieve a user from API of school authority.""" self.logger.debug("Retrieving user with search parameters: %r", search_params) users: List[User] = [ user async for user in UserResource(session=self.session).search(**search_params) ] if len(users) == 1: return users[0] if len(users) > 1: raise UniquenessError( f"Multiple users with the same 'source_uid'={search_params['source_uid']!r} and " f"'record_uid'={search_params['record_uid']!r} exist in the target system: {users!r}." ) else: raise UserNotFoundError(f"No user found with search params: {search_params!r}.")
async def test_modify_password_hashes( check_password, kelvin_session_kwargs, password_hash, new_school_user, ): user = await new_school_user() password, password_hashes = await password_hash() assert user.password != password async with Session(**kelvin_session_kwargs) as session: user_resource = UserResource(session=session) obj: User = await user_resource.get(school=user.school, name=user.name) await check_password(obj.dn, user.password) obj.password = None obj.kelvin_password_hashes = PasswordsHashes(**asdict(password_hashes)) await obj.save() fresh_obj: User = await user_resource.get(school=user.school, name=user.name) await check_password(fresh_obj.dn, password)
async def test_search_no_name_arg( compare_kelvin_obj_with_test_data, new_school, kelvin_session_kwargs, new_school_user, ): school = new_school(1)[0] user1 = await new_school_user(school=school.name) user2 = await new_school_user(school=school.name) async with Session(**kelvin_session_kwargs) as session: objs = [obj async for obj in UserResource(session=session).search(school=school.name)] assert objs, f"No Users in school {school.name!r} found." assert len(objs) >= 2 assert user1.dn in [obj.dn for obj in objs] assert user2.dn in [obj.dn for obj in objs] for obj in objs: if obj.dn == user1.dn: compare_kelvin_obj_with_test_data(obj, **asdict(user1)) elif obj.dn == user2.dn: compare_kelvin_obj_with_test_data(obj, **asdict(user2))
async def test_search_exact( compare_kelvin_obj_with_test_data, new_school, kelvin_session_kwargs, new_school_user, attr, ): user = await new_school_user() value = getattr(user, attr) async with Session(**kelvin_session_kwargs) as session: objs = [ obj async for obj in UserResource(session=session).search(school=user.school, **{attr: value}) ] assert objs, f"No User for school={user.school!r} and {attr!r}={value!r} found." if attr in ("roles", "disabled"): assert len(objs) >= 1 assert user.dn in [o.dn for o in objs] else: assert len(objs) == 1 assert user.dn == objs[0].dn
async def test_move_change_school( compare_kelvin_obj_with_test_data, new_school, kelvin_session_kwargs, new_school_user, schedule_delete_obj, ): user = await new_school_user() old_school = user.school school1, school2 = new_school(2) ou1, ou2 = school1.name, school2.name new_school_ = ou1 if old_school == ou2 else ou2 assert old_school != new_school_ async with Session(**kelvin_session_kwargs) as session: user_resource = UserResource(session=session) obj: User = await user_resource.get(school=old_school, name=user.name) assert obj.school == old_school compare_kelvin_obj_with_test_data(obj, **asdict(user)) obj.school = new_school_ obj.schools = [new_school_] old_url = obj.url schedule_delete_obj(object_type="user", name=user.name) new_obj: User = await obj.save() assert new_obj is obj assert new_obj.name == user.name assert new_obj.school == new_school_ assert f"ou={new_school_}" in new_obj.dn assert new_obj.url == old_url # load fresh object fresh_obj: User = await user_resource.get(school=new_school_, name=user.name) assert fresh_obj.name == user.name assert fresh_obj.school == new_school_ assert fresh_obj.schools == [new_school_] assert fresh_obj.url == old_url compare_kelvin_obj_with_test_data(fresh_obj, **new_obj.as_dict())