1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2018-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
|
|
|
from gvm.errors import RequiredArgument, InvalidArgumentType |
20
|
|
|
|
21
|
|
|
from gvm.protocols.gmpv208 import AlertCondition, AlertEvent, AlertMethod |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class GmpModifyAlertTestMixin: |
25
|
|
|
def test_modify_alert(self): |
26
|
|
|
self.gmp.modify_alert(alert_id='a1') |
27
|
|
|
|
28
|
|
|
self.connection.send.has_been_called_with( |
29
|
|
|
'<modify_alert alert_id="a1"/>' |
30
|
|
|
) |
31
|
|
|
|
32
|
|
|
def test_modify_alert_without_alert_id(self): |
33
|
|
|
with self.assertRaises(RequiredArgument): |
34
|
|
|
self.gmp.modify_alert(alert_id=None) |
35
|
|
|
|
36
|
|
|
with self.assertRaises(RequiredArgument): |
37
|
|
|
self.gmp.modify_alert(alert_id='') |
38
|
|
|
|
39
|
|
|
with self.assertRaises(RequiredArgument): |
40
|
|
|
self.gmp.modify_alert('') |
41
|
|
|
|
42
|
|
|
def test_modify_alert_with_comment(self): |
43
|
|
|
self.gmp.modify_alert(alert_id='a1', comment='lorem') |
44
|
|
|
|
45
|
|
|
self.connection.send.has_been_called_with( |
46
|
|
|
'<modify_alert alert_id="a1">' |
47
|
|
|
'<comment>lorem</comment>' |
48
|
|
|
'</modify_alert>' |
49
|
|
|
) |
50
|
|
|
|
51
|
|
|
def test_modify_alert_with_name(self): |
52
|
|
|
self.gmp.modify_alert(alert_id='a1', name='lorem') |
53
|
|
|
|
54
|
|
|
self.connection.send.has_been_called_with( |
55
|
|
|
'<modify_alert alert_id="a1">' |
56
|
|
|
'<name>lorem</name>' |
57
|
|
|
'</modify_alert>' |
58
|
|
|
) |
59
|
|
|
|
60
|
|
|
def test_modify_alert_with_filter_id(self): |
61
|
|
|
self.gmp.modify_alert(alert_id='a1', filter_id='f1') |
62
|
|
|
|
63
|
|
|
self.connection.send.has_been_called_with( |
64
|
|
|
'<modify_alert alert_id="a1">' '<filter id="f1"/>' '</modify_alert>' |
65
|
|
|
) |
66
|
|
|
|
67
|
|
|
def test_modify_alert_invalid_condition(self): |
68
|
|
|
with self.assertRaises(InvalidArgumentType): |
69
|
|
|
self.gmp.modify_alert( |
70
|
|
|
alert_id='a1', |
71
|
|
|
condition='bar', |
72
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
73
|
|
|
method=AlertMethod.SCP, |
74
|
|
|
) |
75
|
|
|
|
76
|
|
|
def test_modify_alert_invalid_event(self): |
77
|
|
|
with self.assertRaises(InvalidArgumentType): |
78
|
|
|
self.gmp.modify_alert( |
79
|
|
|
alert_id='a1', |
80
|
|
|
condition=AlertCondition.ALWAYS, |
81
|
|
|
event='lorem', |
82
|
|
|
method=AlertMethod.SCP, |
83
|
|
|
) |
84
|
|
|
|
85
|
|
|
def test_modify_alert_invalid_method(self): |
86
|
|
|
with self.assertRaises(InvalidArgumentType): |
87
|
|
|
self.gmp.modify_alert( |
88
|
|
|
alert_id='a1', |
89
|
|
|
condition=AlertCondition.ALWAYS, |
90
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
91
|
|
|
method='ipsum', |
92
|
|
|
) |
93
|
|
|
|
94
|
|
|
def test_modify_alert_with_event_missing_method(self): |
95
|
|
|
with self.assertRaisesRegex(RequiredArgument, "method is required"): |
96
|
|
|
self.gmp.modify_alert( |
97
|
|
|
alert_id='a1', |
98
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
99
|
|
|
condition=AlertCondition.ALWAYS, |
100
|
|
|
) |
101
|
|
|
|
102
|
|
|
with self.assertRaisesRegex(RequiredArgument, "method is required"): |
103
|
|
|
self.gmp.modify_alert( |
104
|
|
|
alert_id='a1', |
105
|
|
|
event=AlertEvent.NEW_SECINFO_ARRIVED, |
106
|
|
|
condition=AlertCondition.ALWAYS, |
107
|
|
|
) |
108
|
|
|
|
109
|
|
|
with self.assertRaisesRegex(RequiredArgument, "method is required"): |
110
|
|
|
self.gmp.modify_alert( |
111
|
|
|
alert_id='a1', |
112
|
|
|
event=AlertEvent.UPDATED_SECINFO_ARRIVED, |
113
|
|
|
condition=AlertCondition.ALWAYS, |
114
|
|
|
) |
115
|
|
|
|
116
|
|
|
def test_modify_alert_with_event_missing_condition(self): |
117
|
|
|
with self.assertRaisesRegex(RequiredArgument, "condition is required"): |
118
|
|
|
self.gmp.modify_alert( |
119
|
|
|
alert_id='a1', |
120
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
121
|
|
|
method=AlertMethod.SCP, |
122
|
|
|
) |
123
|
|
|
|
124
|
|
|
with self.assertRaisesRegex(RequiredArgument, "condition is required"): |
125
|
|
|
self.gmp.modify_alert( |
126
|
|
|
alert_id='a1', |
127
|
|
|
event=AlertEvent.NEW_SECINFO_ARRIVED, |
128
|
|
|
method=AlertMethod.SCP, |
129
|
|
|
) |
130
|
|
|
|
131
|
|
|
with self.assertRaisesRegex(RequiredArgument, "condition is required"): |
132
|
|
|
self.gmp.modify_alert( |
133
|
|
|
alert_id='a1', |
134
|
|
|
event=AlertEvent.UPDATED_SECINFO_ARRIVED, |
135
|
|
|
method=AlertMethod.SCP, |
136
|
|
|
) |
137
|
|
|
|
138
|
|
|
def test_modify_alert_invalid_condition_for_secinfo(self): |
139
|
|
|
with self.assertRaises(InvalidArgumentType): |
140
|
|
|
self.gmp.modify_alert( |
141
|
|
|
alert_id='a1', |
142
|
|
|
condition='Severity at least', |
143
|
|
|
event='Updated SecInfo arrived', |
144
|
|
|
method='Email', |
145
|
|
|
) |
146
|
|
|
|
147
|
|
|
def test_modify_alert_invalid_method_for_secinfo(self): |
148
|
|
|
with self.assertRaises(InvalidArgumentType): |
149
|
|
|
self.gmp.modify_alert( |
150
|
|
|
alert_id='a1', |
151
|
|
|
condition='Always', |
152
|
|
|
event='Updated SecInfo arrived', |
153
|
|
|
method='HTTP Get', |
154
|
|
|
) |
155
|
|
|
|
156
|
|
|
def test_modify_alert_with_event_data(self): |
157
|
|
|
self.gmp.modify_alert( |
158
|
|
|
alert_id='a1', |
159
|
|
|
condition=AlertCondition.ALWAYS, |
160
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
161
|
|
|
method=AlertMethod.EMAIL, |
162
|
|
|
event_data={'foo': 'bar'}, |
163
|
|
|
) |
164
|
|
|
|
165
|
|
|
self.connection.send.has_been_called_with( |
166
|
|
|
'<modify_alert alert_id="a1">' |
167
|
|
|
'<condition>Always</condition>' |
168
|
|
|
'<method>Email</method>' |
169
|
|
|
'<event>Task run status changed' |
170
|
|
|
'<data>bar<name>foo</name></data>' |
171
|
|
|
'</event>' |
172
|
|
|
'</modify_alert>' |
173
|
|
|
) |
174
|
|
|
|
175
|
|
|
def test_modify_alert_with_condition_data(self): |
176
|
|
|
self.gmp.modify_alert( |
177
|
|
|
alert_id='a1', |
178
|
|
|
condition=AlertCondition.ALWAYS, |
179
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
180
|
|
|
method=AlertMethod.EMAIL, |
181
|
|
|
condition_data={'foo': 'bar'}, |
182
|
|
|
) |
183
|
|
|
|
184
|
|
|
self.connection.send.has_been_called_with( |
185
|
|
|
'<modify_alert alert_id="a1">' |
186
|
|
|
'<condition>Always<data>bar<name>foo</name></data></condition>' |
187
|
|
|
'<method>Email</method>' |
188
|
|
|
'<event>Task run status changed</event>' |
189
|
|
|
'</modify_alert>' |
190
|
|
|
) |
191
|
|
|
|
192
|
|
|
def test_modify_alert_with_method_data(self): |
193
|
|
|
self.gmp.modify_alert( |
194
|
|
|
alert_id='a1', |
195
|
|
|
condition=AlertCondition.ALWAYS, |
196
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
197
|
|
|
method=AlertMethod.EMAIL, |
198
|
|
|
method_data={'foo': 'bar'}, |
199
|
|
|
) |
200
|
|
|
|
201
|
|
|
self.connection.send.has_been_called_with( |
202
|
|
|
'<modify_alert alert_id="a1">' |
203
|
|
|
'<condition>Always</condition>' |
204
|
|
|
'<method>Email<data>bar<name>foo</name></data></method>' |
205
|
|
|
'<event>Task run status changed</event>' |
206
|
|
|
'</modify_alert>' |
207
|
|
|
) |
208
|
|
|
|
209
|
|
|
def test_modify_missing_method_for_ticket_received(self): |
210
|
|
|
with self.assertRaises(RequiredArgument): |
211
|
|
|
self.gmp.modify_alert( |
212
|
|
|
alert_id='a1', |
213
|
|
|
condition=AlertCondition.ALWAYS, |
214
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
215
|
|
|
method=None, |
216
|
|
|
) |
217
|
|
|
|
218
|
|
|
def test_modify_missing_condition_for_ticket_received(self): |
219
|
|
|
with self.assertRaises(RequiredArgument): |
220
|
|
|
self.gmp.modify_alert( |
221
|
|
|
alert_id='a1', |
222
|
|
|
condition=None, |
223
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
224
|
|
|
method=AlertMethod.EMAIL, |
225
|
|
|
) |
226
|
|
|
|