def test_delete_unknown_upload_unknown_group(foss: Fossology): if versiontuple(foss.version) > versiontuple("1.0.16"): upload = Upload( foss.rootFolder, "Root Folder", secrets.randbelow(1000), "", "Non Upload", "2020-05-05", hash={ "sha1": None, "md5": None, "sha256": None, "size": None }, ) else: upload = Upload( foss.rootFolder, "Root Folder", secrets.randbelow(1000), "", "Non Upload", "2020-05-05", filesize="1024", filesha1="597d209fd962f401866f12db9fa1f7301aee15a9", ) with pytest.raises(FossologyApiError): foss.delete_upload(upload) with pytest.raises(AuthorizationError) as excinfo: foss.delete_upload(upload, group="test") assert f"Deleting upload {upload.id} for group test not authorized" in str( excinfo.value)
def detail_upload(self, upload_id: int, group: str = None, wait_time: int = 0) -> Upload: """Get detailled information about an upload API Endpoint: GET /uploads/{id} Get information about a given upload. If the upload is not ready wait another ``wait_time`` seconds or look at the ``Retry-After`` to determine how long the wait period shall be. If ``wait_time`` is 0, the time interval specified by the ``Retry-After`` header is used. The function stops trying after **10 attempts**. :Examples: >>> # Wait up to 20 minutes until the upload is ready >>> long_upload = detail_upload(1, 120) >>> # Wait up to 5 minutes until the upload is ready >>> long_upload = detail_upload(1, 30) :param upload_id: the id of the upload :param group: the group the upload shall belong to :param wait_time: use a customized upload wait time instead of Retry-After (in seconds, default: 0) :type upload_id: int :type group: string :type wait_time: int :return: the upload data :rtype: Upload :raises FossologyApiError: if the REST call failed :raises AuthorizationError: if the user can't access the group """ headers = {} if group: headers["groupName"] = group response = self.session.get(f"{self.api}/uploads/{upload_id}", headers=headers) if response.status_code == 200: logger.debug(f"Got details for upload {upload_id}") return Upload.from_json(response.json()) elif response.status_code == 403: description = f"Getting details for upload {upload_id} {get_options(group)}not authorized" raise AuthorizationError(description, response) elif response.status_code == 503: if not wait_time: wait_time = response.headers["Retry-After"] logger.debug( f"Retry GET upload {upload_id} after {wait_time} seconds: {response.json()['message']}" ) time.sleep(int(wait_time)) raise TryAgain else: description = f"Error while getting details for upload {upload_id}" raise FossologyApiError(description, response)
def list_uploads(self, folder=None, group=None, recursive=True, page_size=20, page=1): """Get all uploads available to the registered user API Endpoint: GET /uploads :param folder: only list uploads from the given folder :param group: list uploads from a specific group (not only your own uploads) (default: None) :param recursive: wether to list uploads from children folders or not (default: True) :param page_size: limit the number of uploads per page (default: 20) :param page: the number of the page to fetch uploads from (default: 1) :type folder: Folder :type group: string :type recursive: boolean :type page_size: int :type page: int :return: a list of uploads :rtype: list of Upload :raises FossologyApiError: if the REST call failed :raises AuthorizationError: if the user can't access the group """ params = {} headers = {"limit": str(page_size), "page": str(page)} if group: headers["groupName"] = group if folder: params["folderId"] = folder.id if not recursive: params["recursive"] = "false" response = self.session.get(f"{self.api}/uploads", headers=headers, params=params) if response.status_code == 200: uploads_list = list() for upload in response.json(): uploads_list.append(Upload.from_json(upload)) logger.info( f"Retrieved page {page} of uploads, {response.headers.get('X-TOTAL-PAGES', 'Unknown')} pages are in total available" ) return uploads_list elif response.status_code == 403: description = ( f"Retrieving list of uploads {get_options(group, folder)}not authorized" ) raise AuthorizationError(description, response) else: description = "Unable to retrieve the list of uploads" raise FossologyApiError(description, response)
def test_delete_unknown_upload(foss: Fossology): upload = Upload( foss.rootFolder, "Root Folder", secrets.randbelow(1000), "", "Non Upload", "2020-05-05", { "sha1": None, "md5": None, "sha256": None, "size": None }, ) with pytest.raises(FossologyApiError): foss.delete_upload(upload)
def test_search_upload_does_not_exist(foss: Fossology): # Before 1.0.17 Fossology was not able to limit search to a specific upload if versiontuple(foss.version) > versiontuple("1.0.16"): hash = {"sha1": "", "md5": "", "sha256": "", "size": ""} fake_upload = Upload( secrets.randbelow(1000), "fake_folder", secrets.randbelow(1000), "", "fake_upload", "2020-12-30", hash=hash, ) search_result = foss.search( searchType=SearchTypes.ALLFILES, upload=fake_upload, filename="share", ) assert not search_result
def test_delete_upload(self): test_upload = get_upload() if not test_upload: test_upload = do_upload() foss.delete_upload(test_upload) logger.debug( f"Waiting 10 second after scheduling {test_upload.id} deletion") time.sleep(10) verify_uploads = foss.list_uploads() self.assertEqual(len(verify_uploads), 0, "Upload couldn't be deleted") # Delete arbitrary upload non_upload = Upload( foss.rootFolder, "Root Folder", secrets.randbelow(1000), "", "Non Upload", "2020-05-05", "0", ) self.assertRaises(FossologyApiError, foss.delete_upload, non_upload)
def list_uploads( self, folder: int = None, group: str = None, recursive: bool = True, name: str = None, status: ClearingStatus = None, assignee: str = None, since: str = None, page_size=100, page=1, all_pages=False, ): """Get uploads according to filtering criteria (or all available) API Endpoint: GET /uploads :param folder: only list uploads from the given folder :param group: list uploads from a specific group (not only your own uploads) (default: None) :param recursive: wether to list uploads from children folders or not (default: True) :param name: filter pattern for name and description :param status: status of uploads :param assignee: user name to which uploads are assigned to or "-me-" or "-unassigned-" :param since: uploads since given date in YYYY-MM-DD format :param page_size: limit the number of uploads per page (default: 100) :param page: the number of the page to fetch uploads from (default: 1) :param all_pages: get all uploads (default: False) :type folder: Folder :type group: string :type recursive: boolean :type name: str :type status: ClearingStatus :type assignee: str :type since: str :type page_size: int :type page: int :type all_pages: boolean :return: a tuple containing the list of uploads and the total number of pages :rtype: Tuple(list of Upload, int) :raises FossologyApiError: if the REST call failed :raises AuthorizationError: if the user can't access the group """ headers = {"limit": str(page_size)} if group: headers["groupName"] = group params = list_uploads_parameters( folder=folder, recursive=recursive, name=name, status=status, assignee=assignee, since=since, ) uploads_list = list() if all_pages: # will be reset after the total number of pages has been retrieved from the API x_total_pages = 2 else: x_total_pages = page while page <= x_total_pages: headers["page"] = str(page) response = self.session.get(f"{self.api}/uploads", headers=headers, params=params) if response.status_code == 200: for upload in response.json(): uploads_list.append(Upload.from_json(upload)) x_total_pages = int(response.headers.get("X-TOTAL-PAGES", 0)) if not all_pages or x_total_pages == 0: logger.info( f"Retrieved page {page} of uploads, {x_total_pages} pages are in total available" ) return uploads_list, x_total_pages page += 1 elif response.status_code == 403: description = f"Retrieving list of uploads {get_options(group, folder)}not authorized" raise AuthorizationError(description, response) else: description = f"Unable to retrieve the list of uploads from page {page}" raise FossologyApiError(description, response) logger.info(f"Retrieved all {x_total_pages} of uploads") return uploads_list, x_total_pages
def list_uploads( self, folder=None, group=None, recursive=True, page_size=100, page=1, all_pages=False, ): """Get all uploads available to the registered user API Endpoint: GET /uploads :param folder: only list uploads from the given folder :param group: list uploads from a specific group (not only your own uploads) (default: None) :param recursive: wether to list uploads from children folders or not (default: True) :param page_size: limit the number of uploads per page (default: 100) :param page: the number of the page to fetch uploads from (default: 1) :param all_pages: get all uploads (default: False) :type folder: Folder :type group: string :type recursive: boolean :type page_size: int :type page: int :type all_pages: boolean :return: a tuple containing the list of uploads and the total number of pages :rtype: Tuple(list of Upload, int) :raises FossologyApiError: if the REST call failed :raises AuthorizationError: if the user can't access the group """ params = {} headers = {"limit": str(page_size)} if group: headers["groupName"] = group if folder: params["folderId"] = folder.id if not recursive: params["recursive"] = "false" uploads_list = list() if all_pages: # will be reset after the total number of pages has been retrieved from the API x_total_pages = 2 else: x_total_pages = page while page <= x_total_pages: headers["page"] = str(page) response = self.session.get(f"{self.api}/uploads", headers=headers, params=params) if response.status_code == 200: for upload in response.json(): uploads_list.append(Upload.from_json(upload)) x_total_pages = int(response.headers.get("X-TOTAL-PAGES", 0)) if not all_pages or x_total_pages == 0: logger.info( f"Retrieved page {page} of uploads, {x_total_pages} pages are in total available" ) return uploads_list, x_total_pages page += 1 elif response.status_code == 403: description = f"Retrieving list of uploads {get_options(group, folder)}not authorized" raise AuthorizationError(description, response) else: description = f"Unable to retrieve the list of uploads from page {page}" raise FossologyApiError(description, response) logger.info(f"Retrieved all {x_total_pages} of uploads") return uploads_list, x_total_pages