Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
def element_to_objects(tree):
    """Transform an Element tree to a list of entities recursively.

    Possible child entities are added to each entity `_children` list.

    :param tree: Element
    :returns: list of entities
    """
    entities = []
    for element in tree:
        cls = MAPPINGS.get(element.tag, None)
        if not cls:
            continue

        attrs = xml_children_as_dict(element)
        transformed = transform_attributes(attrs)
        if hasattr(cls, "fill_extra_attributes"):
            transformed = cls.fill_extra_attributes(transformed)
        entity = cls(**transformed)
        try:
            entity.validate()
        except ValueError as ex:
            logger.error("Failed to validate entity %s: %s", entity, ex, extra={
                "attrs": attrs,
                "transformed": transformed,
            })
            continue
        # Do child elements
        entity._children = element_to_objects(element)
        # Add to entities list
        entities.append(entity)
        if cls == DiasporaRequest:
            # We support sharing/following separately, so also generate base Relationship for the following part
            transformed.update({"relationship": "following"})
            relationship = Relationship(**transformed)
            entities.append(relationship)
    return entities
Exemplo n.º 3
0
def element_to_objects(element, sender, sender_key_fetcher=None, user=None):
    """Transform an Element to a list of entities recursively.

    Possible child entities are added to each entity `_children` list.

    :param tree: Element
    :param sender: Payload sender handle
    :param sender_key_fetcher: Function to fetch sender public key. If not given, key will always be fetched
        over network. The function should take sender handle as the only parameter.
    :param user: Optional receiving user object. If given, should have a `handle`.
    :returns: list of entities
    """
    entities = []
    cls = MAPPINGS.get(element.tag, None)
    if not cls:
        return []

    attrs = xml_children_as_dict(element)
    transformed = transform_attributes(attrs, cls)
    if hasattr(cls, "fill_extra_attributes"):
        transformed = cls.fill_extra_attributes(transformed)
    entity = cls(**transformed)
    # Add protocol name
    entity._source_protocol = "diaspora"
    # Save element object to entity for possible later use
    entity._source_object = etree.tostring(element)
    # Save receiving guid to object
    if user and hasattr(user, "guid"):
        entity._receiving_guid = user.guid
    if issubclass(cls, DiasporaRelayableMixin):
        # If relayable, fetch sender key for validation
        entity._xml_tags = get_element_child_info(element, "tag")
        if sender_key_fetcher:
            entity._sender_key = sender_key_fetcher(entity.handle)
        else:
            profile = retrieve_and_parse_profile(entity.handle)
            if profile:
                entity._sender_key = profile.public_key
    else:
        # If not relayable, ensure handles match
        if not check_sender_and_entity_handle_match(sender, entity.handle):
            return []
    try:
        entity.validate()
    except ValueError as ex:
        logger.error("Failed to validate entity %s: %s",
                     entity,
                     ex,
                     extra={
                         "attrs": attrs,
                         "transformed": transformed,
                     })
        return []
    # Do child elements
    for child in element:
        entity._children.extend(element_to_objects(child, sender))
    # Add to entities list
    entities.append(entity)
    if cls == DiasporaRequest:
        # We support sharing/following separately, so also generate base Relationship for the following part
        transformed.update({"relationship": "following"})
        relationship = Relationship(**transformed)
        entities.append(relationship)
    return entities
Exemplo n.º 4
0
 def test_other_relation_raises(self, private_key):
     entity = Relationship(relationship="foo")
     with pytest.raises(ValueError):
         get_outbound_entity(entity, private_key)
Exemplo n.º 5
0
 def test_instance_creation_validates_relationship_value(self):
     with pytest.raises(ValueError):
         entity = Relationship(handle="*****@*****.**",
                               target_handle="*****@*****.**",
                               relationship="hating")
         entity.validate()
Exemplo n.º 6
0
 def test_instance_creation(self):
     entity = Relationship(handle="*****@*****.**",
                           target_handle="*****@*****.**",
                           relationship="following")
     assert entity
Exemplo n.º 7
0
 def test_instance_creation_validates_target_handle_value(self):
     with pytest.raises(ValueError):
         entity = Relationship(handle="*****@*****.**", target_handle="fefle.com", relationship="following")
         entity.validate()