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