예제 #1
0
    def test_exception_handling(self):
        def test_exception_handling(to_raise, to_catch):
            with self.assertRaises(to_catch):
                with self.assertLogs(level=logging.WARNING):
                    with self.openstack_adapter.handle_exceptions():
                        raise to_raise

        matrix = [
            (asyncio.TimeoutError(), TardisTimeout),
            (AuthError(message="Test_Error",
                       response="Not Allowed"), TardisAuthError),
            (
                ContentTypeError(request_info=AttributeDict(real_url="Test"),
                                 history="Test"),
                TardisResourceStatusUpdateFailed,
            ),
            (
                ClientError(message="Test_Error",
                            response="Internal Server Error"),
                TardisDroneCrashed,
            ),
            (ClientConnectionError(), TardisResourceStatusUpdateFailed),
            (Exception, TardisError),
        ]

        for to_raise, to_catch in matrix:
            test_exception_handling(to_raise, to_catch)
예제 #2
0
async def test_handle_response_content_type_error(nw: Network):
    response = CoroutineMock()
    response.status = 123
    response.text = CoroutineMock()
    response.text.return_value = 'some content'
    response.json = CoroutineMock()
    response.json.side_effect = ContentTypeError(None, None)

    with pytest.raises(IdexClientContentTypeError):
        await nw._handle_response(response)
예제 #3
0
async def test_async_setup_entry_handles_auth_error(hass: HomeAssistant):
    """Test that configuring entry handles Plum Cloud authentication error."""
    config_entry = MockConfigEntry(
        domain=DOMAIN,
        data={"username": "******", "password": "******"},
    )
    config_entry.add_to_hass(hass)

    with patch(
        "homeassistant.components.plum_lightpad.utils.Plum.loadCloudData",
        side_effect=ContentTypeError(Mock(), None),
    ), patch(
        "homeassistant.components.plum_lightpad.light.async_setup_entry"
    ) as mock_light_async_setup_entry:
        result = await hass.config_entries.async_setup(config_entry.entry_id)

    assert result is False
    assert len(mock_light_async_setup_entry.mock_calls) == 0
예제 #4
0
 def json(self, encoding='utf-8'):
     if not getattr(self.body, "decode", False):
         raise ContentTypeError(request_info=RequestInfo(self.url, self.method, self.headers), history=[self])
     return json.loads(self.body.decode(encoding))