def test_handle_create_payload_calls_get_outbound_entity_with_author_user( self, mock_get_outbound_entity): mock_get_outbound_entity.return_value = DiasporaPost() author_user = Mock(private_key=RSA.generate(2048), handle="*****@*****.**") entity = DiasporaPost() handle_create_payload(entity, author_user) mock_get_outbound_entity.assert_called_once_with( entity, author_user.private_key)
def test_handle_create_payload___diaspora__calls_magic_envelope_render( self, mock_me): mock_render = Mock() mock_me.return_value = Mock(render=mock_render) author_user = Mock() entity = DiasporaPost() entity.validate = Mock() handle_create_payload(entity, author_user, "diaspora") mock_render.assert_called_once_with()
def test_post_to_xml(self): entity = DiasporaPost(raw_content="raw_content", guid="guid", handle="handle", public=True) result = entity.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><raw_message>raw_content</raw_message><guid>guid</guid>" \ b"<diaspora_handle>handle</diaspora_handle><public>true</public><created_at>" \ b"</created_at></status_message>" assert etree.tostring(result) == converted
def test_post_to_xml(self): entity = DiasporaPost( raw_content="raw_content", guid="guid", handle="handle", public=True, provider_display_name="Socialhome" ) result = entity.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><raw_message>raw_content</raw_message><guid>guid</guid>" \ b"<diaspora_handle>handle</diaspora_handle><public>true</public><created_at>" \ b"</created_at><provider_display_name>Socialhome</provider_display_name></status_message>" assert etree.tostring(result) == converted
def test_get_send_to_nodes_with_like_returns_nodes_for_post(self): Node.create(host="sub.example.com") save_post_metadata(DiasporaPost(guid="12345"), "diaspora", ["sub.example.com"]) nodes = get_send_to_nodes("*****@*****.**", DiasporaLike(target_guid="12345")) assert nodes == ["sub.example.com"]
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.")
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"
class TestReceiveWorkerProcessCallsSendPayload(object): @patch("workers.receive.handle_receive", return_value=("*****@*****.**", "diaspora", [DiasporaPost(raw_content="Awesome #post")])) @patch("workers.receive.send_document", return_value=(200, None)) def test_send_payload_called(self, mock_handle_receive, mock_send_document, mock_pod_preferences): process(Mock()) assert mock_send_document.call_count == 1
def test_handle_create_payload_builds_an_xml(self, mock_protocol_class): mock_protocol = Mock() mock_protocol_class.return_value = mock_protocol author_user = Mock() entity = DiasporaPost() handle_create_payload(entity, author_user) mock_protocol.build_send.assert_called_once_with(entity=entity, from_user=author_user, to_user_key=None)
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 diasporapost_activitypub_id(): return DiasporaPost( raw_content="raw_content", public=True, provider_display_name="Socialhome", id="https://domain.tld/id", guid="guid", actor_id="*****@*****.**", handle="*****@*****.**", )
def diasporapost(): return DiasporaPost( raw_content="raw_content", public=True, provider_display_name="Socialhome", id="guid", guid="guid", actor_id="*****@*****.**", handle="*****@*****.**", )
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_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 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
class TestReceiveWorkerProcessCallsSendPayload: @patch("social_relay.utils.data.get_pod_preferences", return_value={}) @patch("workers.receive.handle_receive", return_value=("*****@*****.**", "diaspora", [DiasporaPost(raw_content="Awesome #post")])) @patch("workers.receive.send_document", return_value=(200, None)) def test_send_payload_called(self, mock_send_document, mock_handle_receive, mock_pod_preferences): process("payload") assert mock_send_document.call_count == 1 mock_send_document.assert_called_once_with( url="https://sub.example.com/receive/public", data="payload", headers=HEADERS, )
def test_get_send_to_nodes_with_post_returns_nodes( self, mock_nodes_who_want_all): nodes = get_send_to_nodes("*****@*****.**", DiasporaPost()) assert nodes == ["sub.example.com"]
def test_get_send_to_nodes_with_post_returns_nodes_with_tags( self, mock_nodes_who_want_tags, mock_nodes_who_want_all): nodes = get_send_to_nodes("*****@*****.**", DiasporaPost()) assert set(nodes) == {"sub.example.com", "tags.example.com"}
@patch("social_relay.utils.data.get_pod_preferences", return_value={}) class TestReceiveWorkerProcessCallsSendPayload(object): @patch("workers.receive.handle_receive", return_value=("*****@*****.**", "diaspora", [DiasporaPost(raw_content="Awesome #post")])) @patch("workers.receive.send_document", return_value=(200, None)) def test_send_payload_called(self, mock_handle_receive, mock_send_document, mock_pod_preferences): process(Mock()) assert mock_send_document.call_count == 1 @pytest.mark.usefixtures('config') @patch("workers.receive.handle_receive", return_value=("*****@*****.**", "diaspora", [ DiasporaPost(raw_content="Awesome #post", guid="foobar") ])) @patch("workers.receive.send_document", return_value=(200, None)) @patch("social_relay.utils.data.get_pod_preferences", return_value={}) class TestReceiveWorkerStoresNodeAndPostMetadata(object): def test_send_payload_stores_unknown_node_into_db(self, mock_handle_receive, mock_send_document, mock_pod_preferences): process(Mock()) assert Node.select().count() == 1 node = Node.get(Node.host == "sub.example.com") assert node assert node.https def test_send_payload_updates_existing_node_in_db(self,
def diasporapost(): return DiasporaPost( raw_content="raw_content", guid="guid", handle="handle", public=True, provider_display_name="Socialhome" )