Esempio n. 1
0
    def _http_alpha_resource(self, method, resource, params=None, data=None, legacy=False, order_by=None, limit=None, valrange=None, sort=None):
        """Makes an HTTP request."""

        if not is_collection(resource):
            resource = [resource]

        url = self._url_for_alpha(*resource)
        print(url)
        headers = self._get_headers_for_alpha_request(method, url, legacy=legacy, order_by=order_by, limit=limit, valrange=valrange, sort=sort)
        print(headers)
        request = Request(url, data, headers, method=method)

        r = urlopen(request)

        if 'ratelimit-remaining' in r.headers:
            self._ratelimit_remaining = r.headers['ratelimit-remaining']

        if 'Request-Id' in r.headers:
            self._last_request_id = r.headers['Request-Id']

        if r.status == 422:
            http_error = HTTPError('%s - %s Client Error: %s' %
                                   (self._last_request_id, r.status, r.content.decode("utf-8")))
            http_error.response = r
            raise http_error

        if r.status == 429:
            raise RateLimitExceeded("You have exceeded your rate limit \n{0}".format(r.content.decode("utf-8")))

        if (not str(r.status).startswith('2')) and (not r.status in [304]):
            pass
        return r
Esempio n. 2
0
 def _request(self, method_name, endpoint_url, data=None, headers=None):
     if endpoint_url.startswith('http'):
         endpoint_url = self.transform_url_to_defined_endpoint(endpoint_url)
     method = getattr(requests, method_name)
     full_url = self._create_full_url(endpoint_url)
     if type(data) is dict or type(data) is list:
         data = json.dumps(data)
     headers = headers or {}
     headers.update(self.headers)
     response = method(full_url, data, headers=headers, timeout=30)
     CURL_LOGGER.info(curlify.to_curl(response.request))
     try:
         if response.status_code >= 500:
             error = HTTPError()
             error.response = response
             raise error
         elif response.status_code >= 400:
             LOGGER.error("ERROR {} {}: {} {}".format(
                 method_name.upper(), response.status_code, full_url,
                 response))
         else:
             LOGGER.debug("SUCCESS {} {}: {} {}".format(
                 method_name.upper(), response.status_code, full_url,
                 json.dumps(data)))
         return self._handle_response(response)
     except HTTPError as e:
         LOGGER.debug('request:%s %s\n%r', method_name, full_url, data)
         LOGGER.error("ERROR {} ({}): {} {} {} {}".format(
             method_name.upper(), e.response.status_code, full_url,
             json.dumps(data), e.response.content, e.response.text))
         raise (e)
Esempio n. 3
0
    def _http_resource(self, method, resource, params=None, data=None, legacy=False, order_by=None, limit=None, valrange=None, sort=None):
        """Makes an HTTP request."""

        if not is_collection(resource):
            resource = [resource]

        url = self._url_for(*resource)

        headers = self._get_headers_for_request(method, url, legacy=legacy, order_by=order_by, limit=limit, valrange=valrange, sort=sort)

        r = self._session.request(method, url, params=params, data=data, headers=headers)

        if 'ratelimit-remaining' in r.headers:
            self._ratelimit_remaining = r.headers['ratelimit-remaining']

        if 'Request-Id' in r.headers:
            self._last_request_id = r.headers['Request-Id']

        #if 'Accept-Ranges' in r.headers:
            #print("Accept-Ranges = {0}".format(r.headers['Accept-Ranges']))

        if r.status_code == 422:
            http_error = HTTPError('%s - %s Client Error: %s' %
                                   (self._last_request_id, r.status_code, r.content.decode("utf-8")))
            http_error.response = r
            raise http_error

        if r.status_code == 429:
            #Rate limit reached
            raise RateLimitExceeded("You have exceeded your rate limit \n{0}".format(r.content.decode("utf-8")))

        if (not str(r.status_code).startswith('2')) and (not r.status_code in [304]):
            pass
        r.raise_for_status()
        return r
Esempio n. 4
0
async def test_turn_on_color_unsupported_api_method(hass: HomeAssistant,
                                                    fritz: Mock):
    """Test turn device on in mapped color mode if unmapped is not supported."""
    device = FritzDeviceLightMock()
    device.get_color_temps.return_value = [2700, 6500]
    device.get_colors.return_value = {
        "Red": [("100", "70", "10"), ("100", "50", "10"), ("100", "30", "10")]
    }
    mockresponse = Mock()
    mockresponse.status_code = 400

    error = HTTPError("Bad Request")
    error.response = mockresponse
    device.set_unmapped_color.side_effect = error

    assert await setup_config_entry(hass,
                                    MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0],
                                    ENTITY_ID, device, fritz)
    assert await hass.services.async_call(
        DOMAIN,
        SERVICE_TURN_ON,
        {
            ATTR_ENTITY_ID: ENTITY_ID,
            ATTR_BRIGHTNESS: 100,
            ATTR_HS_COLOR: (100, 70)
        },
        True,
    )
    assert device.set_state_on.call_count == 1
    assert device.set_level.call_count == 1
    assert device.set_color.call_count == 1
Esempio n. 5
0
    def _http_resource(self,
                       method,
                       resource,
                       params=None,
                       data=None,
                       timeout=None):
        """Makes an HTTP request."""

        if not is_collection(resource):
            resource = [resource]

        url = self._url_for(*resource)
        r = self._session.request(method,
                                  url,
                                  params=params,
                                  data=data,
                                  timeout=timeout)

        if r.status_code == 422:
            http_error = HTTPError('%s Client Error: %s' %
                                   (r.status_code, r.content.decode("utf-8")))
            http_error.response = r
            raise http_error

        r.raise_for_status()

        return r
Esempio n. 6
0
    def test_enrollment_fails(self, mock_refresh, mock_edx_enr):  # pylint: disable=unused-argument
        """
        Test error when backend raises an exception
        """
        error = HTTPError()
        error.response = MagicMock()
        error.response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
        mock_edx_enr.side_effect = error
        resp = self.client.post(self.url, {'course_id': self.course_id}, format='json')
        assert resp.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
        # the response has a structure like {"error": "<message>"}
        assert isinstance(resp.data, dict)
        assert 'error' in resp.data
        assert mock_edx_enr.call_count == 1
        # assert just the second argument, since the first is `self`
        assert mock_edx_enr.call_args[0][1] == self.course_id

        # if instead edX returns a 400 error, an exception is raised by
        # the view and the user gets a different error message
        error.response.status_code = status.HTTP_400_BAD_REQUEST
        mock_edx_enr.side_effect = error
        resp = self.client.post(self.url, {'course_id': self.course_id}, format='json')
        assert resp.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
        assert isinstance(resp.data, list)
        assert len(resp.data) == 1
        assert PossiblyImproperlyConfigured.__name__ in resp.data[0]

        # if the error from the call to edX is is not HTTPError, the user gets a normal json error
        mock_edx_enr.side_effect = ValueError()
        resp = self.client.post(self.url, {'course_id': self.course_id}, format='json')
        assert resp.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
        # the response has a structure like {"error": "<message>"}
        assert isinstance(resp.data, dict)
        assert 'error' in resp.data
    def _mock_response(
            self,
            status=200,
            test_name=None,
            raise_for_status=None):
        """
        Helper function to build a mock response to request get/post etc..
        This object get injected into the code when request.get would have called the api
        """
        mock_resp = mock.Mock()
        # mock raise_for_status call w/optional error
        mock_resp.raise_for_status = mock.Mock()
        
        
        
        if raise_for_status:
            http_error = HTTPError(raise_for_status)
            http_error.response = mock.Mock()
            http_error.response.status_code = status
            
            mock_resp.raise_for_status.side_effect = http_error
        # set status code and content
        mock_resp.status_code = status
 
        whereami = os.path.dirname(__file__)  
        
        filename = '{0}/../mocks/{1}'.format(whereami,test_name)

        if os.path.isfile(filename):
            with open(filename, 'r') as f:
                mock_resp.text = f.read()
        
        return mock_resp
def test_query__error_response(client, mocker, mock_puppetdb_environments,
                               mock_puppetdb_default_nodes):
    app.app.config['WTF_CSRF_ENABLED'] = False

    error_message = "Invalid query: (...)"
    puppetdb_response = HTTPError('Invalid query')
    puppetdb_response.response = MockHTTPResponse(400, error_message)
    mocker.patch.object(app.puppetdb, '_query', side_effect=puppetdb_response)

    data = {
        'query': 'foobar',
        'endpoints': 'pql',
    }
    rv = client.post(
        '/query',
        data=data,
        content_type='application/x-www-form-urlencoded',
    )

    assert rv.status_code == 200

    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'

    vals = soup.find_all('h2', {"id": "results_header"})
    assert len(vals) == 1

    vals = soup.find_all('pre', {"id": "invalid_query"})
    assert len(vals) == 1
    assert error_message in vals[0].string
Esempio n. 9
0
    def test_enrollment_fails(self, mock_refresh, mock_edx_enr):  # pylint: disable=unused-argument
        """
        Test error when backend raises an exception
        """
        error = HTTPError()
        error.response = MagicMock()
        error.response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
        mock_edx_enr.side_effect = error
        resp = self.client.post(self.url, {'course_id': self.course_id}, format='json')
        assert resp.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
        # the response has a structure like {"error": "<message>"}
        assert isinstance(resp.data, dict)
        assert 'error' in resp.data
        assert mock_edx_enr.call_count == 1
        # assert just the second argument, since the first is `self`
        assert mock_edx_enr.call_args[0][1] == self.course_id

        # if instead edX returns a 400 error, an exception is raised by
        # the view and the user gets a different error message
        error.response.status_code = status.HTTP_400_BAD_REQUEST
        mock_edx_enr.side_effect = error
        resp = self.client.post(self.url, {'course_id': self.course_id}, format='json')
        assert resp.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
        assert isinstance(resp.data, list)
        assert len(resp.data) == 1
        assert PossiblyImproperlyConfigured.__name__ in resp.data[0]

        # if the error from the call to edX is is not HTTPError, the user gets a normal json error
        mock_edx_enr.side_effect = ValueError()
        resp = self.client.post(self.url, {'course_id': self.course_id}, format='json')
        assert resp.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
        # the response has a structure like {"error": "<message>"}
        assert isinstance(resp.data, dict)
        assert 'error' in resp.data
Esempio n. 10
0
    def test_validate_catalog_uuid(
        self,
        http_status_code,
        catalog_enterprise_customer_matches,
        expected_is_valid,
        mock_catalog_api_client,
    ):

        catalog_uuid = uuid4()
        enterprise_customer_uuid = uuid4()

        if http_status_code:
            error = HTTPError()
            error.response = mock.MagicMock()
            error.response.status_code = http_status_code
            mock_catalog_api_client(
            ).get_enterprise_catalog.side_effect = error
        else:
            mock_catalog_api_client().get_enterprise_catalog.return_value = {
                'enterprise_customer':
                str(enterprise_customer_uuid)
                if catalog_enterprise_customer_matches else str(uuid4())
            }

        form = make_bound_subscription_form(
            enterprise_customer_uuid=enterprise_customer_uuid,
            enterprise_catalog_uuid=catalog_uuid,
            customer_agreement_has_default_catalog=True)

        assert form.is_valid() is expected_is_valid
        assert mock_catalog_api_client().get_enterprise_catalog.called
Esempio n. 11
0
    def test_validate_enterprise_customer_uuid(
        self,
        has_http_error,
        expected_is_valid,
        mock_catalog_api_client,
        mock_enterprise_api_client,
    ):

        catalog_uuid = uuid4()
        enterprise_customer_uuid = uuid4()
        mock_catalog_api_client().get_enterprise_catalog.return_value = {
            'enterprise_customer': str(enterprise_customer_uuid)
        }

        if has_http_error:
            error = HTTPError()
            error.response = mock.MagicMock()
            error.response.status_code = 404
            mock_enterprise_api_client(
            ).get_enterprise_customer_data.side_effect = error
        else:
            mock_enterprise_api_client(
            ).get_enterprise_customer_data.return_value = {
                'slug': 'test',
                'name': 'test-enterprise'
            }

        form = make_bound_customer_agreement_form(
            customer_agreement=CustomerAgreementFactory(
                enterprise_customer_uuid=enterprise_customer_uuid),
            default_enterprise_catalog_uuid=catalog_uuid,
            subscription_for_auto_applied_licenses=None)
        assert form.is_valid() is expected_is_valid
        assert mock_enterprise_api_client().get_enterprise_customer_data.called
Esempio n. 12
0
    def _http_resource(self, method, resource, params=None, data=None, legacy=False, order_by=None, limit=None, valrange=None, sort=None):
        """Makes an HTTP request."""

        if not is_collection(resource):
            resource = [resource]

        url = self._url_for(*resource)

        headers = self._get_headers_for_request(method, url, legacy=legacy, order_by=order_by, limit=limit, valrange=valrange, sort=sort)

        r = self._session.request(method, url, params=params, data=data, headers=headers)

        if 'ratelimit-remaining' in r.headers:
            self._ratelimit_remaining = r.headers['ratelimit-remaining']

        if 'Request-Id' in r.headers:
            self._last_request_id = r.headers['Request-Id']

        #if 'Accept-Ranges' in r.headers:
            #print "Accept-Ranges = {0}".format(r.headers['Accept-Ranges'])

        if r.status_code == 422:
            http_error = HTTPError('%s - %s Client Error: %s' %
                                   (self._last_request_id, r.status_code, r.content.decode("utf-8")))
            http_error.response = r
            raise http_error

        if r.status_code == 429:
            #Rate limit reached
            raise RateLimitExceeded("You have exceeded your rate limit \n{0}".format(r.content.decode("utf-8")))

        if (not str(r.status_code).startswith('2')) and (not r.status_code in [304]):
            pass
        r.raise_for_status()
        return r
Esempio n. 13
0
 def get_call(url, params=None):
     if params['access_token'] == '222':
         return self.mock_resp
     else:
         error = HTTPError()
         error.response = MagicMock()
         error.response.status_code = 401
         raise error
Esempio n. 14
0
 def ctms_error(self, status_code, detail, reason):
     """Return a CTMS error response"""
     response = Response()
     response.status_code = status_code
     response._content = json.dumps({"detail": detail})
     if reason:
         response.reason = reason
     error = HTTPError()
     error.response = response
     return error
Esempio n. 15
0
 def _rebuild_err(self, exception):
     response = exception.response
     if response.headers["content-type"] == "application/json":
         data = response.json()
         msg = data.get("error_msg", None) or data
     else:
         msg = response.text
     e = HTTPError("%s\n%s" % (str(exception), msg))
     e.response = response
     return e
    def _request(self, method_name, endpoint_url, data=None, headers=None, can_retry=True):
        """
        Making an http call with a specific method
        In case of an Unauthorized response. The auth client will refresh the token and send the
        request again
        :param method_name:
        :param endpoint_url:
        :param data:
        :param headers:
        :param can_retry:
        :return: the response of the http request
        """
        full_url = self._create_full_url(endpoint_url)
        if type(data) is dict or type(data) is list:
            data = json.dumps(data)
        original_headers = headers
        headers = headers or {}
        if self.auth_client is not None:
            headers.update(self.auth_client.get_headers())
        req = Request(method_name, full_url, data=data, headers=headers)
        prepped_request = self.req_session.prepare_request(req)
        response = self.req_session.send(prepped_request, timeout=30)
        self.curl_logger.debug(curlify.to_curl(response.request))
        try:
            if response.status_code >= 500:
                error = HTTPError()
                error.response = response
                raise error

            elif (response.status_code == 401 or response.status_code == 403) and can_retry:
                self.logger.debug(
                    "ERROR - Refreshing token {} {}: {} {}".format(method_name.upper(),
                                                                   response.status_code, full_url,
                                                                   response))
                if self.auth_client is not None:
                    self.auth_client.refresh_token()
                    return self._request(method_name, endpoint_url, data, original_headers,
                                  can_retry=False)
            elif response.status_code > 403:
                self.logger.debug(
                    "ERROR {} {}: {} {}".format(method_name.upper(), response.status_code, full_url,
                                                response))
            else:
                self.logger.debug(
                    "SUCCESS {} {}: {} {}".format(method_name.upper(), response.status_code,
                                                  full_url, json.dumps(data)))
            return self._handle_response(response)
        except HTTPError as e:
            self.logger.debug('request:%s %s\n%r', method_name, full_url, data)
            self.logger.warning(
                "ERROR {} ({}): {} {} {} {}".format(method_name.upper(), e.response.status_code,
                                                    full_url, json.dumps(data), e.response.content,
                                                    e.response.text))
            raise (e)
Esempio n. 17
0
 def list_all_tables(
     self,
     share: Share,
     *,
     max_results: Optional[int] = None,
     page_token: Optional[str] = None,
 ) -> ListAllTablesResponse:
     http_error = HTTPError()
     response = Response()
     response.status_code = 404
     http_error.response = response
     raise http_error
    def test_make_spotify_request_raises_spotify_exception_on_http_error(self, mock_auth, mock_request, spotify_client):
        auth_code = 'test-auth-code'
        mock_http_error = HTTPError()
        mock_http_error.response = mock.Mock()

        mock_response = mock.Mock()
        mock_response.raise_for_status.side_effect = mock_http_error

        mock_auth.return_value = auth_code
        mock_request.return_value = mock_response

        with pytest.raises(SpotifyException):
            spotify_client._make_spotify_request('GET', '/dummy_endpoint')
    def test_remove_usage_spec_error(self):
        self._model.options = self._option

        error = HTTPError()
        error.response = MagicMock(status_code=500)
        self._usage_client.delete_usage_spec.side_effect = error

        plugin_handler = plugin.Plugin(self._model)

        try:
            plugin_handler.remove_usage_specs()
        except HTTPError as e:
            self.assertEquals(500, e.response.status_code)
Esempio n. 20
0
        def __init__(self):
            # inherit from DataSharingRestClient to make sure all the helper methods are the same
            super().__init__(rest_client._profile)
            self.sleeps = []
            self._sleeper = self.sleeps.append

            http_error = HTTPError()
            response = Response()
            response.status_code = 429
            http_error.response = response
            self.http_error = http_error

            self.connection_error = ConnectionError()
Esempio n. 21
0
 def test_dnd_notify_errors(self):
     http_error = HTTPError()
     http_error.response = MagicMock()
     http_error.response.status_code = 404
     self.confd.users.get.side_effect = http_error
     assert_that(
         calling(self.service.notify_dnd).with_args('123', True),
         raises(NoSuchUser),
     )
     http_error.response.status_code = 500
     assert_that(
         calling(self.service.notify_dnd).with_args('123', True),
         raises(HTTPError),
     )
Esempio n. 22
0
 def test_add_feed_post_http_error_404(self, requests_get):
     error = HTTPError("Boom 404!")
     error.response = MagicMock()
     error.response.status_code = 404
     requests_get.side_effect = error
     feed_url = "http://dewkjhdkwjhkjedhwdkwj.de/zu"
     self._login("probes.add_feed")
     response = self.client.post(reverse("probes:add_feed"),
                                 {"url": feed_url},
                                 follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertTemplateUsed(response, "core/probes/add_feed.html")
     self.assertFormError(response, "form", "url", "HTTP error 404")
     requests_get.assert_called_once_with(feed_url, stream=True)
Esempio n. 23
0
 def patched_client(self, path, status=401, reason="Unauthorized"):
     error = HTTPError()
     error.response = mock.MagicMock()
     error.response.status_code = status
     error.response.reason = reason
     error.response.text = ('{"status": "invalid-credentials", '
                            '"errors": [{"location": "body", '
                            '"name": "", '
                            '"description": "Unauthorized"}]}')
     patch = mock.patch(path, side_effect=error)
     try:
         yield patch.start()
     finally:
         patch.stop()
Esempio n. 24
0
 def patched_client(self, path, status=401, reason="Unauthorized"):
     error = HTTPError()
     error.response = mock.MagicMock()
     error.response.status_code = status
     error.response.reason = reason
     error.response.text = ('{"status": "invalid-credentials", '
                            '"errors": [{"location": "body", '
                            '"name": "", '
                            '"description": "Unauthorized"}]}')
     patch = mock.patch(path, side_effect=error)
     try:
         yield patch.start()
     finally:
         patch.stop()
Esempio n. 25
0
    def _http_resource(self, method, resource, params=None, data=None):
        """Makes an HTTP request."""

        if not is_collection(resource):
            resource = [resource]

        url = self._url_for(*resource)
        r = self._session.request(method, url, params=params, data=data)

        if r.status_code == 422:
            http_error = HTTPError('%s Client Error: %s' % (r.status_code, r.content))
            http_error.response = r
            raise http_error
        
        r.raise_for_status()

        return r
Esempio n. 26
0
    def test_no_retry_on_credentials_api_4XX_error(
        self,
        mock_get_completed_programs,
        mock_get_certified_programs,  # pylint: disable=unused-argument
        mock_award_program_certificate,
    ):
        """
        Verify that other 4XX errors cause task to fail but there is no retry.
        """
        exception = HTTPError()
        exception.response = mock.Mock(status_code=418)
        mock_get_completed_programs.return_value = {1: 1, 2: 2}
        mock_award_program_certificate.side_effect = self._make_side_effect(
            [exception, None])

        tasks.award_program_certificates.delay(self.student.username).get()

        assert mock_award_program_certificate.call_count == 2
Esempio n. 27
0
    def test_update_change_request_raises_exception_for_http_error(
            self, mock_pysnow):
        fake_resource = mock.MagicMock()
        fake_change_order = mock.MagicMock()

        fake_exception = HTTPError()
        fake_exception.response = mock.MagicMock()
        fake_exception.response.text.return_value = 'Foobar'

        fake_resource.update.side_effect = fake_exception

        self.mock_pysnow_client.resource.return_value = fake_resource
        mock_pysnow.Client.return_value = self.mock_pysnow_client

        with six.assertRaisesRegex(self, ChangeRequestException,
                                   'Could not update change request due to '):
            self.change_request_handler.update_change_request(
                fake_change_order, payload='{"foo": "bar"}')
Esempio n. 28
0
    def test_create_change_request_raises_exception_for_http_error(
            self, mock_pysnow):
        fake_resource = mock.MagicMock()

        fake_exception = HTTPError()
        fake_exception.response = mock.MagicMock()
        fake_exception.response.text.return_value = 'Foobar'

        fake_resource.create.side_effect = fake_exception

        self.mock_pysnow_client.resource.return_value = fake_resource
        mock_pysnow.Client.return_value = self.mock_pysnow_client

        with six.assertRaisesRegex(self, ChangeRequestException,
                                   'Could not create change request due to.*'):
            self.change_request_handler.create_change_request('Title',
                                                              'Description',
                                                              None,
                                                              payload={})
Esempio n. 29
0
    def _http_resource(self, method, resource, params=None, data=None):
        """Makes an HTTP request."""

        if not is_collection(resource):
            resource = [resource]

        # This is needed because we only send application/json
        data = json_encode(data)

        url = self._url_for(*resource)
        r = self._session.request(method, url, params=params, data=data)

        if r.status_code == 422:
            http_error = HTTPError("%s Client Error: %s" % (r.status_code, r.content))
            http_error.response = r
            raise http_error

        r.raise_for_status()
        return r
Esempio n. 30
0
    def test_no_retry_on_credentials_api_4XX_error(
        self,
        mock_get_inverted_programs,
        mock_get_certified_programs,
        mock_revoke_program_certificate,
    ):
        """
        Verify that other 4XX errors cause task to fail but there is no retry.
        """
        exception = HTTPError()
        exception.response = mock.Mock(status_code=418)
        mock_get_inverted_programs.return_value = self.inverted_programs
        mock_get_certified_programs.return_value = [1, 2]
        mock_revoke_program_certificate.side_effect = self._make_side_effect(
            [exception, None])

        tasks.revoke_program_certificates.delay(self.student.username,
                                                self.course_key).get()

        assert mock_revoke_program_certificate.call_count == 2
Esempio n. 31
0
    def test_ncbi_fails(self):
        exceptions = [ChunkedEncodingError(), ConnectionError(), ReadTimeout(),
                      ExpatError(), RuntimeError('bad record')]
        for code in [400, 429]:
            http_exception = HTTPError()
            http_exception.response = Response()
            http_exception.response.status_code = code
            exceptions.append(http_exception)

        for exception in exceptions:
            self.ncbi_exception = exception
            with self.assertLogs(level='DEBUG') as log:
                seq, tax = self.get_ncbi_data(query='MT345279.1')
                tax = tax.view(DataFrame)
                self.assertEqual(
                    tax['Taxon']['MT345279.1'],
                    'k__Fungi; p__Basidiomycota; c__Agaricomycetes; '
                    'o__Boletales; f__Boletaceae; g__Boletus; s__edulis'
                )
            self.assertTrue('Retrying' in log.output[0])
    def test_http_exception_redirection(self):
        """
        Test ExceptionMiddleware is correctly redirected to login page
        when PSA raises HttpError exception.
        """

        request = RequestFactory().get("dummy_url")
        next_url = get_next_url_for_login_page(request)
        login_url = '/login?next=' + next_url
        request.META['HTTP_REFERER'] = 'http://example.com:8000/login'
        exception = HTTPError()
        exception.response = HttpResponse(status=502)

        # Add error message for error in auth pipeline
        MessageMiddleware().process_request(request)
        response = ExceptionMiddleware().process_exception(request, exception)
        target_url = response.url

        self.assertEqual(response.status_code, 302)
        self.assertTrue(target_url.endswith(login_url))
Esempio n. 33
0
    def _http_resource(self, method, resource, params=None, data=None):
        """Makes an HTTP request."""

        if not is_collection(resource):
            resource = [resource]

        # This is needed because we only send application/json
        data = json_encode(data)

        url = self._url_for(*resource)
        r = self._session.request(method, url, params=params, data=data)

        if r.status_code == 422:
            http_error = HTTPError('%s Client Error: %s' %
                                   (r.status_code, r.content))
            http_error.response = r
            raise http_error

        r.raise_for_status()
        return r
Esempio n. 34
0
    def _get_topic(self, path):
        """
        Retrieve topic object by path
        """

        response = self.session.get(f"{self.base_url}/t/{path}.json",
                                    allow_redirects=False)
        response.raise_for_status()

        if response.status_code >= 300:
            raise RedirectFoundError(response=response)

        topic = response.json()

        # If topic not in category, raise a 404
        if topic["category_id"] != self.category_id:
            error = HTTPError(f"Topic not in category {self.category_id}")
            response.status_code = 404
            error.response = response
            raise error

        return response.json()
Esempio n. 35
0
    def test_http_exception_redirection(self):
        """
        Test ExceptionMiddleware is correctly redirected to login page
        when PSA raises HttpError exception.
        """

        request = RequestFactory().get("dummy_url")
        next_url = get_next_url_for_login_page(request)
        login_url = '/login?next=' + next_url
        request.META['HTTP_REFERER'] = 'http://example.com:8000/login'
        exception = HTTPError()
        exception.response = HttpResponse(status=502)

        # Add error message for error in auth pipeline
        MessageMiddleware().process_request(request)
        response = ExceptionMiddleware().process_exception(
            request, exception
        )
        target_url = response.url

        self.assertEqual(response.status_code, 302)
        self.assertTrue(target_url.endswith(login_url))
Esempio n. 36
0
 def _create_request_error(self, code):
     error = HTTPError()
     error.response = MagicMock()
     error.response.status_code = code
     keyrock_backend_v1.requests.get.side_effect = error
Esempio n. 37
0
 def raise_http_error(*args, **kwargs):  # pylint: disable=unused-argument
     """Mock function to raise an exception"""
     error = HTTPError()
     error.response = MagicMock()
     error.response.status_code = 401
     raise error
 def _not_found_spec(self):
     error = HTTPError()
     error.response = MagicMock(status_code=404)
     self._usage_client.delete_usage_spec.side_effect = error
 def fake_method(**kwargs):
     e = HTTPError('Fake 404')
     e.response = Response()
     setattr(e.response, 'status_code', 404)
     raise e
Esempio n. 40
0
 def raise_http_error(*args, **kwargs):  # pylint: disable=unused-argument
     """Mock function to raise an exception"""
     error = HTTPError()
     error.response = MagicMock()
     error.response.status_code = status_code
     raise error