Skip to content

Commit

Permalink
feat: localpart is lower case and tests ✅✨
Browse files Browse the repository at this point in the history
  • Loading branch information
ad2ien committed Jun 4, 2024
1 parent c23c08b commit 61ef6b6
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
93 changes: 92 additions & 1 deletion tests/test_white_list_module.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
from unittest import mock

import aiounittest
from synapse.api.room_versions import RoomVersions
from synapse.events import EventBase, make_event_from_dict

from tests import MockModuleApi
from white_list_module import EimisWhiteList


class EimisWhiteListTestClass(aiounittest.AsyncTestCase):
def create_event(
message: str,
prev_event_id: EventBase = None,
) -> EventBase:
return make_event_from_dict(
{
"room_id": "room_id",
"type": "m.room.message",
"state_key": "",
"sender": "user_id",
"content": {"body": message},
"auth_events": [],
"prev_events": [prev_event_id],
},
room_version=RoomVersions.V9,
)


class EimisWhiteListGetLastContentTestClass(aiounittest.AsyncTestCase):
def setUp(self):
self.config = EimisWhiteList.parse_config(
{
Expand Down Expand Up @@ -50,3 +72,72 @@ def test_get_last_content_modified_message(self):
result = self.module.get_last_content(real_life_content)

self.assertEqual(result, "patapouf\npignolin\nroger\ntartanfion")


class EimisWhiteListFromContentTestClass(aiounittest.AsyncTestCase):
def setUp(self):
self.config = EimisWhiteList.parse_config(
{
"idp_id": "idp_id",
"room_id": "room_id",
}
)
self.module = EimisWhiteList(self.config, MockModuleApi())

async def test_get_whitelist_from_content_no_event(self):

self.module._api._store = mock.MagicMock()
self.module._api._store.get_latest_event_ids_in_room = mock.AsyncMock(
return_value=[]
)

result = await self.module.get_whitelist_from_content()

self.module._api._store.get_latest_event_ids_in_room.assert_called_once_with(
"room_id"
)
self.assertEqual(result, set())

async def test_get_whitelist_from_content_one_event(self):

event56 = create_event("##Some title\nYvonne")

self.module._api._store = mock.MagicMock()
self.module._api._store.get_latest_event_ids_in_room = mock.AsyncMock(
return_value=["event56"]
)
self.module._api._store.get_event = mock.AsyncMock(return_value=event56)

result = await self.module.get_whitelist_from_content()

self.module._api._store.get_latest_event_ids_in_room.assert_called_once_with(
"room_id"
)
self.module._api._store.get_event.assert_called_once_with(
"event56", allow_none=True
)
self.assertTrue("yvonne" in result)

async def test_get_whitelist_from_content_several_events(self):

event56 = create_event("##Some title\nYvonne")
event57 = create_event("##Experiment 2\nPotiron\nGlandine", event56)

self.module._api._store = mock.MagicMock()
self.module._api._store.get_latest_event_ids_in_room = mock.AsyncMock(
return_value=["event57"]
)
self.module._api._store.get_event = mock.AsyncMock(
side_effect=[event57, event56]
)

result = await self.module.get_whitelist_from_content()

self.module._api._store.get_latest_event_ids_in_room.assert_called_once_with(
"room_id"
)
self.module._api._store.get_event.assert_any_call("event57", allow_none=True)
# self.module._api._store.get_event.assert_any_call('event56', allow_none=True)
self.assertTrue("yvonne" in result)
self.assertTrue("potiron" in result)
self.assertTrue("glandine" in result)
2 changes: 1 addition & 1 deletion white_list_module/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def get_whitelist_from_content(self) -> Collection[str]:

if event.type and event.type == "m.room.message":
content = self.get_last_content(event.content)
whitelist.update(content.split("\n"))
whitelist.update(content.lower().split("\n"))

if event.prev_event_ids():
event_id = event.prev_event_ids()[0]
Expand Down

0 comments on commit 61ef6b6

Please sign in to comment.