# Path: federation/entities/diaspora/entities.py
# class DiasporaComment(DiasporaRelayableMixin, Comment):
#     """Diaspora comment."""
#     _tag_name = "comment"
# 
#     def to_xml(self):
#         element = etree.Element(self._tag_name)
#         properties = [
#             {"guid": self.guid},
#             {"parent_guid": self.root_target_guid or self.target_guid},
#             {"thread_parent_guid": self.target_guid},
#             {"author_signature": self.signature},
#             {"parent_author_signature": self.parent_signature},
#             {"text": self.raw_content},
#             {"author": self.handle},
#             {"created_at": format_dt(self.created_at)},
#         ]
#         if self.id and self.id.startswith("http"):
#             properties.append({
#                 "activitypub_id": self.id,
#             })
#         struct_to_xml(element, properties)
#         return element
# 
# class DiasporaLike(DiasporaRelayableMixin, Reaction):
#     """Diaspora like."""
#     _tag_name = "like"
#     reaction = "like"
# 
#     def to_xml(self):
#         """Convert to XML message."""
#         element = etree.Element(self._tag_name)
#         struct_to_xml(element, [
#             {"parent_type": "Post"},
#             {"guid": self.guid},
#             {"parent_guid": self.target_guid},
#             {"author_signature": self.signature},
#             {"parent_author_signature": self.parent_signature},
#             {"positive": "true"},
#             {"author": self.handle},
#         ])
#         return element
# 
# class DiasporaRetraction(DiasporaEntityMixin, Retraction):
#     """Diaspora Retraction."""
#     _tag_name = "retraction"
#     mapped = {
#         "Like": "Reaction",
#         "Photo": "Image",
#         "Person": "Profile",
#     }
# 
#     def to_xml(self):
#         """Convert to XML message."""
#         element = etree.Element(self._tag_name)
#         struct_to_xml(element, [
#             {"author": self.handle},
#             {"target_guid": self.target_guid},
#             {"target_type": DiasporaRetraction.entity_type_to_remote(self.entity_type)},
#         ])
#         return element
# 
#     @staticmethod
#     def entity_type_from_remote(value):
#         """Convert entity type between Diaspora names and our Entity names."""
#         if value in DiasporaRetraction.mapped:
#             return DiasporaRetraction.mapped[value]
#         return value
# 
#     @staticmethod
#     def entity_type_to_remote(value):
#         """Convert entity type between our Entity names and Diaspora names."""
#         if value in DiasporaRetraction.mapped.values():
#             values = list(DiasporaRetraction.mapped.values())
#             index = values.index(value)
#             return list(DiasporaRetraction.mapped.keys())[index]
#         return value
# 
# Path: federation/entities/diaspora/mappers.py
# def message_to_objects(
#         message: str, sender: str, sender_key_fetcher:Callable[[str], str]=None, user: UserType =None,
# ) -> List:
#     """Takes in a message extracted by a protocol and maps it to entities.
# 
#     :param message: XML payload
#     :type message: str
#     :param sender: Payload sender id
#     :type message: str
#     :param sender_key_fetcher: Function to fetch sender public key. If not given, key will always be fetched
#         over network. The function should take sender handle as the only parameter.
#     :param user: Optional receiving user object. If given, should have a `handle`.
#     :returns: list of entities
#     """
#     doc = etree.fromstring(message)
#     if doc.tag in TAGS:
#         return element_to_objects(doc, sender, sender_key_fetcher, user)
#     return []
# 
# Path: federation/exceptions.py
# class SignatureVerificationError(Exception):
#     """Authenticity of the signature could not be verified given the key."""
#     pass
# 
# Path: federation/tests/fixtures/keys.py
# def get_dummy_private_key():
#     return RSA.importKey(PRIVATE_KEY)


from unittest.mock import patch, Mock
from lxml import etree
from federation.entities.diaspora.entities import DiasporaComment, DiasporaLike, DiasporaRetraction
from federation.entities.diaspora.mappers import message_to_objects
from federation.exceptions import SignatureVerificationError
from federation.tests.fixtures.keys import get_dummy_private_key
from federation.tests.fixtures.payloads import DIASPORA_POST_COMMENT
import pytest




class TestEntitiesConvertToXML:
    def test_post_to_xml(self, diasporapost):
        result = diasporapost.to_xml()
        assert result.tag == "status_message"
        assert len(result.find("created_at").text) > 0
        result.find("created_at").text = ""  # timestamp makes testing painful
        converted = b"<status_message><text>raw_content</text><guid>guid</guid>" \
                    b"<author>alice@example.com</author><public>true</public><created_at>" \
                    b"</created_at><provider_display_name>Socialhome</provider_display_name></status_message>"

# Path: federation/entities/diaspora/entities.py
# class DiasporaComment(DiasporaRelayableMixin, Comment):
#     """Diaspora comment."""
#     _tag_name = "comment"
# 
#     def to_xml(self):
#         element = etree.Element(self._tag_name)
#         properties = [
#             {"guid": self.guid},
#             {"parent_guid": self.root_target_guid or self.target_guid},
#             {"thread_parent_guid": self.target_guid},
#             {"author_signature": self.signature},
#             {"parent_author_signature": self.parent_signature},
#             {"text": self.raw_content},
#             {"author": self.handle},
#             {"created_at": format_dt(self.created_at)},
#         ]
#         if self.id and self.id.startswith("http"):
#             properties.append({
#                 "activitypub_id": self.id,
#             })
#         struct_to_xml(element, properties)
#         return element
# 
# class DiasporaLike(DiasporaRelayableMixin, Reaction):
#     """Diaspora like."""
#     _tag_name = "like"
#     reaction = "like"
# 
#     def to_xml(self):
#         """Convert to XML message."""
#         element = etree.Element(self._tag_name)
#         struct_to_xml(element, [
#             {"parent_type": "Post"},
#             {"guid": self.guid},
#             {"parent_guid": self.target_guid},
#             {"author_signature": self.signature},
#             {"parent_author_signature": self.parent_signature},
#             {"positive": "true"},
#             {"author": self.handle},
#         ])
#         return element
# 
# class DiasporaRetraction(DiasporaEntityMixin, Retraction):
#     """Diaspora Retraction."""
#     _tag_name = "retraction"
#     mapped = {
#         "Like": "Reaction",
#         "Photo": "Image",
#         "Person": "Profile",
#     }
# 
#     def to_xml(self):
#         """Convert to XML message."""
#         element = etree.Element(self._tag_name)
#         struct_to_xml(element, [
#             {"author": self.handle},
#             {"target_guid": self.target_guid},
#             {"target_type": DiasporaRetraction.entity_type_to_remote(self.entity_type)},
#         ])
#         return element
# 
#     @staticmethod
#     def entity_type_from_remote(value):
#         """Convert entity type between Diaspora names and our Entity names."""
#         if value in DiasporaRetraction.mapped:
#             return DiasporaRetraction.mapped[value]
#         return value
# 
#     @staticmethod
#     def entity_type_to_remote(value):
#         """Convert entity type between our Entity names and Diaspora names."""
#         if value in DiasporaRetraction.mapped.values():
#             values = list(DiasporaRetraction.mapped.values())
#             index = values.index(value)
#             return list(DiasporaRetraction.mapped.keys())[index]
#         return value
# 
# Path: federation/entities/diaspora/mappers.py
# def message_to_objects(
#         message: str, sender: str, sender_key_fetcher:Callable[[str], str]=None, user: UserType =None,
# ) -> List:
#     """Takes in a message extracted by a protocol and maps it to entities.
# 
#     :param message: XML payload
#     :type message: str
#     :param sender: Payload sender id
#     :type message: str
#     :param sender_key_fetcher: Function to fetch sender public key. If not given, key will always be fetched
#         over network. The function should take sender handle as the only parameter.
#     :param user: Optional receiving user object. If given, should have a `handle`.
#     :returns: list of entities
#     """
#     doc = etree.fromstring(message)
#     if doc.tag in TAGS:
#         return element_to_objects(doc, sender, sender_key_fetcher, user)
#     return []
# 
# Path: federation/exceptions.py
# class SignatureVerificationError(Exception):
#     """Authenticity of the signature could not be verified given the key."""
#     pass
# 
# Path: federation/tests/fixtures/keys.py
# def get_dummy_private_key():
#     return RSA.importKey(PRIVATE_KEY)


# Path: federation/tests/entities/diaspora/test_entities.py
from unittest.mock import patch, Mock
from lxml import etree
from federation.entities.diaspora.entities import DiasporaComment, DiasporaLike, DiasporaRetraction
from federation.entities.diaspora.mappers import message_to_objects
from federation.exceptions import SignatureVerificationError
from federation.tests.fixtures.keys import get_dummy_private_key
from federation.tests.fixtures.payloads import DIASPORA_POST_COMMENT
import pytest





class TestEntitiesConvertToXML:
    def test_post_to_xml(self, diasporapost):
        result = diasporapost.to_xml()
        assert result.tag == "status_message"
        assert len(result.find("created_at").text) > 0
        result.find("created_at").text = ""  # timestamp makes testing painful
        converted = b"<status_message><text>raw_content</text><guid>guid</guid>" \
                    b"<author>alice@example.com</author><public>true</public><created_at>" \
                    b"</created_at><provider_display_name>Socialhome</provider_display_name></status_message>"

        assert etree.tostring(result) == converted