1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-04 18:46:52 +00:00

Simulate a fake Matrix client in order to run tests

This commit is contained in:
Yohann D'ANELLO 2020-10-30 19:54:22 +01:00
parent 8236a9fe14
commit 6d4cd217b2

View File

@ -18,11 +18,14 @@ class Matrix:
_device_id: str = None
@classmethod
async def _get_client(cls) -> AsyncClient:
async def _get_client(cls) -> Union[AsyncClient, "FakeMatrixClient"]:
"""
Retrieve the bot account.
If not logged, log in and store access token.
"""
if not os.getenv("SYNAPSE_PASSWORD"):
return FakeMatrixClient()
client = AsyncClient("https://correspondances-maths.fr", "@corres2mathbot:correspondances-maths.fr")
client.user_id = "@corres2mathbot:correspondances-maths.fr"
@ -212,9 +215,9 @@ class Matrix:
"""
client = await cls._get_client()
resp: RoomResolveAliasResponse = await client.room_resolve_alias(room_alias)
if isinstance(resp, RoomResolveAliasError):
return None
return resp.room_id
if isinstance(resp, RoomResolveAliasResponse):
return resp.room_id
return None
@classmethod
@async_to_sync
@ -328,9 +331,20 @@ class Matrix:
avatar_uri (str): The internal avatar URI to apply.
"""
client = await cls._get_client()
client
if room_id.startswith("#"):
room_id = await cls.resolve_room_alias(room_id)
return await client.room_put_state(room_id, "m.room.avatar", content={
"url": avatar_uri
}, state_key="")
class FakeMatrixClient:
"""
Simulate a Matrix client to run tests, if no Matrix homeserver is connected.
"""
def __getattribute__(self, item):
async def func(*_, **_2):
return None
return func