Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions fossology/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,28 @@ def import_licenses_csv(
description = f"Unable to import licenses from {csv_file}"
raise FossologyApiError(description, response)

def export_licenses_csv(self, license_id: int = 0) -> str:
"""Export licenses as CSV.

API Endpoint: GET /license/export-csv

:param license_id: id of the license to export, 0 to export all (default: 0)
:type license_id: int
:return: the exported licenses as CSV text
:rtype: str
:raises FossologyApiError: if the REST call failed
"""
response = self.session.get(
f"{self.api}/license/export-csv", params={"id": license_id}
)

if response.status_code == 200:
logger.info(f"Exported licenses as CSV (id={license_id})")
return response.text

description = f"Unable to export licenses as CSV (id={license_id})"
raise FossologyApiError(description, response)

def update_license(
self,
shortname: str,
Expand Down
22 changes: 22 additions & 0 deletions tests/test_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,28 @@ def test_import_licenses_csv_request_payload(
assert "shortname,fullname\nFoo,Foo License" in body


def test_export_import_licenses_csv_roundtrip(foss: fossology.Fossology, tmp_path):
# Export the full license set, then re-import it. The export format matches
# the import format, and re-importing existing licenses is idempotent.
exported = foss.export_licenses_csv()
assert isinstance(exported, str)
assert exported
csv_path = tmp_path / "exported.csv"
csv_path.write_text(exported)
message = foss.import_licenses_csv(str(csv_path))
assert "Read csv" in message


@responses.activate
def test_export_licenses_csv_error(foss_server: str, foss: fossology.Fossology):
responses.add(
responses.GET, f"{foss_server}/api/v1/license/export-csv", status=403
)
with pytest.raises(FossologyApiError) as excinfo:
foss.export_licenses_csv()
assert "Unable to export licenses as CSV (id=0)" in str(excinfo.value)


@responses.activate
def test_detail_license_error(foss_server: str, foss: fossology.Fossology):
responses.add(responses.GET, f"{foss_server}/api/v1/license/Blah", status=500)
Expand Down