Esempio n. 1
0
    def test_results_api_http_get(self, mock_http_request):
        """
        Test the ResultsAPI http_get method
        """
        with patch("veracode.api.get_app_id",
                   return_value=constants.VALID_UPLOAD_API["app_id"]):
            results_api = ResultsAPI(
                app_name=constants.VALID_RESULTS_API["app_name"])

        # Fail when attempting to call the http_get method with invalid
        # arguments
        self.assertRaises(KeyError,
                          results_api.http_get,
                          endpoint="getbuildlist.do")

        # Succeed when calling the http_get method with valid arguments
        mock_http_request.return_value = constants.VALID_RESULTS_API_GETAPPBUILDS_RESPONSE_XML_PASSING_POLICY_COMPLIANCE_STATUS[
            "Element"]
        self.assertIsInstance(
            results_api.http_get(endpoint="getappbuilds.do"),
            InsecureElementTree.Element,
        )

        # Fail when attempting to delete the http_get method, because the
        # deleter is intentionally missing
        self.assertRaises(AttributeError, delattr, results_api, "http_get")
Esempio n. 2
0
def get_latest_completed_build(
    *, results_api: ResultsAPI, only_latest: Optional[bool] = True
) -> Union[InsecureElementTree.Element, bool]:
    """
    Get the latest completed build build_id for a given app_id
    https://help.veracode.com/reader/LMv_dtSHyb7iIxAQznC~9w/Q8E6r4JDAN1lykB08oGDSA
    """
    endpoint = "getappbuilds.do"
    params = {"only_latest": only_latest}
    try:
        appbuilds = results_api.http_get(endpoint=endpoint, params=params)
        if element_contains_error(parsed_xml=appbuilds):
            LOG.error("Veracode returned an error when attempting to call %s", endpoint)
            return False
    except (
        HTTPError,
        ConnectionError,
        Timeout,
        TooManyRedirects,
        RequestException,
    ):
        LOG.error("Exception encountered when calling the Veracode API")
        return False

    # Filter on the provided app_id
    for app in appbuilds:
        if app.get("app_id") == results_api.app_id:
            LOG.debug("Found app_id %s, returning %s", results_api.app_id, app)
            return app

    LOG.error(
        "Unable to find a completed build for app_id %s",
        results_api.app_id,
    )
    return False