Completed
Push — master ( 0d5311...c5499f )
by Jaspar
22s queued 17s
created

tests.protocols.gmpv208.testcmds.test_modify_task   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 221
Duplicated Lines 85.52 %

Importance

Changes 0
Metric Value
eloc 105
dl 189
loc 221
rs 9.76
c 0
b 0
f 0
wmc 33

21 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpModifyTaskCommandTestCase.test_modify_task_with_alerts_ids() 5 5 1
A GmpModifyTaskCommandTestCase.test_modify_task_with_target_id() 5 5 1
A GmpModifyTaskCommandTestCase.test_modify_task_invalid_observers() 6 6 3
A GmpModifyTaskCommandTestCase.test_modify_task_with_config_id() 5 5 1
A GmpModifyTaskCommandTestCase.test_modify_task_with_name() 5 5 1
A GmpModifyTaskCommandTestCase.test_modify_task_missing_task_id() 9 9 4
A GmpModifyTaskCommandTestCase.test_modify_task_with_schedule_id() 5 5 1
A GmpModifyTaskCommandTestCase.test_modify_task_with_schedule() 5 5 1
A GmpModifyTaskCommandTestCase.test_modify_task_with_alterable() 13 13 1
A GmpModifyTaskCommandTestCase.test_modify_task_with_hosts_ordering() 5 5 1
A GmpModifyTaskCommandTestCase.test_modify_task_invalid_schedule_periods() 6 6 3
A GmpModifyTaskCommandTestCase.test_modify_task_with_preferences() 8 8 1
A GmpModifyTaskCommandTestCase.test_modify_task_with_schedule_periods() 13 13 1
A GmpModifyTaskCommandTestCase.test_modify_task_with_observers() 5 5 1
A GmpModifyTaskCommandTestCase.test_modify_task_invalid_alerts_ids() 6 6 3
A GmpModifyTaskCommandTestCase.test_modify_task_with_empty_alert_ids() 5 5 1
A GmpModifyTaskCommandTestCase.test_modify_task_invalid_hosts_ordering() 3 3 2
A GmpModifyTaskCommandTestCase.test_modify_task_with_comment() 5 5 1
A GmpModifyTaskCommandTestCase.test_modify_task_invalid_preferences() 6 6 3
A GmpModifyTaskCommandTestCase.test_modify_task() 4 4 1
A GmpModifyTaskCommandTestCase.test_modify_task_with_scanner_id() 5 5 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 collections import OrderedDict
22
23
from gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType
24
25
from gvm.protocols.gmpv208 import HostsOrdering
26
27
28 View Code Duplication
class GmpModifyTaskCommandTestCase:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
29
    def test_modify_task(self):
30
        self.gmp.modify_task('t1')
31
32
        self.connection.send.has_been_called_with('<modify_task task_id="t1"/>')
33
34
    def test_modify_task_missing_task_id(self):
35
        with self.assertRaises(RequiredArgument):
36
            self.gmp.modify_task(None)
37
38
        with self.assertRaises(RequiredArgument):
39
            self.gmp.modify_task('')
40
41
        with self.assertRaises(RequiredArgument):
42
            self.gmp.modify_task(task_id='')
43
44
    def test_modify_task_with_name(self):
45
        self.gmp.modify_task(task_id='t1', name='foo')
46
47
        self.connection.send.has_been_called_with(
48
            '<modify_task task_id="t1">' '<name>foo</name>' '</modify_task>'
49
        )
50
51
    def test_modify_task_with_config_id(self):
52
        self.gmp.modify_task(task_id='t1', config_id='c1')
53
54
        self.connection.send.has_been_called_with(
55
            '<modify_task task_id="t1">' '<config id="c1"/>' '</modify_task>'
56
        )
57
58
    def test_modify_task_with_target_id(self):
59
        self.gmp.modify_task(task_id='t1', target_id='t1')
60
61
        self.connection.send.has_been_called_with(
62
            '<modify_task task_id="t1">' '<target id="t1"/>' '</modify_task>'
63
        )
64
65
    def test_modify_task_with_scanner_id(self):
66
        self.gmp.modify_task(task_id='t1', scanner_id='s1')
67
68
        self.connection.send.has_been_called_with(
69
            '<modify_task task_id="t1">' '<scanner id="s1"/>' '</modify_task>'
70
        )
71
72
    def test_modify_task_with_schedule_id(self):
73
        self.gmp.modify_task(task_id='t1', schedule_id='s1')
74
75
        self.connection.send.has_been_called_with(
76
            '<modify_task task_id="t1">' '<schedule id="s1"/>' '</modify_task>'
77
        )
78
79
    def test_modify_task_with_comment(self):
80
        self.gmp.modify_task(task_id='t1', comment='bar')
81
82
        self.connection.send.has_been_called_with(
83
            '<modify_task task_id="t1">'
84
            '<comment>bar</comment>'
85
            '</modify_task>'
86
        )
87
88
    def test_modify_task_with_alerts_ids(self):
89
        self.gmp.modify_task(task_id='t1', alert_ids=['a1', 'a2', 'a3'])
90
91
        self.connection.send.has_been_called_with(
92
            '<modify_task task_id="t1">'
93
            '<alert id="a1"/>'
94
            '<alert id="a2"/>'
95
            '<alert id="a3"/>'
96
            '</modify_task>'
97
        )
98
99
    def test_modify_task_invalid_alerts_ids(self):
100
        with self.assertRaises(InvalidArgumentType):
101
            self.gmp.modify_task(task_id='t1', alert_ids='')
102
103
        with self.assertRaises(InvalidArgumentType):
104
            self.gmp.modify_task(task_id='t1', alert_ids='a1')
105
106
    def test_modify_task_with_empty_alert_ids(self):
107
        self.gmp.modify_task(task_id='t1', alert_ids=[])
108
109
        self.connection.send.has_been_called_with(
110
            '<modify_task task_id="t1">' '<alert id="0"/>' '</modify_task>'
111
        )
112
113
    def test_modify_task_with_alterable(self):
114
        self.gmp.modify_task(task_id='t1', alterable=True)
115
116
        self.connection.send.has_been_called_with(
117
            '<modify_task task_id="t1">'
118
            '<alterable>1</alterable>'
119
            '</modify_task>'
120
        )
121
122
        self.gmp.modify_task(task_id='t1', alterable=False)
123
124
        self.connection.send.has_been_called_with(
125
            '<modify_task task_id="t1">'
126
            '<alterable>0</alterable>'
127
            '</modify_task>'
128
        )
129
130
    def test_modify_task_with_hosts_ordering(self):
131
        self.gmp.modify_task(task_id='t1', hosts_ordering=HostsOrdering.REVERSE)
132
133
        self.connection.send.has_been_called_with(
134
            '<modify_task task_id="t1">'
135
            '<hosts_ordering>reverse</hosts_ordering>'
136
            '</modify_task>'
137
        )
138
139
    def test_modify_task_invalid_hosts_ordering(self):
140
        with self.assertRaises(InvalidArgumentType):
141
            self.gmp.modify_task(task_id='t1', hosts_ordering='foo')
142
143
    def test_modify_task_with_schedule(self):
144
        self.gmp.modify_task(task_id='t1', schedule_id='s1')
145
146
        self.connection.send.has_been_called_with(
147
            '<modify_task task_id="t1">' '<schedule id="s1"/>' '</modify_task>'
148
        )
149
150
    def test_modify_task_with_schedule_periods(self):
151
        self.gmp.modify_task(task_id='t1', schedule_periods=0)
152
153
        self.connection.send.has_been_called_with(
154
            '<modify_task task_id="t1">'
155
            '<schedule_periods>0</schedule_periods>'
156
            '</modify_task>'
157
        )
158
159
        self.gmp.modify_task(task_id='t1', schedule_periods=5)
160
161
        self.connection.send.has_been_called_with(
162
            '<modify_task task_id="t1">'
163
            '<schedule_periods>5</schedule_periods>'
164
            '</modify_task>'
165
        )
166
167
    def test_modify_task_invalid_schedule_periods(self):
168
        with self.assertRaises(InvalidArgument):
169
            self.gmp.modify_task(task_id='t1', schedule_periods='foo')
170
171
        with self.assertRaises(InvalidArgument):
172
            self.gmp.modify_task(task_id='t1', schedule_periods=-1)
173
174
    def test_modify_task_with_observers(self):
175
        self.gmp.modify_task(task_id='t1', observers=['u1', 'u2'])
176
177
        self.connection.send.has_been_called_with(
178
            '<modify_task task_id="t1">'
179
            '<observers>u1,u2</observers>'
180
            '</modify_task>'
181
        )
182
183
    def test_modify_task_invalid_observers(self):
184
        with self.assertRaises(InvalidArgumentType):
185
            self.gmp.modify_task(task_id='t1', observers='')
186
187
        with self.assertRaises(InvalidArgumentType):
188
            self.gmp.modify_task(task_id='t1', observers='foo')
189
190
    def test_modify_task_with_preferences(self):
191
        self.gmp.modify_task(
192
            task_id='t1',
193
            preferences=OrderedDict([('foo', 'bar'), ('lorem', 'ipsum')]),
194
        )
195
196
        self.connection.send.has_been_called_with(
197
            '<modify_task task_id="t1">'
198
            '<preferences>'
199
            '<preference>'
200
            '<scanner_name>foo</scanner_name>'
201
            '<value>bar</value>'
202
            '</preference>'
203
            '<preference>'
204
            '<scanner_name>lorem</scanner_name>'
205
            '<value>ipsum</value>'
206
            '</preference>'
207
            '</preferences>'
208
            '</modify_task>'
209
        )
210
211
    def test_modify_task_invalid_preferences(self):
212
        with self.assertRaises(InvalidArgumentType):
213
            self.gmp.modify_task(task_id='t1', preferences='')
214
215
        with self.assertRaises(InvalidArgumentType):
216
            self.gmp.modify_task(task_id='t1', preferences=['foo', 'bar'])
217
218
219
if __name__ == '__main__':
220
    unittest.main()
221