@@ -102,6 +102,7 @@ def test_project_not_found(self) -> None:
102102 args .verbose = True
103103 args .id = "34ef5c5452014c52aa9ce4bc180624d8"
104104 args .outputfile = self .OUTPUTFILE
105+ args .controlfile = None
105106
106107 self .add_login_response ()
107108
@@ -149,25 +150,19 @@ def test_create_bom_multiple_purls(self, capsys):
149150 adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
150151 )
151152
152- cdx_components = sut .create_project_bom (self .get_project_for_test ())
153+ cdx_components , _ = sut .create_project_bom (self .get_project_for_test (),
154+ create_controlfile = False )
153155 captured = capsys .readouterr ()
154156
155157 assert "Multiple purls added" in captured .out
156158 assert cdx_components [0 ].purl == "pkg:deb/debian/cli-support@1.3-1 pkg:pypi/cli-support@1.3"
157159
158- @responses .activate
159- def test_project_by_id (self ):
160- sut = CreateBom ()
161-
162- self .add_login_response ()
163- sut .login (token = TestBasePytest .MYTOKEN , url = TestBasePytest .MYURL )
164-
160+ def add_project_releases_responses (self ):
165161 # the project
166- project = self .get_project_for_test ()
167162 responses .add (
168163 responses .GET ,
169164 url = self .MYURL + "resource/api/projects/p001" ,
170- json = project ,
165+ json = self . get_project_for_test () ,
171166 status = 200 ,
172167 content_type = "application/json" ,
173168 adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
@@ -194,7 +189,7 @@ def test_project_by_id(self):
194189 "attachmentType" : "SOURCE_SELF" ,
195190 "_links" : {
196191 "self" : {
197- "href" : "https://my.server.com/resource/api/attachments/r002a002 "
192+ "href" : "https://my.server.com/resource/api/attachments/r002a003 "
198193 }
199194 }
200195 })
@@ -204,7 +199,7 @@ def test_project_by_id(self):
204199 "attachmentType" : "CLEARING_REPORT" ,
205200 "_links" : {
206201 "self" : {
207- "href" : "https://my.server.com/resource/api/attachments/r002a003 "
202+ "href" : "https://my.server.com/resource/api/attachments/r002a004 "
208203 }
209204 }
210205 })
@@ -217,8 +212,19 @@ def test_project_by_id(self):
217212 content_type = "application/json" ,
218213 adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
219214 )
215+ return release
220216
221- cdx_bom = sut .create_project_cdx_bom ("p001" )
217+ @responses .activate
218+ def test_project_by_id (self ):
219+ sut = CreateBom ()
220+
221+ self .add_login_response ()
222+ sut .login (token = TestBasePytest .MYTOKEN , url = TestBasePytest .MYURL )
223+
224+ release = self .add_project_releases_responses ()
225+ project = self .get_project_for_test ()
226+
227+ cdx_bom , _ = sut .create_project_cdx_bom ("p001" , create_controlfile = False )
222228 cx_comp = cdx_bom .components [0 ]
223229 assert cx_comp .purl == release ["externalIds" ]["package-url" ]
224230
@@ -239,15 +245,15 @@ def test_project_by_id(self):
239245 assert len (ext_refs ) == 1
240246 assert ext_refs [0 ].url == release ["_embedded" ]["sw360:attachments" ][1 ]["filename" ]
241247 assert ext_refs [0 ].type == ExternalReferenceType .OTHER
242- assert ext_refs [0 ].comment , CaPyCliBom . CLI_FILE_COMMENT + " == sw360Id: r002a002"
248+ assert ext_refs [0 ].comment == CaPyCliBom . CLI_FILE_COMMENT
243249 assert ext_refs [0 ].hashes [0 ].alg == "SHA-1"
244250 assert ext_refs [0 ].hashes [0 ].content == release ["_embedded" ]["sw360:attachments" ][1 ]["sha1" ]
245251
246252 ext_refs = [e for e in cx_comp .external_references
247253 if e .comment and e .comment .startswith (CaPyCliBom .CRT_FILE_COMMENT )]
248254 assert len (ext_refs ) == 1
249255 assert ext_refs [0 ].url == release ["_embedded" ]["sw360:attachments" ][3 ]["filename" ]
250- assert ext_refs [0 ].comment , CaPyCliBom . CRT_FILE_COMMENT + " == sw360Id: r002a003"
256+ assert ext_refs [0 ].comment == CaPyCliBom . CRT_FILE_COMMENT
251257 assert ext_refs [0 ].type == ExternalReferenceType .OTHER
252258 assert ext_refs [0 ].hashes [0 ].alg == "SHA-1"
253259 assert ext_refs [0 ].hashes [0 ].content == release ["_embedded" ]["sw360:attachments" ][3 ]["sha1" ]
@@ -260,6 +266,71 @@ def test_project_by_id(self):
260266 assert cdx_bom .metadata .component .version == project ["version" ]
261267 assert cdx_bom .metadata .component .description == project ["description" ]
262268
269+ @responses .activate
270+ def test_project_by_id_controlfile (self ):
271+ sut = CreateBom ()
272+ self .add_login_response ()
273+ sut .login (token = TestBasePytest .MYTOKEN , url = TestBasePytest .MYURL )
274+
275+ self .add_project_releases_responses ()
276+
277+ # attachment info
278+ responses .add (
279+ method = responses .GET ,
280+ url = self .MYURL + "resource/api/attachments/r001a001" ,
281+ body = """
282+ {
283+ "filename": "CLIXML_wheel-0.38.4.xml",
284+ "sha1": "ccd9f1ed2f59c46ff3f0139c05bfd76f83fd9851",
285+ "attachmentType": "COMPONENT_LICENSE_INFO_XML"
286+ }""" ,
287+ status = 200 ,
288+ content_type = "application/json" ,
289+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
290+ )
291+ responses .add (
292+ method = responses .GET ,
293+ url = self .MYURL + "resource/api/attachments/r002a002" ,
294+ body = """
295+ {
296+ "filename": "CLIXML_clipython-1.3.0.xml",
297+ "sha1": "dd4c38387c6811dba67d837af7742d84e61e20de",
298+ "attachmentType": "COMPONENT_LICENSE_INFO_XML",
299+ "checkedBy": "user2@siemens.com",
300+ "checkStatus": "ACCEPTED",
301+ "createdBy": "user1@siemens.com"
302+ }""" ,
303+ status = 200 ,
304+ content_type = "application/json" ,
305+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
306+ )
307+ responses .add (
308+ method = responses .GET ,
309+ url = self .MYURL + "resource/api/attachments/r002a004" ,
310+ body = """
311+ {
312+ "filename": "clipython-1.3.0.docx",
313+ "sha1": "f0d8f2ddd017bdeaecbaec72ff76a6c0a045ec66",
314+ "attachmentType": "CLEARING_REPORT"
315+
316+ }""" ,
317+ status = 200 ,
318+ content_type = "application/json" ,
319+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
320+ )
321+
322+ _ , controlfile = sut .create_project_cdx_bom ("p001" , create_controlfile = True )
323+ assert controlfile ['ProjectName' ] == 'CaPyCLI, 1.9.0'
324+ assert controlfile ['Components' ][0 ]['ComponentName' ] == 'cli-support 1.3'
325+ assert controlfile ['Components' ][0 ]['Sw360Id' ] == 'r002'
326+ assert controlfile ['Components' ][0 ]['Sw360AttachmentId' ] == 'r002a002'
327+ assert controlfile ['Components' ][0 ]['CliFile' ] == 'CLIXML_clipython-1.3.0.xml'
328+ assert controlfile ['Components' ][0 ]['CheckedBy' ] == 'user2@siemens.com'
329+ assert controlfile ['Components' ][0 ]['CheckStatus' ] == 'ACCEPTED'
330+ assert controlfile ['Components' ][0 ]['CreatedBy' ] == 'user1@siemens.com'
331+
332+ assert controlfile ['Components' ][1 ]['ReportFile' ] == 'clipython-1.3.0.docx'
333+
263334 @responses .activate
264335 def test_project_show_by_name (self ):
265336 sut = CreateBom ()
@@ -275,6 +346,7 @@ def test_project_show_by_name(self):
275346 args .name = "CaPyCLI"
276347 args .version = "1.9.0"
277348 args .outputfile = self .OUTPUTFILE
349+ args .controlfile = None
278350
279351 self .add_login_response ()
280352
@@ -358,6 +430,65 @@ def test_project_show_by_name(self):
358430
359431 self .delete_file (self .OUTPUTFILE )
360432
433+ @responses .activate
434+ def test_create_project_bom_release_error (self ):
435+ sut = CreateBom ()
436+
437+ self .add_login_response ()
438+ sut .login (token = TestBasePytest .MYTOKEN , url = TestBasePytest .MYURL )
439+
440+ responses .add (
441+ responses .GET ,
442+ url = self .MYURL + "resource/api/releases/r001" ,
443+ status = 404 ,
444+ content_type = "application/json" ,
445+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
446+ )
447+ responses .add (
448+ responses .GET ,
449+ url = self .MYURL + "resource/api/releases/r002" ,
450+ json = self .get_release_cli_for_test (),
451+ status = 200 ,
452+ content_type = "application/json" ,
453+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
454+ )
455+ with pytest .raises (SystemExit ):
456+ bom , _ = sut .create_project_bom (self .get_project_for_test (), create_controlfile = False )
457+
458+ @responses .activate
459+ def test_create_project_bom_controlfile_attachment_error (self ):
460+ sut = CreateBom ()
461+
462+ self .add_login_response ()
463+ sut .login (token = TestBasePytest .MYTOKEN , url = TestBasePytest .MYURL )
464+
465+ responses .add (
466+ responses .GET ,
467+ url = self .MYURL + "resource/api/releases/r001" ,
468+ json = self .get_release_wheel_for_test (),
469+ status = 200 ,
470+ content_type = "application/json" ,
471+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
472+ )
473+ responses .add (
474+ responses .GET ,
475+ url = self .MYURL + "resource/api/releases/r002" ,
476+ json = self .get_release_cli_for_test (),
477+ status = 200 ,
478+ content_type = "application/json" ,
479+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
480+ )
481+ responses .add (
482+ method = responses .GET ,
483+ url = self .MYURL + "resource/api/attachments/r002a002" ,
484+ status = 404 ,
485+ content_type = "application/json" ,
486+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
487+ )
488+
489+ with pytest .raises (SystemExit ):
490+ bom , _ = sut .create_project_bom (self .get_project_for_test (), create_controlfile = True )
491+
361492
362493if __name__ == "__main__" :
363494 APP = TestCreateBom ()
0 commit comments