Completed
Push — master ( ff96d5...d0ddf5 )
by Björn
21s queued 14s
created

tests.protocols.gmpv9.testcmds.test_modify_alert   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 211
Duplicated Lines 83.41 %

Importance

Changes 0
Metric Value
eloc 121
dl 176
loc 211
rs 10
c 0
b 0
f 0
wmc 29

15 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpModifyAlertTestCase.test_modify_alert_with_method_data() 11 11 1
A GmpModifyAlertTestCase.test_modify_alert_with_event_data() 11 11 1
A GmpModifyAlertTestCase.test_modify_alert_with_filter_id() 5 5 1
A GmpModifyAlertTestCase.test_modify_alert_with_name() 5 5 1
A GmpModifyAlertTestCase.test_modify_alert_with_event_missing_method() 20 20 4
A GmpModifyAlertTestCase.test_modify_alert_with_condition_data() 11 11 1
A GmpModifyAlertTestCase.test_modify_alert_invalid_event() 4 4 2
A GmpModifyAlertTestCase.test_modify_alert_invalid_method() 7 7 2
A GmpModifyAlertTestCase.test_modify_alert() 5 5 1
A GmpModifyAlertTestCase.test_modify_alert_invalid_condition_for_secinfo() 7 7 2
A GmpModifyAlertTestCase.test_modify_alert_without_alert_id() 9 9 4
A GmpModifyAlertTestCase.test_modify_alert_with_event_missing_condition() 20 20 4
A GmpModifyAlertTestCase.test_modify_alert_with_comment() 5 5 1
A GmpModifyAlertTestCase.test_modify_alert_invalid_method_for_secinfo() 7 7 2
A GmpModifyAlertTestCase.test_modify_alert_invalid_condition() 7 7 2

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 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:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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