11
11
12
12
import responses
13
13
14
- from capycli .common .capycli_bom_support import CaPyCliBom , CycloneDxSupport
14
+ from capycli .common .capycli_bom_support import CaPyCliBom
15
+ from capycli .common .json_support import load_json_file
15
16
from capycli .bom .download_attachments import BomDownloadAttachments
16
17
from capycli .main .result_codes import ResultCode
17
- from cyclonedx .model import ExternalReferenceType , HashAlgorithm
18
18
from tests .test_base import AppArguments , TestBase
19
19
20
20
21
21
class TestBomDownloadAttachments (TestBase ):
22
22
INPUTFILE = "sbom_for_download.json"
23
+ CONTROLFILE = "sbom_for_download-control.json"
23
24
INPUTERROR = "plaintext.txt"
24
25
OUTPUTFILE = "output.json"
25
26
@@ -69,6 +70,8 @@ def test_file_not_found(self) -> None:
69
70
args .command .append ("bom" )
70
71
args .command .append ("downloadattachments" )
71
72
args .inputfile = "DOESNOTEXIST"
73
+ args .controlfile = os .path .join (os .path .dirname (__file__ ),
74
+ "fixtures" , TestBomDownloadAttachments .CONTROLFILE )
72
75
73
76
sut .run (args )
74
77
self .assertTrue (False , "Failed to report missing file" )
@@ -85,6 +88,8 @@ def test_error_loading_file(self) -> None:
85
88
args .command .append ("bom" )
86
89
args .command .append ("downloadattachments" )
87
90
args .inputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadAttachments .INPUTERROR )
91
+ args .controlfile = os .path .join (os .path .dirname (__file__ ),
92
+ "fixtures" , TestBomDownloadAttachments .CONTROLFILE )
88
93
89
94
sut .run (args )
90
95
self .assertTrue (False , "Failed to report invalid file" )
@@ -103,6 +108,8 @@ def test_source_folder_does_not_exist(self) -> None:
103
108
args .command .append ("downloadattachments" )
104
109
105
110
args .inputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadAttachments .INPUTFILE )
111
+ args .controlfile = os .path .join (os .path .dirname (__file__ ),
112
+ "fixtures" , TestBomDownloadAttachments .CONTROLFILE )
106
113
args .source = "XXX"
107
114
108
115
sut .run (args )
@@ -113,32 +120,10 @@ def test_source_folder_does_not_exist(self) -> None:
113
120
@responses .activate
114
121
def test_simple_bom (self ) -> None :
115
122
bom = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadAttachments .INPUTFILE )
116
- bom = CaPyCliBom . read_sbom ( bom )
123
+ controlfile = os . path . join ( os . path . dirname ( __file__ ), "fixtures" , TestBomDownloadAttachments . CONTROLFILE )
117
124
118
- # attachment info - CLI
119
- responses .add (
120
- method = responses .GET ,
121
- url = self .MYURL + "resource/api/attachments/794446" ,
122
- body = """
123
- {
124
- "filename": "CLIXML_certifi-2022.12.7.xml",
125
- "sha1": "3cd24769fa3da4af74d0118433619a130da091b0",
126
- "attachmentType": "COMPONENT_LICENSE_INFO_XML",
127
-
128
- "createdTeam": "AA",
129
- "createdComment": "comment1",
130
- "createdOn": "2020-10-08",
131
- "checkStatus": "NOTCHECKED",
132
- "_links": {
133
- "self": {
134
- "href": "https://my.server.com/resource/api/attachments/794446"
135
- }
136
- }
137
- }""" ,
138
- status = 200 ,
139
- content_type = "application/json" ,
140
- adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
141
- )
125
+ bom = CaPyCliBom .read_sbom (bom )
126
+ controlfile = load_json_file (controlfile )
142
127
143
128
# get attachment - CLI
144
129
cli_file = self .get_cli_file_mit ()
@@ -150,35 +135,6 @@ def test_simple_bom(self) -> None:
150
135
content_type = "application/text" ,
151
136
adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
152
137
)
153
-
154
- # attachment info - report
155
- responses .add (
156
- method = responses .GET ,
157
- url = self .MYURL + "resource/api/attachments/63b368" ,
158
- body = """
159
- {
160
- "filename": "certifi-2022.12.7_clearing_report.docx",
161
- "sha1": "3cd24769fa3da4af74d0118433619a130da091b0",
162
- "attachmentType": "CLEARING_REPORT",
163
-
164
- "createdTeam": "BB",
165
- "createdComment": "comment3",
166
- "createdOn": "2020-10-08",
167
-
168
- "checkedOn" : "2021-01-18",
169
- "checkedComment": "comment4",
170
- "checkStatus": "ACCEPTED",
171
- "_links": {
172
- "self": {
173
- "href": "https://my.server.com/resource/api/attachments/63b368"
174
- }
175
- }
176
- }""" ,
177
- status = 200 ,
178
- content_type = "application/json" ,
179
- adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
180
- )
181
-
182
138
# get attachment - report
183
139
responses .add (
184
140
method = responses .GET ,
@@ -191,7 +147,7 @@ def test_simple_bom(self) -> None:
191
147
192
148
with tempfile .TemporaryDirectory () as tmpdirname :
193
149
try :
194
- bom = self .app .download_attachments (bom , tmpdirname )
150
+ bom = self .app .download_attachments (bom , controlfile [ "Components" ], tmpdirname )
195
151
resultfile = os .path .join (tmpdirname , "CLIXML_certifi-2022.12.7.xml" )
196
152
self .assertEqual (bom .components [0 ].external_references [5 ].url , resultfile )
197
153
self .assertTrue (os .path .isfile (resultfile ), "CLI file missing" )
@@ -211,25 +167,8 @@ def test_simple_bom_relpath(self) -> None:
211
167
bom = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadAttachments .INPUTFILE )
212
168
bom = CaPyCliBom .read_sbom (bom )
213
169
214
- # attachment info - CLI
215
- responses .add (
216
- method = responses .GET ,
217
- url = self .MYURL + "resource/api/attachments/794446" ,
218
- body = """
219
- {
220
- "filename": "CLIXML_certifi-2022.12.7.xml",
221
- "sha1": "3cd24769fa3da4af74d0118433619a130da091b0",
222
- "attachmentType": "COMPONENT_LICENSE_INFO_XML",
223
- "_links": {
224
- "self": {
225
- "href": "https://my.server.com/resource/api/attachments/794446"
226
- }
227
- }
228
- }""" ,
229
- status = 200 ,
230
- content_type = "application/json" ,
231
- adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
232
- )
170
+ controlfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadAttachments .CONTROLFILE )
171
+ controlfile = load_json_file (controlfile )
233
172
234
173
# get attachment - CLI
235
174
cli_file = self .get_cli_file_mit ()
@@ -244,7 +183,8 @@ def test_simple_bom_relpath(self) -> None:
244
183
245
184
with tempfile .TemporaryDirectory () as tmpdirname :
246
185
try :
247
- bom = self .app .download_attachments (bom , tmpdirname , tmpdirname , ("COMPONENT_LICENSE_INFO_XML" ,))
186
+ bom = self .app .download_attachments (bom , controlfile ["Components" ],
187
+ tmpdirname , tmpdirname , ("COMPONENT_LICENSE_INFO_XML" ,))
248
188
resultfile = os .path .join (tmpdirname , "CLIXML_certifi-2022.12.7.xml" )
249
189
self .assertEqual (bom .components [0 ].external_references [5 ].url ,
250
190
"file://CLIXML_certifi-2022.12.7.xml" )
@@ -263,59 +203,29 @@ def test_simple_bom_download_errors(self) -> None:
263
203
bom = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadAttachments .INPUTFILE )
264
204
bom = CaPyCliBom .read_sbom (bom )
265
205
266
- # attachment info - CLI, ok
267
- responses .add (
268
- method = responses .GET ,
269
- url = self .MYURL + "resource/api/attachments/794446" ,
270
- body = """
271
- {
272
- "filename": "CLIXML_certifi-2022.12.7.xml",
273
- "sha1": "3cd24769fa3da4af74d0118433619a130da091b0",
274
- "attachmentType": "COMPONENT_LICENSE_INFO_XML",
275
- "_links": {
276
- "self": {
277
- "href": "https://my.server.com/resource/api/attachments/794446"
278
- }
279
- }
280
- }""" ,
281
- status = 200 ,
282
- content_type = "application/json" ,
283
- adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
284
- )
206
+ controlfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadAttachments .CONTROLFILE )
207
+ controlfile = load_json_file (controlfile )
285
208
286
209
# get attachment - CLI, error
287
210
responses .add (
288
211
method = responses .GET ,
289
212
url = self .MYURL + "resource/api/releases/ae8c7ed/attachments/794446" ,
290
- body = "cli_file" ,
291
213
status = 500 ,
292
214
content_type = "application/text" ,
293
215
adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
294
216
)
295
-
296
- # attachment info - report, error
217
+ # get attachment - CLI, error
297
218
responses .add (
298
219
method = responses .GET ,
299
- url = self .MYURL + "resource/api/attachments/63b368" ,
300
- body = """
301
- {
302
- "filename": "certifi-2022.12.7_clearing_report.docx",
303
- "sha1": "3cd24769fa3da4af74d0118433619a130da091b0",
304
- "attachmentType": "CLEARING_REPORT",
305
- "_links": {
306
- "self": {
307
- "href": "https://my.server.com/resource/api/attachments/63b368"
308
- }
309
- }
310
- }""" ,
311
- status = 404 ,
312
- content_type = "application/json" ,
220
+ url = self .MYURL + "resource/api/releases/ae8c7ed/attachments/63b368" ,
221
+ status = 403 ,
222
+ content_type = "application/text" ,
313
223
adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
314
224
)
315
225
316
226
with tempfile .TemporaryDirectory () as tmpdirname :
317
227
try :
318
- bom = self .app .download_attachments (bom , tmpdirname )
228
+ bom = self .app .download_attachments (bom , controlfile [ "Components" ], tmpdirname )
319
229
resultfile = os .path .join (tmpdirname , "CLIXML_certifi-2022.12.7.xml" )
320
230
self .assertFalse (os .path .isfile (resultfile ), "CLI created despite HTTP 500" )
321
231
@@ -335,8 +245,8 @@ def test_simple_bom_no_release_id(self) -> None:
335
245
bom .components [0 ].properties = []
336
246
with tempfile .TemporaryDirectory () as tmpdirname :
337
247
try :
338
- err = self .capture_stdout (self .app .download_attachments , bom , tmpdirname )
339
- assert "No sw360Id for release" in err
248
+ err = self .capture_stdout (self .app .download_attachments , bom , [], tmpdirname )
249
+ self . assertIn ( "No sw360Id for release" , err )
340
250
341
251
return
342
252
except Exception as e : # noqa
@@ -346,18 +256,14 @@ def test_simple_bom_no_release_id(self) -> None:
346
256
self .assertTrue (False , "Error: we must never arrive here" )
347
257
348
258
@responses .activate
349
- def test_simple_bom_no_attachment_id (self ) -> None :
259
+ def test_simple_bom_no_ctrl_file_entry (self ) -> None :
350
260
bom = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadAttachments .INPUTFILE )
351
261
bom = CaPyCliBom .read_sbom (bom )
352
- bom .components [0 ].external_references = []
353
- CycloneDxSupport .set_ext_ref (bom .components [0 ], ExternalReferenceType .OTHER ,
354
- CaPyCliBom .CLI_FILE_COMMENT , "CLIXML_foo.xml" ,
355
- HashAlgorithm .SHA_1 , "123" )
356
262
357
263
with tempfile .TemporaryDirectory () as tmpdirname :
358
264
try :
359
- err = self .capture_stdout (self .app .download_attachments , bom , tmpdirname )
360
- assert "No sw360Id for attachment" in err
265
+ err = self .capture_stdout (self .app .download_attachments , bom , [], tmpdirname )
266
+ assert "Found 0 entries for attachment CLIXML_certifi-2022.12.7.xml " in err
361
267
362
268
return
363
269
except Exception as e : # noqa
0 commit comments