Ejemplo n.º 1
0
def retrieve_product(product_name):
    release_info = Product.find_by_name(product_name)
    if release_info:
        return release_info, HTTPStatus.OK
    else:
        error = f"{product_name} not found in database."
        abort(HTTPStatus.NOT_FOUND, error, status="fail")
Ejemplo n.º 2
0
def create_product_happy_path(
    self,
    product_name,
    release_info_url,
    xpath_version_number,
    xpath_download_url,
    jwt_auth,
):
    create_product_response = create_product(
        self,
        product_name,
        release_info_url,
        xpath_version_number,
        xpath_download_url,
        jwt_auth,
    )
    create_product_data = create_product_response.get_json()
    self.assertEqual(create_product_data["status"], "success")
    self.assertEqual(create_product_data["message"],
                     f"New product added: {product_name}.")
    location_header = create_product_response.headers.get("Location")
    self.assertTrue(
        location_header.endswith(f"/api/v1/products/{product_name}"))
    self.assertEqual(create_product_response.content_type, "application/json")
    self.assertEqual(create_product_response.status_code, HTTPStatus.CREATED)
    product = Product.find_by_name(product_name)
    self.assertIsNotNone(product)
    return product
Ejemplo n.º 3
0
def delete_product(product_name):
    release_info = Product.find_by_name(product_name)
    if not release_info:
        return "", HTTPStatus.NO_CONTENT

    try:
        db.session.delete(release_info)
        db.session.commit()
        return "", HTTPStatus.NO_CONTENT
    except Exception as e:
        error = f"Error: {repr(e)}"
        abort(HTTPStatus.INTERNAL_SERVER_ERROR, error, status="fail")
Ejemplo n.º 4
0
def update_product(product_name, data):
    request_data = {k: v for k, v in data.items()}
    update_product = Product.find_by_name(product_name)
    if not update_product:
        error = f"Product name: {product_name} not found."
        abort(HTTPStatus.NOT_FOUND, error, status="fail")
    try:
        for k, v in request_data.items():
            setattr(update_product, k, v)
        setattr(update_product, "last_update", datetime.utcnow())
        db.session.commit()
        return update_product, HTTPStatus.OK
    except Exception as e:
        error = f"Error: {repr(e)}"
        abort(HTTPStatus.INTERNAL_SERVER_ERROR, error, status="fail")
Ejemplo n.º 5
0
def create_product_python(self, jwt_auth):
    product_name = "python_v3_7"
    release_info_url = "https://www.python.org/downloads/"
    xpath_download_url = '//p[@class="download-buttons"]/a/text()'
    xpath_version_number = '//p[@class="download-buttons"]/a/@href'
    product = create_product_happy_path(
        self,
        product_name,
        release_info_url,
        xpath_download_url,
        xpath_version_number,
        jwt_auth,
    )
    newest_version = "3.7.2"
    download_url = "https://www.python.org/ftp/python/3.7.2/python-3.7.2-macosx10.9.pkg"
    check_time = datetime.utcnow()
    update_time = datetime.utcnow() + timedelta(seconds=3)
    check_time_str = check_time.strftime(DT_STR_FORMAT_NAIVE)
    update_time_str = update_time.strftime(DT_STR_FORMAT_NAIVE)
    product.newest_version_number = newest_version
    product.download_url = download_url
    product.last_checked = check_time
    product.last_update = update_time
    db.session.commit()

    retrieve_product_response = retrieve_product(self, product_name)
    retrieve_product_data = retrieve_product_response.get_json()
    self.assertEqual(retrieve_product_data["product_name"], product_name)
    self.assertEqual(retrieve_product_data["newest_version_number"],
                     newest_version)
    self.assertEqual(retrieve_product_data["download_url"], download_url)
    self.assertEqual(retrieve_product_data["last_checked"], check_time_str)
    self.assertEqual(retrieve_product_data["last_update"], update_time_str)
    self.assertEqual(retrieve_product_response.content_type,
                     "application/json")
    self.assertEqual(retrieve_product_response.status_code, HTTPStatus.OK)

    product = Product.find_by_name(product_name)
    product_repr = (f"Product<("
                    f"id=1, "
                    f"product_name={product_name}, "
                    f"newest_version_number={newest_version})>")
    self.assertEqual(repr(product), product_repr)
    return product
Ejemplo n.º 6
0
def create_product(data):
    product_name = data["product_name"]
    exists = Product.find_by_name(product_name)
    if exists:
        error = f"Product name: {product_name} already exists, must be unique."
        abort(HTTPStatus.CONFLICT, error, status="fail")
    try:
        product_dict = {}
        for k, v in data.items():
            product_dict[k] = v
        product_dict["last_update"] = datetime.utcnow()
        new_product = Product(**product_dict)
        db.session.add(new_product)
        db.session.commit()
        response_object = dict(status="success",
                               message=f"New product added: {product_name}.")
        response = jsonify(response_object)
        response.status_code = HTTPStatus.CREATED
        response.headers["Location"] = f"/api/v1/products/{product_name}"
        return response
    except Exception as e:
        error = f"Error: {repr(e)}"
        abort(HTTPStatus.INTERNAL_SERVER_ERROR, error, status="fail")