Files

34 lines
1.9 KiB
Python
Raw Permalink Normal View History

"""Read-only ONVIF transport regression tests using synthetic responses."""
import io
import unittest
import urllib.error
from unittest.mock import MagicMock, patch
import onvif
class OnvifTests(unittest.TestCase):
def test_tplink_http_400_auth_fault_is_not_a_connection_error(self):
payload = b'<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body><s:Fault><s:Code><s:Value>s:Sender</s:Value><s:Subcode><s:Value>ter:NotAuthorized</s:Value></s:Subcode></s:Code></s:Fault></s:Body></s:Envelope>'
body = io.BytesIO(payload)
error = urllib.error.HTTPError('http://192.168.8.160:2020/onvif/device_service', 400, 'Bad Request', {}, body)
opener = MagicMock()
opener.open.side_effect = error
with patch.object(onvif.urllib.request, 'build_opener', return_value=opener):
with self.assertRaises(PermissionError):
onvif.request('192.168.8.160', 2020, '/onvif/device_service', onvif.DEVICE, 'GetDeviceInformation', '', 'admin', 'fixture-only')
self.assertTrue(body.closed)
def test_plain_auth_error_is_closed(self):
body = io.BytesIO(b'Unauthorized')
opener = MagicMock()
opener.open.side_effect = urllib.error.HTTPError('http://camera', 401, 'Unauthorized', {}, body)
with patch.object(onvif.urllib.request, 'build_opener', return_value=opener):
with self.assertRaises(PermissionError):
onvif.request('192.168.8.160', 2020, '/onvif/device_service', onvif.DEVICE, 'GetDeviceInformation', '', 'admin', 'fixture-only')
self.assertTrue(body.closed)
def test_unsupported_operation_remains_a_fault(self):
root = onvif.ET.fromstring('<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body><s:Fault><s:Code><s:Value>ter:ActionNotSupported</s:Value></s:Code></s:Fault></s:Body></s:Envelope>')
with self.assertRaises(ValueError):
onvif.check_fault(root)