Skip to content

Commit

Permalink
test: start adding some tests along with workflow 👷 ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
ad2ien committed May 29, 2024
1 parent d9fc0a5 commit 45b5e22
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Tests

on:
push:
branches: ["main"]
pull_request:

jobs:
tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
- run: pip install tox
- run: tox -e py
14 changes: 14 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import Any

import attr


@attr.s(auto_attribs=True)
class MockModuleApi:
"""Mock of a synapse.module_api.ModuleApi, only implementing the methods the
WhiteList module will use.
"""

def register_spam_checker_callbacks(self, *args: Any, **kwargs: Any) -> None:
"""Don't fail when the module tries to register its callbacks."""
pass
52 changes: 52 additions & 0 deletions tests/test_white_list_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import aiounittest

from tests import MockModuleApi
from white_list_module import EimisWhiteList


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

def test_get_last_content_no_content(self):

result = self.module.get_last_content({})

self.assertEqual(result, "")

def test_get_last_content_message(self):
real_life_content = {
"msgtype": "m.text",
"body": "## test experiment\nyvonnière",
"format": "org.matrix.custom.html",
"formatted_body": "<h2>test experiment</h2>\n<p>yvonnière</p>\n",
"m.mentions": {},
}
result = self.module.get_last_content(real_life_content)

self.assertEqual(result, "## test experiment\nyvonnière")

def test_get_last_content_modified_message(self):
real_life_content = {
"msgtype": "m.text",
"body": " * patapouf\npignolin\nroger\ntartanfion",
"m.new_content": {
"msgtype": "m.text",
"body": "patapouf\npignolin\nroger\ntartanfion",
"m.mentions": {},
},
"m.mentions": {},
"m.relates_to": {
"rel_type": "m.replace",
"event_id": "$ChWXbbIpW-NUPca17PXdsTSxVAXAKGZeKEae3mcTjdI",
},
}
result = self.module.get_last_content(real_life_content)

self.assertEqual(result, "patapouf\npignolin\nroger\ntartanfion")
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ wheel_build_env = .pkg
deps =
pytest>=6
matrix-synapse~=1.104.0
authlib~=1.3.0
aiounittest~=1.4.2
commands =
pytest {tty:--color=yes} {posargs}
Expand Down
10 changes: 8 additions & 2 deletions white_list_module/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def parse_config(config: Dict[str, Any]) -> EimisWhiteListConfig:
A EimisBroadcastConfig generated from this configuration
"""

# TODO check config
logger.info("EIMIS config EimisWhiteList")

return EimisWhiteListConfig(
Expand Down Expand Up @@ -89,12 +88,16 @@ async def get_whitelist_from_content(self) -> Collection[str]:
last_events = await self._api._store.get_latest_event_ids_in_room(
self._config.room_id
)

if not last_events or len(last_events) == 0:
return whitelist

event_id = list(last_events)[0]

while event_id:
event = await self._api._store.get_event(event_id, allow_none=True)

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

Expand All @@ -117,4 +120,7 @@ def get_last_content(self, event_content) -> str:
while "m.new_content" in event_content.keys():
event_content = event_content["m.new_content"]

if "body" not in event_content.keys():
return ""

return event_content["body"]

0 comments on commit 45b5e22

Please sign in to comment.