Skip to content

Commit

Permalink
Merge pull request #1 from eimis-ans/chore/updates
Browse files Browse the repository at this point in the history
Various updates
  • Loading branch information
ad2ien authored Jun 5, 2024
2 parents 8d40c52 + fbc41ef commit 230696a
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 13 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ jobs:
uses: avto-dev/markdown-lint@v1.5.0
with:
args: "**/*.md"

lint-yaml:
name: "Yaml linter"
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v4

- name: Lint yaml files
uses: ibiqlik/action-yamllint@v3
14 changes: 14 additions & 0 deletions .yamllint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extends: default
rules:
truthy: disable
document-start: disable
line-length:
max: 200
level: warning
brackets:
min-spaces-inside: 0
max-spaces-inside: 1
braces:
forbid: false
min-spaces-inside: 0
max-spaces-inside: 1
15 changes: 9 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ services:
- POSTGRES_PASSWORD=secret
- POSTGRES_MULTIPLE_DATABASES=synapse,keycloak
healthcheck:
test: [ "CMD-SHELL", "sh -c 'pg_isready -U root -d keycloak'" ]
test: ["CMD-SHELL", "sh -c 'pg_isready -U root -d keycloak'"]
interval: 10s
timeout: 3s
retries: 3

keycloak:
image: keycloak/keycloak:23.0.3
image: keycloak/keycloak:24.0.4
ports:
- "8080:8080"
- "8443:8443"
Expand All @@ -44,9 +44,13 @@ services:

keycloak-health:
image: curlimages/curl
command: [ "sh", "-c", "while true; do sleep 1; done" ]
command: ["sh", "-c", "while true; do sleep 1; done"]
healthcheck:
test: [ "CMD-SHELL", "curl --head -fsS --insecure --http1.1 https://keycloak:8443/health/ready" ]
test:
[
"CMD-SHELL",
"curl --head -fsS --insecure --http1.1 https://keycloak:8443/health/ready",
]
interval: 15s
timeout: 30s
retries: 5
Expand All @@ -71,8 +75,7 @@ services:
- SSL_CERT_FILE=/mx-conf/cert/certificate.pem

element:
container_name: element-local
image: vectorim/element-web:v1.11.31
image: vectorim/element-web:v1.11.62
ports:
- "1983:80"
volumes:
Expand Down
8 changes: 4 additions & 4 deletions docker-test-config/mx-conf/homeserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ use_insecure_ssl_client_just_for_testing_do_not_use: true
trusted_key_servers:
- server_name: "matrix.org"
modules:
- module: white_list_module.EimisWhiteList
config:
room_id: "!TQMkeSggGjanthGUgU:matrix.local"
idp_id: "keycloak"
- module: white_list_module.EimisWhiteList
config:
room_id: ""
idp_id: "keycloak"
oidc_providers:
- idp_id: keycloak
idp_name: "EIMIS Connect"
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ dev =
# for tests
matrix-synapse
tox
twisted
aiounittest
# for type checking
mypy == 0.910
Expand Down
127 changes: 126 additions & 1 deletion tests/test_white_list_module.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
from unittest import mock

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

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 +73,105 @@ 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)

async def test_check_registration_whitelist_other_idp(self):

result = await self.module.check_registration_whitelist(
None, "yvonne", [], "not_idp"
)
self.assertEqual(result, RegistrationBehaviour.ALLOW)

async def test_check_registration_whitelist_no_idp(self):
config = EimisWhiteList.parse_config(
{
"idp_id": "",
"room_id": "room_id",
}
)
module = EimisWhiteList(config, MockModuleApi())

result = await module.check_registration_whitelist(
None, "yvonne", [], "not_idp"
)
self.assertEqual(result, RegistrationBehaviour.ALLOW)


def test_parse_config():
config = EimisWhiteList.parse_config(
{
"idp_id": "idp_id",
"room_id": "room_id",
}
)

assert config.idp_id == "oidc-idp_id"
assert config.room_id == "room_id"
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 230696a

Please sign in to comment.