Ejemplo n.º 1
0
def _validate_service_response(response, logger, auth_header):
    """
    Raise an exception if the response was not what we expected.
    """
    if response.status_code == requests.codes.not_found:
        logger.info("%s error received from service: %s" %
                    (response.status_code, response.text))
        raise ItemNotReturned(response.text)

    if response.status_code in [
            requests.codes.forbidden, requests.codes.unauthorized
    ]:
        logger.info("%s error received from service: %s" %
                    (response.status_code, response.text))
        # Log identity header if 401 (unauthorized)
        if response.status_code == requests.codes.unauthorized:
            if isinstance(auth_header,
                          dict) and AUTH_HEADER_NAME in auth_header:
                logger.info("identity '%s'" %
                            get_key_from_headers(auth_header))
            else:
                logger.info("no identity or no key")
        raise RBACDenied(response.text)

    if response.status_code != requests.codes.ok:
        logger.warn("%s error received from service: %s" %
                    (response.status_code, response.text))
        raise ServiceError("Error received from backend service")
Ejemplo n.º 2
0
 def test_comparison_report_api_missing_system_uuid(self, mock_fetch_systems):
     mock_fetch_systems.side_effect = ItemNotReturned("oops")
     response = self.client.get(
         "api/drift/v1/comparison_report?"
         "system_ids[]=d6bba69a-25a8-11e9-81b8-c85b761454fa"
         "&system_ids[]=11b3cbce-25a9-11e9-8457-c85b761454fa",
         headers=fixtures.AUTH_HEADER,
     )
     self.assertEqual(response.status_code, 404)
def ensure_correct_system_count(system_ids_requested, result):
    """
    raise an exception if we didn't get back the number of systems we expected.

    If the count is correct, do nothing.
    """
    if len(result) < len(system_ids_requested):
        system_ids_returned = {system["id"] for system in result}
        missing_ids = set(system_ids_requested) - system_ids_returned
        raise ItemNotReturned("ids [%s] not available to display" %
                              ", ".join(missing_ids))
Ejemplo n.º 4
0
 def test_create_from_inventory_not_found(self, mock_fetch):
     mock_fetch.side_effect = ItemNotReturned("not found!")
     response = self.client.post(
         "api/system-baseline/v1/baselines",
         headers=fixtures.AUTH_HEADER,
         json=fixtures.CREATE_FROM_INVENTORY,
     )
     self.assertEqual(response.status_code, 400)
     self.assertIn(
         "inventory UUID df925152-c45d-11e9-a1f0-c85b761454fa not available",
         response.data.decode("utf-8"),
     )
Ejemplo n.º 5
0
 def test_comparison_report_api_500_errors_preference(
         self, mock_fetch_systems, mock_fetch_baselines):
     mock_fetch_systems.side_effect = ItemNotReturned("oops")
     mock_fetch_baselines.side_effect = ServiceError("oops")
     response = self.client.get(
         "api/drift/v1/comparison_report?"
         "system_ids[]=d6bba69a-25a8-11e9-81b8-c85b761454fa"
         "&system_ids[]=11b3cbce-25a9-11e9-8457-c85b761454fa"
         "&baseline_ids[]=ff35596c-f98e-11e9-aea9-98fa9b07d419",
         headers=fixtures.AUTH_HEADER,
     )
     self.assertEqual(response.status_code, 500)
Ejemplo n.º 6
0
 def test_comparison_report_api_missing_baseline_uuid(
         self, mock_fetch_systems, mock_fetch_baselines):
     mock_fetch_systems.return_value = fixtures.FETCH_SYSTEMS_WITH_PROFILES_RESULT
     mock_fetch_baselines.side_effect = ItemNotReturned("oops")
     response = self.client.get(
         "api/drift/v1/comparison_report?"
         "system_ids[]=d6bba69a-25a8-11e9-81b8-c85b761454fa"
         "&system_ids[]=11b3cbce-25a9-11e9-8457-c85b761454fa"
         "&baseline_ids[]=ff35596c-f98e-11e9-aea9-98fa9b07d419",
         headers=fixtures.AUTH_HEADER,
     )
     self.assertEqual(response.status_code, 404)
Ejemplo n.º 7
0
def _validate_service_response(response, logger):
    """
    Raise an exception if the response was not what we expected.
    """
    if response.status_code == requests.codes.not_found:
        logger.info("%s error received from service: %s" %
                    (response.status_code, response.text))
        raise ItemNotReturned(response.text)

    if response.status_code != requests.codes.ok:
        logger.warn("%s error received from service: %s" %
                    (response.status_code, response.text))
        raise ServiceError("Error received from backend service")