tests.scripts.test_create_consolidated_report   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 428
Duplicated Lines 50.47 %

Importance

Changes 0
Metric Value
eloc 163
dl 216
loc 428
rs 10
c 0
b 0
f 0
wmc 22

8 Methods

Rating   Name   Duplication   Size   Complexity  
C CreateConsolidatedReportsTestCase.test_parse_period() 0 46 9
A CreateConsolidatedReportsTestCase.setUp() 0 3 1
A CreateConsolidatedReportsTestCase.test_generate_task_filter() 0 26 1
A CreateConsolidatedReportsTestCase.test_parse_tags() 0 8 1
A CreateConsolidatedReportsTestCase.test_send_report() 0 42 1
A CreateConsolidatedReportsTestCase.test_combine_reports_with_term() 108 108 4
A CreateConsolidatedReportsTestCase.test_combine_reports_with_id() 108 108 4
A CreateConsolidatedReportsTestCase.test_get_last_reports_from_tasks() 0 46 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2021 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
import unittest
21
from datetime import date
22
23
from unittest.mock import patch
24
from pathlib import Path
25
from lxml import etree
26
from . import GmpMockFactory, load_script
27
28
CWD = Path(__file__).absolute().parent
29
30
31
class CreateConsolidatedReportsTestCase(unittest.TestCase):
32
    def setUp(self):
33
        self.create_consolidated_report = load_script(
34
            (CWD.parent.parent / 'scripts'), 'create-consolidated-report'
35
        )
36
37
    def test_parse_period(self):
38
        with self.assertRaises(SystemExit), self.assertRaises(ValueError):
39
            self.create_consolidated_report.parse_period(
40
                ["a/b/c", "2020/02/03"]
41
            )
42
43
        with self.assertRaises(SystemExit), self.assertRaises(ValueError):
44
            self.create_consolidated_report.parse_period(
45
                ["1/2/3/4", "2020/02/03"]
46
            )
47
48
        with self.assertRaises(SystemExit), self.assertRaises(ValueError):
49
            self.create_consolidated_report.parse_period(
50
                ["2020/02/03", "a/b/c"]
51
            )
52
53
        with self.assertRaises(SystemExit), self.assertRaises(ValueError):
54
            self.create_consolidated_report.parse_period(
55
                ["2020/02/03", "1/2/3/4"]
56
            )
57
58
        with self.assertRaises(SystemExit), self.assertRaises(ValueError):
59
            self.create_consolidated_report.parse_period(
60
                ["2020/20/03", "2001/2/3"]
61
            )
62
63
        with self.assertRaises(SystemExit), self.assertRaises(ValueError):
64
            self.create_consolidated_report.parse_period(
65
                ["2020/12/03", "2001/2/300"]
66
            )
67
68
        with self.assertRaises(SystemExit), self.assertRaises(ValueError):
69
            self.create_consolidated_report.parse_period(
70
                ["2020/12/03", "2001/22/30"]
71
            )
72
73
        with self.assertRaises(SystemExit), self.assertRaises(ValueError):
74
            self.create_consolidated_report.parse_period(
75
                ["2020/12/43", "2001/2/3"]
76
            )
77
78
        date1, date2 = self.create_consolidated_report.parse_period(
79
            ["1980/1/11", "2001/2/3"]
80
        )
81
        self.assertEqual(date1, date(1980, 1, 11))
82
        self.assertEqual(date2, date(2001, 2, 3))
83
84
    def test_parse_tags(self):
85
        tags = ['abc', 'dba8624e-a56c-4901-a3f2-591f062e4c20']
86
87
        filter_tags = self.create_consolidated_report.parse_tags(tags)
88
89
        self.assertEqual(filter_tags[0], 'tag="abc"')
90
        self.assertEqual(
91
            filter_tags[1], 'tag_id="dba8624e-a56c-4901-a3f2-591f062e4c20"'
92
        )
93
94
    def test_generate_task_filter(self):
95
        asserted_task_filter = (
96
            'rows=-1 last>2020-01-01 and last<2020-02-01 and tag="blah"'
97
        )
98
        period_start = date(2020, 1, 1)
99
        period_end = date(2020, 2, 1)
100
        tags = ['tag="blah"']
101
102
        task_filter = self.create_consolidated_report.generate_task_filter(
103
            period_start, period_end, tags
104
        )
105
106
        self.assertEqual(task_filter, asserted_task_filter)
107
108
        asserted_task_filter = (
109
            'rows=-1 last>2020-01-01 and last<2020-02-01 and '
110
            'tag="blah" or last>2020-01-01 and last<2020-02-01'
111
            ' and tag="blah2"'
112
        )
113
        tags = ['tag="blah"', 'tag="blah2"']
114
115
        task_filter = self.create_consolidated_report.generate_task_filter(
116
            period_start, period_end, tags
117
        )
118
119
        self.assertEqual(task_filter, asserted_task_filter)
120
121
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
122
    def test_get_last_reports_from_tasks(self, mock_gmp: GmpMockFactory):
123
        mock_gmp.mock_response(
124
            'get_tasks',
125
            '<get_tasks_response status="200" status_text="OK">'
126
            '  <apply_overrides>0</apply_overrides>'
127
            '  <task id="ef4469db-1cd7-4859-ba5f-45f72f49f09e">'
128
            '    <last_report>'
129
            '      <report id="4108fe11-91c8-4b7b-90da-854e3200af19">'
130
            '      </report>'
131
            '    </last_report>'
132
            '  </task>'
133
            '  <task id="bdd80dd6-9c7b-47b9-a87b-e2ca28fae2df">'
134
            '    <last_report>'
135
            '      <report id="55af942a-fa45-472c-aa50-f2af77d700a0">'
136
            '      </report>'
137
            '    </last_report>'
138
            '  </task>'
139
            '  <task id="6e5373cd-e69a-4008-afc3-a6d05e22507f">'
140
            '    <last_report>'
141
            '      <report id="52b67045-2c2c-4dbd-af58-ecf814d92f07">'
142
            '      </report>'
143
            '    </last_report>'
144
            '  </task>'
145
            '  <task id="bab515c2-156b-451f-9f1c-7af5b2c4b568">'
146
            '    <last_report>'
147
            '      <report id="52b67045-2c2c-4dbd-af58-ecf814d92f07">'
148
            '      </report>'
149
            '    </last_report>'
150
            '  </task>'
151
            '</get_tasks_response>',
152
        )
153
154
        reports = self.create_consolidated_report.get_last_reports_from_tasks(
155
            mock_gmp.gmp_protocol,
156
            task_filter=(
157
                'rows=-1 last>2020-01-01 and ' 'last<2020-02-01 and tag="blah"'
158
            ),
159
        )
160
161
        asserted_reports = [
162
            '4108fe11-91c8-4b7b-90da-854e3200af19',
163
            '55af942a-fa45-472c-aa50-f2af77d700a0',
164
            '52b67045-2c2c-4dbd-af58-ecf814d92f07',
165
        ]
166
        self.assertEqual(reports, asserted_reports)
167
168 View Code Duplication
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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 View Code Duplication
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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):
388
389
        combined_report = etree.fromstring(
390
            '<report id="20574712-c404-4a04-9c83-03144ae02dca" '
391
            'format_id="d5da9f67-8551-4e51-807b-b6a873d70e34" '
392
            'extension="xml" content_type="text/xml">'
393
            '<report id="20574712-c404-4a04-9c83-03144ae02dca">'
394
            '<results start="1" max="-1">'
395
            '<result id="00000001-0000-0000-0000-000000000000"/>'
396
            '<result id="00000001-0000-0000-0000-000000000001"/>'
397
            '<result id="00000001-0000-0000-0000-000000000002"/>'
398
            '<result id="00000001-0000-0000-0000-000000000003"/>'
399
            '<result id="00000001-0000-0000-0000-000000000004"/>'
400
            '</results></report></report>'
401
        )
402
403
        report_id = '0e4d8fb2-47fa-494e-a242-d5327d3772f9'
404
405
        mock_gmp.mock_response(
406
            'import_report',
407
            '<create_report_response status="201" status_text="OK, '
408
            f'resource created" id="{report_id}"/>',
409
        )
410
411
        mock_gmp.mock_response(
412
            'create_container_task',
413
            '<create_task_response status="201" status_text="OK, '
414
            'resource created" id="6488ef71-e2d5-491f-95bd-ed9f915fa179"/>',
415
        )
416
417
        period_start = date(2020, 1, 1)
418
        period_end = date(2020, 2, 1)
419
420
        created_report_id = self.create_consolidated_report.send_report(
421
            gmp=mock_gmp.gmp_protocol,
422
            combined_report=combined_report,
423
            period_start=period_start,
424
            period_end=period_end,
425
        )
426
427
        self.assertEqual(report_id, created_report_id)
428