|
1
|
|
|
|
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
# Copyright (C) 2018 Greenbone Networks GmbH |
|
4
|
|
|
# |
|
5
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
|
6
|
|
|
# |
|
7
|
|
|
# This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
# it under the terms of the GNU General Public License as published by |
|
9
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
# (at your option) any later version. |
|
11
|
|
|
# |
|
12
|
|
|
# This program is distributed in the hope that it will be useful, |
|
13
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
# GNU General Public License for more details. |
|
16
|
|
|
# |
|
17
|
|
|
# You should have received a copy of the GNU General Public License |
|
18
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
|
|
20
|
|
|
import unittest |
|
21
|
|
|
|
|
22
|
|
|
from gvm.errors import RequiredArgument, InvalidArgument |
|
23
|
|
|
from gvm.protocols.gmpv7 import Gmp |
|
24
|
|
|
|
|
25
|
|
|
from .. import MockConnection |
|
26
|
|
|
|
|
27
|
|
|
class GmpCreateAlertTestCase(unittest.TestCase): |
|
28
|
|
|
|
|
29
|
|
|
def setUp(self): |
|
30
|
|
|
self.connection = MockConnection() |
|
31
|
|
|
self.gmp = Gmp(self.connection) |
|
32
|
|
|
|
|
33
|
|
|
def test_missing_name(self): |
|
34
|
|
|
with self.assertRaises(RequiredArgument): |
|
35
|
|
|
self.gmp.create_alert( |
|
36
|
|
|
name='', condition='foo', event='bar', method='lorem') |
|
37
|
|
|
|
|
38
|
|
|
with self.assertRaises(RequiredArgument): |
|
39
|
|
|
self.gmp.create_alert( |
|
40
|
|
|
name=None, condition='foo', event='bar', method='lorem') |
|
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
|
|
|
with self.assertRaises(RequiredArgument): |
|
48
|
|
|
self.gmp.create_alert( |
|
49
|
|
|
name='foo', condition=None, event='bar', method='lorem') |
|
50
|
|
|
|
|
51
|
|
|
def test_missing_event(self): |
|
52
|
|
|
with self.assertRaises(RequiredArgument): |
|
53
|
|
|
self.gmp.create_alert( |
|
54
|
|
|
name='foo', condition='bar', event='', method='lorem') |
|
55
|
|
|
|
|
56
|
|
|
with self.assertRaises(RequiredArgument): |
|
57
|
|
|
self.gmp.create_alert( |
|
58
|
|
|
name='foo', condition='bar', event=None, method='lorem') |
|
59
|
|
|
|
|
60
|
|
|
def test_missing_method(self): |
|
61
|
|
|
with self.assertRaises(RequiredArgument): |
|
62
|
|
|
self.gmp.create_alert( |
|
63
|
|
|
name='foo', condition='bar', event='lorem', method='') |
|
64
|
|
|
|
|
65
|
|
|
with self.assertRaises(RequiredArgument): |
|
66
|
|
|
self.gmp.create_alert( |
|
67
|
|
|
name='foo', condition='bar', event='lorem', method=None) |
|
68
|
|
|
|
|
69
|
|
|
def test_invalid_condition(self): |
|
70
|
|
|
with self.assertRaises(InvalidArgument): |
|
71
|
|
|
self.gmp.create_alert( |
|
72
|
|
|
name='foo', condition='bar', event='Task run status changed', method='Email') |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
def test_invalid_event(self): |
|
75
|
|
|
with self.assertRaises(InvalidArgument): |
|
76
|
|
|
self.gmp.create_alert( |
|
77
|
|
|
name='foo', condition='Always', event='lorem', method='Email') |
|
78
|
|
|
|
|
79
|
|
|
def test_invalid_method(self): |
|
80
|
|
|
with self.assertRaises(InvalidArgument): |
|
81
|
|
|
self.gmp.create_alert( |
|
82
|
|
|
name='foo', condition='Always', event='Task run status changed', method='ipsum') |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
def test_invalid_condition_for_secinfo(self): |
|
85
|
|
|
with self.assertRaises(InvalidArgument): |
|
86
|
|
|
self.gmp.create_alert( |
|
87
|
|
|
name='foo', condition='Severity at least', event='Updated SecInfo arrived', method='Email') |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
def test_invalid_method_for_secinfo(self): |
|
90
|
|
|
with self.assertRaises(InvalidArgument): |
|
91
|
|
|
self.gmp.create_alert( |
|
92
|
|
|
name='foo', condition='Always', event='Updated SecInfo arrived', method='HTTP Get') |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
def test_create_alert(self): |
|
95
|
|
|
self.gmp.create_alert( |
|
96
|
|
|
name='foo', condition='Always', event='Task run status changed', method='Email') |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
self.connection.send.has_been_called_with( |
|
99
|
|
|
'<create_alert>' |
|
100
|
|
|
'<name>foo</name>' |
|
101
|
|
|
'<condition>Always</condition>' |
|
102
|
|
|
'<event>Task run status changed</event>' |
|
103
|
|
|
'<method>Email</method>' |
|
104
|
|
|
'</create_alert>' |
|
105
|
|
|
) |
|
106
|
|
|
|
|
107
|
|
|
def test_create_alert_with_filter_id(self): |
|
108
|
|
|
self.gmp.create_alert( |
|
109
|
|
|
name='foo', condition='Always', event='Task run status changed', method='Email', |
|
|
|
|
|
|
110
|
|
|
filter_id='f1') |
|
111
|
|
|
|
|
112
|
|
|
self.connection.send.has_been_called_with( |
|
113
|
|
|
'<create_alert>' |
|
114
|
|
|
'<name>foo</name>' |
|
115
|
|
|
'<condition>Always</condition>' |
|
116
|
|
|
'<event>Task run status changed</event>' |
|
117
|
|
|
'<method>Email</method>' |
|
118
|
|
|
'<filter id="f1"/>' |
|
119
|
|
|
'</create_alert>' |
|
120
|
|
|
) |
|
121
|
|
|
|
|
122
|
|
|
def test_create_alert_with_comment(self): |
|
123
|
|
|
self.gmp.create_alert( |
|
124
|
|
|
name='foo', condition='Always', event='Task run status changed', method='Email', |
|
|
|
|
|
|
125
|
|
|
comment='hello') |
|
126
|
|
|
|
|
127
|
|
|
self.connection.send.has_been_called_with( |
|
128
|
|
|
'<create_alert>' |
|
129
|
|
|
'<name>foo</name>' |
|
130
|
|
|
'<condition>Always</condition>' |
|
131
|
|
|
'<event>Task run status changed</event>' |
|
132
|
|
|
'<method>Email</method>' |
|
133
|
|
|
'<comment>hello</comment>' |
|
134
|
|
|
'</create_alert>' |
|
135
|
|
|
) |
|
136
|
|
|
|
|
137
|
|
|
def test_create_alert_with_condition_data(self): |
|
138
|
|
|
self.gmp.create_alert( |
|
139
|
|
|
name='foo', condition='Always', event='Task run status changed', method='Email', |
|
|
|
|
|
|
140
|
|
|
condition_data={'foo': 'bar'}) |
|
141
|
|
|
|
|
142
|
|
|
self.connection.send.has_been_called_with( |
|
143
|
|
|
'<create_alert>' |
|
144
|
|
|
'<name>foo</name>' |
|
145
|
|
|
'<condition>Always<data>bar<name>foo</name></data></condition>' |
|
146
|
|
|
'<event>Task run status changed</event>' |
|
147
|
|
|
'<method>Email</method>' |
|
148
|
|
|
'</create_alert>' |
|
149
|
|
|
) |
|
150
|
|
|
|
|
151
|
|
|
def test_create_alert_with_event_data(self): |
|
152
|
|
|
self.gmp.create_alert( |
|
153
|
|
|
name='foo', condition='Always', event='Task run status changed', method='Email', |
|
|
|
|
|
|
154
|
|
|
event_data={'foo': 'bar'}) |
|
155
|
|
|
|
|
156
|
|
|
self.connection.send.has_been_called_with( |
|
157
|
|
|
'<create_alert>' |
|
158
|
|
|
'<name>foo</name>' |
|
159
|
|
|
'<condition>Always</condition>' |
|
160
|
|
|
'<event>Task run status changed<data>bar<name>foo</name></data></event>' |
|
|
|
|
|
|
161
|
|
|
'<method>Email</method>' |
|
162
|
|
|
'</create_alert>' |
|
163
|
|
|
) |
|
164
|
|
|
|
|
165
|
|
|
def test_create_alert_with_method_data(self): |
|
166
|
|
|
self.gmp.create_alert( |
|
167
|
|
|
name='foo', condition='Always', event='Task run status changed', method='Email', |
|
|
|
|
|
|
168
|
|
|
method_data={'foo': 'bar'}) |
|
169
|
|
|
|
|
170
|
|
|
self.connection.send.has_been_called_with( |
|
171
|
|
|
'<create_alert>' |
|
172
|
|
|
'<name>foo</name>' |
|
173
|
|
|
'<condition>Always</condition>' |
|
174
|
|
|
'<event>Task run status changed</event>' |
|
175
|
|
|
'<method>Email<data>bar<name>foo</name></data></method>' |
|
176
|
|
|
'</create_alert>' |
|
177
|
|
|
) |
|
178
|
|
|
|
|
179
|
|
|
if __name__ == '__main__': |
|
180
|
|
|
unittest.main() |
|
181
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.