diff --git a/pyproject.toml b/pyproject.toml index db0d8b5..24630eb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ dev = [ # for tests "pydantic >= 1.7.4, < 2.0", "matrix-synapse == 1.103.0", + "authlib >=0.15.1", "tox", "twisted", "aiounittest", diff --git a/scripts-dev/lint.sh b/scripts-dev/lint.sh index 65d0f4f..4afce2d 100644 --- a/scripts-dev/lint.sh +++ b/scripts-dev/lint.sh @@ -15,4 +15,4 @@ set -x black "${files[@]}" ruff --fix "${files[@]}" -mypy synapse-sso-connect +mypy synapse-sso-proconnect diff --git a/tchap-pro-connect/__init__.py b/synapse_sso_proconnect/__init__.py similarity index 100% rename from tchap-pro-connect/__init__.py rename to synapse_sso_proconnect/__init__.py diff --git a/tchap-pro-connect/pro_connect_mapping.py b/synapse_sso_proconnect/proconnect_mapping.py similarity index 99% rename from tchap-pro-connect/pro_connect_mapping.py rename to synapse_sso_proconnect/proconnect_mapping.py index 90c3e12..ee8ff06 100644 --- a/tchap-pro-connect/pro_connect_mapping.py +++ b/synapse_sso_proconnect/proconnect_mapping.py @@ -78,7 +78,7 @@ async def map_user_attributes( ) # Return a dict with specific email replacements mappings. - async def getReplaceMapping(): + async def getReplaceMapping(self): return { # Specific email replacement "aaa.externe@numerique.gouv.fr" : "aaa@beta.gouv.fr", diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..fcdd259 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,29 @@ +from typing import Any, Dict, Optional +from unittest.mock import AsyncMock, Mock + +import attr +from synapse.module_api import ModuleApi, UserID + +from synapse_sso_proconnect.proconnect_mapping import ProConnectMappingProvider + +class MockHomeserver: + def get_datastores(self): + return Mock(spec=["main"]) + + def get_task_scheduler(self): + return Mock(spec=["register_action"]) + +def create_module( + config_override: Optional[Dict[str, Any]] = None, server_name: str = "example.com" +) -> ProConnectMappingProvider: + # Create a mock based on the ModuleApi spec, but override some mocked functions + # because some capabilities are needed for running the tests. + module_api = Mock(spec=ModuleApi) + + if config_override is None: + config_override = {} + config_override["id_server"] = "example.com" + + config = ProConnectMappingProvider.parse_config(config_override) + + return ProConnectMappingProvider(config, module_api) \ No newline at end of file diff --git a/tests/test_agent_connect_mapping.py b/tests/test_agent_connect_mapping.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_proconnect_mapping.py b/tests/test_proconnect_mapping.py new file mode 100644 index 0000000..81332d5 --- /dev/null +++ b/tests/test_proconnect_mapping.py @@ -0,0 +1,37 @@ +from unittest.mock import AsyncMock +import aiounittest +from tests import create_module +from synapse_sso_proconnect.proconnect_mapping import ProConnectMappingProvider + + +def create_module( +) -> ProConnectMappingProvider: + module_api = AsyncMock() + # Adding _store to the module_api object + module_api._store = AsyncMock() + module_api.getReplaceMapping = AsyncMock( + return_value={"numerique.gouv.fr": "beta.gouv.fr"} + ) + module_api._store.get_user_id_by_threepid.side_effect = lambda typ, email: ( + "test-beta" if email == "test@beta.gouv.fr" + else "test-exemple" if email == "test@example.com" + else None + ) + config= {} + return ProConnectMappingProvider(config, module_api) + + +class ProConnectMappingTest(aiounittest.AsyncTestCase): + def setUp(self) -> None: + self.module = create_module() + + + async def test_with_email_replacement(self): + + # Call the tested function with an email that requires replacement + user_id = await self.module.search_user_id_by_threepid("test@numerique.gouv.fr") + + # Assertions + self.assertEqual(user_id, "test-beta") # Should match the replaced email + self.module.module_api._store.get_user_id_by_threepid.assert_any_call("email", "test@numerique.gouv.fr") + self.module.module_api._store.get_user_id_by_threepid.assert_any_call("email", "test@beta.gouv.fr") \ No newline at end of file diff --git a/tox.ini b/tox.ini index a646c9c..239ce2c 100644 --- a/tox.ini +++ b/tox.ini @@ -16,12 +16,12 @@ commands = extras = dev commands = - - black --check --diff room_access_rules tests - - ruff --diff room_access_rules tests + - black --check --diff synapse_sso_proconnect tests + - ruff --diff synapse_sso_proconnect tests [testenv:check_types] extras = dev commands = - mypy synapse-sso-proconnect + mypy synapse_sso_proconnect