-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bom downloadAttachments): read attachment id from control file
- Loading branch information
Showing
3 changed files
with
81 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"ProjectName": "CaPyCLI, 2.0.0-dev1", | ||
"Components": [ | ||
{ | ||
"ComponentName": "certifi 2022.12.7", | ||
"Sw360Id": "ae8c7ed", | ||
"Sw360AttachmentId": "794446", | ||
"CreatedBy": "[email protected]", | ||
"CreatedTeam": "AA", | ||
"CreatedOn": "2020-10-23", | ||
"CheckStatus": "ACCEPTED", | ||
"CheckedBy": "[email protected]", | ||
"CheckedTeam": "BB", | ||
"CheckedOn": "2020-10-30", | ||
"CliFile": "CLIXML_certifi-2022.12.7.xml" | ||
}, | ||
{ | ||
"ComponentName": "certifi 2022.12.7", | ||
"Sw360Id": "ae8c7ed", | ||
"Sw360AttachmentId": "63b368", | ||
"ReportFile": "certifi-2022.12.7_clearing_report.docx" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,16 @@ | |
|
||
import responses | ||
|
||
from capycli.common.capycli_bom_support import CaPyCliBom, CycloneDxSupport | ||
from capycli.common.capycli_bom_support import CaPyCliBom | ||
from capycli.common.json_support import load_json_file | ||
from capycli.bom.download_attachments import BomDownloadAttachments | ||
from capycli.main.result_codes import ResultCode | ||
from cyclonedx.model import ExternalReferenceType, HashAlgorithm | ||
from tests.test_base import AppArguments, TestBase | ||
|
||
|
||
class TestBomDownloadAttachments(TestBase): | ||
INPUTFILE = "sbom_for_download.json" | ||
CONTROLFILE = "sbom_for_download-control.json" | ||
INPUTERROR = "plaintext.txt" | ||
OUTPUTFILE = "output.json" | ||
|
||
|
@@ -69,6 +70,8 @@ def test_file_not_found(self) -> None: | |
args.command.append("bom") | ||
args.command.append("downloadattachments") | ||
args.inputfile = "DOESNOTEXIST" | ||
args.controlfile = os.path.join(os.path.dirname(__file__), | ||
"fixtures", TestBomDownloadAttachments.CONTROLFILE) | ||
|
||
sut.run(args) | ||
self.assertTrue(False, "Failed to report missing file") | ||
|
@@ -85,6 +88,8 @@ def test_error_loading_file(self) -> None: | |
args.command.append("bom") | ||
args.command.append("downloadattachments") | ||
args.inputfile = os.path.join(os.path.dirname(__file__), "fixtures", TestBomDownloadAttachments.INPUTERROR) | ||
args.controlfile = os.path.join(os.path.dirname(__file__), | ||
"fixtures", TestBomDownloadAttachments.CONTROLFILE) | ||
|
||
sut.run(args) | ||
self.assertTrue(False, "Failed to report invalid file") | ||
|
@@ -103,6 +108,8 @@ def test_source_folder_does_not_exist(self) -> None: | |
args.command.append("downloadattachments") | ||
|
||
args.inputfile = os.path.join(os.path.dirname(__file__), "fixtures", TestBomDownloadAttachments.INPUTFILE) | ||
args.controlfile = os.path.join(os.path.dirname(__file__), | ||
"fixtures", TestBomDownloadAttachments.CONTROLFILE) | ||
args.source = "XXX" | ||
|
||
sut.run(args) | ||
|
@@ -113,32 +120,10 @@ def test_source_folder_does_not_exist(self) -> None: | |
@responses.activate | ||
def test_simple_bom(self) -> None: | ||
bom = os.path.join(os.path.dirname(__file__), "fixtures", TestBomDownloadAttachments.INPUTFILE) | ||
bom = CaPyCliBom.read_sbom(bom) | ||
controlfile = os.path.join(os.path.dirname(__file__), "fixtures", TestBomDownloadAttachments.CONTROLFILE) | ||
|
||
# attachment info - CLI | ||
responses.add( | ||
method=responses.GET, | ||
url=self.MYURL + "resource/api/attachments/794446", | ||
body=""" | ||
{ | ||
"filename": "CLIXML_certifi-2022.12.7.xml", | ||
"sha1": "3cd24769fa3da4af74d0118433619a130da091b0", | ||
"attachmentType": "COMPONENT_LICENSE_INFO_XML", | ||
"createdBy": "[email protected]", | ||
"createdTeam": "AA", | ||
"createdComment": "comment1", | ||
"createdOn": "2020-10-08", | ||
"checkStatus": "NOTCHECKED", | ||
"_links": { | ||
"self": { | ||
"href": "https://my.server.com/resource/api/attachments/794446" | ||
} | ||
} | ||
}""", | ||
status=200, | ||
content_type="application/json", | ||
adding_headers={"Authorization": "Token " + self.MYTOKEN}, | ||
) | ||
bom = CaPyCliBom.read_sbom(bom) | ||
controlfile = load_json_file(controlfile) | ||
|
||
# get attachment - CLI | ||
cli_file = self.get_cli_file_mit() | ||
|
@@ -150,35 +135,6 @@ def test_simple_bom(self) -> None: | |
content_type="application/text", | ||
adding_headers={"Authorization": "Token " + self.MYTOKEN}, | ||
) | ||
|
||
# attachment info - report | ||
responses.add( | ||
method=responses.GET, | ||
url=self.MYURL + "resource/api/attachments/63b368", | ||
body=""" | ||
{ | ||
"filename": "certifi-2022.12.7_clearing_report.docx", | ||
"sha1": "3cd24769fa3da4af74d0118433619a130da091b0", | ||
"attachmentType": "CLEARING_REPORT", | ||
"createdBy": "[email protected]", | ||
"createdTeam": "BB", | ||
"createdComment": "comment3", | ||
"createdOn": "2020-10-08", | ||
"checkedBy": "[email protected]", | ||
"checkedOn" : "2021-01-18", | ||
"checkedComment": "comment4", | ||
"checkStatus": "ACCEPTED", | ||
"_links": { | ||
"self": { | ||
"href": "https://my.server.com/resource/api/attachments/63b368" | ||
} | ||
} | ||
}""", | ||
status=200, | ||
content_type="application/json", | ||
adding_headers={"Authorization": "Token " + self.MYTOKEN}, | ||
) | ||
|
||
# get attachment - report | ||
responses.add( | ||
method=responses.GET, | ||
|
@@ -191,7 +147,7 @@ def test_simple_bom(self) -> None: | |
|
||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
try: | ||
bom = self.app.download_attachments(bom, tmpdirname) | ||
bom = self.app.download_attachments(bom, controlfile["Components"], tmpdirname) | ||
resultfile = os.path.join(tmpdirname, "CLIXML_certifi-2022.12.7.xml") | ||
self.assertEqual(bom.components[0].external_references[5].url, resultfile) | ||
self.assertTrue(os.path.isfile(resultfile), "CLI file missing") | ||
|
@@ -211,25 +167,8 @@ def test_simple_bom_relpath(self) -> None: | |
bom = os.path.join(os.path.dirname(__file__), "fixtures", TestBomDownloadAttachments.INPUTFILE) | ||
bom = CaPyCliBom.read_sbom(bom) | ||
|
||
# attachment info - CLI | ||
responses.add( | ||
method=responses.GET, | ||
url=self.MYURL + "resource/api/attachments/794446", | ||
body=""" | ||
{ | ||
"filename": "CLIXML_certifi-2022.12.7.xml", | ||
"sha1": "3cd24769fa3da4af74d0118433619a130da091b0", | ||
"attachmentType": "COMPONENT_LICENSE_INFO_XML", | ||
"_links": { | ||
"self": { | ||
"href": "https://my.server.com/resource/api/attachments/794446" | ||
} | ||
} | ||
}""", | ||
status=200, | ||
content_type="application/json", | ||
adding_headers={"Authorization": "Token " + self.MYTOKEN}, | ||
) | ||
controlfile = os.path.join(os.path.dirname(__file__), "fixtures", TestBomDownloadAttachments.CONTROLFILE) | ||
controlfile = load_json_file(controlfile) | ||
|
||
# get attachment - CLI | ||
cli_file = self.get_cli_file_mit() | ||
|
@@ -244,7 +183,8 @@ def test_simple_bom_relpath(self) -> None: | |
|
||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
try: | ||
bom = self.app.download_attachments(bom, tmpdirname, tmpdirname, ("COMPONENT_LICENSE_INFO_XML",)) | ||
bom = self.app.download_attachments(bom, controlfile["Components"], | ||
tmpdirname, tmpdirname, ("COMPONENT_LICENSE_INFO_XML",)) | ||
resultfile = os.path.join(tmpdirname, "CLIXML_certifi-2022.12.7.xml") | ||
self.assertEqual(bom.components[0].external_references[5].url, | ||
"file://CLIXML_certifi-2022.12.7.xml") | ||
|
@@ -263,59 +203,29 @@ def test_simple_bom_download_errors(self) -> None: | |
bom = os.path.join(os.path.dirname(__file__), "fixtures", TestBomDownloadAttachments.INPUTFILE) | ||
bom = CaPyCliBom.read_sbom(bom) | ||
|
||
# attachment info - CLI, ok | ||
responses.add( | ||
method=responses.GET, | ||
url=self.MYURL + "resource/api/attachments/794446", | ||
body=""" | ||
{ | ||
"filename": "CLIXML_certifi-2022.12.7.xml", | ||
"sha1": "3cd24769fa3da4af74d0118433619a130da091b0", | ||
"attachmentType": "COMPONENT_LICENSE_INFO_XML", | ||
"_links": { | ||
"self": { | ||
"href": "https://my.server.com/resource/api/attachments/794446" | ||
} | ||
} | ||
}""", | ||
status=200, | ||
content_type="application/json", | ||
adding_headers={"Authorization": "Token " + self.MYTOKEN}, | ||
) | ||
controlfile = os.path.join(os.path.dirname(__file__), "fixtures", TestBomDownloadAttachments.CONTROLFILE) | ||
controlfile = load_json_file(controlfile) | ||
|
||
# get attachment - CLI, error | ||
responses.add( | ||
method=responses.GET, | ||
url=self.MYURL + "resource/api/releases/ae8c7ed/attachments/794446", | ||
body="cli_file", | ||
status=500, | ||
content_type="application/text", | ||
adding_headers={"Authorization": "Token " + self.MYTOKEN}, | ||
) | ||
|
||
# attachment info - report, error | ||
# get attachment - CLI, error | ||
responses.add( | ||
method=responses.GET, | ||
url=self.MYURL + "resource/api/attachments/63b368", | ||
body=""" | ||
{ | ||
"filename": "certifi-2022.12.7_clearing_report.docx", | ||
"sha1": "3cd24769fa3da4af74d0118433619a130da091b0", | ||
"attachmentType": "CLEARING_REPORT", | ||
"_links": { | ||
"self": { | ||
"href": "https://my.server.com/resource/api/attachments/63b368" | ||
} | ||
} | ||
}""", | ||
status=404, | ||
content_type="application/json", | ||
url=self.MYURL + "resource/api/releases/ae8c7ed/attachments/63b368", | ||
status=403, | ||
content_type="application/text", | ||
adding_headers={"Authorization": "Token " + self.MYTOKEN}, | ||
) | ||
|
||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
try: | ||
bom = self.app.download_attachments(bom, tmpdirname) | ||
bom = self.app.download_attachments(bom, controlfile["Components"], tmpdirname) | ||
resultfile = os.path.join(tmpdirname, "CLIXML_certifi-2022.12.7.xml") | ||
self.assertFalse(os.path.isfile(resultfile), "CLI created despite HTTP 500") | ||
|
||
|
@@ -335,8 +245,8 @@ def test_simple_bom_no_release_id(self) -> None: | |
bom.components[0].properties = [] | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
try: | ||
err = self.capture_stdout(self.app.download_attachments, bom, tmpdirname) | ||
assert "No sw360Id for release" in err | ||
err = self.capture_stdout(self.app.download_attachments, bom, [], tmpdirname) | ||
self.assertIn("No sw360Id for release", err) | ||
|
||
return | ||
except Exception as e: # noqa | ||
|
@@ -346,18 +256,14 @@ def test_simple_bom_no_release_id(self) -> None: | |
self.assertTrue(False, "Error: we must never arrive here") | ||
|
||
@responses.activate | ||
def test_simple_bom_no_attachment_id(self) -> None: | ||
def test_simple_bom_no_ctrl_file_entry(self) -> None: | ||
bom = os.path.join(os.path.dirname(__file__), "fixtures", TestBomDownloadAttachments.INPUTFILE) | ||
bom = CaPyCliBom.read_sbom(bom) | ||
bom.components[0].external_references = [] | ||
CycloneDxSupport.set_ext_ref(bom.components[0], ExternalReferenceType.OTHER, | ||
CaPyCliBom.CLI_FILE_COMMENT, "CLIXML_foo.xml", | ||
HashAlgorithm.SHA_1, "123") | ||
|
||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
try: | ||
err = self.capture_stdout(self.app.download_attachments, bom, tmpdirname) | ||
assert "No sw360Id for attachment" in err | ||
err = self.capture_stdout(self.app.download_attachments, bom, [], tmpdirname) | ||
assert "Found 0 entries for attachment CLIXML_certifi-2022.12.7.xml" in err | ||
|
||
return | ||
except Exception as e: # noqa | ||
|