From 1f031716855380167c9d4242359fb81f7b044247 Mon Sep 17 00:00:00 2001 From: Marion Deveaud Date: Sun, 5 Nov 2023 22:14:51 +0100 Subject: [PATCH] fix(tests): adapt all tests to API version 1.5.1 --- .github/workflows/fossologytests.yml | 2 -- docs-source/sample_workflow.rst | 3 +- fossology/uploads.py | 43 ++++++++++-------------- tests/test_init.py | 2 +- tests/test_license.py | 8 ----- tests/test_upload_clearing.py | 14 ++++++++ tests/test_upload_licenses_copyrights.py | 8 ++--- tests/test_uploads.py | 2 +- tests/test_users.py | 11 ++---- 9 files changed, 41 insertions(+), 52 deletions(-) diff --git a/.github/workflows/fossologytests.yml b/.github/workflows/fossologytests.yml index 410a80c..35064fa 100644 --- a/.github/workflows/fossologytests.yml +++ b/.github/workflows/fossologytests.yml @@ -44,10 +44,8 @@ jobs: run: nmap fossology -p 80 - name: Run tests run: | - export API_LATEST=true poetry run coverage run --source=fossology -m pytest poetry run coverage report -m - continue-on-error: true - name: upload codecoverage results only if we are on the repository fossology/fossology-python if: ${{ github.repository == 'fossology/fossology-python' }} run: poetry run codecov -t ${{ secrets.CODECOV_TOKEN }} diff --git a/docs-source/sample_workflow.rst b/docs-source/sample_workflow.rst index 9450a33..3c0fc34 100644 --- a/docs-source/sample_workflow.rst +++ b/docs-source/sample_workflow.rst @@ -23,7 +23,8 @@ Needed imports and Variables >>> from getpass import getpass >>> import requests >>> from fossology import Fossology, fossology_token ->>> from fossology.obj import Group, AccessLevel, TokenScope +>>> from fossology.obj import Group +>>> from fossology.enums import AccessLevel, TokenScope >>> from fossology.exceptions import FossologyApiError >>> FOSSOLOGY_SERVER = "http://fossology/repo" >>> os.environ["FOSSOLOGY_USER"] = "fossy" diff --git a/fossology/uploads.py b/fossology/uploads.py index be2b29d..746ad75 100644 --- a/fossology/uploads.py +++ b/fossology/uploads.py @@ -152,7 +152,7 @@ def upload_file( :Example for a file upload: >>> from fossology import Fossology - >>> from fossology.obj import AccessLevel + >>> from fossology.enums import AccessLevel >>> foss = Fossology(FOSS_URL, FOSS_TOKEN, username) # doctest: +SKIP >>> my_upload = foss.upload_file( ... foss.rootFolder, @@ -254,18 +254,19 @@ def upload_file( f"{self.api}/uploads", files=files, headers=headers ) elif vcs or url or server: + data = dict if vcs: headers["uploadType"] = "vcs" - data = json.dumps(vcs) + data = {"location": vcs} elif url: headers["uploadType"] = "url" - data = json.dumps(url) + data = {"location": url} elif server: headers["uploadType"] = "server" - data = json.dumps(server) + data = {"location": server} headers["Content-Type"] = "application/json" response = self.session.post( - f"{self.api}/uploads", data=data, headers=headers + f"{self.api}/uploads", data=json.dumps(data), headers=headers ) else: logger.info( @@ -306,15 +307,6 @@ def upload_file( description = f"Upload {description} is not authorized" raise AuthorizationError(description, response) - elif server and response.status_code == 500: - description = ( - f"Upload {description} could not be performed; " - f"did you add a prefix for '{server['path']}' in Fossology config " - f"variable 'Admin->Customize->Whitelist for serverupload'? " - f"Has fossy user read access to {server['path']}?" - ) - raise FossologyApiError(description, response) - else: description = f"Upload {description} could not be performed" raise FossologyApiError(description, response) @@ -923,20 +915,19 @@ def schedule_bulk_scan( >>> bulk_scan_spec = { ... "bulkActions": [ ... { - ... "licenseShortName": string (example: 'MIT'), - ... "licenseText": string (example: 'License text'), - ... "acknowledgement": string (example: 'Acknowledgment text'), - ... "comment": string (example: 'Comment text'), - ... "licenseAction": LicenseAction (ADD/REMOVE), + ... "licenseShortName": 'MIT', + ... "licenseText": 'License text', + ... "acknowledgement": 'Acknowledgment text', + ... "comment": 'Comment text', + ... "licenseAction": 'ADD', # or 'REMOVE' ... } ... ], - ... "refText": string (example: 'Reference Text'), - ... "bulkScope": BulkScope (folder/upload), - ... "forceDecision": boolean (example: 'false'), - ... "ignoreIrre": boolean (example: 'false'), - ... "delimiters": string (example: 'DEFAULT'), - ... "scanOnlyFindings": boolean (example: 'true'), - ... } + ... "refText": 'Reference Text', + ... "bulkScope": 'folder', # or upload + ... "forceDecision": 'false', + ... "ignoreIrre": 'false', + ... "delimiters": 'DEFAULT', + ... "scanOnlyFindings": 'true', ... } :param upload: the upload for the bulk scan diff --git a/tests/test_init.py b/tests/test_init.py index f4c0c96..3c26cd0 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -11,7 +11,7 @@ def test_get_info(foss: Fossology): assert foss.info.name == "FOSSology API" assert foss.info.license.name == "GPL-2.0-only" - assert foss.info.fossology.version + assert foss.info.version @responses.activate diff --git a/tests/test_license.py b/tests/test_license.py index c831b6a..c74b96a 100644 --- a/tests/test_license.py +++ b/tests/test_license.py @@ -11,8 +11,6 @@ from fossology.exceptions import FossologyApiError from fossology.obj import License, Obligation -shortname = "GPL-2.0+" - @pytest.fixture() def test_license(): @@ -48,12 +46,6 @@ def test_detail_license_not_found(foss: fossology.Fossology): assert "License Unknown not found" in str(excinfo.value) -def test_detail_license(foss: fossology.Fossology): - detail_license = foss.detail_license(shortname, group="fossy") - assert detail_license - assert isinstance(detail_license, License) - - @responses.activate def test_list_licenses_error(foss_server: str, foss: fossology.Fossology): responses.add(responses.GET, f"{foss_server}/api/v1/license", status=500) diff --git a/tests/test_upload_clearing.py b/tests/test_upload_clearing.py index 5e11e94..1c2e434 100644 --- a/tests/test_upload_clearing.py +++ b/tests/test_upload_clearing.py @@ -10,12 +10,14 @@ from fossology.obj import Upload +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") def test_upload_get_clearing_history(foss: Fossology, upload_with_jobs: Upload): files, _ = foss.search(license="BSD") history = foss.get_clearing_history(upload_with_jobs, files[0].uploadTreeId) assert not history +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") def test_upload_get_clearing_history_with_unknown_item_raises_api_error( foss: Fossology, upload_with_jobs: Upload ): @@ -24,6 +26,7 @@ def test_upload_get_clearing_history_with_unknown_item_raises_api_error( assert f"Upload {upload_with_jobs.id} or item 1 not found" in str(excinfo.value) +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") @responses.activate def test_upload_get_clearing_history_500_error( foss: Fossology, foss_server: str, upload_with_jobs: Upload @@ -41,12 +44,14 @@ def test_upload_get_clearing_history_500_error( ) +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") def test_upload_get_bulk_history(foss: Fossology, upload_with_jobs: Upload): files, _ = foss.search(license="BSD") history = foss.get_bulk_history(upload_with_jobs, files[0].uploadTreeId) assert not history +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") def test_upload_get_bulk_history_with_unknown_item_raises_api_error( foss: Fossology, upload_with_jobs: Upload ): @@ -55,6 +60,7 @@ def test_upload_get_bulk_history_with_unknown_item_raises_api_error( assert f"Upload {upload_with_jobs.id} or item 1 not found" in str(excinfo.value) +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") @responses.activate def test_upload_get_bulk_history_500_error( foss: Fossology, foss_server: str, upload_with_jobs: Upload @@ -72,6 +78,7 @@ def test_upload_get_bulk_history_500_error( ) +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") def test_upload_schedule_bulk_scan( foss: Fossology, upload_with_jobs: Upload, foss_bulk_scan_spec: dict ): @@ -85,6 +92,7 @@ def test_upload_schedule_bulk_scan( assert history[0].addedLicenses == ["MIT"] +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") def test_schedule_bulk_scan_with_unknown_item_raises_api_error( foss: Fossology, upload_with_jobs: Upload, foss_bulk_scan_spec: dict ): @@ -93,6 +101,7 @@ def test_schedule_bulk_scan_with_unknown_item_raises_api_error( assert f"Upload {upload_with_jobs.id} or item 1 not found" in str(excinfo.value) +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") @responses.activate def test_schedule_bulk_scan_500_error( foss: Fossology, @@ -113,12 +122,14 @@ def test_schedule_bulk_scan_500_error( ) +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") def test_upload_get_prev_next(foss: Fossology, upload_with_jobs: Upload): files, _ = foss.search(license="BSD") prev_next = foss.get_prev_next(upload_with_jobs, files[0].uploadTreeId) assert prev_next +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") def test_upload_get_prev_next_with_licenses(foss: Fossology, upload_with_jobs: Upload): files, _ = foss.search(license="BSD") prev_next = foss.get_prev_next( @@ -127,6 +138,7 @@ def test_upload_get_prev_next_with_licenses(foss: Fossology, upload_with_jobs: U assert prev_next +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") def test_upload_get_prev_next_no_clearing(foss: Fossology, upload_with_jobs: Upload): files, _ = foss.search(license="BSD") prev_next = foss.get_prev_next( @@ -135,6 +147,7 @@ def test_upload_get_prev_next_no_clearing(foss: Fossology, upload_with_jobs: Upl assert prev_next +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") def test_upload_get_prev_next_with_unknown_item_raises_api_error( foss: Fossology, upload_with_jobs: Upload ): @@ -143,6 +156,7 @@ def test_upload_get_prev_next_with_unknown_item_raises_api_error( assert f"Upload {upload_with_jobs.id} or item 1 not found" in str(excinfo.value) +@pytest.mark.skip(reason="Not yet released, waiting for API version 1.6.0") @responses.activate def test_upload_get_prev_next_500_error( foss: Fossology, foss_server: str, upload_with_jobs: Upload diff --git a/tests/test_upload_licenses_copyrights.py b/tests/test_upload_licenses_copyrights.py index 95dc2d4..afdcc32 100644 --- a/tests/test_upload_licenses_copyrights.py +++ b/tests/test_upload_licenses_copyrights.py @@ -13,7 +13,7 @@ def test_upload_licenses(foss: Fossology, upload_with_jobs: Upload): # Default agent "nomos" licenses = foss.upload_licenses(upload_with_jobs) - assert len(licenses) == 47 + assert len(licenses) == 56 def test_upload_licenses_with_containers(foss: Fossology, upload_with_jobs: Upload): @@ -23,17 +23,17 @@ def test_upload_licenses_with_containers(foss: Fossology, upload_with_jobs: Uplo def test_upload_licenses_agent_ojo(foss: Fossology, upload_with_jobs: Upload): licenses = foss.upload_licenses(upload_with_jobs, agent="ojo") - assert len(licenses) == 0 + assert len(licenses) == 9 def test_upload_licenses_agent_monk(foss: Fossology, upload_with_jobs: Upload): licenses = foss.upload_licenses(upload_with_jobs, agent="monk") - assert len(licenses) == 13 + assert len(licenses) == 22 def test_upload_licenses_and_copyrights(foss: Fossology, upload_with_jobs: Upload): licenses = foss.upload_licenses(upload_with_jobs, copyright=True) - assert len(licenses) == 47 + assert len(licenses) == 56 def test_upload_licenses_with_unknown_group_raises_authorization_error( diff --git a/tests/test_uploads.py b/tests/test_uploads.py index b7b5261..2e4e7a0 100644 --- a/tests/test_uploads.py +++ b/tests/test_uploads.py @@ -151,7 +151,7 @@ def test_copy_upload(foss: Fossology, upload: Upload): def test_move_upload_to_non_existing_folder(foss: Fossology, upload: Upload): non_folder = Folder(secrets.randbelow(1000), "Non folder", "", foss.rootFolder) - with pytest.raises(FossologyApiError): + with pytest.raises(AuthorizationError): foss.move_upload(upload, non_folder, "move") diff --git a/tests/test_users.py b/tests/test_users.py index f2a410b..2c6a044 100644 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -87,8 +87,9 @@ def test_unknown_user(foss: Fossology): def test_list_users(foss: Fossology): + # Fixture created_foss_user creates a new user for the test session users = foss.list_users() - assert len(users) == 1 + assert len(users) == 2 @responses.activate @@ -161,11 +162,3 @@ def test_delete_user(foss_server: str, foss: Fossology): with pytest.raises(FossologyApiError) as excinfo: foss.delete_user(user) assert f"Error while deleting user {user.name} ({user.id})" in str(excinfo.value) - - -@responses.activate -def test_noversion(foss_server: str, foss: Fossology): - responses.add(responses.GET, f"{foss_server}/api/v1/version", status=404) - with pytest.raises(FossologyApiError) as excinfo: - foss.get_version() - assert "Error while getting API version" in str(excinfo.value)