1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-05 02:06: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 _device_id: str = None
@classmethod @classmethod
async def _get_client(cls) -> AsyncClient: async def _get_client(cls) -> Union[AsyncClient, "FakeMatrixClient"]:
""" """
Retrieve the bot account. Retrieve the bot account.
If not logged, log in and store access token. 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 = AsyncClient("https://correspondances-maths.fr", "@corres2mathbot:correspondances-maths.fr")
client.user_id = "@corres2mathbot:correspondances-maths.fr" client.user_id = "@corres2mathbot:correspondances-maths.fr"
@ -212,9 +215,9 @@ class Matrix:
""" """
client = await cls._get_client() client = await cls._get_client()
resp: RoomResolveAliasResponse = await client.room_resolve_alias(room_alias) resp: RoomResolveAliasResponse = await client.room_resolve_alias(room_alias)
if isinstance(resp, RoomResolveAliasError): if isinstance(resp, RoomResolveAliasResponse):
return None return resp.room_id
return resp.room_id return None
@classmethod @classmethod
@async_to_sync @async_to_sync
@ -328,9 +331,20 @@ class Matrix:
avatar_uri (str): The internal avatar URI to apply. avatar_uri (str): The internal avatar URI to apply.
""" """
client = await cls._get_client() client = await cls._get_client()
client
if room_id.startswith("#"): if room_id.startswith("#"):
room_id = await cls.resolve_room_alias(room_id) room_id = await cls.resolve_room_alias(room_id)
return await client.room_put_state(room_id, "m.room.avatar", content={ return await client.room_put_state(room_id, "m.room.avatar", content={
"url": avatar_uri "url": avatar_uri
}, state_key="") }, 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