Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
odelcroi committed Dec 4, 2024
1 parent b1c88d4 commit f3c9785
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 5 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion scripts-dev/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ set -x

black "${files[@]}"
ruff --fix "${files[@]}"
mypy synapse-sso-connect
mypy synapse-sso-proconnect
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
"[email protected]" : "[email protected]",
Expand Down
29 changes: 29 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file.
37 changes: 37 additions & 0 deletions tests/test_proconnect_mapping.py
Original file line number Diff line number Diff line change
@@ -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 == "[email protected]"
else "test-exemple" if email == "[email protected]"
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("[email protected]")

# 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", "[email protected]")
self.module.module_api._store.get_user_id_by_threepid.assert_any_call("email", "[email protected]")
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f3c9785

Please sign in to comment.