async def test_query_on_login(alice: Client, tmp_path): await alice.profile.set_name("Alice Margarine") await alice.profile.set_avatar(MXC("mxc://localhost/1")) alice2 = await new_device_from(alice, tmp_path) assert not alice2.sync.next_batch # not synced assert alice2.profile.name == "Alice Margarine" assert alice2.profile.avatar == MXC("mxc://localhost/1")
async def test_changes_no_room_joined(alice: Client): assert not alice.rooms assert not alice.sync.next_batch # not synced await alice.profile.set_name("Alice Margatroid") await alice.profile.set_avatar(MXC("mxc://localhost/1")) await alice.sync.once() assert alice.profile.name == "Alice Margatroid" assert alice.profile.avatar == MXC("mxc://localhost/1")
async def test_profile_properties(alice: Client, room: Room): await alice.profile.set_name("Alice Margatroid") await alice.profile.set_avatar(MXC("mxc://localhost/1")) await alice.sync.once() assert room.state.me.display_name == alice.profile.name assert room.state.me.avatar_url == alice.profile.avatar
async def test_unthumbnailable_formats(alice: Client): thumb = Thumbnailable(body="", mxc=MXC("mxc://a/b")) assert not await thumb.set_thumbnail_from(alice, BytesIO(), "text/plain") assert not await thumb.set_thumbnail_from( alice, BytesIO(), "image/svg+xml", )
async def test_thumb_dimensions(alice: Client, data: TestData): thumb = Thumbnailable(body="", mxc=MXC("mxc://a/b")) async with aiofiles.open(data.tiny_unicolor_bmp, "rb") as file: got = await thumb.set_thumbnail_from(alice, file, "image/bmp") assert got assert PILImage.open(BytesIO(got)).size == (1, 1) # unchanged async with aiofiles.open(data.large_unicolor_png, "rb") as file: got = await thumb.set_thumbnail_from(alice, file, "image/png") assert got assert PILImage.open(BytesIO(got)).size == (800, 600) # downscaled
async def test_changes(alice: Client, bob: Client, room: Room, room2: Room): await alice.sync.once() # We'll make sure the callback is only called once when multiple member # events (due to multiple rooms) come for the same change assert len(alice.rooms) == 2 assert alice.profile.name != "Alice Margatroid" assert alice.profile.avatar != "mxc://localhost/1" # Make sure too we don't query profile due to events not concerning us assert room.id not in bob.rooms await bob.rooms.join(room.id) calls = [] alice.profile.callback = lambda self: calls.append(self) await alice.profile.set_name("Alice Margatroid") await alice.profile.set_avatar(MXC("mxc://localhost/1")) await alice.sync.once() assert calls == [alice.profile] assert alice.profile.name == "Alice Margatroid" assert alice.profile.avatar == MXC("mxc://localhost/1")
def create_encrypted_media_info(**init_kwargs): return EncryptedMediaInfo( **{ "mxc": MXC("mxc://a/b"), "init_vector": "", "key": "", "sha256": "AAAA", "key_operations": ["encrypt", "decrypt"], "key_type": "oct", "key_algorithm": "A256CTR", "key_extractable": True, "version": "v2", **init_kwargs, })
async def test_thumb_jpg_vs_png(alice: Client, data: TestData): def png_len(image: Path) -> int: buffer = BytesIO() PILImage.open(image).save(buffer, "PNG", optimize=True) return len(buffer.getvalue()) def jpg_len(image: Path) -> int: buffer = BytesIO() PILImage.open(image).convert("RGB").save( buffer, "JPEG", optimize=True, quality=75, progressive=True, ) return len(buffer.getvalue()) assert png_len(data.gradient_png) > jpg_len(data.gradient_png) assert png_len(data.gradient_hole_png) > jpg_len(data.gradient_hole_png) assert png_len(data.large_unicolor_png) < jpg_len(data.large_unicolor_png) thumb = Thumbnailable(body="", mxc=MXC("mxc://a/b")) async with aiofiles.open(data.gradient_png, "rb") as file: got = await thumb.set_thumbnail_from(alice, file, "image/png") assert got assert PILImage.open(BytesIO(got)).format == "JPEG" async with aiofiles.open(data.gradient_hole_png, "rb") as file: # needed to not have data be None buffer = BytesIO(await file.read()) out = BytesIO() PILImage.open(buffer).resize((1024, 768)).save(out, "PNG") out.seek(0) got = await thumb.set_thumbnail_from(alice, out, "image/png") assert got assert PILImage.open(BytesIO(got)).format == "PNG" async with aiofiles.open(data.large_unicolor_png, "rb") as file: got = await thumb.set_thumbnail_from(alice, file, "image/png") assert got assert PILImage.open(BytesIO(got)).format == "PNG"
async def test_thumb_not_enough_saved_space(alice: Client, data: TestData): thumb = Thumbnailable(body="", mxc=MXC("mxc://a/b")) formats = THUMBNAIL_POSSIBLE_PIL_FORMATS # Not enough space saved, but original image is in an unacceptable format assert PILImage.open(data.tiny_unicolor_bmp).format not in formats async with aiofiles.open(data.tiny_unicolor_bmp, "rb") as file: got = await thumb.set_thumbnail_from(alice, file, "image/bmp") assert got original_size = data.tiny_unicolor_bmp.stat().st_size assert len(got) > original_size * THUMBNAIL_SIZE_MAX_OF_ORIGINAL # Not enough space saved assert "PNG" in formats buffer = BytesIO() PILImage.open(data.tiny_unicolor_bmp).save(buffer, "PNG", optimize=True) buffer.seek(0) assert not await thumb.set_thumbnail_from(alice, buffer, "image/png")
def test_media_init(): Media(body="", mxc=MXC("mxc://a/b")) Media(body="", encrypted=create_encrypted_media_info()) with raises(TypeError): Media(body="")