Make retrieve_remote_profile accept an ID

merge-requests/131/head
Jason Robinson 2018-08-12 17:58:59 +03:00
rodzic d2cba74a9d
commit 4c24e57bff
2 zmienionych plików z 12 dodań i 11 usunięć

Wyświetl plik

@ -1,5 +1,9 @@
import importlib import importlib
import logging import logging
from typing import Optional
from federation.entities.base import Profile
from federation.utils.diaspora import parse_profile_diaspora_id
logger = logging.getLogger("federation") logger = logging.getLogger("federation")
@ -19,19 +23,16 @@ def retrieve_remote_content(id, sender_key_fetcher=None):
return utils.retrieve_and_parse_content(id, sender_key_fetcher=sender_key_fetcher) return utils.retrieve_and_parse_content(id, sender_key_fetcher=sender_key_fetcher)
def retrieve_remote_profile(handle): def retrieve_remote_profile(id: str) -> Optional[Profile]:
"""High level retrieve profile method. """High level retrieve profile method.
Retrieve the profile from a remote location, using either the given protocol or by checking each Retrieve the profile from a remote location, using either the given protocol or by checking each
protocol until a user can be constructed from the remote documents. protocol until a user can be constructed from the remote documents.
Currently, due to no other protocols supported, always use the Diaspora protocol. Currently, due to no other protocols supported, always use the Diaspora protocol.
:param handle: The profile handle in format username@domain.tld
:returns: ``federation.entities.base.Profile`` or ``None``
""" """
# TODO add support for AP
protocol_name = "diaspora" protocol_name = "diaspora"
utils = importlib.import_module("federation.utils.%s" % protocol_name) utils = importlib.import_module("federation.utils.%s" % protocol_name)
if not handle.islower(): handle, _guid = parse_profile_diaspora_id(id.lower())
logger.warning("retrieve_remote_profile - Handle is not lower case! Will use lower case version to continue.") return utils.retrieve_and_parse_profile(handle)
return utils.retrieve_and_parse_profile(handle.lower())

Wyświetl plik

@ -19,12 +19,12 @@ class TestRetrieveRemoteProfile:
def test_calls_diaspora_retrieve_and_parse_profile(self, mock_import): def test_calls_diaspora_retrieve_and_parse_profile(self, mock_import):
mock_retrieve = Mock() mock_retrieve = Mock()
mock_import.return_value = mock_retrieve mock_import.return_value = mock_retrieve
retrieve_remote_profile("foo@bar") retrieve_remote_profile("diaspora://user@example.com/profile/1234")
mock_retrieve.retrieve_and_parse_profile.assert_called_once_with("foo@bar") mock_retrieve.retrieve_and_parse_profile.assert_called_once_with("user@example.com")
@patch("federation.fetchers.importlib.import_module") @patch("federation.fetchers.importlib.import_module")
def test_calls_diaspora_retrieve_and_parse_profile__lower_cases_handle_when_needed(self, mock_import): def test_calls_diaspora_retrieve_and_parse_profile__lower_cases_handle_when_needed(self, mock_import):
mock_retrieve = Mock() mock_retrieve = Mock()
mock_import.return_value = mock_retrieve mock_import.return_value = mock_retrieve
retrieve_remote_profile("foo@Bar") retrieve_remote_profile("diaspora://uSer@ExamPle.com/profile/1234")
mock_retrieve.retrieve_and_parse_profile.assert_called_once_with("foo@bar") mock_retrieve.retrieve_and_parse_profile.assert_called_once_with("user@example.com")