Code Duplication    Length = 108-108 lines in 2 locations

tests/scripts/test_create_consolidated_report.py 2 locations

@@ 277-384 (lines=108) @@
274
275
        self.assertEqual(i, 2)
276
277
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
278
    def test_combine_reports_with_id(self, mock_gmp: GmpMockFactory):
279
        reports = [
280
            '00000000-0000-0000-0000-000000000000',
281
            '00000000-0000-0000-0000-000000000001',
282
            '00000000-0000-0000-0000-000000000002',
283
        ]
284
285
        mock_gmp.mock_responses(
286
            'get_report',
287
            [
288
                '<get_reports_response status="200" status_text="OK">'
289
                '<report id="00000000-0000-0000-0000-000000000000">'
290
                '<name>2020-11-13T14:47:28Z</name>'
291
                '<creation_time>2020-11-13T14:47:28Z</creation_time>'
292
                '<modification_time>2020-11-13T14:47:28Z</modification_time>'
293
                '<task id="00000000-0000-0000-0001-000000000000">'
294
                '<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>'
295
                '</task>'
296
                '<report id="00000000-0000-0000-0000-000000000000">'
297
                '<scan_run_status>Done</scan_run_status>'
298
                '<timestamp>2020-11-13T14:47:48Z</timestamp>'
299
                '<scan_start>2020-11-13T14:47:28Z</scan_start>'
300
                '<ports max="-1" start="1">'
301
                '<port>0<host>127.0.0.0</host></port>'
302
                '<port>1<host>127.0.0.1</host></port></ports>'
303
                '<results start="1" max="100">'
304
                '<result id="00000001-0000-0000-0000-000000000000">'
305
                '</result>'
306
                '<result id="00000001-0000-0000-0000-000000000001">'
307
                '</result></results>'
308
                '<scan_end>2020-11-13T14:47:28Z</scan_end>'
309
                '</report></report></get_reports_response>',
310
                '<get_reports_response status="200" status_text="OK">'
311
                '<report id="00000000-0000-0000-0000-000000000001">'
312
                '<name>2020-11-13T14:47:28Z</name>'
313
                '<creation_time>2020-11-13T14:47:28Z</creation_time>'
314
                '<modification_time>2020-11-13T14:47:28Z</modification_time>'
315
                '<task id="00000000-0000-0000-0002-000000000000">'
316
                '<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>'
317
                '</task>'
318
                '<report id="00000000-0000-0000-0000-000000000001">'
319
                '<scan_run_status>Done</scan_run_status>'
320
                '<timestamp>2020-11-13T14:47:48Z</timestamp>'
321
                '<scan_start>2020-11-13T14:47:28Z</scan_start>'
322
                '<ports max="-1" start="1">'
323
                '<port>2<host>127.0.0.2</host></port>'
324
                '<port>3<host>127.0.0.3</host></port></ports>'
325
                '<results start="1" max="100">'
326
                '<result id="00000001-0000-0000-0000-000000000002"></result>'
327
                '<result id="00000001-0000-0000-0000-000000000003">'
328
                '</result></results>'
329
                '<scan_end>2020-11-13T14:47:28Z</scan_end>'
330
                '<host><ip>127.0.0.0</ip></host></report>'
331
                '</report></get_reports_response>',
332
                '<get_reports_response status="200" status_text="OK">'
333
                '<report id="00000000-0000-0000-0000-000000000002">'
334
                '<name>2020-11-13T14:47:28Z</name>'
335
                '<creation_time>2020-11-13T14:47:28Z</creation_time>'
336
                '<modification_time>2020-11-13T14:47:28Z</modification_time>'
337
                '<task id="00000000-0000-0000-0003-000000000000">'
338
                '<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>'
339
                '</task>'
340
                '<report id="00000000-0000-0000-0000-000000000002">'
341
                '<scan_run_status>Done</scan_run_status>'
342
                '<timestamp>2020-11-13T14:47:48Z</timestamp>'
343
                '<scan_start>2020-11-13T14:47:28Z</scan_start>'
344
                '<results start="1" max="100">'
345
                '<result id="00000001-0000-0000-0000-000000000004">'
346
                '</result></results>'
347
                '<scan_end>2020-11-13T14:47:28Z</scan_end>'
348
                '<host><ip>127.0.0.1</ip></host></report>'
349
                '</report></get_reports_response>',
350
            ],
351
        )
352
353
        combined_report = self.create_consolidated_report.combine_reports(
354
            mock_gmp.gmp_protocol, reports, filter_term="", filter_id='123'
355
        )
356
357
        ports = combined_report.find('report').find('ports').findall('port')
358
        i = 0
359
        for port in ports:
360
            self.assertEqual(port.text, f'{str(i)}')
361
            i += 1
362
363
        self.assertEqual(i, 4)
364
365
        results = (
366
            combined_report.find('report').find('results').findall('result')
367
        )
368
        i = 0
369
        for result in results:
370
            self.assertEqual(
371
                result.get('id'), f'00000001-0000-0000-0000-00000000000{str(i)}'
372
            )
373
            i += 1
374
375
        self.assertEqual(i, 5)
376
377
        hosts = combined_report.find('report').findall('host')
378
379
        i = 0
380
        for host in hosts:
381
            self.assertEqual(host.find('ip').text, f'127.0.0.{str(i)}')
382
            i += 1
383
384
        self.assertEqual(i, 2)
385
386
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
387
    def test_send_report(self, mock_gmp: GmpMockFactory):
@@ 168-275 (lines=108) @@
165
        ]
166
        self.assertEqual(reports, asserted_reports)
167
168
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
169
    def test_combine_reports_with_term(self, mock_gmp: GmpMockFactory):
170
        reports = [
171
            '00000000-0000-0000-0000-000000000000',
172
            '00000000-0000-0000-0000-000000000001',
173
            '00000000-0000-0000-0000-000000000002',
174
        ]
175
176
        mock_gmp.mock_responses(
177
            'get_report',
178
            [
179
                '<get_reports_response status="200" status_text="OK">'
180
                '<report id="00000000-0000-0000-0000-000000000000">'
181
                '<name>2020-11-13T14:47:28Z</name>'
182
                '<creation_time>2020-11-13T14:47:28Z</creation_time>'
183
                '<modification_time>2020-11-13T14:47:28Z</modification_time>'
184
                '<task id="00000000-0000-0000-0001-000000000000">'
185
                '<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>'
186
                '</task>'
187
                '<report id="00000000-0000-0000-0000-000000000000">'
188
                '<scan_run_status>Done</scan_run_status>'
189
                '<timestamp>2020-11-13T14:47:48Z</timestamp>'
190
                '<scan_start>2020-11-13T14:47:28Z</scan_start>'
191
                '<ports max="-1" start="1">'
192
                '<port>0<host>127.0.0.0</host></port>'
193
                '<port>1<host>127.0.0.1</host></port></ports>'
194
                '<results start="1" max="100">'
195
                '<result id="00000001-0000-0000-0000-000000000000">'
196
                '</result>'
197
                '<result id="00000001-0000-0000-0000-000000000001">'
198
                '</result></results>'
199
                '<scan_end>2020-11-13T14:47:28Z</scan_end>'
200
                '</report></report></get_reports_response>',
201
                '<get_reports_response status="200" status_text="OK">'
202
                '<report id="00000000-0000-0000-0000-000000000001">'
203
                '<name>2020-11-13T14:47:28Z</name>'
204
                '<creation_time>2020-11-13T14:47:28Z</creation_time>'
205
                '<modification_time>2020-11-13T14:47:28Z</modification_time>'
206
                '<task id="00000000-0000-0000-0002-000000000000">'
207
                '<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>'
208
                '</task>'
209
                '<report id="00000000-0000-0000-0000-000000000001">'
210
                '<scan_run_status>Done</scan_run_status>'
211
                '<timestamp>2020-11-13T14:47:48Z</timestamp>'
212
                '<scan_start>2020-11-13T14:47:28Z</scan_start>'
213
                '<ports max="-1" start="1">'
214
                '<port>2<host>127.0.0.2</host></port>'
215
                '<port>3<host>127.0.0.3</host></port></ports>'
216
                '<results start="1" max="100">'
217
                '<result id="00000001-0000-0000-0000-000000000002"></result>'
218
                '<result id="00000001-0000-0000-0000-000000000003">'
219
                '</result></results>'
220
                '<scan_end>2020-11-13T14:47:28Z</scan_end>'
221
                '<host><ip>127.0.0.0</ip></host></report>'
222
                '</report></get_reports_response>',
223
                '<get_reports_response status="200" status_text="OK">'
224
                '<report id="00000000-0000-0000-0000-000000000002">'
225
                '<name>2020-11-13T14:47:28Z</name>'
226
                '<creation_time>2020-11-13T14:47:28Z</creation_time>'
227
                '<modification_time>2020-11-13T14:47:28Z</modification_time>'
228
                '<task id="00000000-0000-0000-0003-000000000000">'
229
                '<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>'
230
                '</task>'
231
                '<report id="00000000-0000-0000-0000-000000000002">'
232
                '<scan_run_status>Done</scan_run_status>'
233
                '<timestamp>2020-11-13T14:47:48Z</timestamp>'
234
                '<scan_start>2020-11-13T14:47:28Z</scan_start>'
235
                '<results start="1" max="100">'
236
                '<result id="00000001-0000-0000-0000-000000000004">'
237
                '</result></results>'
238
                '<scan_end>2020-11-13T14:47:28Z</scan_end>'
239
                '<host><ip>127.0.0.1</ip></host></report>'
240
                '</report></get_reports_response>',
241
            ],
242
        )
243
244
        combined_report = self.create_consolidated_report.combine_reports(
245
            mock_gmp.gmp_protocol, reports, filter_term="foo", filter_id=None
246
        )
247
248
        ports = combined_report.find('report').find('ports').findall('port')
249
        i = 0
250
        for port in ports:
251
            self.assertEqual(port.text, f'{str(i)}')
252
            i += 1
253
254
        self.assertEqual(i, 4)
255
256
        results = (
257
            combined_report.find('report').find('results').findall('result')
258
        )
259
        i = 0
260
        for result in results:
261
            self.assertEqual(
262
                result.get('id'), f'00000001-0000-0000-0000-00000000000{str(i)}'
263
            )
264
            i += 1
265
266
        self.assertEqual(i, 5)
267
268
        hosts = combined_report.find('report').findall('host')
269
270
        i = 0
271
        for host in hosts:
272
            self.assertEqual(host.find('ip').text, f'127.0.0.{str(i)}')
273
            i += 1
274
275
        self.assertEqual(i, 2)
276
277
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
278
    def test_combine_reports_with_id(self, mock_gmp: GmpMockFactory):