Skip to content

Commit

Permalink
add TU to test whole module
Browse files Browse the repository at this point in the history
  • Loading branch information
odelcroi committed Dec 10, 2024
1 parent c44b93b commit 6949a15
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 38 deletions.
42 changes: 42 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from unittest.mock import AsyncMock
from synapse_sso_proconnect.proconnect_mapping import ProConnectMappingProvider
from synapse.types import UserID, UserInfo




def create_module(config
) -> ProConnectMappingProvider:
module_api = AsyncMock()
# Adding _store to the module_api object
module_api._store = AsyncMock()
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 "@test:numerique" if email == "[email protected]"
else "@test:old" if email == "[email protected]"
else None
)
module_api.get_userinfo_by_id.side_effect = lambda mapped_user_id: (
UserInfo(
user_id=UserID.from_string(mapped_user_id),
is_admin=False,
is_guest=False,
consent_server_notice_sent=None,
consent_ts=None,
consent_version=None,
appservice_id=None,
creation_ts=0,
user_type=None,
is_deactivated=False,
locked=False,
is_shadow_banned=False,
approved=True,
suspended=False,
)
)

parsed_config = ProConnectMappingProvider.parse_config(config)
return ProConnectMappingProvider(parsed_config, module_api)


66 changes: 28 additions & 38 deletions tests/test_proconnect_mapping.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,63 @@
from unittest.mock import AsyncMock
import aiounittest
from synapse_sso_proconnect.proconnect_mapping import ProConnectMappingProvider
from tests import create_module

from authlib.oidc.core import UserInfo # type: ignore


def create_module(config
) -> ProConnectMappingProvider:
module_api = AsyncMock()
# Adding _store to the module_api object
module_api._store = AsyncMock()
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 "test-numerique" if email == "[email protected]"
else "test-old" if email == "[email protected]"
else None
)
parsed_config = ProConnectMappingProvider.parse_config(config)
return ProConnectMappingProvider(parsed_config, module_api)


class ProConnectMappingTest(aiounittest.AsyncTestCase):
#def setUp(self) -> None:

async def test_with_map_should_replace(self):
async def test_module(self):
self.module = create_module({"user_id_lookup_fallback_rules":
[
{"match":"very-new.fr", "search": "beta.fr"},
{ "match":"new.fr","search":"beta.fr"}
]})
# Call the tested function with an email that requires replacement
user_id = await self.module.search_user_id_by_threepid("[email protected]")
# user_id = await self.module.search_user_id_by_threepid("[email protected]")
user_info = await self.module.map_user_attributes(
userinfo=UserInfo(email="[email protected]"), token="", failures=0)

# Assertions
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]")
self.assertEqual(user_id, "test-beta") # Should match the replaced email
self.assertEqual(user_info['localpart'], 'test') # Should match the replaced email


async def test_replace_by_priority(self):
async def test_search_user_id_with_map_should_replace(self):
self.module = create_module({"user_id_lookup_fallback_rules":
[{"match":"[email protected]", "search": "[email protected]"},
{ "match":"new.fr","search":"beta.fr"}
[
{"match":"very-new.fr", "search": "beta.fr"},
{ "match":"new.fr","search":"beta.fr"}
]})


#replace by domain leads to a dead-end but it lower in the list

# Call the tested function with an email that requires replacement
user_id = await self.module.search_user_id_by_threepid("[email protected]")

# Assertions
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]")
self.assertEqual(user_id, "test-old") # Should match the replaced email
self.module.module_api._store.get_user_id_by_threepid.assert_any_call("email", "[email protected]")
self.assertEqual(user_id, "@test:beta") # Should match the replaced email


async def test_with_map_should_not_replace(self):
async def test_search_user_id_replace_by_priority(self):
self.module = create_module({"user_id_lookup_fallback_rules":
[{ "match":"new.fr","search":"beta.fr"}]})
[{"match":"[email protected]", "search": "[email protected]"},
{ "match":"new.fr","search":"beta.fr"}
]}) #replace by domain leads to a dead-end but it lower in the list

# Call the tested function with an email that requires replacement
user_id = await self.module.search_user_id_by_threepid("[email protected]")

user_id = await self.module.search_user_id_by_threepid("[email protected]")
# Assertions
self.assertEqual(user_id, "test-numerique")
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]")
self.assertEqual(user_id, "@test:old") # Should match the replaced email

async def test_with_empty_map(self):
async def test_search_user_id_with_empty_map(self):

self.module = create_module({"user_id_lookup_fallback_rules":[]})

# 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-numerique")
self.assertEqual(user_id, "@test:numerique")

0 comments on commit 6949a15

Please sign in to comment.