"""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:Senderter:NotAuthorized' 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('ter:ActionNotSupported') with self.assertRaises(ValueError): onvif.check_fault(root)