예제 #1
0
def test_request_fetch(response, mocker):
    """
    fetch content from URL
    """
    mocker.patch("requests.get", autospec=True)
    requests.get.return_value = response
    request.fetch("http://localhost")
    requests.get.assert_called_with("http://localhost", request.headers)
예제 #2
0
def test_request_fetch_no_200_return_code(response, mocker):
    """
    fetch will exit if return code is not 200
    """
    mocker.patch("requests.get", autospec=True)
    response.status_code = 500
    requests.get.return_value = response
    with pytest.raises(SystemExit):
        request.fetch("http://localhost")
예제 #3
0
def test_request_fetch_with_json_response(mocker):
    """
    fetch will exit with unvalid json response
    """
    class Response:
        status_code = 200

        def json(self):
            raise json.decoder.JSONDecodeError("foo", "bar", 0)

    response = Response()
    mocker.patch("requests.get", autospec=True)
    requests.get.return_value = response
    with pytest.raises(SystemExit):
        request.fetch("http://localhost")
예제 #4
0
파일: products.py 프로젝트: SweBarre/sps
def get(pattern, cache_filename, no_cache, update_cache):
    cache_path = Path(cache_filename)
    data = {}
    fetch_from_scc = False
    if cache_path.exists() and cache_path.is_file() and not no_cache:
        data = cache.load(cache_filename)
        try:
            data["product"]
        except KeyError as err:
            fetch_from_scc = True
    else:
        fetch_from_scc = True
    if fetch_from_scc:
        response = request.fetch("https://scc.suse.com/api/package_search/products")
        try:
            data["product"] = response["data"]
        except KeyError as err:
            print_err(f"Error: No data key found in scc api response, {err}")
            sys.exit(1)

    ret = []
    if not pattern:
        ret = data["product"]
    else:
        for product in data["product"]:
            for k in product.keys():
                if pattern in str(product[k]):
                    ret.append(product)
                    break

    if update_cache:
        cache.save("product", cache_filename, ret)
    return ret
예제 #5
0
def test_request_fetch_with_json_response(response, mocker):
    """
    fetch will not exit with valid json response
    """
    mocker.patch("requests.get", autospec=True)
    requests.get.return_value = response
    assert request.fetch("http://localhost") == {"foo": "bar"}
예제 #6
0
def test_request_with_html_response(mocker):
    class Response:
        status_code = 200
        text = "Testing testing"

    mocker.patch("requests.get", autospec=True)
    requests.get.return_value = Response()
    assert request.fetch("http://localhost", type="html") == "Testing testing"
예제 #7
0
파일: packages.py 프로젝트: SweBarre/sps
def get(product, pattern, cache_filename):
    """Get the packages information from SUSE Customer Center
    for a particular product

    Parameters
    ----------
    product: str
        product id or identifier
    pattern: str
        search pattern in package name, if blank all packages will be
        returned
    cache_filename: str
        path to the cache file name to be used to lookup product id
        if product identifier is specified instead of product it

    Returns
    -------
    list
        a list of packages that matches the search result
        each package is a dict

    Raises
    ------
    SystemExit
        if the response from SUSE Customer Center is not parsable
    """

    try:
        product_id = int(product)
    except ValueError:
        product_id = cache.lookup_product(product, cache_filename)

    url = f"https://scc.suse.com/api/package_search/packages?product_id={product_id}&query={pattern}"
    response = request.fetch(url)
    try:
        response["data"]
    except KeyError as err:
        print_err(f"Error: No data key found, {err}")
        sys.exit(1)
    return response["data"]
예제 #8
0
def get(pattern, cachefile, no_cache, update_cache) -> List:
    """Fetch the products used by SCC Patches

    Parameters
    ----------
    pattern: str
        Only return products where 'pattern' matches product name
    cachefile: str
        Patch to cache file
    no_cache: bool
        If true the cache file will not be used
    update_cache: bool
        if true the cache file will be updated

    Returns:
    List
        A list of Dict representing the products

    Raises:
    SystemExit
        if response from SUSE Customer Center is not parsable
    """

    data = {}
    if not update_cache and (
        Path(cachefile).exists() and Path(cachefile).is_file() and not no_cache
    ):
        data = cache.load(cachefile)
        try:
            data["patchproducts"]
        except KeyError as err:
            print_err("no patch product key found in cachefile")
            sys.exit(1)
    else:
        response = request.fetch("https://scc.suse.com/patches", "html")
        matches = re.findall(".*productsData=(.*)[^ \t\n\r\f\v]+.*", response)
        if len(matches) != 1:
            if len(matches) > 1:
                print_err("to many matches in response from SCC")
            else:
                print_err("no matches in response from SCC")
            exit(1)
        try:
            data["patchproducts"] = json.loads(matches[0])
        except json.decoder.JSONDecodeError as err:
            print_err(f"Couldn't parse json response {err}")
            sys.exit(1)

    ret = []
    if not pattern:
        ret = data["patchproducts"]
    else:
        for product in data["patchproducts"]:
            for k in product.keys():
                if pattern in str(product[k]):
                    ret.append(product)
                    break

    if update_cache:
        cache.save("patchproducts", cachefile, data["patchproducts"])
    return ret
예제 #9
0
def test_request_fetch_bogus_url():
    """
    fetch will exit with bogus URL
    """
    with pytest.raises(SystemExit):
        request.fetch("lsdfjlsdjf")