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, InvalidArgument, InvalidArgumentType |
20
|
|
|
|
21
|
|
|
from gvm.protocols.gmpv208 import AlertCondition, AlertEvent, AlertMethod |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class GmpCreateAlertTestMixin: |
25
|
|
|
def test_missing_name(self): |
26
|
|
|
with self.assertRaises(RequiredArgument): |
27
|
|
|
self.gmp.create_alert( |
28
|
|
|
name='', |
29
|
|
|
condition=AlertCondition.ALWAYS, |
30
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
31
|
|
|
method=AlertMethod.EMAIL, |
32
|
|
|
) |
33
|
|
|
|
34
|
|
|
with self.assertRaises(RequiredArgument): |
35
|
|
|
self.gmp.create_alert( |
36
|
|
|
name=None, |
37
|
|
|
condition=AlertCondition.ALWAYS, |
38
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
39
|
|
|
method=AlertMethod.EMAIL, |
40
|
|
|
) |
41
|
|
|
|
42
|
|
|
def test_missing_condition(self): |
43
|
|
|
with self.assertRaises(RequiredArgument): |
44
|
|
|
self.gmp.create_alert( |
45
|
|
|
name='foo', condition='', event='bar', method='lorem' |
46
|
|
|
) |
47
|
|
|
|
48
|
|
|
with self.assertRaises(RequiredArgument): |
49
|
|
|
self.gmp.create_alert( |
50
|
|
|
name='foo', condition=None, event='bar', method='lorem' |
51
|
|
|
) |
52
|
|
|
|
53
|
|
|
def test_missing_event(self): |
54
|
|
|
with self.assertRaises(RequiredArgument): |
55
|
|
|
self.gmp.create_alert( |
56
|
|
|
name='foo', condition='bar', event='', method='lorem' |
57
|
|
|
) |
58
|
|
|
|
59
|
|
|
with self.assertRaises(RequiredArgument): |
60
|
|
|
self.gmp.create_alert( |
61
|
|
|
name='foo', condition='bar', event=None, method='lorem' |
62
|
|
|
) |
63
|
|
|
|
64
|
|
|
def test_missing_method(self): |
65
|
|
|
with self.assertRaises(RequiredArgument): |
66
|
|
|
self.gmp.create_alert( |
67
|
|
|
name='foo', condition='bar', event='lorem', method='' |
68
|
|
|
) |
69
|
|
|
|
70
|
|
|
with self.assertRaises(RequiredArgument): |
71
|
|
|
self.gmp.create_alert( |
72
|
|
|
name='foo', condition='bar', event='lorem', method=None |
73
|
|
|
) |
74
|
|
|
|
75
|
|
|
def test_invalid_condition(self): |
76
|
|
|
with self.assertRaises(InvalidArgumentType): |
77
|
|
|
self.gmp.create_alert( |
78
|
|
|
name='foo', |
79
|
|
|
condition='bar', |
80
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
81
|
|
|
method=AlertMethod.EMAIL, |
82
|
|
|
) |
83
|
|
|
|
84
|
|
|
def test_invalid_event(self): |
85
|
|
|
with self.assertRaises(InvalidArgumentType): |
86
|
|
|
self.gmp.create_alert( |
87
|
|
|
name='foo', |
88
|
|
|
condition=AlertCondition.ALWAYS, |
89
|
|
|
event='lorem', |
90
|
|
|
method=AlertMethod.EMAIL, |
91
|
|
|
) |
92
|
|
|
|
93
|
|
|
def test_invalid_method(self): |
94
|
|
|
with self.assertRaises(InvalidArgumentType): |
95
|
|
|
self.gmp.create_alert( |
96
|
|
|
name='foo', |
97
|
|
|
condition=AlertCondition.ALWAYS, |
98
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
99
|
|
|
method='ipsum', |
100
|
|
|
) |
101
|
|
|
|
102
|
|
|
def test_invalid_condition_for_secinfo(self): |
103
|
|
|
with self.assertRaises(InvalidArgument): |
104
|
|
|
self.gmp.create_alert( |
105
|
|
|
name='foo', |
106
|
|
|
condition=AlertCondition.SEVERITY_AT_LEAST, |
107
|
|
|
event=AlertEvent.UPDATED_SECINFO_ARRIVED, |
108
|
|
|
method=AlertMethod.EMAIL, |
109
|
|
|
) |
110
|
|
|
|
111
|
|
|
def test_invalid_method_for_secinfo(self): |
112
|
|
|
with self.assertRaises(InvalidArgument): |
113
|
|
|
self.gmp.create_alert( |
114
|
|
|
name='foo', |
115
|
|
|
condition=AlertCondition.ALWAYS, |
116
|
|
|
event=AlertEvent.UPDATED_SECINFO_ARRIVED, |
117
|
|
|
method=AlertMethod.HTTP_GET, |
118
|
|
|
) |
119
|
|
|
|
120
|
|
|
def test_missing_method_for_ticket_received(self): |
121
|
|
|
with self.assertRaises(RequiredArgument): |
122
|
|
|
self.gmp.create_alert( |
123
|
|
|
name='foo', |
124
|
|
|
condition=AlertCondition.ALWAYS, |
125
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
126
|
|
|
method=None, |
127
|
|
|
) |
128
|
|
|
|
129
|
|
|
def test_missing_condition_for_ticket_received(self): |
130
|
|
|
with self.assertRaises(RequiredArgument): |
131
|
|
|
self.gmp.create_alert( |
132
|
|
|
name='foo', |
133
|
|
|
condition=None, |
134
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
135
|
|
|
method=AlertMethod.EMAIL, |
136
|
|
|
) |
137
|
|
|
|
138
|
|
|
def test_invalid_method_for_ticket_received(self): |
139
|
|
|
with self.assertRaises(InvalidArgument): |
140
|
|
|
self.gmp.create_alert( |
141
|
|
|
name='foo', |
142
|
|
|
condition=AlertCondition.ALWAYS, |
143
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
144
|
|
|
method=AlertMethod.HTTP_GET, |
145
|
|
|
) |
146
|
|
|
|
147
|
|
|
def test_invalid_condition_for_task_run_status_changed(self): |
148
|
|
|
with self.assertRaises(InvalidArgument): |
149
|
|
|
self.gmp.create_alert( |
150
|
|
|
name='foo', |
151
|
|
|
condition=AlertCondition.ERROR, |
152
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
153
|
|
|
method=AlertMethod.EMAIL, |
154
|
|
|
) |
155
|
|
|
|
156
|
|
|
def test_invalid_condition_for_ticket_received(self): |
157
|
|
|
with self.assertRaises(InvalidArgument): |
158
|
|
|
self.gmp.create_alert( |
159
|
|
|
name='foo', |
160
|
|
|
condition=AlertCondition.FILTER_COUNT_AT_LEAST, |
161
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
162
|
|
|
method=AlertMethod.EMAIL, |
163
|
|
|
) |
164
|
|
|
|
165
|
|
|
def test_create_alert(self): |
166
|
|
|
self.gmp.create_alert( |
167
|
|
|
name='foo', |
168
|
|
|
condition=AlertCondition.ALWAYS, |
169
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
170
|
|
|
method=AlertMethod.EMAIL, |
171
|
|
|
) |
172
|
|
|
|
173
|
|
|
self.connection.send.has_been_called_with( |
174
|
|
|
'<create_alert>' |
175
|
|
|
'<name>foo</name>' |
176
|
|
|
'<condition>Always</condition>' |
177
|
|
|
'<event>Task run status changed</event>' |
178
|
|
|
'<method>Email</method>' |
179
|
|
|
'</create_alert>' |
180
|
|
|
) |
181
|
|
|
|
182
|
|
|
def test_create_alert_with_filter_id(self): |
183
|
|
|
self.gmp.create_alert( |
184
|
|
|
name='foo', |
185
|
|
|
condition=AlertCondition.ALWAYS, |
186
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
187
|
|
|
method=AlertMethod.EMAIL, |
188
|
|
|
filter_id='f1', |
189
|
|
|
) |
190
|
|
|
|
191
|
|
|
self.connection.send.has_been_called_with( |
192
|
|
|
'<create_alert>' |
193
|
|
|
'<name>foo</name>' |
194
|
|
|
'<condition>Always</condition>' |
195
|
|
|
'<event>Task run status changed</event>' |
196
|
|
|
'<method>Email</method>' |
197
|
|
|
'<filter id="f1"/>' |
198
|
|
|
'</create_alert>' |
199
|
|
|
) |
200
|
|
|
|
201
|
|
|
def test_create_alert_with_comment(self): |
202
|
|
|
self.gmp.create_alert( |
203
|
|
|
name='foo', |
204
|
|
|
condition=AlertCondition.ALWAYS, |
205
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
206
|
|
|
method=AlertMethod.EMAIL, |
207
|
|
|
comment='hello', |
208
|
|
|
) |
209
|
|
|
|
210
|
|
|
self.connection.send.has_been_called_with( |
211
|
|
|
'<create_alert>' |
212
|
|
|
'<name>foo</name>' |
213
|
|
|
'<condition>Always</condition>' |
214
|
|
|
'<event>Task run status changed</event>' |
215
|
|
|
'<method>Email</method>' |
216
|
|
|
'<comment>hello</comment>' |
217
|
|
|
'</create_alert>' |
218
|
|
|
) |
219
|
|
|
|
220
|
|
|
def test_create_alert_with_condition_data(self): |
221
|
|
|
self.gmp.create_alert( |
222
|
|
|
name='foo', |
223
|
|
|
condition=AlertCondition.ALWAYS, |
224
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
225
|
|
|
method=AlertMethod.EMAIL, |
226
|
|
|
condition_data={'foo': 'bar'}, |
227
|
|
|
) |
228
|
|
|
|
229
|
|
|
self.connection.send.has_been_called_with( |
230
|
|
|
'<create_alert>' |
231
|
|
|
'<name>foo</name>' |
232
|
|
|
'<condition>Always<data>bar<name>foo</name></data></condition>' |
233
|
|
|
'<event>Task run status changed</event>' |
234
|
|
|
'<method>Email</method>' |
235
|
|
|
'</create_alert>' |
236
|
|
|
) |
237
|
|
|
|
238
|
|
|
def test_create_alert_with_event_data(self): |
239
|
|
|
self.gmp.create_alert( |
240
|
|
|
name='foo', |
241
|
|
|
condition=AlertCondition.ALWAYS, |
242
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
243
|
|
|
method=AlertMethod.EMAIL, |
244
|
|
|
event_data={'foo': 'bar'}, |
245
|
|
|
) |
246
|
|
|
|
247
|
|
|
self.connection.send.has_been_called_with( |
248
|
|
|
'<create_alert>' |
249
|
|
|
'<name>foo</name>' |
250
|
|
|
'<condition>Always</condition>' |
251
|
|
|
'<event>Task run status changed' |
252
|
|
|
'<data>bar<name>foo</name></data>' |
253
|
|
|
'</event>' |
254
|
|
|
'<method>Email</method>' |
255
|
|
|
'</create_alert>' |
256
|
|
|
) |
257
|
|
|
|
258
|
|
|
def test_create_alert_with_method_data(self): |
259
|
|
|
self.gmp.create_alert( |
260
|
|
|
name='foo', |
261
|
|
|
condition=AlertCondition.ALWAYS, |
262
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
263
|
|
|
method=AlertMethod.EMAIL, |
264
|
|
|
method_data={'foo': 'bar'}, |
265
|
|
|
) |
266
|
|
|
|
267
|
|
|
self.connection.send.has_been_called_with( |
268
|
|
|
'<create_alert>' |
269
|
|
|
'<name>foo</name>' |
270
|
|
|
'<condition>Always</condition>' |
271
|
|
|
'<event>Task run status changed</event>' |
272
|
|
|
'<method>Email<data>bar<name>foo</name></data></method>' |
273
|
|
|
'</create_alert>' |
274
|
|
|
) |
275
|
|
|
|