def test_relationship_of_sharing_or_following_is_converted_to_diasporarequest( self, private_key): entity = Relationship(relationship="sharing") assert isinstance(get_outbound_entity(entity, private_key), DiasporaRequest) entity = Relationship(relationship="following") assert isinstance(get_outbound_entity(entity, private_key), DiasporaRequest)
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
def test_entity_is_validated__fail(self, private_key): entity = Share( actor_id="*****@*****.**", handle="*****@*****.**", id="1" * 16, guid="1" * 16, created_at=datetime.now(), target_id="2" * 16, ) with pytest.raises(ValueError): get_outbound_entity(entity, private_key)
def test_entity_is_validated__success(self, private_key): entity = Share( actor_id="*****@*****.**", handle="*****@*****.**", id="1" * 16, guid="1" * 16, created_at=datetime.now(), target_handle="*****@*****.**", target_id="2" * 16, target_guid="2" * 16, ) get_outbound_entity(entity, private_key)
def handle_create_payload(entity, author_user, to_user_key=None, parent_user=None): """Create a payload with the correct protocol. Any given user arguments must have ``private_key`` and ``handle`` attributes. :arg entity: Entity object to send. Can be a base entity or a protocol specific one. :arg author_user: User authoring the object. :arg to_user_key: Public key of user private payload is being sent to, required for private payloads. :arg parent_user: (Optional) User object of the parent object, if there is one. This must be given for the Diaspora protocol if a parent object exists, so that a proper ``parent_author_signature`` can be generated. If given, the payload will be sent as this user. :returns: Built payload message (str) """ # Just use Diaspora protocol for now protocol = Protocol() outbound_entity = get_outbound_entity(entity, author_user.private_key) if parent_user: outbound_entity.sign_with_parent(parent_user.private_key) send_as_user = parent_user if parent_user else author_user data = protocol.build_send(entity=outbound_entity, from_user=send_as_user, to_user_key=to_user_key) return data
def get_full_xml_representation(entity): """Get full XML representation of an entity. This contains the <XML><post>..</post></XML> wrapper. Accepts either a Base entity or a Diaspora entity. """ from federation.entities.diaspora.mappers import get_outbound_entity diaspora_entity = get_outbound_entity(entity) xml = diaspora_entity.to_xml() return "<XML><post>%s</post></XML>" % etree.tostring(xml).decode("utf-8")
def get_full_xml_representation(entity, private_key): """Get full XML representation of an entity. This contains the <XML><post>..</post></XML> wrapper. Accepts either a Base entity or a Diaspora entity. Author `private_key` must be given so that certain entities can be signed. """ from federation.entities.diaspora.mappers import get_outbound_entity diaspora_entity = get_outbound_entity(entity, private_key) xml = diaspora_entity.to_xml() return "<XML><post>%s</post></XML>" % etree.tostring(xml).decode("utf-8")
def test_build_send_does_right_calls(self, mock_me): mock_render = Mock(return_value="rendered") mock_me_instance = Mock(render=mock_render) mock_me.return_value = mock_me_instance protocol = Protocol() entity = DiasporaPost() private_key = get_dummy_private_key() outbound_entity = get_outbound_entity(entity, private_key) data = protocol.build_send(outbound_entity, from_user=Mock( private_key=private_key, handle="johnny@localhost", )) mock_me.assert_called_once_with( etree.tostring(entity.to_xml()), private_key=private_key, author_handle="johnny@localhost", ) mock_render.assert_called_once_with() assert data == "rendered"
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
def handle_create_payload(from_user, to_user, entity): """Create a payload with the correct protocol. Since we don't know the protocol, we need to first query the recipient. However, for a PoC implementation, supporting only Diaspora, we're going to assume that for now. Args: from_user (obj) - User sending the object to_user (obj) - Contact entry to send to entity (obj) - Entity object to send `from_user` must have `private_key` and `handle` attributes. `to_user` must have `key` attribute. """ # Just use Diaspora protocol for now protocol = Protocol() outbound_entity = get_outbound_entity(entity) data = protocol.build_send(from_user=from_user, to_user=to_user, entity=outbound_entity) return data
def test_build_send_does_right_calls__private_payload(self, mock_encrypt, mock_me): mock_render = Mock(return_value="rendered") mock_me_instance = Mock(render=mock_render) mock_me.return_value = mock_me_instance protocol = Protocol() entity = DiasporaPost() entity.validate = Mock() private_key = get_dummy_private_key() outbound_entity = get_outbound_entity(entity, private_key) data = protocol.build_send(outbound_entity, to_user_key="public key", from_user=UserType( private_key=private_key, id="johnny@localhost", handle="johnny@localhost", )) mock_me.assert_called_once_with( etree.tostring(entity.to_xml()), private_key=private_key, author_handle="johnny@localhost", ) mock_render.assert_called_once_with() mock_encrypt.assert_called_once_with( "rendered", "public key", ) assert data == "encrypted"
def handle_create_payload(entity, from_user, to_user=None): """Create a payload with the correct protocol. Since we don't know the protocol, we need to first query the recipient. However, for a PoC implementation, supporting only Diaspora, we're going to assume that for now. ``from_user`` must have ``private_key`` and ``handle`` attributes. ``to_user`` must have ``key`` attribute. :arg entity: Entity object to send :arg from_user: Profile sending the object :arg to_user: Profile entry to send to (required for non-public content) :returns: Built payload message (str) """ # Just use Diaspora protocol for now protocol = Protocol() outbound_entity = get_outbound_entity(entity) data = protocol.build_send(entity=outbound_entity, from_user=from_user, to_user=to_user) return data
def test_post_is_converted_to_diasporapost(self, private_key): entity = Post() assert isinstance(get_outbound_entity(entity, private_key), DiasporaPost)
def test_post_is_converted_to_diasporapost(self): entity = Post() assert isinstance(get_outbound_entity(entity), DiasporaPost)
def test_other_relation_raises(self): entity = Relationship(relationship="foo") with pytest.raises(ValueError): get_outbound_entity(entity)
def test_returns_entity_if_outbound_doc_on_entity(self, private_key, diasporacomment): entity = Comment() entity.outbound_doc = diasporacomment.to_xml() assert get_outbound_entity(entity, private_key) == entity
def test_other_relation_raises(self, private_key): entity = Relationship(relationship="foo") with pytest.raises(ValueError): get_outbound_entity(entity, private_key)
def test_other_reaction_raises(self): entity = Reaction(reaction="foo") with pytest.raises(ValueError): get_outbound_entity(entity)
def test_signs_relayable_if_no_signature(self, private_key): entity = DiasporaComment() outbound = get_outbound_entity(entity, private_key) assert outbound.signature != ""
def test_follow_is_converted_to_diasporacontact(self, private_key): entity = Follow() assert isinstance(get_outbound_entity(entity, private_key), DiasporaContact)
def test_retraction_is_converted_to_diasporaretraction(self): entity = Retraction() assert isinstance(get_outbound_entity(entity), DiasporaRetraction)
def test_comment_is_converted_to_diasporacomment(self, private_key): entity = Comment() assert isinstance(get_outbound_entity(entity, private_key), DiasporaComment)
def test_relationship_of_sharing_or_following_is_converted_to_diasporarequest(self): entity = Relationship(relationship="sharing") assert isinstance(get_outbound_entity(entity), DiasporaRequest) entity = Relationship(relationship="following") assert isinstance(get_outbound_entity(entity), DiasporaRequest)
def test_reaction_of_like_is_converted_to_diasporalike(self, private_key): entity = Reaction(reaction="like") assert isinstance(get_outbound_entity(entity, private_key), DiasporaLike)
def test_profile_is_converted_to_diasporaprofile(self): entity = Profile() assert isinstance(get_outbound_entity(entity), DiasporaProfile)
def test_profile_is_converted_to_diasporaprofile(self, private_key): entity = Profile() assert isinstance(get_outbound_entity(entity, private_key), DiasporaProfile)
def test_retraction_is_converted_to_diasporaretraction(self, private_key): entity = Retraction() assert isinstance(get_outbound_entity(entity, private_key), DiasporaRetraction)
def test_profile_is_converted_to_diasporaprofile(self, private_key): entity = Profile(handle="*****@*****.**", guid="1234") assert isinstance(get_outbound_entity(entity, private_key), DiasporaProfile)
def test_share_is_converted_to_diasporareshare(self, private_key): entity = Share() assert isinstance(get_outbound_entity(entity, private_key), DiasporaReshare)
def test_comment_is_converted_to_diasporacomment(self): entity = Comment() assert isinstance(get_outbound_entity(entity), DiasporaComment)
def test_returns_entity_if_outbound_doc_on_entity(self, private_key): entity = Comment() entity.outbound_doc = "foobar" assert get_outbound_entity(entity, private_key) == entity
def test_reaction_of_like_is_converted_to_diasporaplike(self): entity = Reaction(reaction="like") assert isinstance(get_outbound_entity(entity), DiasporaLike)