def test_move_upload(self): test_upload = get_upload() if not test_upload: test_upload = do_upload() logger.debug( f"{test_upload.id} uploaded from test 'test_move_upload'") move_folder = foss.create_folder(foss.rootFolder, "MoveUploadTest", "Test move upload function") foss.move_upload(test_upload, move_folder) moved_upload = foss.detail_upload(test_upload.id) self.assertEqual(moved_upload.folderid, move_folder.id, "Upload couldn't be moved") foss.copy_upload(moved_upload, foss.rootFolder) list_uploads = foss.list_uploads() test_upload = None for upload in list_uploads: if upload.folderid == foss.rootFolder.id: test_upload = upload self.assertIsNotNone(test_upload, "Upload couldn't be copied") # Clean up foss.delete_folder(move_folder) # Move/Copy to arbitrary folder non_folder = Folder(secrets.randbelow(1000), "Non folder", "", foss.rootFolder) self.assertRaises(FossologyApiError, foss.move_upload, test_upload, non_folder) self.assertRaises(FossologyApiError, foss.copy_upload, test_upload, non_folder)
def test_create_folder(self): name = "FossPythonTest" desc = "Created via the Fossology Python API" test_folder = foss.create_folder(foss.rootFolder, name, description=desc) self.assertEqual(test_folder.name, name, f"Main test {name} folder couldn't be created") self.assertEqual( test_folder.description, desc, "Description of folder on the server is wrong", ) # Recreate folder to test API response 200 test_folder = foss.create_folder(foss.rootFolder, name, description=desc) self.assertEqual(test_folder.name, name, f"Main test {name} folder couldn't be created") # Create folder in arbitrary parent parent = Folder(secrets.randbelow(1000), "Parent", "", 0) self.assertRaises(AuthorizationError, foss.create_folder, parent, "No Parent") foss.delete_folder(test_folder)
def test_move_copy_arbitrary_folder(foss: Fossology, upload: Upload): non_folder = Folder(secrets.randbelow(1000), "Non folder", "", foss.rootFolder) with pytest.raises(AuthorizationError): foss.move_upload(upload, non_folder) with pytest.raises(AuthorizationError): foss.copy_upload(upload, non_folder)
def test_delete_folder_error(foss_server: str, foss: Fossology): folder = Folder(secrets.randbelow(1000), "Folder", "", foss.rootFolder) responses.add( responses.DELETE, f"{foss_server}/api/v1/folders/{folder.id}", status=404 ) with pytest.raises(FossologyApiError) as excinfo: foss.delete_folder(folder) assert f"Unable to delete folder {folder.id}" in str(excinfo.value)
def test_move_copy_upload(foss: Fossology, upload: Upload, move_folder: Folder): foss.move_upload(upload, move_folder) moved_upload = foss.detail_upload(upload.id) assert moved_upload.folderid == move_folder.id foss.copy_upload(moved_upload, foss.rootFolder) list_uploads = foss.list_uploads() test_upload = None for upload in list_uploads: if upload.folderid == foss.rootFolder.id: test_upload = upload assert test_upload # To arbitrary folder non_folder = Folder(secrets.randbelow(1000), "Non folder", "", foss.rootFolder) with pytest.raises(AuthorizationError): foss.move_upload(upload, non_folder) with pytest.raises(AuthorizationError): foss.copy_upload(upload, non_folder)
def list_folders(self): """List all folders accessible to the authenticated user API Endpoint: GET /folders :return: a list of folders :rtype: list() :raises FossologyApiError: if the REST call failed """ response = self.session.get(f"{self.api}/folders") if response.status_code == 200: folders_list = list() response_list = response.json() for folder in response_list: sub_folder = Folder.from_json(folder) folders_list.append(sub_folder) return folders_list else: description = f"Unable to get a list of folders for {self.user.name}" raise FossologyApiError(description, response)
def detail_folder(self, folder_id): """Get details of folder. API Endpoint: GET /folders/{id} :param id: the ID of the folder to be analysed :type id: int :return: the requested folder :rtype: Folder() object :raises FossologyApiError: if the REST call failed """ response = self.session.get(f"{self.api}/folders/{folder_id}") if response.status_code == 200: detailled_folder = Folder.from_json(response.json()) for folder in self.folders: if folder.id == folder_id: self.folders.remove(folder) self.folders.append(detailled_folder) return detailled_folder else: description = f"Error while getting details for folder {folder_id}" raise FossologyApiError(description, response)
def test_copy_upload_to_non_existing_folder(foss: Fossology, upload: Upload): non_folder = Folder(secrets.randbelow(1000), "Non folder", "", foss.rootFolder) with pytest.raises(AuthorizationError): foss.copy_upload(upload, non_folder)
def test_create_folder_error(foss_server: str, foss: Fossology): parent = Folder(secrets.randbelow(1000), "NonFolder", "", foss.rootFolder) responses.add(responses.POST, f"{foss_server}/api/v1/folders", status=404) with pytest.raises(FossologyApiError) as excinfo: foss.create_folder(parent, "TestFolderNoParent") assert "Unable to create folder TestFolderNoParent" in str(excinfo.value)
def test_create_folder_no_parent(foss: Fossology): parent = Folder(secrets.randbelow(1000), "Parent", "", 0) with pytest.raises(AuthorizationError) as excinfo: foss.create_folder(parent, "FossFolderNoParent") assert f"Folder creation in folder {parent.id} not authorized" in str(excinfo.value)