def test_profile_to_xml(self): entity = DiasporaProfile( handle="*****@*****.**", raw_content="foobar", name="Bob Bobertson", public=True, tag_list=["socialfederation", "federation"], image_urls={ "large": "urllarge", "medium": "urlmedium", "small": "urlsmall" } ) result = entity.to_xml() assert result.tag == "profile" converted = b"<profile><diaspora_handle>[email protected]</diaspora_handle>" \ b"<first_name>Bob Bobertson</first_name><last_name></last_name><image_url>urllarge</image_url>" \ b"<image_url_small>urlsmall</image_url_small><image_url_medium>urlmedium</image_url_medium>" \ b"<gender></gender><bio>foobar</bio><location></location><searchable>true</searchable>" \ b"<nsfw>false</nsfw><tag_string>#socialfederation #federation</tag_string></profile>" assert etree.tostring(result) == converted
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 parse_profile_from_hcard(hcard: str, handle: str): """ Parse all the fields we can from a hCard document to get a Profile. :arg hcard: HTML hcard document (str) :arg handle: User handle in [email protected] format :returns: ``federation.entities.diaspora.entities.DiasporaProfile`` instance """ from federation.entities.diaspora.entities import DiasporaProfile # Circulars doc = html.fromstring(hcard) profile = DiasporaProfile( name=_get_element_text_or_none(doc, ".fn"), image_urls={ "small": _get_element_attr_or_none(doc, ".entity_photo_small .photo", "src"), "medium": _get_element_attr_or_none(doc, ".entity_photo_medium .photo", "src"), "large": _get_element_attr_or_none(doc, ".entity_photo .photo", "src"), }, public=True, id=handle, handle=handle, guid=_get_element_text_or_none(doc, ".uid"), public_key=_get_element_text_or_none(doc, ".key"), username=handle.split('@')[0], _source_protocol="diaspora", ) return profile
def diasporaprofile(): return DiasporaProfile( handle="*****@*****.**", raw_content="foobar", name="Bob Bobertson", public=True, tag_list=["socialfederation", "federation"], image_urls={ "large": "urllarge", "medium": "urlmedium", "small": "urlsmall" } )
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_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_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 diasporaprofile_activitypub_id(): return DiasporaProfile( raw_content="foobar", name="Bob Bobertson", public=True, tag_list=["socialfederation", "federation"], image_urls={ "large": "urllarge", "medium": "urlmedium", "small": "urlsmall" }, id="http://example.com/alice", handle="*****@*****.**", guid="guid", )
def test_calls_retrieve_and_parse_profile(self, mock_retrieve): mock_retrieve.return_value = Profile(guid="guidguidguidguid") attrs = {"handle": "foo"} attrs = DiasporaProfile.fill_extra_attributes(attrs) assert attrs == {"handle": "foo", "guid": "guidguidguidguid"}
def test_raises_if_no_handle(self): attrs = {"foo": "bar"} with pytest.raises(ValueError): DiasporaProfile.fill_extra_attributes(attrs)