コード例 #1
0
ファイル: outbound.py プロジェクト: teotikalki/federation
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
コード例 #2
0
 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"
コード例 #3
0
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.
    """
    protocol = Protocol()
    data = protocol.build_send(from_user=from_user, to_user=to_user, entity=entity)
    return data
コード例 #4
0
ファイル: outbound.py プロジェクト: gitter-badger/federation
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
コード例 #5
0
 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"