Passed
Pull Request — master (#436)
by Jaspar
04:52
created

tests.protocols.gmpv208.testcmds.test_create_alert   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 281
Duplicated Lines 87.19 %

Importance

Changes 0
Metric Value
eloc 164
dl 245
loc 281
rs 9.36
c 0
b 0
f 0
wmc 38

20 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpCreateAlertTestCase.test_invalid_method_for_ticket_received() 7 7 2
A GmpCreateAlertTestCase.test_create_alert_with_method_data() 11 11 1
A GmpCreateAlertTestCase.test_create_alert_with_event_data() 11 11 1
A GmpCreateAlertTestCase.test_missing_condition() 9 9 3
A GmpCreateAlertTestCase.test_create_alert_with_comment() 11 11 1
A GmpCreateAlertTestCase.test_invalid_method_for_secinfo() 7 7 2
A GmpCreateAlertTestCase.test_invalid_method() 7 7 2
A GmpCreateAlertTestCase.test_invalid_condition() 7 7 2
A GmpCreateAlertTestCase.test_missing_event() 9 9 3
A GmpCreateAlertTestCase.test_invalid_condition_for_secinfo() 7 7 2
A GmpCreateAlertTestCase.test_create_alert_with_condition_data() 11 11 1
A GmpCreateAlertTestCase.test_create_alert() 10 10 1
A GmpCreateAlertTestCase.test_invalid_condition_for_task_run_status_changed() 7 7 2
A GmpCreateAlertTestCase.test_missing_condition_for_ticket_received() 7 7 2
A GmpCreateAlertTestCase.test_missing_method() 9 9 3
A GmpCreateAlertTestCase.test_invalid_condition_for_ticket_received() 7 7 2
A GmpCreateAlertTestCase.test_missing_name() 15 15 3
A GmpCreateAlertTestCase.test_invalid_event() 7 7 2
A GmpCreateAlertTestCase.test_missing_method_for_ticket_received() 7 7 2
A GmpCreateAlertTestCase.test_create_alert_with_filter_id() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
import unittest
20
21
from gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType
22
23
from gvm.protocols.gmpv208 import AlertCondition, AlertEvent, AlertMethod
24
25
26 View Code Duplication
class GmpCreateAlertTestCase:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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_missing_method_for_ticket_received(self):
123
        with self.assertRaises(RequiredArgument):
124
            self.gmp.create_alert(
125
                name='foo',
126
                condition=AlertCondition.ALWAYS,
127
                event=AlertEvent.TICKET_RECEIVED,
128
                method=None,
129
            )
130
131
    def test_missing_condition_for_ticket_received(self):
132
        with self.assertRaises(RequiredArgument):
133
            self.gmp.create_alert(
134
                name='foo',
135
                condition=None,
136
                event=AlertEvent.TICKET_RECEIVED,
137
                method=AlertMethod.EMAIL,
138
            )
139
140
    def test_invalid_method_for_ticket_received(self):
141
        with self.assertRaises(InvalidArgument):
142
            self.gmp.create_alert(
143
                name='foo',
144
                condition=AlertCondition.ALWAYS,
145
                event=AlertEvent.TICKET_RECEIVED,
146
                method=AlertMethod.HTTP_GET,
147
            )
148
149
    def test_invalid_condition_for_task_run_status_changed(self):
150
        with self.assertRaises(InvalidArgument):
151
            self.gmp.create_alert(
152
                name='foo',
153
                condition=AlertCondition.ERROR,
154
                event=AlertEvent.TASK_RUN_STATUS_CHANGED,
155
                method=AlertMethod.EMAIL,
156
            )
157
158
    def test_invalid_condition_for_ticket_received(self):
159
        with self.assertRaises(InvalidArgument):
160
            self.gmp.create_alert(
161
                name='foo',
162
                condition=AlertCondition.FILTER_COUNT_AT_LEAST,
163
                event=AlertEvent.TICKET_RECEIVED,
164
                method=AlertMethod.EMAIL,
165
            )
166
167
    def test_create_alert(self):
168
        self.gmp.create_alert(
169
            name='foo',
170
            condition=AlertCondition.ALWAYS,
171
            event=AlertEvent.TASK_RUN_STATUS_CHANGED,
172
            method=AlertMethod.EMAIL,
173
        )
174
175
        self.connection.send.has_been_called_with(
176
            '<create_alert>'
177
            '<name>foo</name>'
178
            '<condition>Always</condition>'
179
            '<event>Task run status changed</event>'
180
            '<method>Email</method>'
181
            '</create_alert>'
182
        )
183
184
    def test_create_alert_with_filter_id(self):
185
        self.gmp.create_alert(
186
            name='foo',
187
            condition=AlertCondition.ALWAYS,
188
            event=AlertEvent.TASK_RUN_STATUS_CHANGED,
189
            method=AlertMethod.EMAIL,
190
            filter_id='f1',
191
        )
192
193
        self.connection.send.has_been_called_with(
194
            '<create_alert>'
195
            '<name>foo</name>'
196
            '<condition>Always</condition>'
197
            '<event>Task run status changed</event>'
198
            '<method>Email</method>'
199
            '<filter id="f1"/>'
200
            '</create_alert>'
201
        )
202
203
    def test_create_alert_with_comment(self):
204
        self.gmp.create_alert(
205
            name='foo',
206
            condition=AlertCondition.ALWAYS,
207
            event=AlertEvent.TASK_RUN_STATUS_CHANGED,
208
            method=AlertMethod.EMAIL,
209
            comment='hello',
210
        )
211
212
        self.connection.send.has_been_called_with(
213
            '<create_alert>'
214
            '<name>foo</name>'
215
            '<condition>Always</condition>'
216
            '<event>Task run status changed</event>'
217
            '<method>Email</method>'
218
            '<comment>hello</comment>'
219
            '</create_alert>'
220
        )
221
222
    def test_create_alert_with_condition_data(self):
223
        self.gmp.create_alert(
224
            name='foo',
225
            condition=AlertCondition.ALWAYS,
226
            event=AlertEvent.TASK_RUN_STATUS_CHANGED,
227
            method=AlertMethod.EMAIL,
228
            condition_data={'foo': 'bar'},
229
        )
230
231
        self.connection.send.has_been_called_with(
232
            '<create_alert>'
233
            '<name>foo</name>'
234
            '<condition>Always<data>bar<name>foo</name></data></condition>'
235
            '<event>Task run status changed</event>'
236
            '<method>Email</method>'
237
            '</create_alert>'
238
        )
239
240
    def test_create_alert_with_event_data(self):
241
        self.gmp.create_alert(
242
            name='foo',
243
            condition=AlertCondition.ALWAYS,
244
            event=AlertEvent.TASK_RUN_STATUS_CHANGED,
245
            method=AlertMethod.EMAIL,
246
            event_data={'foo': 'bar'},
247
        )
248
249
        self.connection.send.has_been_called_with(
250
            '<create_alert>'
251
            '<name>foo</name>'
252
            '<condition>Always</condition>'
253
            '<event>Task run status changed'
254
            '<data>bar<name>foo</name></data>'
255
            '</event>'
256
            '<method>Email</method>'
257
            '</create_alert>'
258
        )
259
260
    def test_create_alert_with_method_data(self):
261
        self.gmp.create_alert(
262
            name='foo',
263
            condition=AlertCondition.ALWAYS,
264
            event=AlertEvent.TASK_RUN_STATUS_CHANGED,
265
            method=AlertMethod.EMAIL,
266
            method_data={'foo': 'bar'},
267
        )
268
269
        self.connection.send.has_been_called_with(
270
            '<create_alert>'
271
            '<name>foo</name>'
272
            '<condition>Always</condition>'
273
            '<event>Task run status changed</event>'
274
            '<method>Email<data>bar<name>foo</name></data></method>'
275
            '</create_alert>'
276
        )
277
278
279
if __name__ == '__main__':
280
    unittest.main()
281