Ejemplo n.º 1
0
def common_tests(class_, xml_, dict_, parent_id):
    """Common tests for the MS classes."""
    xml_content = XML.fromstring(xml_.encode('utf8'))

    # MusicServiceItem.from_xml and MusicServiceItem.to_dict
    item_from_xml = class_.from_xml(xml_content, FAKE_MUSIC_SERVICE, parent_id)
    assert item_from_xml.to_dict == dict_

    # MusicServiceItem.from_dict and MusicServiceItem.to_dict
    item_from_dict = class_.from_dict(dict_)
    assert item_from_dict.to_dict == dict_

    # MusicServiceItem.didl_metadata
    # NOTE! These tests is reliant on the attributes being put in the same
    # order by ElementTree and so it might fail if that changes
    if item_from_xml.can_play:
        dict_encoded = {}
        for key, value in dict_.items():
            try:
                is_str = isinstance(value, unicode)
            except NameError:
                is_str = isinstance(value, str)
            if is_str:
                dict_encoded[key] = escape(value).\
                    encode('ascii', 'xmlcharrefreplace').decode('ascii')

            else:
                dict_encoded[key] = value
        didl = DIDL_TEMPLATE.format(item_class=class_.item_class,
                                    **dict_encoded)
        assert XML.tostring(item_from_xml.didl_metadata).decode('ascii') == \
            didl
        assert XML.tostring(item_from_dict.didl_metadata).decode('ascii') == \
            didl
    else:
        with pytest.raises(DIDLMetadataError):
            # pylint: disable=pointless-statement
            item_from_xml.didl_metadata

    # Text attributes with mandatory content
    for name in ['item_id', 'extended_id', 'title', 'service_id']:
        getter_attributes_test(name, item_from_xml, item_from_dict,
                               dict_[name])
    # Text attributes with voluntary content
    for name in ['parent_id', 'album_art_uri']:
        getter_attributes_test(name, item_from_xml, item_from_dict,
                               dict_.get(name))
    # Boolean attribute
    getter_attributes_test('can_play', item_from_xml, item_from_dict,
                           bool(dict_.get('can_play')))
    return item_from_xml, item_from_dict
Ejemplo n.º 2
0
def test_call():
    """Calling a command should result in an http request."""

    s = SoapMessage(
        endpoint='http://endpoint.example.com',
        method='getData',
        parameters=[('one', '1')],
        http_headers={'user-agent': 'sonos'},
        soap_action='ACTION',
        soap_header="<a_header>data</a_header>",
        namespace="http://namespace.com",
        other_arg=4)

    response = mock.MagicMock()
    response.headers = {}
    response.status_code = 200
    response.content = DUMMY_VALID_RESPONSE
    with mock.patch('requests.post', return_value=response) as fake_post:
        result = s.call()
        assert XML.tostring(result)
        fake_post.assert_called_once_with(
            'http://endpoint.example.com',
            headers={'SOAPACTION': '"ACTION"',
                     'Content-Type': 'text/xml; charset="utf-8"', 'user-agent':
                         'sonos'},
            data=mock.ANY, other_arg=4)
Ejemplo n.º 3
0
def test_call():
    """Calling a command should result in an http request."""

    s = SoapMessage(
        endpoint="http://endpoint.example.com",
        method="getData",
        parameters=[("one", "1")],
        http_headers={"user-agent": "sonos"},
        soap_action="ACTION",
        soap_header="<a_header>data</a_header>",
        namespace="http://namespace.com",
        other_arg=4,
    )

    response = mock.MagicMock()
    response.headers = {}
    response.status_code = 200
    response.content = DUMMY_VALID_RESPONSE
    with mock.patch("requests.post", return_value=response) as fake_post:
        result = s.call()
        assert XML.tostring(result)
        fake_post.assert_called_once_with(
            "http://endpoint.example.com",
            headers={
                "SOAPACTION": '"ACTION"',
                "Content-Type": 'text/xml; charset="utf-8"',
                "user-agent": "sonos",
            },
            data=mock.ANY,
            other_arg=4,
        )
Ejemplo n.º 4
0
 def test_create_didl_resource_to_from_element(self):
     res = data_structures.DidlResource('a%20uri', 'a:protocol:info:xx',
                                        bitrate=3)
     elt = res.to_element()
     assert XML.tostring(elt) == (
         b'<res bitrate="3" protocolInfo="a:protocol:info:xx">a%20uri</res>')
     assert data_structures.DidlResource.from_element(elt) == res
Ejemplo n.º 5
0
 def test_didl_object_to_element(self):
     didl_object = data_structures.DidlObject(
         title='a_title', parent_id='pid', item_id='iid', creator='a_creator')
     # we seem to have to go through this to get ElementTree to deal
     # with namespaces properly!
     elt = XML.fromstring(XML.tostring(didl_object.to_element(True)))
     elt2 = XML.fromstring(
         '<dummy xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" ' +
         'xmlns:dc="http://purl.org/dc/elements/1.1/" ' +
         'xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/">' +
         '<item id="iid" parentID="pid" restricted="true">' +
         '<dc:title>a_title</dc:title>' +
         '<dc:creator>a_creator</dc:creator>' +
         '<upnp:class>object</upnp:class><desc id="cdudn" ' +
         'nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">' +
         'RINCON_AssociatedZPUDN</desc></item></dummy>')[0]
     assert_xml_equal(elt2, elt)