def test_comment_to_xml(self):
     entity = DiasporaComment(raw_content="raw_content", guid="guid", target_guid="target_guid", handle="handle")
     result = entity.to_xml()
     assert result.tag == "comment"
     converted = b"<comment><guid>guid</guid><parent_guid>target_guid</parent_guid>" \
                 b"<author_signature></author_signature><text>raw_content</text>" \
                 b"<diaspora_handle>handle</diaspora_handle></comment>"
     assert etree.tostring(result) == converted
Exemple #2
0
 def test_signing_comment_works(self, mock_format_dt):
     entity = DiasporaComment(
         raw_content="raw_content", guid="guid", target_guid="target_guid", handle="handle",
         created_at="created_at",
     )
     entity.sign(get_dummy_private_key())
     assert entity.signature == "OWvW/Yxw4uCnx0WDn0n5/B4uhyZ8Pr6h3FZaw8J7PCXyPluOfYXFoHO21bykP8c2aVnuJNHe+lmeAkUC" \
                                "/kHnl4yxk/jqe3uroW842OWvsyDRQ11vHxhIqNMjiepFPkZmXX3vqrYYh5FrC/tUsZrEc8hHoOIHXFR2" \
                                "kGD0gPV+4EEG6pbMNNZ+SBVun0hvruX8iKQVnBdc/+zUI9+T/MZmLyqTq/CvuPxDyHzQPSHi68N9rJyr" \
                                "4Xa1K+R33Xq8eHHxs8LVNRqzaHGeD3DX8yBu/vP9TYmZsiWlymbuGwLCa4Yfv/VS1hQZovhg6YTxV4CR" \
                                "v4ToGL+CAJ7UHEugRRBwDw=="
 def test_handle_create_payload_calls_sign_with_parent(
         self, mock_get_outbound_entity):
     comment = DiasporaComment()
     mock_get_outbound_entity.return_value = comment
     author_user = Mock(private_key=RSA.generate(2048),
                        handle="*****@*****.**")
     parent_user = Mock(private_key=RSA.generate(2048),
                        handle="*****@*****.**")
     entity = DiasporaComment()
     with patch.object(comment, "sign_with_parent") as mock_sign:
         handle_create_payload(entity, author_user, parent_user=parent_user)
         mock_sign.assert_called_once_with(parent_user.private_key)
Exemple #4
0
 def test_comment_to_xml(self):
     entity = DiasporaComment(
         raw_content="raw_content", guid="guid", target_guid="target_guid", handle="handle",
         signature="signature"
     )
     result = entity.to_xml()
     assert result.tag == "comment"
     assert len(result.find("created_at").text) > 0
     result.find("created_at").text = ""  # timestamp makes testing painful
     converted = b"<comment><guid>guid</guid><parent_guid>target_guid</parent_guid>" \
                 b"<author_signature>signature</author_signature><parent_author_signature>" \
                 b"</parent_author_signature><text>raw_content</text><author>handle</author>" \
                 b"<created_at></created_at></comment>"
     assert etree.tostring(result) == converted
 def test_calls_verify_signature(self, mock_verify):
     entity = DiasporaComment()
     entity._sender_key = "key"
     entity._source_object = "<obj></obj>"
     entity.signature = "sig"
     mock_verify.return_value = False
     with pytest.raises(SignatureVerificationError):
         entity._validate_signatures()
     mock_verify.reset_mock()
     mock_verify.return_value = True
     entity._validate_signatures()
Exemple #6
0
def get_outbound_entity(entity):
    """Get the correct outbound entity for this protocol.

    We might have to look at entity values to decide the correct outbound entity.
    If we cannot find one, we should raise as conversion cannot be guaranteed to the given protocol.

    :arg entity: An entity instance which can be of a base or protocol entity class.
    :returns: Protocol specific entity class instance.
    :raises ValueError: If conversion cannot be done.
    """
    cls = entity.__class__
    if cls in [DiasporaPost, DiasporaRequest, DiasporaComment, DiasporaLike, DiasporaProfile, DiasporaRetraction]:
        # Already fine
        return entity
    elif cls == Post:
        return DiasporaPost.from_base(entity)
    elif cls == Comment:
        return DiasporaComment.from_base(entity)
    elif cls == Reaction:
        if entity.reaction == "like":
            return DiasporaLike.from_base(entity)
    elif cls == Relationship:
        if entity.relationship in ["sharing", "following"]:
            # Unfortunately we must send out in both cases since in Diaspora they are the same thing
            return DiasporaRequest.from_base(entity)
    elif cls == Profile:
        return DiasporaProfile.from_base(entity)
    elif cls == Retraction:
        return DiasporaRetraction.from_base(entity)
    raise ValueError("Don't know how to convert this base entity to Diaspora protocol entities.")
Exemple #7
0
def get_outbound_entity(entity):
    """Get the correct outbound entity for this protocol.

    We might have to look at entity values to decide the correct outbound entity.
    If we cannot find one, we should raise as conversion cannot be guaranteed to the given protocol.

    :arg entity: An entity instance which can be of a base or protocol entity class.
    :returns: Protocol specific entity class instance.
    :raises ValueError: If conversion cannot be done.
    """
    cls = entity.__class__
    if cls in [DiasporaPost, DiasporaRequest, DiasporaComment, DiasporaLike, DiasporaProfile, DiasporaRetraction]:
        # Already fine
        return entity
    elif cls == Post:
        return DiasporaPost.from_base(entity)
    elif cls == Comment:
        return DiasporaComment.from_base(entity)
    elif cls == Reaction:
        if entity.reaction == "like":
            return DiasporaLike.from_base(entity)
    elif cls == Relationship:
        if entity.relationship in ["sharing", "following"]:
            # Unfortunately we must send out in both cases since in Diaspora they are the same thing
            return DiasporaRequest.from_base(entity)
    elif cls == Profile:
        return DiasporaProfile.from_base(entity)
    elif cls == Retraction:
        return DiasporaRetraction.from_base(entity)
    raise ValueError("Don't know how to convert this base entity to Diaspora protocol entities.")
Exemple #8
0
def get_outbound_entity(entity, private_key):
    """Get the correct outbound entity for this protocol.

    We might have to look at entity values to decide the correct outbound entity.
    If we cannot find one, we should raise as conversion cannot be guaranteed to the given protocol.

    Private key of author is needed to be passed for signing the outbound entity.

    :arg entity: An entity instance which can be of a base or protocol entity class.
    :returns: Protocol specific entity class instance.
    :raises ValueError: If conversion cannot be done.
    """
    if getattr(entity, "outbound_doc", None):
        # If the entity already has an outbound doc, just return the entity as is
        return entity
    outbound = None
    cls = entity.__class__
    if cls in [
            DiasporaPost, DiasporaRequest, DiasporaComment, DiasporaLike,
            DiasporaProfile, DiasporaRetraction, DiasporaContact,
            DiasporaReshare
    ]:
        # Already fine
        outbound = entity
    elif cls == Post:
        outbound = DiasporaPost.from_base(entity)
    elif cls == Comment:
        outbound = DiasporaComment.from_base(entity)
    elif cls == Reaction:
        if entity.reaction == "like":
            outbound = DiasporaLike.from_base(entity)
    elif cls == Relationship:
        if entity.relationship in ["sharing", "following"]:
            # Unfortunately we must send out in both cases since in Diaspora they are the same thing
            outbound = DiasporaRequest.from_base(entity)
    elif cls == Follow:
        outbound = DiasporaContact.from_base(entity)
    elif cls == Profile:
        outbound = DiasporaProfile.from_base(entity)
    elif cls == Retraction:
        outbound = DiasporaRetraction.from_base(entity)
    elif cls == Share:
        outbound = DiasporaReshare.from_base(entity)
    if not outbound:
        raise ValueError(
            "Don't know how to convert this base entity to Diaspora protocol entities."
        )
    if isinstance(outbound, DiasporaRelayableMixin) and not outbound.signature:
        # Sign by author if not signed yet. We don't want to overwrite any existing signature in the case
        # that this is being sent by the parent author
        outbound.sign(private_key)
        # If missing, also add same signature to `parent_author_signature`. This is required at the moment
        # in all situations but is apparently being removed.
        # TODO: remove this once Diaspora removes the extra signature
        outbound.parent_signature = outbound.signature
    return outbound
 def test_signing_comment_works(self, mock_format_dt):
     entity = DiasporaComment(
         raw_content="raw_content",
         created_at="created_at",
         actor_id="handle",
         handle="handle",
         id="guid",
         guid="guid",
         target_id="target_guid",
         target_guid="target_guid",
         root_target_id="target_guid",
         root_target_guid="target_guid",
     )
     entity.sign(get_dummy_private_key())
     assert entity.signature == "XZYggFdQHOicguZ0ReVJkYiK5othHgBgAtwnSmm4NR31qeLa76Ur/i2B5Xi9dtopDlNS8EbFy+MLJ1ds" \
                                "ovDjPsVC1nLZrL57y0v+HtwJas6hQqNbvmEyr1q6X+0p1i93eINzt/7bxcP5uEGxy8J4ItsJzbDVLlC5" \
                                "3ZtIg7pmhR0ltqNqBHrgL8WDokfGKFlXqANchbD+Xeyv2COGbI78LwplVdYjHW1+jefjpYhMCxayIvMv" \
                                "WS8TV1hMTqUz+zSqoCHU04RgjjGW8e8vINDblQwMfEMeJ5T6OP5RiU3zCqDc3uL2zxHHh9IGC+clVuhP" \
                                "HTv8tHUHNLgc2vIzRtGh6w=="
Exemple #10
0
def diasporacomment_activitypub_id():
    return DiasporaComment(
        raw_content="raw_content",
        signature="signature",
        id="https://domain.tld/id",
        guid="guid",
        actor_id="*****@*****.**",
        handle="*****@*****.**",
        target_id="target_guid",
        target_guid="target_guid",
    )
 def test_already_fine_entities_are_returned_as_is(self):
     entity = DiasporaPost()
     assert get_outbound_entity(entity) == entity
     entity = DiasporaLike()
     assert get_outbound_entity(entity) == entity
     entity = DiasporaComment()
     assert get_outbound_entity(entity) == entity
     entity = DiasporaRequest()
     assert get_outbound_entity(entity) == entity
     entity = DiasporaProfile()
     assert get_outbound_entity(entity) == entity
Exemple #12
0
def diasporacomment():
    return DiasporaComment(
        raw_content="raw_content",
        signature="signature",
        id="guid",
        guid="guid",
        actor_id="*****@*****.**",
        handle="*****@*****.**",
        target_id="target_guid",
        target_guid="target_guid",
    )
Exemple #13
0
 def test_already_fine_entities_are_returned_as_is(self, private_key):
     entity = DiasporaPost()
     assert get_outbound_entity(entity, private_key) == entity
     entity = DiasporaLike()
     assert get_outbound_entity(entity, private_key) == entity
     entity = DiasporaComment()
     assert get_outbound_entity(entity, private_key) == entity
     entity = DiasporaProfile(handle="*****@*****.**", guid="1234")
     assert get_outbound_entity(entity, private_key) == entity
     entity = DiasporaContact()
     assert get_outbound_entity(entity, private_key) == entity
     entity = DiasporaReshare()
     assert get_outbound_entity(entity, private_key) == entity
Exemple #14
0
def diasporacomment():
    return DiasporaComment(
        raw_content="raw_content", guid="guid", target_guid="target_guid", handle="handle",
        signature="signature"
    )
 def test_raises_if_no_sender_key(self):
     entity = DiasporaComment()
     with pytest.raises(SignatureVerificationError):
         entity._validate_signatures()
 def test_sign_with_parent__calls_to_xml(self, mock_validate):
     entity = DiasporaComment()
     with patch.object(entity, "to_xml") as mock_to_xml:
         entity.sign_with_parent(get_dummy_private_key())
         mock_to_xml.assert_called_once_with()
 def test_signs_relayable_if_no_signature(self, private_key):
     entity = DiasporaComment()
     outbound = get_outbound_entity(entity, private_key)
     assert outbound.signature != ""