tests.scripts.test_start_alert_scan   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 95
dl 0
loc 212
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A StartAlertScanTestCase.setUp() 0 3 1
A StartAlertScanTestCase.test_get_config() 0 39 2
A StartAlertScanTestCase.test_get_alert() 0 24 1
A StartAlertScanTestCase.test_get_target() 0 31 1
A StartAlertScanTestCase.test_create_and_start_task() 0 80 1
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2020-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 unittest.mock import patch
22
from pathlib import Path
23
from . import GmpMockFactory, load_script
24
25
CWD = Path(__file__).absolute().parent
26
27
28
class StartAlertScanTestCase(unittest.TestCase):
29
    def setUp(self):
30
        self.start_alert_scan = load_script(
31
            (CWD.parent.parent / 'scripts'), 'start-alert-scan'
32
        )
33
34
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
35
    def test_get_config(self, mock_gmp: GmpMockFactory):
36
        configs_file = CWD / 'get_configs.xml'
37
        configs = configs_file.read_text()
38
        mock_gmp.mock_response('get_configs', configs)
39
40
        # Full and Fast
41
        config_id = self.start_alert_scan.get_config(
42
            gmp=mock_gmp.gmp_protocol, config=0
43
        )
44
        self.assertEqual(config_id, 'daba56c8-73ec-11df-a475-002264764cea')
45
46
        # Full and Fast ultimate
47
        config_id = self.start_alert_scan.get_config(
48
            gmp=mock_gmp.gmp_protocol, config=1
49
        )
50
        self.assertEqual(config_id, '698f691e-7489-11df-9d8c-002264764cea')
51
52
        # Full and Fast deep
53
        config_id = self.start_alert_scan.get_config(
54
            gmp=mock_gmp.gmp_protocol, config=2
55
        )
56
        self.assertEqual(config_id, '708f25c4-7489-11df-8094-002264764cea')
57
58
        # Full and Fast deep ultimate
59
        config_id = self.start_alert_scan.get_config(
60
            gmp=mock_gmp.gmp_protocol, config=3
61
        )
62
        self.assertEqual(config_id, '74db13d6-7489-11df-91b9-002264764cea')
63
64
        # System Discovery
65
        config_id = self.start_alert_scan.get_config(
66
            gmp=mock_gmp.gmp_protocol, config=4
67
        )
68
        self.assertEqual(config_id, 'bbca7412-a950-11e3-9109-406186ea4fc5')
69
70
        with self.assertRaises(ValueError):
71
            config_id = self.start_alert_scan.get_config(
72
                gmp=mock_gmp.gmp_protocol, config=-1
73
            )
74
75
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
76
    def test_get_alert(self, mock_gmp: GmpMockFactory):
77
        sender_email = "[email protected]"
78
        recipient_email = "[email protected]"
79
        alert_name = "test_alert"
80
        alert_id = '3eefd4b9-59ec-48d6-b84d-f6a73bdb909f'
81
82
        alerts_file = CWD / 'get_alerts.xml'
83
        alerts = alerts_file.read_text()
84
        mock_gmp.mock_response('get_alerts', alerts)
85
        mock_gmp.mock_response(
86
            'create_alert',
87
            '<create_alert_response status="201" status_text='
88
            f'"OK, resource created" id="{alert_id}"/>',
89
        )
90
91
        returned_id = self.start_alert_scan.get_alert(
92
            gmp=mock_gmp.gmp_protocol,
93
            alert_name=alert_name,
94
            recipient_email=recipient_email,
95
            sender_email=sender_email,
96
        )
97
98
        self.assertEqual(alert_id, returned_id)
99
100
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
101
    def test_get_target(self, mock_gmp: GmpMockFactory):
102
        target_name = "test_target"
103
        hosts = ['127.0.0.1', '8.8.8.8']
104
        ports = 'T:1-3,5,7,9,11,13,17-25'
105
        port_list_name = "test_port_list"
106
107
        port_list_id = '6742e61a-a7b0-45dd-a8e1-35751c970958'
108
        target_id = '3b76a0c2-14fc-4de2-868c-35132977a25e'
109
110
        mock_gmp.mock_response(
111
            'create_port_list',
112
            '<create_port_list_response status="201" status_text='
113
            f'"OK, resource created" id="{port_list_id}"/>',
114
        )
115
116
        mock_gmp.mock_response(
117
            'create_target',
118
            '<create_target_response status="201" status_text='
119
            f'"OK, resource created" id="{target_id}"/>',
120
        )
121
122
        returned_id = self.start_alert_scan.get_target(
123
            gmp=mock_gmp.gmp_protocol,
124
            target_name=target_name,
125
            hosts=hosts,
126
            ports=ports,
127
            port_list_name=port_list_name,
128
        )
129
130
        self.assertEqual(target_id, returned_id)
131
132
    @patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory)
133
    def test_create_and_start_task(self, mock_gmp: GmpMockFactory):
134
        alert_name = 'test_alert'
135
        alert_id = '3eefd4b9-59ec-48d6-b84d-f6a73bdb909f'
136
        target_id = '3b76a0c2-14fc-4de2-868c-35132977a25e'
137
        config_id = 'daba56c8-73ec-11df-a475-002264764cea'
138
        scanner_id = '08b69003-5fc2-4037-a479-93b440211c73'
139
140
        task_id = 'd78453ab-d907-44b6-abe0-2ef54a77f1c2'
141
142
        mock_gmp.mock_response(
143
            'create_task',
144
            '<create_task_response status="201" status_text='
145
            f'"OK, resource created" id="{task_id}"/>',
146
        )
147
148
        mock_gmp.mock_response(
149
            'get_tasks',
150
            """
151
<get_tasks_response status="200" status_text="OK">
152
  <apply_overrides>0</apply_overrides>
153
  <filters id="">
154
    <term>
155
        apply_overrides=0 min_qod=70
156
        name="Alert Scan for Alert test_alert" first=1 rows=100 sort=name
157
    </term>
158
    <keywords>
159
      <keyword>
160
        <column>apply_overrides</column>
161
        <relation>=</relation>
162
        <value>0</value>
163
      </keyword>
164
      <keyword>
165
        <column>min_qod</column>
166
        <relation>=</relation>
167
        <value>70</value>
168
      </keyword>
169
      <keyword>
170
        <column>name</column>
171
        <relation>=</relation>
172
        <value>"Alert Scan for Alert test_alert"</value>
173
      </keyword>
174
      <keyword>
175
        <column>first</column>
176
        <relation>=</relation>
177
        <value>1</value>
178
      </keyword>
179
      <keyword>
180
        <column>rows</column>
181
        <relation>=</relation>
182
        <value>100</value>
183
      </keyword>
184
      <keyword>
185
        <column>sort</column>
186
        <relation>=</relation>
187
        <value>name</value>
188
      </keyword>
189
    </keywords>
190
  </filters>
191
  <sort>
192
    <field>name<order>ascending</order></field>
193
  </sort>
194
  <tasks start="1" max="100"/>
195
  <task_count>27<filtered>0</filtered><page>0</page></task_count>
196
</get_tasks_response>
197
            """,
198
        )
199
200
        task_name = "Alert Scan for Alert {}".format(alert_name)
201
202
        returned_name = self.start_alert_scan.create_and_start_task(
203
            gmp=mock_gmp.gmp_protocol,
204
            config_id=config_id,
205
            target_id=target_id,
206
            scanner_id=scanner_id,
207
            alert_id=alert_id,
208
            alert_name=alert_name,
209
        )
210
211
        self.assertEqual(task_name, returned_name)
212