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