def _post(endpoint, json): json.update({ 'client_id': config.plaid.client_id, 'secret': config.plaid.development, }) response = hc.post('https://%s.plaid.com%s' % (config.plaid.environment, endpoint), json=json) if response.status_code >= 300: raise httpx.HTTPStatusError(response.content, request=response._request, response=response) return response.json()
def _check_result(request: httpx.Request, response: httpx.Response): if response.status_code != 200: raise httpx.HTTPStatusError("Error status_code", request=request, response=response) try: return response.json() except json.decoder.JSONDecodeError: raise httpx.DecodingError("Error decode to json", request=request)
async def test_collection_get_raises(self): collection_obj = self.client.collections["fruits"] with mock.patch.object(collection_obj.api_call, "request") as mocked_request: mocked_request.side_effect = ValueError("error") with self.assertRaises(ValueError) as ctx: _ = await collection_obj.retrieve() self.assertTrue(isinstance(ctx.exception, ValueError)) with mock.patch.object(collection_obj.api_call, "request") as mocked_request: httpx_response_mock = mock.Mock() httpx_response_mock.status_code = 300 mocked_request.side_effect = httpx.HTTPStatusError( "msg", request=mock.Mock(), response=httpx_response_mock) with self.assertRaises(httpx.HTTPStatusError) as ctx: _ = await collection_obj.retrieve() self.assertTrue(isinstance(ctx.exception, httpx.HTTPStatusError))
async def test_user_fetching_serial_fails(hass: HomeAssistant) -> None: """Test user setup without a serial number.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["type"] == "form" assert result["errors"] == {} with patch( "homeassistant.components.enphase_envoy.config_flow.EnvoyReader.getData", return_value=True, ), patch( "homeassistant.components.enphase_envoy.config_flow.EnvoyReader.get_full_serial_number", side_effect=httpx.HTTPStatusError( "any", request=MagicMock(), response=MagicMock() ), ), patch( "homeassistant.components.enphase_envoy.async_setup_entry", return_value=True, ) as mock_setup_entry: result2 = await hass.config_entries.flow.async_configure( result["flow_id"], { "host": "1.1.1.1", "username": "******", "password": "******", }, ) await hass.async_block_till_done() assert result2["type"] == "create_entry" assert result2["title"] == "Envoy" assert result2["data"] == { "host": "1.1.1.1", "name": "Envoy", "username": "******", "password": "******", } assert len(mock_setup_entry.mock_calls) == 1
async def test_form_invalid_auth(hass: HomeAssistant) -> None: """Test we handle invalid auth.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}) with patch( "homeassistant.components.enphase_envoy.config_flow.EnvoyReader.getData", side_effect=httpx.HTTPStatusError("any", request=MagicMock(), response=MagicMock()), ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], { "host": "1.1.1.1", "username": "******", "password": "******", }, ) assert result2["type"] == "form" assert result2["errors"] == {"base": "invalid_auth"}
def app_status(request: httpx.Request) -> httpx.Response: res = httpx.Response(403, json={}) raise httpx.HTTPStatusError(message="status error", request=request, response=res)